Class: Blockchain0x::Client

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

Constant Summary collapse

DEFAULT_BASE_URL =
'https://api.blockchain0x.com'
DEFAULT_TIMEOUT =
30
DEFAULT_MAX_RETRIES =
3
USER_AGENT =
"blockchain0x-ruby/#{VERSION}"

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: DEFAULT_BASE_URL, network: nil, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, adapter: :net_http, connection: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    required; sk_test_… or sk_live_…

  • base_url (String) (defaults to: DEFAULT_BASE_URL)

    override the default API host

  • network (Symbol, String) (defaults to: nil)

    :mainnet / :testnet override

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    per-request wall-clock seconds

  • max_retries (Integer) (defaults to: DEFAULT_MAX_RETRIES)

    retry budget on 5xx + 429

  • adapter (Symbol) (defaults to: :net_http)

    Faraday adapter (default :net_http)

  • connection (Faraday::Connection) (defaults to: nil)

    override for tests

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
# File 'lib/blockchain0x/client.rb', line 40

def initialize(api_key:, base_url: DEFAULT_BASE_URL, network: nil, timeout: DEFAULT_TIMEOUT,
               max_retries: DEFAULT_MAX_RETRIES, adapter: :net_http, connection: nil)
  raise ArgumentError, 'api_key is required' if api_key.nil? || api_key.empty?

  @api_key = api_key
  @network = (network || network_from_key(api_key)).to_s
  @max_retries = max_retries
  @connection = connection || build_connection(base_url, timeout, adapter)
end

Instance Method Details

#api_keysObject



50
51
52
53
54
55
# File 'lib/blockchain0x/client.rb', line 50

def api_keys
  @api_keys ||= begin
    require_relative 'resources/api_keys'
    Resources::APIKeys.new(self)
  end
end

#delete(path, idempotency_key: nil) ⇒ Object



91
92
93
# File 'lib/blockchain0x/client.rb', line 91

def delete(path, idempotency_key: nil)
  request(:delete, path, idempotency_key: idempotency_key)
end

#get(path, params: nil) ⇒ Object

—- low-level verb helpers ————————————–



79
80
81
# File 'lib/blockchain0x/client.rb', line 79

def get(path, params: nil)
  request(:get, path, params: params)
end

#patch(path, body: nil, idempotency_key: nil) ⇒ Object



87
88
89
# File 'lib/blockchain0x/client.rb', line 87

def patch(path, body: nil, idempotency_key: nil)
  request(:patch, path, body: body, idempotency_key: idempotency_key)
end

#payment_requestsObject



57
58
59
60
61
62
# File 'lib/blockchain0x/client.rb', line 57

def payment_requests
  @payment_requests ||= begin
    require_relative 'resources/payment_requests'
    Resources::PaymentRequests.new(self)
  end
end

#paymentsObject



64
65
66
67
68
69
# File 'lib/blockchain0x/client.rb', line 64

def payments
  @payments ||= begin
    require_relative 'resources/payments'
    Resources::Payments.new(self)
  end
end

#post(path, body: nil, idempotency_key: nil) ⇒ Object



83
84
85
# File 'lib/blockchain0x/client.rb', line 83

def post(path, body: nil, idempotency_key: nil)
  request(:post, path, body: body, idempotency_key: idempotency_key)
end

#transactionsObject



71
72
73
74
75
76
# File 'lib/blockchain0x/client.rb', line 71

def transactions
  @transactions ||= begin
    require_relative 'resources/transactions'
    Resources::Transactions.new(self)
  end
end