Class: MtGox::Websocket::Client

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/mtgox/websocket/client.rb

Constant Summary collapse

@@default_origin =
'http://example.com'
@@event_types =
[:open, :close, :subscribe, :unsubscribe, :private, :remark, :error]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#subscribe, #unsubscribe

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
# File 'lib/mtgox/websocket/client.rb', line 14

def initialize(args={})
  @args = args
  @event_listeners = {}
  @@event_types.each { |et| @event_listeners[et] = [] }
end

Class Method Details

.connect(args = {}) ⇒ Object



34
35
36
37
38
# File 'lib/mtgox/websocket/client.rb', line 34

def self.connect(args={})
  client = new(args)
  client.connect
  client
end

Instance Method Details

#connectObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mtgox/websocket/client.rb', line 20

def connect
  @conn = ::Faye::WebSocket::Client.new('ws://websocket.mtgox.com:80/mtgox', nil, { 
    headers: {'Origin' => @args[:headers] && @args[:headers][:origin]  || @@default_origin}
  })
      
  @conn.on(:open) { dispatch_event(:open) }
  @conn.on(:close) { dispatch_event(:close) }
  @conn.on(:error) { dispatch_event(:error) }
  @conn.on(:message) do |event|
    data = ::Hashie::Mash.new(JSON.parse(event.data))
    dispatch_event(data.op.to_sym, data)
  end
end

#on(event_type, &block) ⇒ Object



40
41
42
# File 'lib/mtgox/websocket/client.rb', line 40

def on(event_type, &block)
  add_event_listener(event_type, &block)
end