Module: StocksExchangeApiClient

Defined in:
lib/stocks_exchange_api_client.rb,
lib/stocks_exchange_api_client/public.rb,
lib/stocks_exchange_api_client/private.rb,
lib/stocks_exchange_api_client/version.rb,
lib/stocks_exchange_api_client/public_v3.rb,
lib/stocks_exchange_api_client/private_v3.rb,
lib/stocks_exchange_api_client/configuration.rb,
lib/stocks_exchange_api_client/errors/configuration_error.rb

Defined Under Namespace

Modules: Errors Classes: Configuration, Private, PrivateApiV3, Public, PublicApiV3

Constant Summary collapse

METHOD_NAME =
{
    get_info: 'GetInfo',
    active_orders: 'ActiveOrders',
    trade: 'Trade',
    cancel_order: 'CancelOrder',
    trade_history: 'TradeHistory',
    trade_register_history: 'TradeRegisterHistory',
    user_history: 'UserHistory',
    trans_history: 'TransHistory',
    grafic: 'Grafic',
    generate_wallets: 'GenerateWallets',
    deposit: 'Deposit',
    withdraw: 'Withdraw'
}.freeze
HEX_ALGORITHM =
'sha512'.freeze
JSON_SETTINGS =
'settings.json'.freeze
ASC =
'ASC'.freeze
ALL =
'ALL'.freeze
MAX_COUNT =
50
STATUS_ORDER =
{
  pending: 1,
  processing: 2,
  finished: 3,
  canceled: 4
}.freeze
STATUS_ORDER_HUMAN_NAME =
{
  1 => 'PENDING',
  2 => 'PROCESSING',
  3 => 'FINISHED',
  4 => 'CANCELED'
}.freeze
OWNER =
{
  all: 'ALL',
  own: 'OWN'
}.freeze
INTERVAL =
{
  day: '1D',
  month: '1M',
  three_month: '3M',
  year: '1Y'
}.freeze
DEFAULT_PAIRS =
'ETH_BTC'.freeze
VERSION =
'2.0.2'

Class Method Summary collapse

Class Method Details

.configurationObject



146
147
148
# File 'lib/stocks_exchange_api_client.rb', line 146

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



43
44
45
# File 'lib/stocks_exchange_api_client.rb', line 43

def configure
  yield(configuration)
end

.get_tokenObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/stocks_exchange_api_client.rb', line 117

def get_token
  if File.exist?(JSON_SETTINGS)
    current_token = JSON.parse(File.read(JSON_SETTINGS))
  else
    current_token = {'access_token' => configuration.option[:token_object][:access_token],
                     'refresh_token' => configuration.option[:token_object][:refresh_token],
                     'expires_in' => nil,
                     'expires_in_date' => nil}
  end
  if !current_token.nil? && !current_token['expires_in_date'].nil?
    return current_token['access_token'] if DateTime.parse(current_token['expires_in_date']).to_datetime > DateTime.now.to_datetime
  end
  begin
    response = HTTParty.post(configuration.option[:access_token_url], body: {
        grant_type: 'refresh_token',
        refresh_token: current_token['refresh_token'],
        client_id: configuration.option[:client_id],
        client_secret: configuration.option[:client_secret],
        scope: configuration.option[:scope]
    }).body
    current_token = JSON.parse(response)
    current_token['expires_in_date'] = Time.at(Time.now.to_i + current_token['expires_in']).to_s
    File.write(JSON_SETTINGS, current_token.to_json)
    current_token['access_token']
  rescue StandardError => e
    puts "Rescued: #{e.inspect}"
  end
end

.make_api_request(method = :post, params = {}, type) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/stocks_exchange_api_client.rb', line 47

def make_api_request(method = :post, params = {}, type)
  configuration.validate!
  if method == :post
    params[:method] = type
    params[:nonce] = Time.now.to_i
    encode_www_form = URI.encode_www_form(params)
    sign = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new(HEX_ALGORITHM), configuration.option[:api_secret], encode_www_form)
    response = JSON.parse(
        HTTParty.post(configuration.url, body: params, headers: {
            Sign: sign,
            Key: configuration.option[:api_key]
        }).body
    )
    response = OpenStruct.new(response)
  end
  if method == :get
    response = JSON.parse(HTTParty.get("#{configuration.url}/#{type}").body)
  end
  response
end

.make_api_request_v3(url, params = {}, method = :get, _type = :url, auth = true) ⇒ 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/stocks_exchange_api_client.rb', line 68

def make_api_request_v3(url, params = {}, method = :get, _type = :url, auth = true)
  configuration.validate!
  post_data = URI.encode_www_form(params)
  url_full = "#{configuration.url}/#{url}#{post_data == '' ? '' : '?'}#{post_data}"
  if auth
    if method == :post
      response = HTTParty.post(url_full, body: params, headers: {
          'Authorization' => "Bearer #{get_token}",
          'Accept' => 'application/json',
          'User-Agent' => 'stocks.exchange-client'
      }).body
    end
    if method == :get
      response = HTTParty.get(url_full, headers: {
          'Authorization' => "Bearer #{get_token}",
          'Accept' => 'application/json',
          'User-Agent' => 'stocks.exchange-client'
      }).body
    end
    if method == :delete
      response = HTTParty.delete(url_full, headers: {
          'Authorization' => "Bearer #{get_token}",
          'Accept' => 'application/json',
          'User-Agent' => 'stocks.exchange-client'
      }).body
    end
  else
    if method == :post
      response = HTTParty.post(url_full, body: params, headers: {
          'Accept' => 'application/json',
          'User-Agent' => 'stocks.exchange-client'
      }).body
    end
    if method == :get
      response = HTTParty.get(url_full, headers: {
          'Accept' => 'application/json',
          'User-Agent' => 'stocks.exchange-client'
      }).body
    end
    if method == :delete
      response = HTTParty.delete(url_full, headers: {
          'Accept' => 'application/json',
          'User-Agent' => 'stocks.exchange-client'
      }).body
    end
  end
  response
end