Class: TonglianRubySdk::Client
- Inherits:
-
Object
- Object
- TonglianRubySdk::Client
- Defined in:
- lib/tonglian-ruby-sdk.rb
Overview
Client class to handle request and responses to and from Tonglian gateway
Constant Summary collapse
- REQUEST_STUB =
{ 'charset' => 'utf-8', 'format' => 'JSON', 'signType' => 'SHA256WithRSA', 'version' => '1.0' }.freeze
Instance Method Summary collapse
-
#initialize(api_end_point, app_id, private_path, private_passwd, public_path) ⇒ Client
constructor
A new instance of Client.
- #request(method, params) ⇒ Object
Constructor Details
#initialize(api_end_point, app_id, private_path, private_passwd, public_path) ⇒ Client
Returns a new instance of Client.
23 24 25 26 27 |
# File 'lib/tonglian-ruby-sdk.rb', line 23 def initialize(api_end_point, app_id, private_path, private_passwd, public_path) @api_end_point = api_end_point @app_id = app_id @signer = Signer.new(private_path, private_passwd, public_path) end |
Instance Method Details
#request(method, params) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tonglian-ruby-sdk.rb', line 29 def request(method, params) data = REQUEST_STUB.dup data['appId'] = @app_id data['method'] = method data['timestamp'] = data['bizContent'] = params.to_json data['sign'] = @signer.sign(data) url = URI(@api_end_point) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true if @api_end_point.downcase.starts_with?('https') # Enable SSL for HTTPS request = Net::HTTP::Post.new(url.request_uri) request['Content-Type'] = 'application/x-www-form-urlencoded' request.body = URI.encode_www_form(data) response = http.request(request) object = JSON.parse(response.body) @signer.verify?(object) || raise('Invalid response signature!') { 'code' => response.code, 'data' => object } end |