elastirad 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95156251835b98cdd37c6609f5e11e3c22fc5887
4
+ data.tar.gz: 607df84376fee0048434528edc0222b1286bdda5
5
+ SHA512:
6
+ metadata.gz: 64ff790b4f28a2253228e13f36a5dd21703486c361abf5b62c4e9873ccf2bf6407900329e502595c463ae81a8b827a693b2c6aa16f0428f6e55e5343c92a416d
7
+ data.tar.gz: 50c43595c370835210c2e232fbc7069ffec012d047a9fb0cfad0b6bd4680a3c2c2d22b46867d64fb2260c1be7c9b907c54392182f8c982e06683e5a04bb1b453
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ CHANGELOG
2
+ ---------
3
+ - **2014-03-14**: 0.0.1
4
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 John Wang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ Elastirad: A RAD Client for Elasticsearch
2
+ ====================================================
3
+
4
+ Synopsis
5
+ --------
6
+
7
+ Elastirad is a custom rapid application development (RAD) client for
8
+ Elasticsearch's Elasticsearch::API that provides a simple interface for
9
+ making Elasticsearch requests based on Elasticsearch's online documentation.
10
+
11
+ The primary goal for Elastirad is to enable use of the Elasticsearch online
12
+ curl-based documentation alone without needing to understand the syntax for
13
+ the Ruby SDK.
14
+
15
+ Installing
16
+ ----------
17
+
18
+ Download and install elastirad with the following:
19
+
20
+ gem install elastirad
21
+
22
+ #Examples
23
+ ---------
24
+
25
+ require 'elastirad'
26
+
27
+ # Defaults to http://localhost:9200
28
+
29
+ rad = Elastirad::Client.new
30
+
31
+ # All of the following arguments are optional
32
+ # Setting :index will enable request code to not include the index
33
+ # for greater flexibility when switching between deployments, e.g.
34
+ # dev, staging, production, etc.
35
+
36
+ rad = Elastirad::Client.new( \
37
+ :protocol => 'https',
38
+ :hostname => 'localhost',
39
+ :port => 9200,
40
+ :index => 'articles'
41
+ )
42
+
43
+ # path can be a simple string. Leading slash will over ride default :index
44
+
45
+ result_hash = rad.rad_request({ :path => '/articles/_count' })
46
+
47
+ # path can also be an array
48
+
49
+ result_hash = rad.rad_request({ :path => ['/articles/article', 1 ] })
50
+
51
+ # default index can be used without leading slash
52
+
53
+ result_hash = rad.rad_request({ :path => ['article', 1 ] })
54
+
55
+ # optional :verb can be used for non-GET requests, :get is used by default
56
+
57
+ article = { :title => 'Hello World', :by => 'John Doe' }
58
+
59
+ result_hash = rad.rad_request({ \
60
+ :verb => 'put',
61
+ :path => ['article', 1 ],
62
+ :body => article
63
+ })
64
+
65
+ # :body can be a hash or JSON string
66
+
67
+ result_hash = rad.rad_request({ \
68
+ :verb => 'put',
69
+ :path => ['article', 1 ],
70
+ :body => JSON.dump( article )
71
+ })
72
+
73
+ # :put verb can automatically be added using #rad_index method
74
+
75
+ result_has = rad.rad_index({ \
76
+ :path => ['article', 1 ],
77
+ :body => article
78
+ })
79
+
80
+ #Documentation
81
+ --------------
82
+
83
+ This gem is 100% documented with YARD, an exceptional documentation library. To see documentation for this, and all the gems installed on your system use:
84
+
85
+ $ gem install yard
86
+ $ yard server -g
87
+
88
+ #Links
89
+ ------
90
+
91
+ Elasticsearch::API
92
+
93
+ https://github.com/elasticsearch/elasticsearch-ruby/tree/master/elasticsearch-api
94
+
95
+ #Copyright and License
96
+ ----------------------
97
+
98
+ Elastirad © 2014 by [John Wang](mailto:johncwang@gmail.com).
99
+
100
+ Elastirad is licensed under the MIT license. Please see the LICENSE document for more information.
101
+
102
+ Warranty
103
+ --------
104
+
105
+ This software is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :test
6
+
7
+ desc 'Test the JsonDoc library.'
8
+ Rake::TestTask.new do |t|
9
+ t.libs << 'lib'
10
+ t.pattern = 'test/**/test_*.rb'
11
+ t.verbose = false
12
+ end
13
+
14
+ desc 'Generate YARD documentation.'
15
+ task :gendoc do
16
+ #puts 'yard doc generation disabled until JRuby build native extensions for redcarpet or yard removes the dependency.'
17
+ system "yardoc"
18
+ system "yard stats --list-undoc"
19
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,128 @@
1
+ require 'multi_json'
2
+ require 'faraday'
3
+ require 'elasticsearch/api'
4
+
5
+ module Elastirad
6
+ class Client
7
+ include Elasticsearch::API
8
+
9
+ def initialize(dOptions={})
10
+ iPort = iPort.to_i if iPort.is_a?(String)
11
+ @sProtocol = dOptions.has_key?(:protocol) && dOptions[:protocol] \
12
+ ? dOptions[:protocol] : 'http'
13
+ @sHostname = dOptions.has_key?(:hostname) && dOptions[:hostname] \
14
+ ? dOptions[:hostname] : 'localhost'
15
+ @iPort = dOptions.has_key?(:port) && dOptions[:port] \
16
+ ? dOptions[:port].to_i : 9200
17
+ @sIndex = dOptions.has_key?(:index) && dOptions[:index] \
18
+ ? dOptions[:index].strip : nil
19
+ @sUrl = self.makeUrl(@sProtocol,@sHostname,@iPort)
20
+ @oFaraday = Faraday::Connection.new url: @sUrl || 'http://localhost:9200'
21
+ @dVerbs = {:put=>1,:get=>1,:post=>1,:delete=>1}
22
+ end
23
+
24
+ def makeUrl(sProtocol='http',sHostname='localhost',iPort=9200)
25
+ if sHostname.nil?
26
+ sHostname = 'localhost'
27
+ elsif sHostname.is_a?(String)
28
+ sHostname.strip!
29
+ if sHostname.length < 1
30
+ sHostname = 'localhost'
31
+ end
32
+ else
33
+ raise ArgumentError, 'E_HOSTNAME_IS_NOT_A_STRING'
34
+ end
35
+ if iPort.nil?
36
+ iPort = 9200
37
+ elsif iPort.is_a?(String) && iPort =~ /^[0-9]+$/
38
+ iPort = iPort.to_i
39
+ elsif ! iPort.kind_of?(Integer)
40
+ raise ArgumentError, 'E_PORT_IS_NOT_AN_INTEGER'
41
+ end
42
+ sBaseUrl = "#{sProtocol}://#{sHostname}"
43
+ sBaseUrl.sub!(/\/+\s*$/,'')
44
+ sBaseUrl += ':' + iPort.to_s if iPort != 80
45
+ return sBaseUrl
46
+ end
47
+
48
+ def rad_index(dEsRes={})
49
+ dEsRes[:verb] = :put
50
+ return self.request(dEsRes)
51
+ end
52
+
53
+ def rad_request(dEsReq={})
54
+ oEsRes = self.perform_request \
55
+ getVerbFromEsReq(dEsReq),
56
+ getPathFromEsReq(dEsReq),
57
+ nil,
58
+ getBodyFromEsReq(dEsReq)
59
+
60
+ dEsResBody = oEsRes.body \
61
+ ? MultiJson.load( oEsRes.body, :symbolize_keys => true ) : nil
62
+ end
63
+
64
+ def perform_request(method,path,params,body)
65
+ @oFaraday.run_request \
66
+ method.downcase.to_sym,
67
+ path,
68
+ body,
69
+ {'Content-Type' => 'application/json'}
70
+ end
71
+
72
+ private
73
+
74
+ def getVerbFromEsReq(dEsReq={})
75
+ sVerb = dEsReq.has_key?(:verb) ? dEsReq[:verb] : :get
76
+ yVerb = sVerb.downcase.to_sym
77
+ unless @dVerbs.has_key?( yVerb )
78
+ raise ArgumentError, 'E_BAD_VERB'
79
+ end
80
+ return yVerb
81
+ end
82
+
83
+ def getPathFromEsReq(dEsReq={})
84
+ aPath = []
85
+ bHasIndex = false
86
+
87
+ if dEsReq.has_key?(:path)
88
+ if dEsReq[:path].is_a?(Array)
89
+ aPath.push(*dEsReq[:path])
90
+ elsif dEsReq[:path].is_a?(String)
91
+ aPath.push(dEsReq[:path])
92
+ end
93
+ else
94
+ if dEsReq.has_key?(:index) && dEsReq[:index].is_a?(String)
95
+ aPath.push(dEsReq[:index])
96
+ bHasIndex = true
97
+ end
98
+ if dEsReq.has_key?(:type) && dEsReq[:type].is_a?(String)
99
+ aPath.push(dEsReq[:type])
100
+ end
101
+ if dEsReq.has_key?(:id) && dEsReq[:id].is_a?(String)
102
+ aPath.push(dEsReq[:id])
103
+ end
104
+ end
105
+ sPath = aPath.join('/')
106
+ sPath.strip!
107
+ sPath.gsub!(/\/+/,'/')
108
+ if (sPath.index('/')!=0 || !bHasIndex) && ( @sIndex.is_a?(String) && @sIndex.length>0 )
109
+ sPath = "#{@sIndex}/#{sPath}"
110
+ end
111
+ return sPath
112
+ end
113
+
114
+ def getBodyFromEsReq(dEsReq={})
115
+ jBody = nil
116
+ if dEsReq.has_key?(:body)
117
+ if dEsReq[:body].is_a?(Hash)
118
+ jBody = MultiJson.dump(dEsReq[:body])
119
+ elsif dEsReq[:body].is_a?(String) && dEsReq[:body].length > 0
120
+ jBody = dEsReq[:body]
121
+ end
122
+ jBody = nil if jBody == '{}' || jBody.length < 1
123
+ end
124
+ return jBody
125
+ end
126
+
127
+ end
128
+ end
data/lib/elastirad.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Elastirad
2
+ autoload :Client, 'elastirad/client'
3
+ end
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'elastirad'
3
+
4
+ class ElastiradTest < Test::Unit::TestCase
5
+ def testSetup
6
+
7
+ rad1 = Elastirad::Client.new
8
+
9
+ assert_equal 'elastirad/objects1', rad1.instance_eval{ getPathFromEsReq({:path=>"elastirad/objects1"}) }
10
+ assert_equal 'elastirad/objects2', rad1.instance_eval{ getPathFromEsReq({:path=>["elastirad/objects2"]}) }
11
+ assert_equal 'elastirad/objects3', rad1.instance_eval{ getPathFromEsReq({:path=>["elastirad/","/objects3"]}) }
12
+ assert_equal '{"query":{"match_all":{}}}', rad1.instance_eval{ getBodyFromEsReq({:body=>{:query=>{:match_all=>{}}}}) }
13
+
14
+ rad2 = Elastirad::Client.new(:index => 'elastirad')
15
+
16
+ assert_equal 'elastirad/objects/id', rad2.instance_eval{ getPathFromEsReq({:path=>"objects/id"}) }
17
+
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elastirad
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Wang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: elasticsearch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ - !ruby/object:Gem::Dependency
54
+ name: multi_json
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ description: A simple Elasticsearch client
74
+ email: john@johnwang.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - CHANGELOG.md
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - VERSION
84
+ - lib/elastirad.rb
85
+ - lib/elastirad/client.rb
86
+ - test/test_setup.rb
87
+ homepage: http://johnwang.com/
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Elastirad - a RAD Elasticsearch client supporting minimal reading of documentation
111
+ test_files: []