Class: YuntongxunSdk::API

Inherits:
Object
  • Object
show all
Defined in:
lib/yuntongxun_sdk/api.rb

Defined Under Namespace

Classes: RequestFailureError

Instance Method Summary collapse

Constructor Details

#initialize(account_sid = YuntongxunSdk.config.account_sid, auth_token = YuntongxunSdk.config.auth_token, app_id = YuntongxunSdk.config.app_id) ⇒ API

Returns a new instance of API.



3
4
5
6
7
# File 'lib/yuntongxun_sdk/api.rb', line 3

def initialize( = YuntongxunSdk.config., auth_token = YuntongxunSdk.config.auth_token, app_id = YuntongxunSdk.config.app_id)
  @account_sid = 
  @auth_token = auth_token
  @app_id = app_id
end

Instance Method Details

#api(path, args = {}, verb = "post", options = {}) ⇒ Object

Note:

You’ll rarely need to call this method directly.

Makes a request to the appropriate YuntongxunSdk API.

Parameters:

  • path

    the server path for this request (leading / is prepended if not present)

  • args (defaults to: {})

    arguments to be sent to YuntongxunSdk

  • verb (defaults to: "post")

    the HTTP method to use

  • options (defaults to: {})

    request-related options



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/yuntongxun_sdk/api.rb', line 33

def api(path, args = {}, verb = "post", options = {})
  args = {
    appId: @app_id
  }.merge(args)

  batch = DateTime.now.in_time_zone("Beijing").strftime("%Y%m%d%H%M%S")
  signature = "#{@account_sid}#{@auth_token}#{batch}"
  sig = Digest::MD5.hexdigest(signature).upcase

  gateway = options.delete(:gateway) || YuntongxunSdk.config.gateway
  # add a leading / if needed...
  path = "/#{path}" unless path =~ /^\//
  url = "#{gateway}/2013-12-26/Accounts/#{@account_sid}#{path}?sig=#{sig}"

  headers = {
    "Accept" => "application/json",
    "Content-Type" => "application/json;charset=utf-8",
    "Authorization" => Base64.strict_encode64("#{@account_sid}:#{batch}")
  }

  response = YuntongxunSdk.make_request(url, verb, args, headers)
  build_reponse(response)
end