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 |
# 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) @client.auth.token.request(method, uri_path, :headers => client_headers, :params => params) rescue OAuth2::Error => e raise "Invaild API: '#{@client.site}/#{uri_path}' with message:\n#{e.message}" end end |