Class: InvertirOnline::Client::REST

Inherits:
Object
  • Object
show all
Defined in:
lib/invertir-online/client/rest.rb,
lib/invertir-online/client/rest/clients.rb,
lib/invertir-online/client/rest/methods.rb,
lib/invertir-online/client/rest/endpoints.rb,
lib/invertir-online/client/rest/sign_request_middleware.rb,
lib/invertir-online/client/rest/timestamp_request_middleware.rb

Defined Under Namespace

Classes: SignRequestMiddleware, TimestampRequestMiddleware

Constant Summary collapse

BASE_URL =
'https://api.invertironline.com/'.freeze
METHODS =
[
  # Account API Methods
  # #create_buy_order!
  { name: :create_buy_order!, client: :signed,
    action: :post, endpoint: :buy_order },
  # #create_sell_order!
  { name: :create_sell_order!, client: :signed,
    action: :post, endpoint: :sell_order },
  # #all_orders
  { name: :all_orders, client: :signed,
    action: :get, endpoint: :all_orders },
  # #account_info
  { name: :account_info, client: :signed,
    action: :get, endpoint: :account_info },
  # #price
  { name: :price, client: :signed,
    action: :get, endpoint: :price },
  {
    name: :klines, client: :signed,
    action: :get, endpoint: :klines }
].freeze
ENDPOINTS =
{

  # Account API Endpoints
  buy_order:        'v2/operar/Comprar',
  sell_order:       'v2/operar/Vender',
  all_orders:       'v2/operaciones',
  account_info:     'v2/estadocuenta',
  price:            'v2/mercado/titulos/simbolo/cotizacion',
  klines:           'v2/mercado/titulos/simbolo/cotizacion/seriehistorica/fecha_desde/fecha_hasta/ajustada'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user: '', password: '', adapter: Faraday.default_adapter) ⇒ REST

Returns a new instance of REST.



15
16
17
18
19
20
21
# File 'lib/invertir-online/client/rest.rb', line 15

def initialize(user: '', password: '',
               adapter: Faraday.default_adapter)
  @clients = {}
  response = Faraday.post('https://api.invertironline.com/token', username: user, password: password, grant_type: 'password')
  token = JSON.parse(response.body)["access_token"]
  @clients[:signed] = verified_client token, adapter
end

Class Method Details

.add_query_param(query, key, value) ⇒ Object



34
35
36
37
38
# File 'lib/invertir-online/client/rest.rb', line 34

def self.add_query_param(query, key, value)
  query = query.to_s
  query << '&' unless query.empty?
  query << "#{Faraday::Utils.escape key}=#{Faraday::Utils.escape value}"
end

Instance Method Details

#camelize(str) ⇒ Object



48
49
50
51
# File 'lib/invertir-online/client/rest.rb', line 48

def camelize(str)
  str.split('_')
     .map.with_index { |word, i| i.zero? ? word : word.capitalize }.join
end

#replace_path_variables(endpoint, options) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/invertir-online/client/rest.rb', line 40

def replace_path_variables(endpoint, options)
  replaced_endpoint = endpoint
  options.each do |k, v|
    replaced_endpoint.gsub!(k.to_s, v)
  end
  replaced_endpoint
end

#verified_client(token, adapter) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/invertir-online/client/rest/clients.rb', line 6

def verified_client(token, adapter)
  Faraday.new(url: "#{BASE_URL}/api") do |conn|
    conn.response :json, content_type: /\bjson$/
    conn.headers['Authorization'] = "Bearer #{token}"
    conn.adapter adapter
  end
end