Skip to content

Commit

Permalink
Stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
airhorns committed Nov 20, 2011
1 parent 8221d9a commit f7e853f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 27 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Expand Up @@ -21,6 +21,15 @@ group :sms do
gem 'twilio-ruby'
end

group :http do
gem 'eventmachine'
gem "hiredis"
gem "em-synchrony", :require => ['em-synchrony', 'em-synchrony/em-http']
gem "em-http-request"
gem "redis", :require => ["redis/connection/synchrony", "redis"]
gem 'redis-scripted', :require => "redis/scripted"
end

group :test do
gem 'turn'
gem 'minitest', '~> 2.7.0'
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
@@ -1,10 +1,18 @@
GEM
remote: http://rubygems.org/
specs:
addressable (2.2.6)
ansi (1.4.0)
async-rack (0.5.1)
rack (~> 1.1)
builder (3.0.0)
em-http-request (1.0.0)
addressable (>= 2.2.3)
em-socksify
eventmachine (>= 1.0.0.beta.3)
http_parser.rb (>= 0.5.2)
em-socksify (0.1.0)
eventmachine
em-synchrony (1.0.0)
eventmachine (>= 1.0.0.beta.1)
eventmachine (1.0.0.beta.4)
Expand Down Expand Up @@ -60,6 +68,7 @@ PLATFORMS
ruby

DEPENDENCIES
em-http-request
em-synchrony
eventmachine
goliath
Expand Down
13 changes: 6 additions & 7 deletions exchange_server.rb
Expand Up @@ -21,17 +21,16 @@ def response(env)
else
manager.sell(order.number_from, order.shares, order.price, order.twilio, order.broker_url)
end
id = " "
resp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Response>
<Exchange><Accept OrderRefId=\"#{id}\" /></Exchange>
</Response>"
<Response>
<Exchange><Accept OrderRefId=\"#{id}\" /></Exchange>
</Response>"
[200, {}, resp]
rescue InvalidOrderError => e
resp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Response>
<Exchange><Reject Reason=\"#{e.message}\" /></Exchange>
</Response>"
<Response>
<Exchange><Reject Reason=\"#{e.message}\" /></Exchange>
</Response>"
[400, {}, resp]
end
end
Expand Down
46 changes: 46 additions & 0 deletions http_notifier.rb
@@ -0,0 +1,46 @@
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

2 changes: 1 addition & 1 deletion redis_scripts/fill_order.lua
Expand Up @@ -24,7 +24,7 @@ end

function next_order_key(order_type)
id = redis.call('INCR', (stock .. '_' .. order_type .. '_nextid'))
return id, (stock .. "_" .. order_type .. "_order_" .. id)
return id, (stock .. "_" .. string.upper(string.sub(order_type, 1, 2)) .. "_order_" .. id)
end

function store_table(table, key)
Expand Down
27 changes: 14 additions & 13 deletions sms_server.rb
Expand Up @@ -14,19 +14,24 @@
trap(:INT) { puts; exit }

# put your own credentials here
account_sid = "AC888a285988894223a40b8d0df20d6d58"
auth_token = "6ea425d908a216d20d505eb013d55985"
number = "+15148005440"
$account_sid = "AC888a285988894223a40b8d0df20d6d58"
$auth_token = "6ea425d908a216d20d505eb013d55985"
$number = "+15148005440"

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new account_sid, auth_token
@client = Twilio::REST::Client.new $account_sid, $auth_token

def notify_via_sms(order, trade)
puts "Notifying #{order['from']}", @client.account.sms.messages.create(
:from => number,
:to => order['from'],
:body => sms_body(order, trade)
)
begin
puts "Notifying #{order['from']} about #{trade['id']}", @client.account.sms.messages.create(
:from => $number,
:to => order['from'],
:body => sms_body(order, trade)
)
rescue Twilio::REST::RequestError => e
puts "Twilio ERROR!"
puts e.message
end
end

def sms_body(order, trade)
Expand All @@ -39,10 +44,6 @@ def sms_body(order, trade)
buy_order = TradeManager.get_root(trade['buy_order'])
sell_order = TradeManager.get_root(trade['sell_order'])

puts "*******"
puts buy_order
puts sell_order

[buy_order, sell_order].each do |order|
notify_via_sms(order, trade) if order['twilio'] == 'true'
end
Expand Down
12 changes: 6 additions & 6 deletions test/test.rb
Expand Up @@ -3,9 +3,9 @@
@manager = StockManager.new("apple")
@manager.reset!

@manager.buy("1234", 50, 91, true, "a")
@manager.buy("1234", 150, 91, true, "a")
@manager.buy("1234", 100, 92, true, "a")
@manager.sell("1234", 200, 90, true, "a")
@manager.sell("1234", 200, 90, true, "a")
@manager.buy("1234", 100, 92, true, "a")
@manager.buy("+19176395561", 50, 91, true, "a")
@manager.buy("+19176395561", 150, 91, true, "a")
@manager.buy("+19176395561", 100, 92, true, "a")
@manager.sell("+19176395561", 200, 90, true, "a")
@manager.sell("+19176395561", 200, 90, true, "a")
@manager.buy("+19176395561", 100, 92, true, "a")

0 comments on commit f7e853f

Please sign in to comment.