Class: Bitstamper::Rest::Client
- Inherits:
-
Object
- Object
- Bitstamper::Rest::Client
- Includes:
- Errors, Private::Balances, Private::Deposits, Private::Orders, Private::Transactions, Private::Withdrawals, Public::Currencies, Public::OrderBook, Public::Ticker, Public::TradingPairs, Public::Transactions
- Defined in:
- lib/bitstamper/rest/client.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #auth(data = {}) ⇒ Object
- #check_credentials! ⇒ Object
- #configured? ⇒ Boolean
- #get(path, params: {}, options: {}) ⇒ Object
-
#initialize(configuration: ::Bitstamper.configuration) ⇒ Client
constructor
A new instance of Client.
- #parse(response) ⇒ Object
- #path_with_currency_pair(path, currency_pair) ⇒ Object
- #post(path, params: {}, data: {}, options: {}) ⇒ Object
- #request(path, method: :get, params: {}, data: {}, options: {}) ⇒ Object
- #to_uri(path) ⇒ Object
Methods included from Private::Transactions
#find_user_transaction, #user_transactions
Methods included from Private::Orders
#buy, #cancel_all_orders!, #cancel_order!, #create_order, #find_open_order, #fix_order_direction, #open_orders, #order_status, #sell
Methods included from Private::Withdrawals
#bank_withdrawal_status, #cancel_bank_withdrawal, #withdraw, #withdrawal_requests
Methods included from Private::Deposits
#bch_deposit_address, #btc_deposit_address, #deposit_address, #eth_deposit_address, #ltc_deposit_address, #unconfirmed_bitcoins, #xrp_deposit_address
Methods included from Private::Balances
Methods included from Public::Currencies
Methods included from Public::TradingPairs
Methods included from Public::Transactions
#find_transaction, #transactions
Methods included from Public::OrderBook
Methods included from Public::Ticker
#daily_ticker, #hourly_ticker, #ticker
Methods included from Errors
#error?, #process_error, #process_hash_error
Constructor Details
#initialize(configuration: ::Bitstamper.configuration) ⇒ Client
Returns a new instance of Client.
6 7 8 9 |
# File 'lib/bitstamper/rest/client.rb', line 6 def initialize(configuration: ::Bitstamper.configuration) self.url = "https://www.bitstamp.net/api" self.configuration = configuration end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
4 5 6 |
# File 'lib/bitstamper/rest/client.rb', line 4 def configuration @configuration end |
#url ⇒ Object
Returns the value of attribute url.
4 5 6 |
# File 'lib/bitstamper/rest/client.rb', line 4 def url @url end |
Instance Method Details
#auth(data = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bitstamper/rest/client.rb', line 57 def auth(data = {}) if configured? data[:key] = self.configuration.key data[:nonce] = (Time.now.to_f * 1_000_000_000).to_i.to_s = data[:nonce] + self.configuration.client_id.to_s + data[:key] data[:signature] = HMAC::SHA256.hexdigest(self.configuration.secret, ).upcase end return data end |
#check_credentials! ⇒ Object
29 30 31 32 33 |
# File 'lib/bitstamper/rest/client.rb', line 29 def check_credentials! unless configured? raise ::Bitstamper::Errors::MissingConfigError.new("Bitstamp Gem not properly configured") end end |
#configured? ⇒ Boolean
25 26 27 |
# File 'lib/bitstamper/rest/client.rb', line 25 def configured? !self.configuration.key.to_s.empty? && !self.configuration.secret.to_s.empty? && !self.configuration.client_id.to_s.empty? end |
#get(path, params: {}, options: {}) ⇒ Object
49 50 51 |
# File 'lib/bitstamper/rest/client.rb', line 49 def get(path, params: {}, options: {}) request path, method: :get, options: end |
#parse(response) ⇒ Object
44 45 46 47 |
# File 'lib/bitstamper/rest/client.rb', line 44 def parse(response) error?(response) response end |
#path_with_currency_pair(path, currency_pair) ⇒ Object
35 36 37 38 |
# File 'lib/bitstamper/rest/client.rb', line 35 def path_with_currency_pair(path, currency_pair) path += "/#{::Bitstamper::Utilities.fix_currency_pair(currency_pair)}" if !currency_pair.to_s.empty? return path end |
#post(path, params: {}, data: {}, options: {}) ⇒ Object
53 54 55 |
# File 'lib/bitstamper/rest/client.rb', line 53 def post(path, params: {}, data: {}, options: {}) request path, method: :post, params: params, data: data.merge(auth), options: end |
#request(path, method: :get, params: {}, data: {}, options: {}) ⇒ Object
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 |
# File 'lib/bitstamper/rest/client.rb', line 68 def request(path, method: :get, params: {}, data: {}, options: {}) user_agent = .fetch(:user_agent, self.configuration.faraday.fetch(:user_agent, nil)) proxy = .fetch(:proxy, nil) connection = Faraday.new(url: to_uri(path)) do |builder| builder.headers[:user_agent] = user_agent if !user_agent.to_s.empty? builder.request :url_encoded if method.eql?(:post) builder.response :logger if self.configuration.verbose_faraday? builder.response :json if proxy puts "[Bitstamper::Rest::Client] - Will connect to Bitstamp using proxy: #{proxy.inspect}" if self.configuration.verbose_faraday? builder.proxy = proxy end builder.adapter self.configuration.faraday.fetch(:adapter, :net_http) end case method when :get connection.get do |request| request.params = params if params && !params.empty? end&.body when :post connection.post do |request| request.body = data request.params = params if params && !params.empty? end&.body end end |
#to_uri(path) ⇒ Object
40 41 42 |
# File 'lib/bitstamper/rest/client.rb', line 40 def to_uri(path) "#{self.url}#{path}/" end |