Module: Coinbase::Util

Included in:
Client, Currency, Products
Defined in:
lib/coinbase/util.rb

Overview

Utility file for general methods

Instance Method Summary collapse

Instance Method Details

#base_uriObject



39
40
41
# File 'lib/coinbase/util.rb', line 39

def base_uri
  Coinbase::Client.configuration[:url]
end

#build_query_params(params = nil) ⇒ Object

Build query params for URL



32
33
34
35
36
37
# File 'lib/coinbase/util.rb', line 32

def build_query_params(params = nil)
  return params if params.blank?

  query_array = params.keys.map { |key| [key.to_s, params[key]] }
  URI.encode_www_form(query_array)
end

#format_response(response) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/coinbase/util.rb', line 17

def format_response(response)
  {
    status: response.code,
    result: (JSON.parse(response.body) rescue response.body),
    pagination: pagination_params(response.headers)
  }
end

#pagination_params(headers = {}) ⇒ Object



25
26
27
28
29
# File 'lib/coinbase/util.rb', line 25

def pagination_params(headers = {})
  return {} if headers['cb-before'].blank? || headers['cb-after'].blank?

  { before: headers['cb-before'], after: headers['cb-after'] }
end

#send_request(path) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/coinbase/util.rb', line 7

def send_request(path)
  headers = {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'User-Agent' => 'request'
  }
  response = HTTParty.get("#{base_uri}#{path}", headers: headers)
  format_response(response)
end