class Ucpaas::Client
client for ucpass service
Constants
- ATTRIBUTES
Attributes
logger[RW]
Public Class Methods
new(sid, token)
click to toggle source
# File lib/ucpaas/client.rb, line 38 def initialize(sid, token) @sid, @token = sid, token @base_url = 'https://api.ucpaas.com' @soft_version = '2014-06-30' @logger = Logger.new(STDOUT) @logger.level = Logger::INFO end
Public Instance Methods
formated_time()
click to toggle source
# File lib/ucpaas/client.rb, line 103 def formated_time Time.now.getlocal('+08:00').strftime('%Y%m%d%H%M%S') end
get(origin, params = {})
click to toggle source
# File lib/ucpaas/client.rb, line 46 def get(origin, params = {}) logger.info { format('GET: %s %s', origin, params) } resp = connection.get do |request| time = formated_time request.url path(origin), params.merge(sig: sign(time)) request.headers.merge!(headers(time)) end handle(resp) end
handle(resp)
click to toggle source
# File lib/ucpaas/client.rb, line 67 def handle(resp) logger.debug { resp } fail UcpaasError, resp.status unless resp.status == 200 response = MultiJson.load(resp.body) resp_code = response['resp']['respCode'] return response if resp_code == '000000' handle_error(response) end
handle_error(response)
click to toggle source
# File lib/ucpaas/client.rb, line 76 def handle_error(response) logger.error response code = response['resp']['respCode'] fail ERRORS[code[0..2]], code end
headers(time)
click to toggle source
# File lib/ucpaas/client.rb, line 86 def headers(time) { accept: 'application/json', content_type: 'application/json;charset=utf-8;', authorization: authorization(time) } end
path(origin)
click to toggle source
# File lib/ucpaas/client.rb, line 82 def path(origin) format('%s/%s/Accounts/%s%s', base_url, soft_version, sid, origin) end
post(origin, params)
click to toggle source
# File lib/ucpaas/client.rb, line 56 def post(origin, params) logger.info { format('POST: %s %s', origin, params) } resp = connection.post do |request| time = formated_time request.url path(origin), sig: sign(time) request.headers.merge!(headers(time)) request.body = MultiJson.dump(params) end handle(resp) end
sign(time)
click to toggle source
# File lib/ucpaas/client.rb, line 94 def sign(time) data = format('%s%s%s', sid, token, time) Digest::MD5.hexdigest(data).upcase end
Private Instance Methods
connection()
click to toggle source
# File lib/ucpaas/client.rb, line 109 def connection @connection ||= begin Faraday.new do |faraday| faraday.adapter Faraday.default_adapter end end end