Class: Qihu::DianJing::API::Base
- Inherits:
-
Object
- Object
- Qihu::DianJing::API::Base
- Defined in:
- lib/qihu/dianjing/api/base.rb
Instance Method Summary collapse
-
#initialize(client, options = {}) ⇒ Base
constructor
A new instance of Base.
- #method_missing(name, *args, &block) ⇒ Object
Constructor Details
#initialize(client, options = {}) ⇒ Base
Returns a new instance of Base.
5 6 7 |
# File 'lib/qihu/dianjing/api/base.rb', line 5 def initialize(client, ={}) @client = client end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/qihu/dianjing/api/base.rb', line 9 def method_missing(name, *args, &block) if name.match(/_/) name = name.to_s.split('_').each_with_index.map{|k, i| (i > 0) ? k.capitalize : k}.join("") end params = { :format => 'json' } params = params.merge(args[0]) if args[0].is_a?(Hash) begin allowed_methods = [:get, :post, :put, :delete] if params.include?(:method) mehtod = params.delete(:method).downcase.to_sym method = :get unless allowed_methods.include?method else method = name.match(/^get/) ? :get : :post end uri_path = uri_path(name) if method == :get r = @client.auth.token.request(method, uri_path, :headers => client_headers, :params => params) else r = @client.auth.token.request(method, uri_path, :headers => client_headers, :body => params) end if self.json?(r.headers) data = self.to_json(r.body) elsif self.xml?(r.headers) root = "#{self.url_section}_#{name}_response" data = self.to_xml(r.body) data = data[root] end if data.has_key?('failures') key = data['failures'].kind_of?(Hash) ? 'item' : 0 code = data['failures'][key]['code'].to_i error = data['failures'][key]['message'] raise Qihu::FailuresError.new(code, error) end return r rescue OAuth2::Error => e raise Qihu::InvailAPIError.new("#{@client.site}/#{uri_path}", e.) end end |