esnek 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -13,9 +13,22 @@ For the installation of elasticsearch please follow the guides at http://www.ela
13
13
  == Usage:
14
14
 
15
15
  require 'esnek'
16
- es = Esnek.new('http://localhost:9200')
16
+ es = Esnek.new('http://localhost:9200',true)
17
+
18
+ #curl -XGET 'http://localhost:9200/_cluster/state'
17
19
  es._cluster.state.get
18
20
 
21
+ #curl -XGET http://localhost:9200/twitter/tweet/_search?q=user:kimchy
22
+ es.twitter.tweet._search(:q => 'good').get
23
+
24
+ #curl -XGET 'http://localhost:9200/twitter/tweet/1'
25
+ es.twitter.tweet.__1.get
26
+
27
+ #curl -XPUT http://localhost:9200/twitter/tweet/2 -d '{ "user": "kimchy", "post_date": "2009-11-15T14:12:12", "message": "You know, for Search"}'
28
+ es.twitter.tweet.__2.put do
29
+ {"user" => "alper", "post_date" => "2011-11-15T14:12:12", "message" => "For esnek"}
30
+ end
31
+
19
32
  == Notes
20
33
 
21
34
  Esnek was initially developed using Redcar under Ubuntu Linux 10.10.
data/lib/esnek/base.rb CHANGED
@@ -2,23 +2,46 @@ require 'rest_client'
2
2
  require 'json'
3
3
  require 'ostruct'
4
4
 
5
+ # _Esnek_ provides a quick Ruby interface for JSON APIs, such as _ElasticSearch_ (http://www.elasticsearch.org); a scalable, fast, distributed,
6
+ # highly-available, real time search RESTful search engine communicating by JSON over HTTP, based on _Lucene_ (http://lucene.apache.org).
5
7
  class Esnek
6
8
  attr_accessor :chain, :url_root
7
- def initialize(url_root)
9
+ def initialize(url_root, debug=false)
8
10
  @url_root = url_root
11
+ @debug = debug
9
12
  @chain = []
10
13
  end
11
14
 
12
15
  def method_missing(method_sym, *args, &block)
13
16
  if [:get, :put, :post, :delete].include?(method_sym)
14
- url = @url_root.gsub(/\/$/,'') + '/' + @chain.map{|e| e[:method]}.join('/')
15
- params = @chain.inject({}){|s,e| s.merge!(e[:arg][:params] || {}) if e[:arg].is_a?(Hash)}
17
+ @chain << {:method => nil, :arg => (args.empty? ? {} : args[0]) }
18
+ url = @url_root.gsub(/\/$/,'') + '/' + @chain.map{|e| e[:method]}.compact.join('/')
19
+ params = @chain.inject({}){|s,e| s.merge!(e[:arg] || {}) if e[:arg].is_a?(Hash)}
20
+ if block_given?
21
+ data = block.call.to_json rescue nil
22
+ end
23
+ if @debug
24
+ puts url
25
+ puts data.inspect
26
+ puts @chain.inspect
27
+ end
16
28
  @chain = []
17
- OpenStruct.new JSON.parse RestClient.send(method_sym, url, {:params => params})
29
+
30
+ resp = block_given? ? RestClient.send(method_sym, url, data, :params => params) :
31
+ RestClient.send(method_sym, url,:params => params)
32
+
33
+ if resp.headers[:content_type].include?('application/json')
34
+ OpenStruct.new JSON.parse resp
35
+ else
36
+ resp
37
+ end
18
38
  else
19
- @chain << {:method => method_sym, :arg => args[0] || {} }
39
+ @chain << {:method => method_sym.to_s.gsub(/^__/,''), :arg => (args.empty? ? {} : args[0]) }
20
40
  self
21
41
  end
42
+ rescue
43
+ @chain = []
44
+ raise $!
22
45
  end
23
46
 
24
47
  def respond_to?(method_sym)
data/lib/esnek/version.rb CHANGED
@@ -1 +1 @@
1
- ESNEK_VERSION='0.1.1'
1
+ ESNEK_VERSION='0.1.2'
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: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alper Akgun