Class: Cryptohopper::Client

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

Overview

Synchronous Cryptohopper API client.

Examples:

ch = Cryptohopper::Client.new(api_key: ENV.fetch("CRYPTOHOPPER_TOKEN"))
me = ch.user.get
ticker = ch.exchange.ticker(exchange: "binance", market: "BTC/USDT")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, app_key: nil, base_url: nil, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, user_agent: nil) ⇒ Client

rubocop:disable Metrics/ParameterLists – keyword args; readability wins over splitting into a value-object struct.

Parameters:

  • api_key (String)

    40-char OAuth2 bearer token.

  • app_key (String, nil) (defaults to: nil)

    Optional OAuth client_id, sent as ‘x-api-app-key`.

  • base_url (String, nil) (defaults to: nil)

    Override for staging.

  • timeout (Integer, Float) (defaults to: DEFAULT_TIMEOUT)

    Per-request timeout in seconds.

  • max_retries (Integer) (defaults to: DEFAULT_MAX_RETRIES)

    Retries on 429 (respecting Retry-After).

  • user_agent (String, nil) (defaults to: nil)

    Appended after ‘cryptohopper-sdk-ruby/<v>`.

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cryptohopper/client.rb', line 56

def initialize(api_key:, app_key: nil, base_url: nil, timeout: DEFAULT_TIMEOUT,
               max_retries: DEFAULT_MAX_RETRIES, user_agent: nil)
  raise ArgumentError, "api_key is required" if api_key.nil? || api_key.empty?

  @api_key = api_key
  @app_key = app_key
  @base_url = (base_url || DEFAULT_BASE_URL).chomp("/")
  @timeout = timeout
  @max_retries = max_retries
  @user_agent_suffix = user_agent

  @user = Resources::User.new(self)
  @hoppers = Resources::Hoppers.new(self)
  @exchange = Resources::Exchange.new(self)
  @strategy = Resources::Strategies.new(self)
  @backtest = Resources::Backtests.new(self)
  @market = Resources::Market.new(self)
  @signals = Resources::Signals.new(self)
  @arbitrage = Resources::Arbitrage.new(self)
  @marketmaker = Resources::MarketMaker.new(self)
  @template = Resources::Templates.new(self)
  @ai = Resources::AI.new(self)
  @platform = Resources::Platform.new(self)
  @chart = Resources::Chart.new(self)
  @subscription = Resources::Subscription.new(self)
  @social = Resources::Social.new(self)
  @tournaments = Resources::Tournaments.new(self)
  @webhooks = Resources::Webhooks.new(self)
  @app = Resources::App.new(self)
end

Instance Attribute Details

#aiObject (readonly)

Returns the value of attribute ai.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def ai
  @ai
end

#appObject (readonly)

Returns the value of attribute app.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def app
  @app
end

#arbitrageObject (readonly)

Returns the value of attribute arbitrage.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def arbitrage
  @arbitrage
end

#backtestObject (readonly)

Returns the value of attribute backtest.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def backtest
  @backtest
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def base_url
  @base_url
end

#chartObject (readonly)

Returns the value of attribute chart.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def chart
  @chart
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def exchange
  @exchange
end

#hoppersObject (readonly)

Returns the value of attribute hoppers.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def hoppers
  @hoppers
end

#marketObject (readonly)

Returns the value of attribute market.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def market
  @market
end

#marketmakerObject (readonly)

Returns the value of attribute marketmaker.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def marketmaker
  @marketmaker
end

#platformObject (readonly)

Returns the value of attribute platform.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def platform
  @platform
end

#signalsObject (readonly)

Returns the value of attribute signals.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def signals
  @signals
end

#socialObject (readonly)

Returns the value of attribute social.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def social
  @social
end

#strategyObject (readonly)

Returns the value of attribute strategy.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def strategy
  @strategy
end

#subscriptionObject (readonly)

Returns the value of attribute subscription.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def subscription
  @subscription
end

#templateObject (readonly)

Returns the value of attribute template.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def template
  @template
end

#tournamentsObject (readonly)

Returns the value of attribute tournaments.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def tournaments
  @tournaments
end

#userObject (readonly)

Returns the value of attribute user.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def user
  @user
end

#webhooksObject (readonly)

Returns the value of attribute webhooks.



41
42
43
# File 'lib/cryptohopper/client.rb', line 41

def webhooks
  @webhooks
end

Instance Method Details

#_request(method, path, params: nil, body: nil, max_retries: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal transport. Resources call this. Users shouldn’t.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cryptohopper/client.rb', line 91

def _request(method, path, params: nil, body: nil, max_retries: nil)
  retries = max_retries || @max_retries
  attempt = 0
  loop do
    return do_request(method, path, params: params, body: body)
  rescue Error => e
    raise unless e.code == "RATE_LIMITED" && attempt < retries

    wait = e.retry_after_ms ? e.retry_after_ms / 1000.0 : (2**attempt)
    sleep(wait)
    attempt += 1
  end
end