module Bitcointerminal::AuthenticatedConnection

Private Instance Methods

authenticated_get(url, params = {}) click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 7
def authenticated_get(url, params = {})
  new_rest_connection.get do |req|
    req.url build_url(url)
    params.each do |k,v|
      req.params[k] = v
    end
    full_path = fullpath(req.path, req.params.to_query)
    req.headers = get_auth_headers(full_path)

    req.options.timeout = config.rest_timeout
    req.options.open_timeout = config.rest_open_timeout
  end
end
authenticated_patch(url, options = {}) click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 34
def authenticated_patch(url, options = {})
  # p "authenticated_patch: #{url} #{options}"
  body = options[:params] || {}
  new_rest_connection.patch do |req|
    req.url build_url(url)
    req.headers = get_auth_headers(url,body)
    req.body = body.to_json
    req.options.timeout = config.rest_timeout
    req.options.open_timeout = config.rest_open_timeout
  end
end
authenticated_post(url, options = {}) click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 22
def authenticated_post(url, options = {})
  body = options[:params] || {}
  # p "x: #{url} #{body}"
  new_rest_connection.post do |req|
    req.url build_url(url)
    req.headers = get_auth_headers(url,body)
    req.body = body.to_json
    req.options.timeout = config.rest_timeout
    req.options.open_timeout = config.rest_open_timeout
  end
end
base_api_endpoint() click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 103
def base_api_endpoint
  # puts "config: #{config.api_endpoint}"
  url = URI.parse(config.api_endpoint)
  "#{url.scheme}://#{url.host}:#{url.port}"
end
build_payload(url, params = {}, nonce) click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 63
def build_payload(url, params = {}, nonce)
  payload = {}
  payload['nonce'] = nonce
  payload['request'] = url
  payload.merge!(params) if params
  # pp "payload1: #{payload.inspect}"
  # pp "payload: #{payload.to_json.encoding} #{payload['request'].encoding}"
  # pp "fuck it: #{payload.to_json}"
  Base64.strict_encode64(payload.to_json)

end
build_url(url) click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 87
def build_url(url)
  # URI.join(config.api_endpoint, url).path
  URI.join(config.api_endpoint, url).request_uri
end
fullpath(path, query) click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 109
def fullpath(path, query)
  if query.empty?
    path
  else
    "#{path}?#{query}"
  end
end
get_auth_headers(url, params = {}) click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 47
def get_auth_headers(url, params = {})
  nonce = new_nonce

  complete_url = build_url(url)
  # p "url: #{url}, complete_url: #{complete_url}"
  headers = {}
  payload = build_payload(complete_url, params, nonce)
  # puts "encode64: #{payload}"
  headers['bterm-signature'] = sign(payload)
  headers['bterm-apikey'] = config.api_key
  headers['bterm-nonce'] = nonce
  headers['Content-Type'] = 'application/json'
  headers['Accept'] = 'application/json'
  return headers
end
new_nonce() click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 75
def new_nonce
  (Time.now.to_f * 10_000).to_i.to_s
end
new_rest_connection() click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 92
def new_rest_connection
  Faraday.new(url: base_api_endpoint) do |conn|
    # conn.use Bitfinex::CustomErrors
    conn.response :logger, Logger.new(STDOUT) , bodies: true  if config.debug_connection
    # conn.request :json
    # conn.response :json, :content_type => /\bjson$/
    # conn.response :logger
    conn.adapter :net_http
  end
end
sign(payload) click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 79
def sign(payload)
  OpenSSL::HMAC.hexdigest('sha384', config.secret_key, payload)
end
valid_key?() click to toggle source
# File lib/bitcointerminal/authenticated_rest.rb, line 83
def valid_key?
  !! (config.api_key && config.secret_key)
end