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
-
.config ⇒ Object
Returns the value of attribute config.
Class Method Summary collapse
- .api_host ⇒ Object
- .authorization(sid, timestamp) ⇒ Object
- .base_endpoint ⇒ Object
- .configure {|self.config ||= Config.new| ... } ⇒ Object
- .endpoint_url(url) ⇒ Object
- .http_get(options) ⇒ Object
- .http_post(options, post_body) ⇒ Object
- .option_value(options, key) ⇒ Object
- .resource(options) ⇒ Object
- .signature(sid, token, timestamp) ⇒ Object
Class Attribute Details
.config ⇒ Object
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_host ⇒ Object
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 (sid, ) Base64.encode64("#{sid}:#{}").gsub(/\s/, '') end |
.base_endpoint ⇒ Object
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
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() r = resource() 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(, post_body) r = resource() 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(, key) return [key.to_s] if .key?(key.to_s) return [key.intern] if .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() account_sid = option_value(, 'account_sid') account_token = option_value(, 'account_token') sub_account_sid = option_value(, 'sub_account_sid') sub_account_token = option_value(, 'sub_account_token') func = option_value(, 'func') funcdes = option_value(, 'funcdes') = option_value(, 'timestamp') || Time.now.strftime('%Y%m%d%H%M%S') if account_sid url_prefix = "/Accounts/#{account_sid}" auth = (account_sid, ) sign = signature(account_sid, account_token, ) elsif sub_account_sid url_prefix = "/SubAccounts/#{sub_account_sid}" auth = (sub_account_sid, ) sign = signature(sub_account_sid, sub_account_token, ) else fail "account_sid or sub_account_sid is required" end url = "#{url_prefix}/#{func}/#{funcdes}?sig=#{sign}" headers = option_value(config., 'headers') || {} headers = headers.merge( 'Accept' => 'application/json', 'Content-Type' => 'json', 'Authorization' => auth ) opts = { headers: headers } RestClient::Resource.new(endpoint_url(url), config..merge(opts)) end |
.signature(sid, token, timestamp) ⇒ Object
64 65 66 |
# File 'lib/yun_tong_xun.rb', line 64 def signature(sid, token, ) Digest::MD5.hexdigest("#{sid}#{token}#{}").upcase end |