Class: BigDoor::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_secret, app_key, app_host = DEFAULT_APP_HOST) ⇒ Client

Creates new BigDoor::Client object

Parameters:

  • app_secret (String)

    The API secret supplied by BigDoor. (see API Keys publisher.bigdoor.com/)

  • app_key (String)

    The API key supplied by BigDoor. (see API Keys publisher.bigdoor.com/)

  • app_host (String) (defaults to: DEFAULT_APP_HOST)

    An alternative host to enable use with testing servers.



34
35
36
37
38
39
40
# File 'lib/big_door/client.rb', line 34

def initialize( app_secret, app_key, app_host = DEFAULT_APP_HOST )
    @app_key = app_key
    @app_secret = app_secret
    @app_host = app_host

    @base_url = "/api/publisher/#{@app_key}"
end

Instance Attribute Details

#app_hostObject

Returns the value of attribute app_host.



20
21
22
# File 'lib/big_door/client.rb', line 20

def app_host
  @app_host
end

#app_keyObject

This class provides low-level interface to BigDoor public REST API



18
19
20
# File 'lib/big_door/client.rb', line 18

def app_key
  @app_key
end

#app_secretObject

Returns the value of attribute app_secret.



19
20
21
# File 'lib/big_door/client.rb', line 19

def app_secret
  @app_secret
end

Instance Method Details

#add_required_params(method, params, payload) ⇒ Object

Add requered fields to params and payload

@param [Symbol] method 
  HTTP request method

@param [Hash] params  
  The parameters to be sent via the query string.

@param [Hash] payload
  The parameters to be sent via the PUT or POST request body.

@return [Hash, Hash] Updated params and payload

Raises:

  • (ArgumentError)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/big_door/client.rb', line 111

def add_required_params( method, params, payload )
    raise ArgumentError.new('unkown method') unless [:get, :put, :post, :delete].include?(method)

    params = {} unless params
    payload = {} unless payload
    
    is_postish = [:post, :put].include?(method)

    if is_postish && payload.key?('time')
        params['time'] = payload['time']
    end
    if params && !params.key?('time')
        params['time'] = Time.now.to_i.to_s
    end
    if is_postish && !payload.key?('time')
        payload['time'] = params['time']
    end

    if is_postish && !payload.key?('token')
        payload['token'] = self.generate_token
    end
    
    if method == :delete && !params.key?('delete_token') 
        params['delete_token'] = self.generate_token
    end

    [params, payload]
end

#delete(url, params = nil) ⇒ Object

Makes DELETE HTTP request to API with optional params

@param [String] url 
  relative API end point (eg. currency/12345 )

@param [Hash] params
  The parameters to be sent via the query string.

@return [String] should return empty string in case of success


230
231
232
# File 'lib/big_door/client.rb', line 230

def delete( url, params = nil)
    do_request( :delete, url, params)
end

#do_request(method, end_point, params = nil, payload = nil) ⇒ Object

Makes HTTP request to API with optional params and www-form-urlencoded payload

 @param [Symbol] method 
   HTTP request method

@param [String] url 
  relative API end point (eg. currency/12345 )

@param [Hash] params
  The parameters to be sent via the query string.

@param [Hash] payload
  The parameters to be sent via the PUT or POST request body.

@return [Hash] Decoded JSON response or empty/undefined string


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/big_door/client.rb', line 252

def do_request( method, end_point, params = nil, payload = nil )
    headers = {
        'User-Agent' => "BigDoorKit-Ruby/#{VERSION}",
    }
    
    if [:post, :put].include?(method)
        headers['Content-Type'] = 'application/x-www-form-urlencoded'
    end

    params, payload = sign_request( method, @base_url + '/' + end_point, params, payload )

    url = Addressable::URI.parse( @app_host + @base_url + '/' + end_point )
    $log.debug( sprintf 'url  object = %s', url.inspect )
    $log.debug( sprintf 'params = %s', params.inspect )
    url.query_values = params

    $log.debug( sprintf 'method url = %s %s', method, url )
    $log.debug( sprintf 'payload = %s', payload.inspect )
    
    response = nil
    ms = Benchmark.measure {
        response = RestClient::Request.execute(:method => method, :url => url.to_s, :payload => payload, :headers => headers, :raw_response => false)
    }
    $log.debug("delay #{'%.1f' % (ms.real * 1000)} ms")
    if response && !response.empty?
        # $log.debug( sprintf 'undecoded_response = %s', response.inspect )
        decoded_response = JSON.parse( response )
        $log.debug( sprintf 'decoded_response  = %s', JSON.pretty_generate(decoded_response) )
        decoded_response[0]
    end
rescue RestClient::Exception => ex
    $log.debug(sprintf 'Error response body: %s', ex.http_body)
end

#flatten_params(params) ⇒ String

Converts params hash to string

Parameters:

  • params

    Hash representing request params, except sig and format fields

Returns:

  • (String)

    Request params concatanated

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/big_door/client.rb', line 50

def flatten_params( params )
    raise ArgumentError.new('params should be defined') unless params
    result = ''
    keys = params.keys.sort{|a,b| a.to_s <=> b.to_s}
    keys.each do |key| 
        next if key == 'sig' 
        next if key == 'format'
        result += "#{key}#{params[key]}"
    end
    $log.debug(sprintf "flatten_params = %s", result )
    result
end

#generate_signature(url, params = nil, payload = nil) ⇒ Object

Generate appropriate request signature given a url and optional

params and payload.

@param [String] url
  The full URL, including the base /api/publisher/[app_key]

@param [Hash] params  
  The parameters to be sent via the query string.

@param [Hash] payload
  The parameters to be sent via the PUT or POST request body.

@return [String] request signature to be sent as +sign+ query param


78
79
80
81
82
83
84
85
86
87
# File 'lib/big_door/client.rb', line 78

def generate_signature( url, params = nil, payload = nil )
    signature = url
    signature += flatten_params( params ) if params
    signature += flatten_params( payload ) if payload 
    signature += @app_secret

    $log.debug(sprintf "signature = %s", signature )

    Digest::SHA256.hexdigest(signature)
end

#generate_tokenObject

Generate random token for request

@return [String] Random token to be sent as +token+


93
94
95
# File 'lib/big_door/client.rb', line 93

def generate_token
    UUIDTools::UUID.random_create.hexdigest
end

#get(url, params = nil) ⇒ Object

Makes GET HTTP request to API with optional params

@param [String] url 
  relative API end point (eg. currency_type/1 )

@param [Hash] params
  The parameters to be sent via the query string.

@return [Hash] Decoded JSON response


177
178
179
# File 'lib/big_door/client.rb', line 177

def get( url, params = nil)
    do_request( :get, url, params)
end

#post(url, params, payload) ⇒ Object

Makes POST HTTP request to API with optional params and www-form-urlencoded payload

@param [String] url 
  relative API end point (eg. currency_type )

@param [Hash] params
  The parameters to be sent via the query string.

@param [Hash] payload
  The parameters to be sent via the PUT or POST request body.

@return [Hash] Decoded JSON response


196
197
198
# File 'lib/big_door/client.rb', line 196

def post( url, params, payload )
    do_request( :post, url, params, payload)
end

#put(url, params, payload) ⇒ Object

Makes PUT HTTP request to API with optional params and www-form-urlencoded payload

@param [String] url 
  relative API end point (eg. currency/12345 )

@param [Hash] params
  The parameters to be sent via the query string.

@param [Hash] payload
  The parameters to be sent via the PUT or POST request body.

@return [Hash] Decoded JSON response


215
216
217
# File 'lib/big_door/client.rb', line 215

def put( url, params, payload  )
    do_request( :put, url, params, payload)
end

#sign_request(method, url, params, payload) ⇒ Object

Sign request

@param [Symbol] method 
  HTTP request method

@param [String] url 
  HTTP request URL

@param [Hash] params  
  The parameters to be sent via the query string.

@param [Hash] payload
  The parameters to be sent via the PUT or POST request body.

@return [Hash, Hash] Updated params and payload


157
158
159
160
161
162
163
164
# File 'lib/big_door/client.rb', line 157

def sign_request( method, url, params, payload )

    params, payload = add_required_params( method, params, payload )

    params['sig'] = self.generate_signature( url, params, payload )
    
    [params, payload]
end