Module: YunTongXun

Defined in:
lib/yun_tong_xun.rb,
lib/yun_tong_xun/calls.rb,
lib/yun_tong_xun/config.rb,
lib/yun_tong_xun/result_handler.rb

Defined Under Namespace

Modules: Calls Classes: Config, ResultHandler

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



5
6
7
# File 'lib/yun_tong_xun/config.rb', line 5

def config
  @config
end

Class Method Details

.api_hostObject



68
69
70
# File 'lib/yun_tong_xun.rb', line 68

def api_host
  config.env.to_s == 'production' ? 'https://app.cloopen.com:8883' : 'https://sandboxapp.cloopen.com:8883'
end

.authorization(sid, timestamp) ⇒ Object



60
61
62
# File 'lib/yun_tong_xun.rb', line 60

def authorization(sid, timestamp)
  Base64.encode64("#{sid}:#{timestamp}").gsub(/\s/, '')
end

.base_endpointObject



72
73
74
# File 'lib/yun_tong_xun.rb', line 72

def base_endpoint
  "#{api_host}/#{config.soft_version}"
end

.configure {|self.config ||= Config.new| ... } ⇒ Object

Yields:



7
8
9
# File 'lib/yun_tong_xun/config.rb', line 7

def configure
  yield self.config ||= Config.new
end

.endpoint_url(url) ⇒ Object



76
77
78
# File 'lib/yun_tong_xun.rb', line 76

def endpoint_url(url)
  "#{base_endpoint}#{url}"
end

.http_get(options) ⇒ Object



18
19
20
21
# File 'lib/yun_tong_xun.rb', line 18

def http_get(options)
  r = resource(options)
  ResultHandler.new(r.get)
end

.http_post(options, post_body) ⇒ Object



13
14
15
16
# File 'lib/yun_tong_xun.rb', line 13

def http_post(options, post_body)
  r = resource(options)
  ResultHandler.new(r.post(post_body.to_json))
end

.option_value(options, key) ⇒ Object



80
81
82
83
84
# File 'lib/yun_tong_xun.rb', line 80

def option_value(options, key)
  return options[key.to_s] if options.key?(key.to_s)
  return options[key.intern] if options.key?(key.intern)
  nil
end

.resource(options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/yun_tong_xun.rb', line 23

def resource(options)
   = option_value(options, 'account_sid')
   = option_value(options, 'account_token')
   = option_value(options, 'sub_account_sid')
   = option_value(options, 'sub_account_token')
  func = option_value(options, 'func')
  funcdes = option_value(options, 'funcdes')
  timestamp = option_value(options, 'timestamp') || Time.now.strftime('%Y%m%d%H%M%S')

  if 
    url_prefix = "/Accounts/#{}"
    auth = authorization(, timestamp)
    sign = signature(, , timestamp)
  elsif 
    url_prefix = "/SubAccounts/#{}"
    auth = authorization(, timestamp)
    sign = signature(, , timestamp)
  else
    fail "account_sid or sub_account_sid is required"
  end

  url = "#{url_prefix}/#{func}/#{funcdes}?sig=#{sign}"

  headers = option_value(config.rest_client_options, 'headers') || {}
  headers = headers.merge(
    'Accept' => 'application/json',
    'Content-Type' => 'json',
    'Authorization' => auth
  )

  opts = {
    headers: headers
  }

  RestClient::Resource.new(endpoint_url(url), config.rest_client_options.merge(opts))
end

.signature(sid, token, timestamp) ⇒ Object



64
65
66
# File 'lib/yun_tong_xun.rb', line 64

def signature(sid, token, timestamp)
  Digest::MD5.hexdigest("#{sid}#{token}#{timestamp}").upcase
end