Class: CoinbaseCommerce::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

API Client for the Coinbase API. Entry point for making requests to the Coinbase API. Full API docs available here: commerce.coinbase.com/docs/api/



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

def initialize(options = {})
  # set API key and API URL
  check_api_key!(options[:api_key])
  @api_key = options[:api_key]
  @api_uri = URI.parse(options[:api_url] || BASE_API_URL)
  @api_ver = options[:api_ver] || API_VERSION
  # create client obj
  @conn = Faraday.new do |c|
    c.use Faraday::Request::Multipart
    c.use Faraday::Request::UrlEncoded
    c.use Faraday::Response::RaiseError
    c.adapter Faraday.default_adapter
  end
end

Instance Method Details

#api_url(url = "", api_base = nil) ⇒ Object



44
45
46
# File 'lib/coinbase_commerce/client.rb', line 44

def api_url(url = "", api_base = nil)
  (api_base || CoinbaseCommerce::BASE_API_URL) + url
end

#chargeObject

Set client-resource relations with all API resources provide client instance to each resource



29
30
31
32
# File 'lib/coinbase_commerce/client.rb', line 29

def charge
  APIResources::Charge.client = self
  APIResources::Charge
end

#check_api_key!(api_key) ⇒ Object

Raises:

  • (AuthenticationError)


58
59
60
# File 'lib/coinbase_commerce/client.rb', line 58

def check_api_key!(api_key)
  raise AuthenticationError, "No API key provided" unless api_key
end

#checkoutObject



34
35
36
37
# File 'lib/coinbase_commerce/client.rb', line 34

def checkout
  APIResources::Checkout.client = self
  APIResources::Checkout
end

#eventObject



39
40
41
42
# File 'lib/coinbase_commerce/client.rb', line 39

def event
  APIResources::Event.client = self
  APIResources::Event
end

#execute_request_with_rescues(api_base) ⇒ Object

сollect errors during request execution if they occurred



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/coinbase_commerce/client.rb', line 102

def execute_request_with_rescues(api_base)
  begin
    resp = yield
  rescue StandardError => e
    case e
    when Faraday::ClientError
      if e.response
        Errors.handle_error_response(e.response)
      else
        Errors.handle_network_error(e, api_base)
      end
    else
      raise
    end
  end
  resp
end

#request(method, path, params = {}) ⇒ Object

request section



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/coinbase_commerce/client.rb', line 63

def request(method, path, params = {})
  @last_response = nil
  url = api_url(path, @api_uri)
  headers = request_headers(@api_key)

  body = nil
  query_params = nil

  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    query_params = params
  else
    body = params.to_json
  end

  u = URI.parse(path)
  unless u.query.nil?
    query_params ||= {}
    query_params = Hash[URI.decode_www_form(u.query)].merge(query_params)
  end


  http_resp = execute_request_with_rescues(@api_uri) do
    @conn.run_request(method, url, body, headers) do |req|
      req.params = query_params unless query_params.nil?
    end
  end

  begin
    resp = CoinbaseCommerceResponse.from_faraday_response(http_resp)
  rescue JSON::ParserError
    raise Errors.general_api_error(http_resp.status, http_resp.body)
  end

  @last_response = resp
  resp
end

#request_headers(api_key) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/coinbase_commerce/client.rb', line 48

def request_headers(api_key)
  {
      "User-Agent" => "CoinbaseCommerce/#{CoinbaseCommerce::VERSION}",
      "Accept" => "application/json",
      "X-CC-Api-Key" => api_key,
      "X-CC-Version" => @api_ver,
      "Content-Type" => "application/json",
  }
end