Class: Esnek
- Inherits:
-
Object
- Object
- Esnek
- Defined in:
- lib/esnek/base.rb
Overview
Esnek provides a quick Ruby interface for JSON APIs, such as ElasticSearch (www.elasticsearch.org); a scalable, fast, distributed, highly-available, real time search RESTful search engine communicating by JSON over HTTP, based on Lucene (lucene.apache.org).
Instance Method Summary collapse
-
#initialize(esnek_url_root, options = {:verify_ssl=>true, :json_api=>true,:json_return=>true, :header=>{}}) ⇒ Esnek
constructor
attr_accessor :esnek_chain, :esnek_url_root, :esnek_params, :esnek_url.
- #method_missing(method_sym, *args, &block) ⇒ Object
- #parse_json(resp) ⇒ Object
- #respond_to?(method_sym) ⇒ Boolean
Constructor Details
#initialize(esnek_url_root, options = {:verify_ssl=>true, :json_api=>true,:json_return=>true, :header=>{}}) ⇒ Esnek
attr_accessor :esnek_chain, :esnek_url_root, :esnek_params, :esnek_url
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/esnek/base.rb', line 8 def initialize(esnek_url_root, ={:verify_ssl=>true, :json_api=>true,:json_return=>true, :header=>{}}) @esnek_url_root = esnek_url_root @esnek_chain = [] @json_api = [:json_api].nil? ? true : [:json_api] @json_return = [:json_return].nil? ? @json_api : [:json_return] @verify_ssl = [:verify_ssl] @header= [:header] || {} if [:oauth] # Esnek assumes that oauth ruby gem is installed [:oauth][:scheme] ||= :header consumer = OAuth::Consumer.new([:oauth][:consumer_key],[:oauth][:consumer_secret], {:site => [:oauth][:site], :scheme => [:oauth][:scheme]}) @access_token = OAuth::AccessToken.from_hash(consumer, {:oauth_token => [:oauth][:oauth_token], :oauth_token_secret => [:oauth][:oauth_token_secret]} ) else @access_token = nil end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/esnek/base.rb', line 46 def method_missing(method_sym, *args, &block) if [:get, :put, :post, :delete, :patch, :head].include?(method_sym) @esnek_chain << {:method => nil, :arg => (args.empty? ? {} : args[0]) } @esnek_url = @esnek_url_root.gsub(/\/$/,'') + '/' + @esnek_chain.map{|e| e[:method]}.compact.join('/') @esnek_params = @esnek_chain.inject({}){|s,e| s.merge!(e[:arg] || {}) if e[:arg].is_a?(Hash)} @esnek_chain = [] heades = {:params => @esnek_params}.merge(@header) data = block_given? ? block.call : nil #rescue nil # if a JSON api is set in initializer both the payload data and the result will be un/jsonized if @json_api heades.merge!({:content_type => :json, :accept => :json}) data = data.to_json if data #rescue nil elsif data.is_a? Hash data = data.map{|k,v| "#{CGI::escape(k)}=#{CGI::escape(v)}"}.join('&') end # if a oauth token exist, use it; unfortunately restclient does not allow a proper RestClient.reset_before_execution_procs RestClient.add_before_execution_proc do |req, par| @access_token.sign! req end if @access_token resp = if [:put, :post,:patch].include?(method_sym) RestClient::Request.execute(:method=>method_sym, :url=>@esnek_url, :payload=>data, :headers=>heades, :verify_ssl=>@verify_ssl) else RestClient::Request.execute(:method=>method_sym, :url=>@esnek_url, :headers=>heades, :verify_ssl=>@verify_ssl) end if @json_return parse_json(resp) else resp end else @esnek_chain << {:method => method_sym.to_s.gsub(/^__/,''), :arg => (args.empty? ? {} : args[0]) } self end rescue @esnek_chain = [] raise $! end |
Instance Method Details
#parse_json(resp) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/esnek/base.rb', line 24 def parse_json(resp) j = JSON.parse resp case when j.is_a?(Hash) r = OpenStruct.new(j) class<<r;def table;@table;end;end; r when j.is_a?(Array) j.map do |e| r = if e.is_a?(Hash) rr = OpenStruct.new(e) class<<rr;def table;@table;end;end; rr else e end r end else j end end |
#respond_to?(method_sym) ⇒ Boolean
87 88 89 90 91 92 93 |
# File 'lib/esnek/base.rb', line 87 def respond_to?(method_sym) if [:head,:get, :put, :post,:patch, :delete].include?(method_sym) true else super end end |