esnek 0.2.6 → 0.5.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 51bd712e9e708a69ead2eb6f40485677a391f8c2
4
+ data.tar.gz: 4963c8831cd8c6d079ddceadcf2c5143acded2b0
5
+ SHA512:
6
+ metadata.gz: cc709556d2794cf54de28aaebf388c06bf4ef69ff71c3105a45cb60418e7cef38bdd209c99c35e2d6e76d26a861dab0979ec56ccdc689982a9bc0424d2bf93d8
7
+ data.tar.gz: 641901f95ab9cc5400e1075fbe84e47f6e8bb9aaf163fcd23bb6c5ab1875911a66443a804133084faa328690f28251a370931225be2e2ee3c0d45139fa90ecba
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.gem
15
+ mkmf.log
data/CHANGES CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  == Expected
4
4
 
5
+ == 0.4.x
6
+ * MIT License adopted
7
+
8
+ == 0.4.x
9
+ * hash post data in the block are now converted into a form post data
10
+ * oauth support with an access token through restclient
11
+ * json_return option controls jsonifaction of return values
12
+
13
+ == 0.3.x
14
+ * bug fixed for content type inclusions
15
+ * custom headers are supported
16
+ * Non Json data payloads are now supported
17
+
5
18
  == 0.2.x
6
19
  * tests with elasticsearch API
7
20
  * Authentication mechanisms
@@ -15,5 +28,3 @@
15
28
  == 0.0.x
16
29
  * the very initial release
17
30
 
18
-
19
-
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ require './lib/esnek/version'
2
+ RUBY_ENGINE = 'ruby' unless defined? RUBY_ENGINE
3
+ source 'https://rubygems.org' unless ENV['QUICK']
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ if RUBY_ENGINE == "ruby" and RUBY_VERSION > '1.9.2'
8
+ gem 'rest-client'
9
+ gem 'json'
10
+ gem "minitest", "~> 4.0"
11
+ gem 'rdoc'
12
+ end
13
+
data/LICENSE CHANGED
@@ -1,13 +1,19 @@
1
- Esnek is free software: you can redistribute it and/or modify
2
- it under the terms of the GNU General Public License as published by
3
- the Free Software Foundation, either version 3 of the License, or
4
- (at your option) any later version.
1
+ Copyright (c) Alper Akgun
5
2
 
6
- Esnek is distributed in the hope that it will be useful,
7
- but WITHOUT ANY WARRANTY; without even the implied warranty of
8
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
- GNU General Public License for more details.
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
10
9
 
11
- You should have received a copy of the GNU General Public License
12
- along with Devrinim. If not, see <http://www.gnu.org/licenses/>.
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
13
12
 
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc CHANGED
@@ -1,17 +1,34 @@
1
1
  = Esnek
2
2
 
3
- _Esnek_ provides a minimalistic Ruby interface for JSON APIs, such as ElasticSearch, Google APIs, Facebook Graph API..
4
- Esnek is initially developed for _ElasticSearch_ http://www.elasticsearch.org which is based on _Lucene_ http://lucene.apache.org .
3
+ _Esnek_ is a minimalistic client for any Web API in general and no API
4
+ in particular. Esnek has been used in production for a few years already.
5
+ ElasticSearch, Google APIs, Facebook Graph API, LinkedIn, Facebook,
6
+ Twitter OAuth login integrations.
7
+
5
8
 
6
9
  == Installation:
7
10
 
8
- gem install esnek # In april-june 2011, under development so use "gem update" frequently.
11
+ gem install esnek
9
12
 
10
13
  == Quick Start
11
14
 
15
+ === Google URL Shortener
16
+ require 'esnek'
17
+ gapi = Esnek.new('https://www.googleapis.com')
18
+ res = gapi.urlshortener.v1.url.post {{:longUrl => "http://www.resimit.com/"}}
19
+ # Use res.table[:id] instead of res.id, because id method already exist for Object
20
+ puts res.longUrl
21
+ puts res.inspect
22
+
23
+ === Facebook Graph API
24
+ require 'esnek'
25
+ fb = Esnek.new('http://graph.facebook.com')
26
+ res = fb.send(:"http://www.resimit.com").get
27
+ # Notice that since "http://www.resimit.com" is a complex ruby method name, we use "send" to call it
28
+ puts res.shares
29
+
12
30
  === ElasticSearch
13
31
 
14
- To install ElasticSearch please follow the guides at http://www.elasticsearch.org.
15
32
  To use esnek just instantiate Esnek with the base API URL.
16
33
  require 'esnek'
17
34
  es = Esnek.new('http://localhost:9200')
@@ -37,23 +54,6 @@ In order to post a JSON data simply pass a block (do...end) which returns a hash
37
54
  {"user" => "alper", "post_date" => "2011-11-15T14:12:12", "message" => "For esnek"}
38
55
  end
39
56
 
40
- === Google Translate API
41
- require 'esnek'
42
- gapi = Esnek.new('https://www.googleapis.com')
43
- gapi.language.translate.v2.get :q => "hello world", :source => :en, :target => :tr, :key => INSERT_YOUR_KEY
44
-
45
- === Google URL Shortener
46
- require 'esnek'
47
- gapi = Esnek.new('https://www.googleapis.com')
48
- res = gapi.urlshortener.v1.url.post {{:longUrl => "http://www.sayarus.com/"}}
49
- puts res.longUrl # Use res.table[:id] instead of res.id, because id method already exist for Object
50
-
51
- === Facebook Graph API
52
- require 'esnek'
53
- fb = Esnek.new('http://graph.facebook.com')
54
- res = fb.send(:"http://www.wsirussia.ru").get
55
- Notice that since "http://www.wsirussia.ru" is an invalid ruby method name, we use "send" to call it
56
- puts res.shares
57
57
 
58
58
  == Advanced
59
59
  With Esnek you make a chained method call where each method is a directory in the URL
@@ -88,7 +88,8 @@ If you append a block and the block returns a hash, the hash is converted into a
88
88
  end
89
89
 
90
90
  === Return Values
91
- Esnek converts the returned JSON into an Ostruct object.
91
+ Esnek converts the returned JSON into an Ostruct object. You may use
92
+ the returned object's inspect method to see how it works for a given API.
92
93
 
93
94
  res.my_field # access as if it is an object's attrribute
94
95
  res.table[:my_field] # For field names such as "id", you may use the "table" attribute.
@@ -113,4 +114,5 @@ variable to avoid modifying the code (in this case you can use a file name, "std
113
114
 
114
115
  == Notes
115
116
 
116
- Esnek was initially developed using Redcar under Ubuntu Linux 10.10.
117
+ Esnek was initially developed using Emacs under Ubuntu Linux.
118
+
data/Rakefile CHANGED
@@ -1,28 +1,8 @@
1
- require 'rubygems'
2
- require 'rake'
3
- require 'rake/clean'
4
- require 'rake/gempackagetask'
5
- require 'rake/rdoctask'
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'esnek/version'
6
4
  require 'rake/testtask'
7
- require './lib/esnek/version'
8
-
9
-
10
- spec = eval(File.read('esnek.gemspec'))
11
-
12
- Rake::GemPackageTask.new(spec) do |p|
13
- p.gem_spec = spec
14
- p.need_tar = false
15
- p.need_zip = false
16
- end
17
-
18
- Rake::RDocTask.new do |rdoc|
19
- files =['README.rdoc', 'LICENSE','CHANGES', 'AUTHORS', 'lib/**/*.rb', 'test/**/*.rb']
20
- rdoc.rdoc_files.add(files)
21
- rdoc.main = "README"
22
- rdoc.title = "Esnek Documentation"
23
- rdoc.rdoc_dir = 'doc/rdoc'
24
- rdoc.options
25
- end
5
+ require "bundler/gem_tasks"
26
6
 
27
7
  Rake::TestTask.new do |t|
28
8
  t.test_files = FileList['test/**/*.rb']
data/esnek.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'esnek/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'esnek'
8
+ s.version = ESNEK_VERSION
9
+ #s.has_rdoc = true
10
+ s.extra_rdoc_files = ['README.rdoc', 'LICENSE','CHANGES','AUTHORS']
11
+ s.summary = 'Esnek provides a minimalistic Ruby interface for JSON APIs, such as ElasticSearch'
12
+ s.description = s.summary
13
+ s.authors = ['Alper Akgun']
14
+ s.email = 'esnek@sayarus.com'
15
+ s.homepage = "https://github.com/sayarus/esnek"
16
+ s.license = "MIT"
17
+ s.files = `git ls-files -z`.split("\x0")
18
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
+ s.require_paths = ["lib"]
21
+ s.bindir = "bin"
22
+ s.add_dependency("json", ">= 1.4")
23
+ s.add_dependency("rest-client", ">= 1.6.1")
24
+ s.add_development_dependency "bundler", "~> 1.6"
25
+ s.add_development_dependency "rake", "~> 10.0"
26
+ end
27
+
28
+
29
+
30
+
31
+
32
+
data/lib/esnek/base.rb CHANGED
@@ -1,14 +1,21 @@
1
1
  require 'rest_client'
2
2
  require 'json'
3
- require 'ostruct'
4
3
 
5
4
  # _Esnek_ provides a quick Ruby interface for JSON APIs, such as _ElasticSearch_ (http://www.elasticsearch.org); a scalable, fast, distributed,
6
5
  # highly-available, real time search RESTful search engine communicating by JSON over HTTP, based on _Lucene_ (http://lucene.apache.org).
7
6
  class Esnek
8
- attr_accessor :chain, :url_root
9
- def initialize(url_root)
10
- @url_root = url_root
11
- @chain = []
7
+ #attr_accessor :esnek_chain, :esnek_url_root, :esnek_params, :esnek_url
8
+ def initialize(esnek_url_root,options={:json_api=>true,:json_return=>true, :header=>{}})
9
+ @esnek_url_root = esnek_url_root
10
+ @esnek_chain = []
11
+ @json_api = options[:json_api].nil? ? true : options[:json_api]
12
+ @json_return = options[:json_return].nil? ? @json_api : options[:json_return]
13
+ @header= options[:header] || {}
14
+ if options[:oauth] # Esnek assumes that oauth ruby gem is installed
15
+ options[:oauth][:scheme] ||= :header
16
+ consumer = OAuth::Consumer.new(options[:oauth][:consumer_key],options[:oauth][:consumer_secret], {:site => options[:oauth][:site], :scheme => options[:oauth][:scheme]})
17
+ @access_token = OAuth::AccessToken.from_hash(consumer, {:oauth_token => options[:oauth][:oauth_token], :oauth_token_secret => options[:oauth][:oauth_token_secret]} )
18
+ end
12
19
  end
13
20
 
14
21
  def parse_json(resp)
@@ -19,39 +26,64 @@ class Esnek
19
26
  class << r;def table;@table;end;end
20
27
  r
21
28
  when j.is_a?(Array)
22
- j.map{|e| r=OpenStruct.new(e);class << r;def table;@table;end;end;r}
29
+ j.map{|e| r= if e.is_a?(Hash)
30
+ OpenStruct.new(e)
31
+ class<<r;def table;@table;end;end;
32
+ else
33
+ e
34
+ end
35
+ r}
23
36
  else
24
37
  j
25
38
  end
26
39
  end
27
40
  def method_missing(method_sym, *args, &block)
28
- if [:get, :put, :post, :delete].include?(method_sym)
29
- @chain << {:method => nil, :arg => (args.empty? ? {} : args[0]) }
30
- url = @url_root.gsub(/\/$/,'') + '/' + @chain.map{|e| e[:method]}.compact.join('/')
31
- params = @chain.inject({}){|s,e| s.merge!(e[:arg] || {}) if e[:arg].is_a?(Hash)}
32
- if block_given?
33
- data = block.call.to_json rescue nil
41
+ if [:get, :put, :post, :delete, :patch, :head].include?(method_sym)
42
+ @esnek_chain << {:method => nil, :arg => (args.empty? ? {} : args[0]) }
43
+ @esnek_url = @esnek_url_root.gsub(/\/$/,'') + '/' + @esnek_chain.map{|e| e[:method]}.compact.join('/')
44
+ @esnek_params = @esnek_chain.inject({}){|s,e| s.merge!(e[:arg] || {}) if e[:arg].is_a?(Hash)}
45
+ @esnek_chain = []
46
+ heades = {:params => @esnek_params}.merge(@header)
47
+ data = block_given? ? block.call : nil #rescue nil
48
+
49
+ # if a JSON api is set in initializer both the payload data and the result will be un/jsonized
50
+ if @json_api
51
+ heades.merge!({:content_type => :json, :accept => :json})
52
+ data = data.to_json if data #rescue nil
53
+ elsif data.is_a? Hash
54
+ data = data.map{|k,v| "#{CGI::escape(k)}=#{CGI::escape(v)}"}.join('&')
55
+ end
56
+ # if a oauth token exist, use it; unfortunately restclient does not allow a proper
57
+ RestClient.reset_before_execution_procs
58
+ RestClient.add_before_execution_proc do |req, par|
59
+ @access_token.sign! req
60
+ end if @access_token
61
+ resp = if [:put, :post,:patch].include?(method_sym)
62
+ RestClient.send(method_sym, @esnek_url, data, heades)
63
+ else
64
+ RestClient.send(method_sym, @esnek_url, heades)
65
+ end
66
+
67
+ if @json_return
68
+ parse_json(resp)
69
+ else
70
+ resp
34
71
  end
35
- @chain = []
36
- resp = block_given? ? RestClient.send(method_sym, url, data,:params => params,:content_type => :json, :accept => :json) :
37
- RestClient.send(method_sym, url,:params => params,:content_type => :json, :accept => :json)
38
-
39
- resp.headers[:content_type].include?('application/json') ? parse_json(resp) : resp
40
72
  else
41
- @chain << {:method => method_sym.to_s.gsub(/^__/,''), :arg => (args.empty? ? {} : args[0]) }
73
+ @esnek_chain << {:method => method_sym.to_s.gsub(/^__/,''), :arg => (args.empty? ? {} : args[0]) }
42
74
  self
43
75
  end
44
76
  rescue
45
- @chain = []
77
+ @esnek_chain = []
46
78
  raise $!
47
79
  end
48
-
80
+
49
81
  def respond_to?(method_sym)
50
- if [:get, :put, :post, :delete].include?(method_sym)
82
+ if [:head,:get, :put, :post,:patch, :delete].include?(method_sym)
51
83
  true
52
84
  else
53
85
  super
54
86
  end
55
87
  end
56
-
57
- end
88
+
89
+ end
data/lib/esnek/version.rb CHANGED
@@ -1 +1 @@
1
- ESNEK_VERSION='0.2.6'
1
+ ESNEK_VERSION='0.5.0'
data/test/base.rb ADDED
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require './lib/esnek'
4
+ require 'minitest/autorun'
5
+
6
+ describe Esnek do
7
+ before do
8
+ end
9
+
10
+ describe "When I query Google URL Shortener API" do
11
+ it "should return a shortened URL" do
12
+ gapi = Esnek.new('https://www.googleapis.com')
13
+ res = gapi.urlshortener.v1.url.post {{:longUrl => "http://www.resimit.com/"}}
14
+ puts res.inspect
15
+ end
16
+ end
17
+
18
+ describe "When I query Facebook graph API to count the # of shares of url" do
19
+ it "should return an object which responds to :shares" do
20
+ fb = Esnek.new('http://graph.facebook.com')
21
+ res = fb.send(:"http://www.resimit.com").get
22
+ assert res.respond_to?(:shares)
23
+ puts res.inspect
24
+ end
25
+ end
26
+
27
+ end
metadata CHANGED
@@ -1,106 +1,117 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: esnek
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 6
10
- version: 0.2.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Alper Akgun
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-11-06 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2014-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: json
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
26
17
  - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 1
29
- segments:
30
- - 1
31
- - 5
32
- - 1
33
- version: 1.5.1
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
34
20
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: rest-client
38
21
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
42
31
  - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 13
45
- segments:
46
- - 1
47
- - 6
48
- - 1
32
+ - !ruby/object:Gem::Version
49
33
  version: 1.6.1
50
34
  type: :runtime
51
- version_requirements: *id002
52
- description: Esnek provides a minimalistic Ruby interface for JSON APIs, such as ElasticSearch
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Esnek provides a minimalistic Ruby interface for JSON APIs, such as
70
+ ElasticSearch
53
71
  email: esnek@sayarus.com
54
72
  executables: []
55
-
56
73
  extensions: []
57
-
58
- extra_rdoc_files:
74
+ extra_rdoc_files:
59
75
  - README.rdoc
60
76
  - LICENSE
61
77
  - CHANGES
62
78
  - AUTHORS
63
- files:
79
+ files:
80
+ - ".gitignore"
81
+ - AUTHORS
82
+ - CHANGES
83
+ - Gemfile
64
84
  - LICENSE
65
85
  - README.rdoc
66
86
  - Rakefile
67
- - AUTHORS
68
- - CHANGES
87
+ - esnek.gemspec
88
+ - lib/esnek.rb
69
89
  - lib/esnek/base.rb
70
90
  - lib/esnek/version.rb
71
- - lib/esnek.rb
91
+ - test/base.rb
72
92
  homepage: https://github.com/sayarus/esnek
73
- licenses: []
74
-
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
75
96
  post_install_message:
76
97
  rdoc_options: []
77
-
78
- require_paths:
98
+ require_paths:
79
99
  - lib
80
- required_ruby_version: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
83
102
  - - ">="
84
- - !ruby/object:Gem::Version
85
- hash: 3
86
- segments:
87
- - 0
88
- version: "0"
89
- required_rubygems_version: !ruby/object:Gem::Requirement
90
- none: false
91
- requirements:
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
92
107
  - - ">="
93
- - !ruby/object:Gem::Version
94
- hash: 3
95
- segments:
96
- - 0
97
- version: "0"
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
98
110
  requirements: []
99
-
100
111
  rubyforge_project:
101
- rubygems_version: 1.7.2
112
+ rubygems_version: 2.2.2
102
113
  signing_key:
103
- specification_version: 3
114
+ specification_version: 4
104
115
  summary: Esnek provides a minimalistic Ruby interface for JSON APIs, such as ElasticSearch
105
- test_files: []
106
-
116
+ test_files:
117
+ - test/base.rb