/
http_notifier.rb
46 lines (37 loc) · 1.08 KB
/
http_notifier.rb
1
2
3
4
5
6
7
8
9
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
37
38
39
40
41
42
43
44
45
require "rubygems"
require "bundler/setup"
require 'net/http'
require 'uri'
Bundler.require :http
$redis = Redis::Scripted.connect(scripts_path: "./redis_scripts")
subscriber = Redis.new
$: << "."
require 'trade_manager'
def notify_via_http(order, trade)
http = EventMachine::HttpRequest.new(order['broker'], :connect_timeout => 1)
http.post({
'MessageType' => 'E',
'OrderReferenceIdentifier' => order['id'],
'ExecutedShares' => trade['shares'],
'ExecutionPrice' => trade['price'],
'MatchNumber' => trade['id'],
'to' => order['from']
})
http.errback {
puts "Error sending http request!"
puts http
}
http.callback {
puts "Notified #{order['from']} about #{trade['id']} to status: #{http.response_header.status}"
}
end
subscriber.subscribe('trades') do |on|
on.message do |channel, message|
trade = TradeManager.get(message)
buy_order = TradeManager.get_root(trade['buy_order'])
sell_order = TradeManager.get_root(trade['sell_order'])
[buy_order, sell_order].each do |order|
notify_via_http(order, trade)
end
end
end