Class: CoinbaseCommerceClient::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/coinbase_commerce_client/client.rb', line 24

def initialize(options = {})
  check_api_key!(options[:api_key])
  @api_key = options[:api_key] || CoinbaseCommerceClient.config.api_key
  @api_uri = URI.parse(options[:api_url] || BASE_API_URL)
  @api_ver = options[:api_ver] || API_VERSION

  @conn = Faraday.new do |c|
    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



52
53
54
# File 'lib/coinbase_commerce_client/client.rb', line 52

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

#chargeObject



37
38
39
40
# File 'lib/coinbase_commerce_client/client.rb', line 37

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

#check_api_key!(api_key) ⇒ Object



66
67
68
# File 'lib/coinbase_commerce_client/client.rb', line 66

def check_api_key!(api_key)
  raise CoinbaseCommerceClient::Errors::AuthenticationError, 'No API key provided' unless (api_key || CoinbaseCommerceClient.config.api_key)
end

#checkoutObject



42
43
44
45
# File 'lib/coinbase_commerce_client/client.rb', line 42

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

#eventObject



47
48
49
50
# File 'lib/coinbase_commerce_client/client.rb', line 47

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

#execute_request_with_rescues(api_base) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/coinbase_commerce_client/client.rb', line 108

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
    when Faraday::ServerError
      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



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
100
101
102
103
104
105
106
# File 'lib/coinbase_commerce_client/client.rb', line 70

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 = CoinbaseCommerceClientResponse.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



56
57
58
59
60
61
62
63
64
# File 'lib/coinbase_commerce_client/client.rb', line 56

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