class Qihu::DianJing::API::Base
Public Class Methods
new(client, options={})
click to toggle source
# File lib/qihu/dianjing/api/base.rb, line 5 def initialize(client, options={}) @client = client end
Public Instance Methods
method_missing(name, *args, &block)
click to toggle source
# 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.message) end end
Protected Instance Methods
client_headers()
click to toggle source
# File lib/qihu/dianjing/api/base.rb, line 87 def client_headers { :apiKey => @client.auth.token.client.id, :accessToken => @client.auth.token.token, :serveToken => Time.now.to_i.to_s, } end
json?(headers)
click to toggle source
Whether or not the content is json content type
@return [Boolean]
# File lib/qihu/dianjing/api/base.rb, line 75 def json?(headers) (headers[:content_type].include?'json') ? true : false end
to_json(body)
click to toggle source
Convert response content to JSON format
@return [Hash]
# File lib/qihu/dianjing/api/base.rb, line 60 def to_json(body) MultiJson.load(body) end
to_xml(body, parser=nil)
click to toggle source
Convert response content to XML format
@return [Hash]
# File lib/qihu/dianjing/api/base.rb, line 67 def to_xml(body, parser=nil) MultiXml.parser = p if parser and [:ox, :libxml, :nokogiri, :rexml].include?parser.to_sym MultiXml.parse(body) end
uri_path(uri)
click to toggle source
# File lib/qihu/dianjing/api/base.rb, line 99 def uri_path(uri) class_name = self.url_section "#{@client.version}/#{class_name}/#{uri}" end
url_section()
click to toggle source
# File lib/qihu/dianjing/api/base.rb, line 95 def url_section self.class.name.split("::")[-1].downcase end
xml?(headers)
click to toggle source
Whether or not the content is xml content type
@return [Boolean]
# File lib/qihu/dianjing/api/base.rb, line 82 def xml?(headers) (['xml', 'html'].any? { |word| headers[:content_type].include?(word) }) ? true : false end