Class: FadadaRubySdk::Client

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

Overview

Client class to handle request and responses to and from Tonglian gateway

Instance Method Summary collapse

Constructor Details

#initialize(api_end_point, app_id, app_secret) ⇒ Client

Returns a new instance of Client.



6
7
8
9
# File 'lib/client.rb', line 6

def initialize(api_end_point, app_id, app_secret)
  @api_end_point = api_end_point
  @signer = Signer.new(app_id, app_secret)
end

Instance Method Details

#get_signature(access_token, params) ⇒ Object

in some cases the frontend needs signture to directly request FaDaDa API



32
33
34
35
# File 'lib/client.rb', line 32

def get_signature(access_token, params)
  # to return headers and params
  @signer.sign(access_token, params)
end

#request(url, access_token, params) ⇒ Object

the url is relative to the api_end_point



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/client.rb', line 12

def request(url, access_token, params)
  headers, params = @signer.sign(access_token, params)

  url = URI("#{@api_end_point}#{url}")
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true if @api_end_point.downcase.start_with?('https') # Enable SSL for HTTPS

  request = Net::HTTP::Post.new(url.request_uri)
  request['Content-Type'] = 'application/x-www-form-urlencoded'
  headers.each do |key, value|
    request[key] = value
  end

  request.body = URI.encode_www_form(params)
  response = http.request(request)

  JSON.parse(response.body)
end