Esnek is a minimalistic client for any Web API in general and no API in particular. Esnek has been used in production for a few years already. ElasticSearch, Google APIs, Facebook Graph API, LinkedIn, Facebook, Twitter OAuth login integrations.
gem install esnek
esnek = Esnek.new('https://jsonplaceholder.typicode.com/') todo = esnek.todos.__1.get puts todo.id puts todo.inspect
require 'esnek'
To use esnek just instantiate Esnek with the base API URL.
require 'esnek' es = Esnek.new('https://localhost:9200')
Assuming Elastic Search is running at port 9200 on your localhost, the following code gets the state of the cluster:
#curl -XGET 'https://localhost:9200/_cluster/state' es._cluster.state.get
You may pass options as a hash parameter for each directory in your URL:
#curl -XGET https://localhost:9200/twitter/tweet/_search?q=user:kimchy es.twitter.tweet._search(:q => 'good').get
For literals such as 1, use __1; esnek simply omits __ (2 underscores):
#curl -XGET 'https://localhost:9200/twitter/tweet/1' es.twitter.tweet.__1.get
In order to post a JSON data simply pass a block (do…end) which returns a hash:
#curl -XPUT https://localhost:9200/twitter/tweet/2 -d '{ # "user": "kimchy", "post_date": "2009-11-15T14:12:12", "message": "You know, for Search" # }' es.twitter.tweet.__2.put do {"user" => "alper", "post_date" => "2011-11-15T14:12:12", "message" => "For esnek"} end
With Esnek you make a chained method call where each method is a directory in the URL for your target JSON API. You finish by appending a method for the HTTP verb; get, post, put or delete.
require 'esnek'
Choose any JSON over HTTP API and identify the base url:
gapi = Esnek.new('https://www.googleapis.com')
Form a chained method call and make sure you end with get, post, put or delete.
gapi.language.translate.v2.get :q => "hello world", :source => :en, :target => :tr, :key => INSERT_YOUR_KEY
Any query string should be given as a hash parameter to any method in the method chain.
gapi.language.translate.v2(:key => INSERT_YOUR_KEY).get(:q => "hello world", :source => :en, :target => :tr)
If you face any portion of the URL which cannot be a valid Ruby method name, use send(:‘!1invalid_method’)
res = fb.send(:"https://www.wsirussia.ru").get
Alternatively you may prefix digits with double underscore __
es.twitter.tweet.__1.get
If you append a block and the block returns a hash, the hash is converted into a JSON and posted as data.
es.twitter.tweet.__2.put do {"user" => "alper", "post_date" => "2011-11-15T14:12:12", "message" => "For esnek"} end
Esnek converts the returned JSON into an Ostruct object. You may use the returned object’s inspect method to see how it works for a given API.
res.my_field # access as if it is an object's attrribute res.table[:my_field] # For field names such as "id", you may use the "table" attribute.
Notice that Ostruct converts only the first level hash keys into Ostruct object attributes. Consult your specific API documentation on how the return value is structured.
Esnek is based on Restclient, so some of the settings of Restclient apply to Esnek too. For example Esnek will use the proxy specified by RestClient.proxy:
RestClient.proxy = "https://proxy.example.com/" # or =ENV['http_proxy']
To enable logging the calls made to the API, you may set RestClient.log with a ruby Logger or set an environment variable to avoid modifying the code (in this case you can use a file name, “stdout” or “stderr”):
$ RESTCLIENT_LOG=stdout path/to/my/program $ RESTCLIENT_LOG=stdout irb
Esnek was initially developed using Emacs under Ubuntu Linux.