Class: Ucpaas::Client

Inherits:
Object
  • Object
show all
Includes:
Call, Manage, Sms
Defined in:
lib/ucpaas/client.rb

Overview

client for ucpass service

Constant Summary collapse

ATTRIBUTES =
%w(base_url sid token soft_version).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Manage

#clients, #find_or_create_client

Methods included from Call

#dull_call

Methods included from Sms

#send_sms

Constructor Details

#initialize(sid, token) ⇒ Client

Returns a new instance of Client.



38
39
40
41
42
43
44
# 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

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



36
37
38
# File 'lib/ucpaas/client.rb', line 36

def logger
  @logger
end

Instance Method Details

#authorization(time) ⇒ Object



99
100
101
# File 'lib/ucpaas/client.rb', line 99

def authorization(time)
  Base64.strict_encode64(format('%s:%s', sid, time))
end

#formated_timeObject



103
104
105
# 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 = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



67
68
69
70
71
72
73
74
# 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) ⇒ Object



76
77
78
79
80
# 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) ⇒ Object



86
87
88
89
90
91
92
# 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) ⇒ Object



82
83
84
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



94
95
96
97
# File 'lib/ucpaas/client.rb', line 94

def sign(time)
  data = format('%s%s%s', sid, token, time)
  Digest::MD5.hexdigest(data).upcase
end