Socket
Book a DemoInstallSign in
Socket

esnek

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esnek

0.5.3
bundlerRubygems
Version published
Maintainers
1
Created
Source

= 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

===
require 'esnek'
esnek = Esnek.new('https://jsonplaceholder.typicode.com/') todo = esnek.todos.__1.get puts todo.id puts todo.inspect

=== Facebook Graph API require 'esnek'

=== 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.

FAQs

Package last updated on 18 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.