Module: StocksExchangeApiClient

Defined in:
lib/stocks_exchange_api_client.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, PrivateApiV3, 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
VERSION =
'2.2.1'

Class Method Summary collapse

Class Method Details

.configurationObject



126
127
128
# File 'lib/stocks_exchange_api_client.rb', line 126

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



39
40
41
# File 'lib/stocks_exchange_api_client.rb', line 39

def configure
  yield(configuration)
end

.get_tokenObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/stocks_exchange_api_client.rb', line 92

def get_token
  if !configuration.s2s
    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
  if configuration.s2s
    configuration.option[:token_object][:access_token]
  end
end

.make_api_request_v3(url, params = {}, method = :get, _type = :url, auth = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/stocks_exchange_api_client.rb', line 43

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