Class: Stockfighter::GM

Inherits:
Object
  • Object
show all
Defined in:
lib/stockfighter/gm.rb

Constant Summary collapse

GM_URL =
"https://www.stockfighter.io/gm"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, level:, polling: false) ⇒ GM

Returns a new instance of GM.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stockfighter/gm.rb', line 10

def initialize(key:, level:, polling:false)
  @api_key = key
  @level = level

  @callback_types = ['success', 'info']
  @message_callbacks = Hash.new { |h,k| h[k] = [] }
  @state_change_callbacks = []

  new_level_response = perform_request("post", "#{GM_URL}/levels/#{level}")
  previous_state = new_level_response['state']

  if polling
    # websocket API functionality instead of polling would be great here
    scheduler = Rufus::Scheduler.new(:overlap => false)
    scheduler.every '10s' do
      response = get_instance()

      current_state = response['state']
      if previous_state != current_state
        @state_change_callbacks.each { |callback|
          callback.call(previous_state, current_state)
        }
        previous_state = current_state
      end
    end
  end
end

Instance Attribute Details

#instance_idObject

Returns the value of attribute instance_id.



8
9
10
# File 'lib/stockfighter/gm.rb', line 8

def instance_id
  @instance_id
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/stockfighter/gm.rb', line 67

def active?
  response = get_instance()
  response["done"]
end

#add_message_callback(type, &block) ⇒ Object



38
39
40
41
# File 'lib/stockfighter/gm.rb', line 38

def add_message_callback(type, &block)
  raise "Unknown message callback type #{type}" unless @callback_types.include? type
  @message_callbacks[type] << block
end

#add_state_change_callback(&block) ⇒ Object



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

def add_state_change_callback(&block)
  @state_change_callbacks << block
end

#configObject



47
48
49
50
51
52
53
# File 'lib/stockfighter/gm.rb', line 47

def config
  if @config[:account] && @config[:venue] && @config[:symbol]
    @config
  else
    nil
  end
end

#get_instanceObject



72
73
74
# File 'lib/stockfighter/gm.rb', line 72

def get_instance
  perform_request("get", "#{GM_URL}/instances/#{@instance_id}")
end

#restartObject



55
56
57
# File 'lib/stockfighter/gm.rb', line 55

def restart
  perform_request("post", "#{GM_URL}/instances/#{@instance_id}/restart")
end

#resumeObject



63
64
65
# File 'lib/stockfighter/gm.rb', line 63

def resume
  perform_request("post", "#{GM_URL}/instances/#{@instance_id}/resume")
end

#stopObject



59
60
61
# File 'lib/stockfighter/gm.rb', line 59

def stop
  perform_request("post", "#{GM_URL}/instances/#{@instance_id}/stop")
end