esnek 0.2.1 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +1 -0
- data/README.rdoc +57 -9
- data/lib/esnek/base.rb +15 -13
- data/lib/esnek/version.rb +1 -1
- metadata +6 -6
data/CHANGES
CHANGED
data/README.rdoc
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
= Esnek
|
2
|
-
_UNDER HEAVY DEVELOPMENT IN APRIL 2011_
|
3
2
|
|
4
|
-
_Esnek_ provides a
|
5
|
-
|
3
|
+
_Esnek_ provides a minimalistic Ruby interface for JSON APIs, such as ElasticSearch, Google APIs, Facebook Graph API..
|
4
|
+
|
5
|
+
Esnek is mainly developed for _ElasticSearch_ in mind. ElasticSearch (http://www.elasticsearch.org)
|
6
|
+
is a scalable, fast, distributed, highly-available, real time search RESTful search engine communicating
|
7
|
+
by JSON over HTTP, based on _Lucene_ (http://lucene.apache.org).
|
6
8
|
|
7
9
|
== Installation:
|
8
10
|
|
9
11
|
gem install esnek
|
10
12
|
|
13
|
+
Esnek is under heavy development in april 2011, so use "gem update" frequently.
|
14
|
+
|
11
15
|
For the installation of elasticsearch please follow the guides at http://www.elasticsearch.org
|
12
16
|
|
13
|
-
==
|
17
|
+
== Quick Start
|
14
18
|
|
15
19
|
=== ElasticSearch
|
16
20
|
|
@@ -18,8 +22,7 @@ To use esnek just instantiate Esnek with the base API URL.
|
|
18
22
|
require 'esnek'
|
19
23
|
es = Esnek.new('http://localhost:9200')
|
20
24
|
|
21
|
-
|
22
|
-
For example assuming elastic search is running at port 9200 on your localhost, the following code gets the state of the cluster.
|
25
|
+
For example assuming elastic search is running at port 9200 on your localhost, the following code gets the state of the cluster for Elastic Search
|
23
26
|
|
24
27
|
#curl -XGET 'http://localhost:9200/_cluster/state'
|
25
28
|
es._cluster.state.get
|
@@ -56,19 +59,64 @@ In order to post a JSON data simply pass a block (do...end)
|
|
56
59
|
require 'esnek'
|
57
60
|
fb = Esnek.new('http://graph.facebook.com')
|
58
61
|
res = fb.send(:"http://www.wsirussia.ru").get
|
62
|
+
Notice that since "http://www.wsirussia.ru" is an invalid ruby method name, we use "send" to call it
|
59
63
|
puts res.shares
|
60
64
|
|
61
|
-
==
|
65
|
+
== Advanced
|
66
|
+
With esnek just make a chained method call where each method is a directory in the URL
|
67
|
+
for your specific JSON API and end append a final method for HTTP verb; get, post, put or delete.
|
68
|
+
|
69
|
+
=== General Usage
|
70
|
+
require 'esnek'
|
71
|
+
Choose any JSON over HTTP API and identify the base url such as https://www.googleapis.com
|
72
|
+
|
73
|
+
gapi = Esnek.new('https://www.googleapis.com')
|
74
|
+
|
75
|
+
Form a chained method call where each directory in the URL becomes a method and dont forget that
|
76
|
+
your method chain must end with get, post, put or delete.
|
77
|
+
|
78
|
+
gapi.language.translate.v2.get :q => "hello world", :source => :en, :target => :tr, :key => INSERT_YOUR_KEY
|
79
|
+
|
80
|
+
Any query string should be given as a hash to any method in the method call, the following is valid too.
|
81
|
+
|
82
|
+
gapi.language.translate.v2(:key => INSERT_YOUR_KEY).get(:q => "hello world", :source => :en, :target => :tr)
|
83
|
+
|
84
|
+
If you face any portion of the URL which cannot be a valid Ruby method name use send(:'!1invalid_method')
|
85
|
+
|
86
|
+
res = fb.send(:"http://www.wsirussia.ru").get
|
87
|
+
|
88
|
+
Alternatively you may prefix digits with double underscore __
|
89
|
+
|
90
|
+
es.twitter.tweet.__1.get
|
91
|
+
|
92
|
+
If you append a block and pass a hash, the hash is converted into a JSON and posted as data.
|
93
|
+
|
94
|
+
es.twitter.tweet.__2.put do
|
95
|
+
{"user" => "alper", "post_date" => "2011-11-15T14:12:12", "message" => "For esnek"}
|
96
|
+
end
|
97
|
+
|
98
|
+
=== Return Values
|
99
|
+
Esnek simply converts the returned JSON into an Ostruct ruby object.
|
100
|
+
|
101
|
+
res.my_field
|
102
|
+
res.table[:my_field] # alternative form for field names such as .id which already exist in the base object
|
103
|
+
|
104
|
+
Notice that Ostruct converts only the first level hash keys into object attributes.
|
105
|
+
The deeper hashes are intact and should be accessed as Hash.
|
106
|
+
|
107
|
+
Consult your specific API documentation on how the return value is structured
|
62
108
|
|
109
|
+
=== Proxy
|
110
|
+
Esnek is based on restclient and some of the settings from Restclient apply to esnek to.
|
63
111
|
Esnek will use the proxy specified by RestClient.proxy:
|
64
112
|
|
65
113
|
RestClient.proxy = "http://proxy.example.com/"
|
66
114
|
or the proxy url is set in an environment variable
|
67
115
|
RestClient.proxy = ENV['http_proxy']
|
68
116
|
|
69
|
-
|
117
|
+
=== Logging
|
70
118
|
|
71
|
-
To enable logging you
|
119
|
+
To enable logging the calls made to the API, you may
|
72
120
|
|
73
121
|
* set RestClient.log with a ruby Logger
|
74
122
|
* or set an environment variable to avoid modifying the code (in this case you can use a file name, "stdout" or "stderr"):
|
data/lib/esnek/base.rb
CHANGED
@@ -11,6 +11,19 @@ class Esnek
|
|
11
11
|
@chain = []
|
12
12
|
end
|
13
13
|
|
14
|
+
def parse_json(resp)
|
15
|
+
j = JSON.parse resp
|
16
|
+
case
|
17
|
+
when j.is_a?(Hash)
|
18
|
+
r=OpenStruct.new(j)
|
19
|
+
class << r;def table;@table;end;end
|
20
|
+
r
|
21
|
+
when j.is_a?(Array)
|
22
|
+
j.map{|e| r=OpenStruct.new(e);class << r;def table;@table;end;end;r}
|
23
|
+
else
|
24
|
+
j
|
25
|
+
end
|
26
|
+
end
|
14
27
|
def method_missing(method_sym, *args, &block)
|
15
28
|
if [:get, :put, :post, :delete].include?(method_sym)
|
16
29
|
@chain << {:method => nil, :arg => (args.empty? ? {} : args[0]) }
|
@@ -19,22 +32,11 @@ class Esnek
|
|
19
32
|
if block_given?
|
20
33
|
data = block.call.to_json rescue nil
|
21
34
|
end
|
22
|
-
@chain = []
|
23
|
-
|
35
|
+
@chain = []
|
24
36
|
resp = block_given? ? RestClient.send(method_sym, url, data,:params => params,:content_type => :json, :accept => :json) :
|
25
37
|
RestClient.send(method_sym, url,:params => params,:content_type => :json, :accept => :json)
|
26
38
|
|
27
|
-
|
28
|
-
r = OpenStruct.new JSON.parse resp
|
29
|
-
class << r
|
30
|
-
def table
|
31
|
-
@table
|
32
|
-
end
|
33
|
-
end
|
34
|
-
r
|
35
|
-
else
|
36
|
-
resp
|
37
|
-
end
|
39
|
+
resp.headers[:content_type].include?('application/json') ? parse_json(resp) : resp
|
38
40
|
else
|
39
41
|
@chain << {:method => method_sym.to_s.gsub(/^__/,''), :arg => (args.empty? ? {} : args[0]) }
|
40
42
|
self
|
data/lib/esnek/version.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ESNEK_VERSION='0.2.
|
1
|
+
ESNEK_VERSION='0.2.4'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: esnek
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 4
|
10
|
+
version: 0.2.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alper Akgun
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-23 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: json
|
@@ -49,7 +49,7 @@ dependencies:
|
|
49
49
|
version: 1.6.1
|
50
50
|
type: :runtime
|
51
51
|
version_requirements: *id002
|
52
|
-
description: Esnek provides a
|
52
|
+
description: Esnek provides a minimalistic Ruby interface for JSON APIs, such as ElasticSearch
|
53
53
|
email: esnek@sayarus.com
|
54
54
|
executables: []
|
55
55
|
|
@@ -101,6 +101,6 @@ rubyforge_project:
|
|
101
101
|
rubygems_version: 1.7.2
|
102
102
|
signing_key:
|
103
103
|
specification_version: 3
|
104
|
-
summary: Esnek provides a
|
104
|
+
summary: Esnek provides a minimalistic Ruby interface for JSON APIs, such as ElasticSearch
|
105
105
|
test_files: []
|
106
106
|
|