Class: ChockABlock::Broker

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/chock_a_block/broker.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, account, venue, symbol) ⇒ Broker

Initialize a Broker to interact with API placing orders and getting order stats… etc.

Parameters:

  • api_key (String)

    API auth key

  • account (String)

    account id for given game

  • venue (String)

    venue name

  • symbol (String)

    stock symbol



12
13
14
15
16
17
# File 'lib/chock_a_block/broker.rb', line 12

def initialize(api_key, , venue, symbol)
  @api_key = api_key
  @account = 
  @venue   = venue
  @symbol  = symbol
end

Instance Method Details

#cancel_order(id) ⇒ Hash

Cancel specific order details API Consumer

Returns:

  • (Hash)

    representing the order cancelled



77
78
79
80
81
82
83
# File 'lib/chock_a_block/broker.rb', line 77

def cancel_order(id)
  response = HTTP['X-Starfighter-Authorization' => @api_key].delete(
    BASE_URL + "/venues/#{@venue}/stocks/#{@symbol}/orders/#{id}"
  )
  fail AuthenticationError, @api_key if response.code == 401
  JSON.parse response.body
end

#get_order(id) ⇒ Hash

Get specific order details API Consumer

Returns:

  • (Hash)

    representing the order in query



63
64
65
66
67
68
69
# File 'lib/chock_a_block/broker.rb', line 63

def get_order(id)
  response = HTTP['X-Starfighter-Authorization' => @api_key].get(
    BASE_URL + "/venues/#{@venue}/stocks/#{@symbol}/orders/#{id}"
  )
  fail AuthenticationError, @api_key if response.code == 401
  JSON.parse response.body
end

#get_ordersHash

Get all the orders of a specific stock API Consumer

Returns:

  • (Hash)

    representing the order in query



49
50
51
52
53
54
55
# File 'lib/chock_a_block/broker.rb', line 49

def get_orders
  response = HTTP['X-Starfighter-Authorization' => @api_key].get(
    BASE_URL + "/venues/#{@venue}/accounts/#{@account}/stocks/#{@symbol}/orders"
  )
  fail AuthenticationError, @api_key if response.code == 401
  JSON.parse(response.body)['orders']
end

#place_order(price, qty) ⇒ Hash

Place an order API Consumer

Parameters:

  • price (Integer)

    price for buying

  • qty (Integer)

    quantity wanted to buy

Returns:

  • (Hash)

    representing the placed order



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chock_a_block/broker.rb', line 26

def place_order(price, qty)
  response = HTTP['X-Starfighter-Authorization' => @api_key].post(
    BASE_URL + "/venues/#{@venue}/stocks/#{@symbol}/orders",
    json: {
      account:   @account,
      venue:     @venue,
      stock:     @symbol,
      price:     price,
      qty:       qty,
      direction: :buy,
      orderType: :limit
    }
  )
  response_body = JSON.parse response.body
  fail AuthenticationError, @api_key if response.code == 401
  fail OrderError, response_body['error'] unless response_body['ok']
  response_body
end