Class: UmengVerify::Client
- Inherits:
-
Object
- Object
- UmengVerify::Client
- Defined in:
- lib/umeng_verify/client.rb
Instance Attribute Summary collapse
-
#app_key ⇒ Object
Returns the value of attribute app_key.
-
#app_secret ⇒ Object
Returns the value of attribute app_secret.
-
#host ⇒ Object
Returns the value of attribute host.
-
#http ⇒ Object
Returns the value of attribute http.
Instance Method Summary collapse
- #info(token) ⇒ Object
-
#initialize(app_key, app_secret) ⇒ Client
constructor
A new instance of Client.
- #request(method, path, body = {}, headers = {}) ⇒ Object
- #verify(token, phone_number) ⇒ Object
Constructor Details
#initialize(app_key, app_secret) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 |
# File 'lib/umeng_verify/client.rb', line 12 def initialize(app_key, app_secret) @app_secret = app_secret @app_key = app_key @host = "verify5.market.alicloudapi.com" @http = Net::HTTP.new(@host, 443) @http.use_ssl = true end |
Instance Attribute Details
#app_key ⇒ Object
Returns the value of attribute app_key.
11 12 13 |
# File 'lib/umeng_verify/client.rb', line 11 def app_key @app_key end |
#app_secret ⇒ Object
Returns the value of attribute app_secret.
11 12 13 |
# File 'lib/umeng_verify/client.rb', line 11 def app_secret @app_secret end |
#host ⇒ Object
Returns the value of attribute host.
11 12 13 |
# File 'lib/umeng_verify/client.rb', line 11 def host @host end |
#http ⇒ Object
Returns the value of attribute http.
11 12 13 |
# File 'lib/umeng_verify/client.rb', line 11 def http @http end |
Instance Method Details
#info(token) ⇒ Object
20 21 22 |
# File 'lib/umeng_verify/client.rb', line 20 def info(token) request("POST", "/api/v1/mobile/info", token: token) end |
#request(method, path, body = {}, headers = {}) ⇒ Object
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 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/umeng_verify/client.rb', line 28 def request(method, path, body = {}, headers = {}) headers["Content-Type"] = "application/json; charset=UTF-8" headers["Accept"] = "application/json" headers["Content-MD5"] = Base64.strict_encode64(Digest::MD5.digest(JSON.dump(body))) headers["Date"] = Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT") headers["X-Ca-Version"] = "1" headers["X-Ca-Stage"] = "RELEASE" headers["X-Ca-Key"] = app_key headers["X-Ca-Timestamp"] = (Time.now.to_f * 1000).to_i.to_s headers["X-Ca-Nonce"] = SecureRandom.hex query = URI(path).query form = query ? URI.decode_www_form(query).to_h : {} signature_headers = ["X-Ca-Version", "X-Ca-Stage", "X-Ca-Key", "X-Ca-Timestamp", "X-Ca-Nonce"].sort string_to_sign = [ method, headers["Accept"], headers["Content-MD5"], headers["Content-Type"], headers["Date"], signature_headers.map { |k| "#{k}:#{headers[k]}" }.join("\n"), form.empty? ? path : "#{path}?#{form.sort.map { |k, v| "#{k}=#{v}" }.join('&')}", ].join("\n") headers["X-Ca-Signature"] = Base64.strict_encode64(OpenSSL::HMAC.digest("SHA256", app_secret, string_to_sign)) headers["X-Ca-Signature-Headers"] = signature_headers.join(",") if method == Net::HTTP::Get::METHOD request = Net::HTTP::Get.new(path, headers) else request = Net::HTTP::Post.new(path, headers) request.body = JSON.dump(body) end resp = http.request(request) raise ResponseError.new(resp.code.to_i, resp["X-Ca-Request-Id"], resp["X-Ca-Error-Message"]) unless resp.code.to_i == 200 JSON.parse(resp.body) end |
#verify(token, phone_number) ⇒ Object
24 25 26 |
# File 'lib/umeng_verify/client.rb', line 24 def verify(token, phone_number) request("POST", "/api/v1/mobile/verify", token: token, phoneNumber: phone_number) end |