-
Notifications
You must be signed in to change notification settings - Fork 1
/
TradeThem.rb
155 lines (127 loc) · 4.4 KB
/
TradeThem.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require 'rubygems'
require 'twitter'
require 'yaml' #muahaha
require_relative "twitterComm"
require_relative 'models'
require_relative 'functions'
class TradeThem
def initialize
@last_tweet_file = "config/last_tweet.txt"
end
def debug(s)
puts(s)
end
def opposite_type(type)
return SellTransaction if type =~ /buy/i
return BuyTransaction if type =~ /sell/i
end
def configure
@twitComm = TwitterComm.new
twitter_config = YAML::load_file 'config/twitter.yml'
#@TradeThem feed api setup
Twitter.configure do |config|
config.consumer_key = twitter_config['trade']['consumer_key']
config.consumer_secret = twitter_config['trade']['consumer_secret']
config.oauth_token = twitter_config['trade']['oauth_token']
config.oauth_token_secret = twitter_config['trade']['oauth_token_secret']
end
end
def main
last_tweet_id = File.read(@last_tweet_file).chomp rescue nil
incoming_tweets = @twitComm.getMentions(last_tweet_id.to_i)
#TESTING
#twitComm.send_lack_of_shares_tweet("@SnareHanger", "Company X")
#twitComm.send_insufficient_funds("@SnareHanger")
#twitComm.send_portfolio_value_tweet("SnareHanger", 8230.25)
#Go through incoming tweets and process them
incoming_tweets.each do |tweet|
debug tweet.inspect
next if tweet.nil? #can be nil if invalid format
last_tweet_id = tweet[:id]
debug "last_tweet_id: #{last_tweet_id}"
#"buy" => "Buy" :-P
tweet[:type].gsub!(/^(\w{1})/) {|s| s.upcase}
company = Company.find_by_symbol(tweet[:company].upcase)
buyer = Player.find_by_username(tweet[:buyer]) unless tweet[:buyer].blank?
seller = Player.find_by_username(tweet[:seller]) unless tweet[:seller].blank?
if company.nil?
@twitComm.tweet_error CompanyNotFoundError.new(tweet[:company].upcase) and next
end
#whether to create new transaction or not
new_tx = false
to = tweet.delete(:to)
if to.nil? || to.empty?
debug "to is blank - new transaction"
new_tx = true
else
#see if there are any transactions pending
tx_klass = opposite_type(tweet[:type])
txs = tx_klass.active.not_completed.where(
:company_id => company,
:quantity => tweet[:quantity],
:price => tweet[:price]
) #order - most recent first? or oldest first? thinking oldest
#.order("created_at ASC")
#oldest_first #scope
#additional filter on buyer or seller
#remember, we are checking to see if this is a confirmation
#i.e. original tx is opposite of tweet[:type]
case tweet[:type]
when /buy/i
from = buyer
txs = txs.where(:seller_id => seller.id)
when /sell/i
from = seller
txs = txs.where(:buyer_id => buyer.id)
end
#any pending transactions?
if txs.any?
tr = txs.first
begin
debug "Completing older transaction: " + tr.inspect
tr.complete!(from)
#TODO: Update twitter with confirmed transaction
rescue
@twitComm.tweet_error($!) and next
end
else #it's a counter-offer
puts "Counter offer detected"
new_tx = true
end
end
#should work, if everything was parsed correctly
if new_tx
puts "New Transaction"
#puts tweet
tx = Transaction.new
tx.type = tweet[:type] + "Transaction"
tx.expiration_date = Time.now + 2*3600 #2 hours from now
tx.company = company
tx.quantity = tweet[:quantity]
tx.price = tweet[:price]
case tweet[:type]
when /buy/i
tx.buyer = buyer
if tx.buyer.nil?
@twitComm.tweet_error PlayerNotFoundError.new(tweet[:buyer]) and next
end
when /sell/i
tx.seller = seller
if tx.seller.nil?
@twitComm.tweet_error PlayerNotFoundError.new(tweet[:seller]) and next
end
end
puts tx.inspect
tx.save!
end
end #process tweet loop
#run csv updates
write_assets_file Player.all(:include => :portfolio_items)
write_stock_price_file Company.all
#save the last tweet
debug "last_tweet_id: #{last_tweet_id}"
File.open(@last_tweet_file, "w") do |f|
f.puts last_tweet_id
end
end
end