class PeatioAPI::Client

Constants

VERSION

Attributes

auth[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/peatio_api/client.rb, line 10
def initialize(options={})
  options.symbolize_keys!
  setup_auth_keys options
  @endpoint = options[:endpoint] || 'https://yunbi.com'
  @timeout  = options[:timeout]  || 60
end

Public Instance Methods

get(path, params={}) click to toggle source
# File lib/peatio_api/client.rb, line 26
def get(path, params={})
  check_auth!

  uri = URI("#{@endpoint}#{path}")

  request(:get, path, @auth, params) do |http, signed_params|
    uri.query = URI.encode_www_form signed_params
    http.request_get(uri.request_uri)
  end
end
get_public(path, params={}) click to toggle source
# File lib/peatio_api/client.rb, line 17
def get_public(path, params={})
  uri = URI("#{@endpoint}#{path}")
  uri.query = URI.encode_www_form params

  request(:get, path, nil, params) do |http, _|
    http.request_get(uri.request_uri)
  end
end
post(path, params={}) click to toggle source
# File lib/peatio_api/client.rb, line 37
def post(path, params={})
  check_auth!

  request(:post, path, @auth, params) do |http, signed_params|
    http.request_post(path, signed_params.to_query)
  end
end

Private Instance Methods

check_auth!() click to toggle source
# File lib/peatio_api/client.rb, line 76
def check_auth!
  raise ArgumentError, 'Missing access key and/or secret key' if @auth.nil?
end
parse(response) click to toggle source
# File lib/peatio_api/client.rb, line 60
def parse(response)
  JSON.parse response.body
rescue JSON::ParserError
  {http_error: {code: response.code, body: response.body}}
end
request(action, path, auth, params={}) { |http, params| ... } click to toggle source
# File lib/peatio_api/client.rb, line 47
def request(action, path, auth, params={})
  uri = URI("#{@endpoint}#{path}")
  params = auth.signed_params action.to_s.upcase, path, params if auth

  http = Net::HTTP.new(uri.hostname, uri.port)
  http.open_timeout = @timeout
  http.use_ssl = true if @endpoint.start_with?('https://')

  http.start do |http|
    parse yield(http, params)
  end
end
setup_auth_keys(options) click to toggle source
# File lib/peatio_api/client.rb, line 66
def setup_auth_keys(options)
  if options[:access_key] && options[:secret_key]
    @access_key = options[:access_key]
    @secret_key = options[:secret_key]
    @auth       = Auth.new @access_key, @secret_key
  else
    #raise ArgumentError, 'Missing access key and/or secret key'
  end
end