class QuestradeApi::REST::Order

Public Class Methods

create(authorization, account_number, params = {}) click to toggle source
# File lib/questrade_api/rest/order.rb, line 29
def self.create(authorization, account_number, params = {})
  params[:accountNumber] = account_number

  response = post(access_token: authorization.access_token,
                  endpoint: endpoint(account_number),
                  url: authorization.url,
                  body: params.to_json)

  build_orders(authorization, account_number, response)
end
endpoint(account_id) click to toggle source
# File lib/questrade_api/rest/order.rb, line 53
def self.endpoint(account_id)
  "#{BASE_ENDPOINT}/accounts/#{account_id}/orders"
end
fetch(authorization, account_number, params) click to toggle source
Calls superclass method QuestradeApi::REST::Base#fetch
# File lib/questrade_api/rest/order.rb, line 40
def self.fetch(authorization, account_number, params)
  response = super(access_token: authorization.access_token,
                   endpoint: endpoint(account_number),
                   url: authorization.url,
                   params: params)

  build_orders(authorization, account_number, response)
end
new(authorization, params = {}) click to toggle source
Calls superclass method QuestradeApi::REST::Base::new
# File lib/questrade_api/rest/order.rb, line 6
def initialize(authorization, params = {})
  super(authorization)

  @id = params[:id]
  @account_id = params[:account_id]

  @raw_body = params[:data]
  build_data(params[:data]) if @raw_body
end

Private Class Methods

build_orders(authorization, account_number, response) click to toggle source
# File lib/questrade_api/rest/order.rb, line 77
def self.build_orders(authorization, account_number, response)
  result = response

  if response.status == 200
    result = OpenStruct.new(orders: [])
    result.orders = parse_orders(authorization, account_number, response.body)
  end

  result
end
parse_orders(authorization, account_id, body) click to toggle source
# File lib/questrade_api/rest/order.rb, line 64
def self.parse_orders(authorization, account_id, body)
  raw = JSON.parse(body)

  orders = []

  raw['orders'].each do |order|
    orders << new(authorization,
                  account_id: account_id, id: order['id'], data: order)
  end

  orders
end

Public Instance Methods

endpoint() click to toggle source
# File lib/questrade_api/rest/order.rb, line 49
def endpoint
  self.class.endpoint(account_id) + "/#{id}"
end
update(params = {}) click to toggle source
# File lib/questrade_api/rest/order.rb, line 16
def update(params = {})
  params[:accountNumber] = account_id

  response = self.class.post(access_token: authorization.access_token,
                             endpoint: endpoint,
                             url: authorization.url,
                             body: params.to_json)

  parse_order(response.body) if response.status == 200

  response
end

Private Instance Methods

parse_order(body) click to toggle source
# File lib/questrade_api/rest/order.rb, line 59
def parse_order(body)
  raw = JSON.parse(body)
  build_data(raw['orders'].first) if raw['orders'].size > 0
end