module Rongcloud::Service
Constants
- API_URI
Public Class Methods
default_request_config(config)
click to toggle source
# File lib/rongcloud/service.rb, line 60 def self.default_request_config(config) config[:host] ||= Rongcloud.api_host config[:uri] ||= nil config[:params] ||= {} config[:headers] ||= Rongcloud::Sign.gen_headers config end
req_get(config)
click to toggle source
# File lib/rongcloud/service.rb, line 12 def self.req_get(config) config = default_request_config(config) conn = Faraday.new(:url => config[:host]) response = conn.get do |req| req.url config[:uri] config[:headers].each { |key, value| req.headers[key.to_s] = value.to_s } config[:params] && config[:params].each { |key, value| req.params[key.to_s] = value.to_s } end response_body = response.body warn("get response #{response_body}") res_data(response_body) end
req_post(config, post_format='urlencode')
click to toggle source
# File lib/rongcloud/service.rb, line 28 def self.req_post(config, post_format='urlencode') config = default_request_config(config) conn = Faraday.new(:url => config[:host]) response = conn.post do |req| req.url config[:uri] config[:headers].each { |key, value| req.headers[key.to_s] = value.to_s } if post_format.to_s == 'urlencode' req.headers['Content-Type'] = 'application/x-www-form-urlencoded' req.body = config[:params].collect { |key, value| "#{key}=#{value.to_s.urlencode}" }.join('&') elsif post_format.to_s == 'json' req.headers['Content-Type'] = 'application/json' req.body = config[:params].to_json end warn("post #{req.body}") end response_body = response.body warn("post response #{response_body}") res_data(response_body) end
res_data(response_body)
click to toggle source
# File lib/rongcloud/service.rb, line 51 def self.res_data(response_body) sym_keyed_hash(JSON.parse(response_body)) end
sym_keyed_hash(hash)
click to toggle source
返回key为sym的hash
# File lib/rongcloud/service.rb, line 56 def self.sym_keyed_hash(hash) hash.inject({}) { |memo, (key, v)| memo[key.to_sym]=v; memo } end