class Ubea::Exchange::Base

Attributes

last_rtt[R]
order_book[R]
updated_at[R]

Public Instance Methods

balance() click to toggle source
# File lib/ubea/exchanges/base.rb, line 42
def balance
  raise NotImplementedError, to_s
end
id() click to toggle source
# File lib/ubea/exchanges/base.rb, line 20
def id
  @id ||= begin
    self.class.to_s.gsub(/.*::/, "").gsub(/([A-Z])/, "_\\1").gsub(/\A_/, "").downcase
  end
end
Also aliased as: to_param
name() click to toggle source
# File lib/ubea/exchanges/base.rb, line 34
def name
  raise NotImplementedError, to_s
end
short_name() click to toggle source
# File lib/ubea/exchanges/base.rb, line 28
def short_name
  @short_name ||= self.class.to_s.gsub(/.*::/, "")
end
Also aliased as: to_s
to_param()
Alias for: id
to_s()
Alias for: short_name
trade_fee() click to toggle source
# File lib/ubea/exchanges/base.rb, line 38
def trade_fee
  raise NotImplementedError, to_s
end

Protected Instance Methods

retryable_http_request() { || ... } click to toggle source
# File lib/ubea/exchanges/base.rb, line 48
def retryable_http_request
  exceptions = [
    Faraday::ConnectionFailed,
    Faraday::TimeoutError,
    Faraday::SSLError,
    Net::ReadTimeout,
    OpenURI::HTTPError,
    Zlib::BufError,
  ].freeze

  Retryable.retryable(tries: 5, sleep: 1, on: exceptions) do
    yield
  end
end

Private Instance Methods

get_html(url) click to toggle source
# File lib/ubea/exchanges/base.rb, line 73
def get_html(url)
  Retryable.retryable(tries: 5, sleep: 1) do
    open(url).read
  end
end
get_json(url) click to toggle source
# File lib/ubea/exchanges/base.rb, line 79
def get_json(url)
  html = get_html(url) or return

  begin
    JSON.parse(html)
  rescue JSON::ParserError
    nil
  end
end
http_adapter(base_uri) click to toggle source
# File lib/ubea/exchanges/base.rb, line 65
def http_adapter(base_uri)
  @http ||= Faraday.new(url: base_uri) do |faraday|
    faraday.request :url_encoded
    faraday.adapter :net_http
    # faraday.response :logger                  # log requests to STDOUT
  end
end
mark_as_refreshed() click to toggle source
# File lib/ubea/exchanges/base.rb, line 89
def mark_as_refreshed
  now = Time.now.utc
  @last_rtt = now - updated_at if updated_at
  @updated_at = now

  Log.debug "Order book for #{self} has been refreshed"
end