Skip to content

Latest commit

 

History

History
118 lines (78 loc) · 4.16 KB

README.rdoc

File metadata and controls

118 lines (78 loc) · 4.16 KB

Esnek

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.

Installation:

gem install esnek

Quick Start

Google URL Shortener

require 'esnek'  
gapi = Esnek.new('https://www.googleapis.com')
res = gapi.urlshortener.v1.url.post {{:longUrl => "http://www.resimit.com/"}}
# Use res.table[:id] instead of res.id, because id method already exist for Object
puts res.longUrl
puts res.inspect

Facebook Graph API

require 'esnek'  
fb = Esnek.new('http://graph.facebook.com')
res = fb.send(:"http://www.resimit.com").get
# Notice that since "http://www.resimit.com" is a complex ruby method name, we use "send" to call it
puts res.shares

ElasticSearch

To use esnek just instantiate Esnek with the base API URL.

require 'esnek'  
es = Esnek.new('http://localhost:9200')

Assuming Elastic Search is running at port 9200 on your localhost, the following code gets the state of the cluster:

#curl -XGET 'http://localhost:9200/_cluster/state'
es._cluster.state.get

You may pass options as a hash parameter for each directory in your URL:

#curl -XGET http://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 'http://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 http://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

Advanced

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.

General Usage

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(:"http://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

Return Values

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.

Proxy

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 = "http://proxy.example.com/" # or =ENV['http_proxy']

Logging

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

Notes

Esnek was initially developed using Emacs under Ubuntu Linux.