-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
209 lines (163 loc) · 7.25 KB
/
Rakefile
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
require File.join(File.dirname(__FILE__), 'env')
namespace :categories do
desc "Create the initial categories to sort deals into"
task :create do
# Create a new geoloqi session as the application
geoloqi = Geoloqi::Session.new :access_token => CONFIG['geoloqi']['app_access_token']
# For each category in our env.rb create a geoloqi layer to store deals for that category
CATEGORIES.each do |k,v|
puts "\n\ncreating layer category for #{k}..."
puts geoloqi.post 'layer/create', {:name => k, :key => v, :public => 1}
end
end
desc "Create the initial categories to sort deals into"
task :delete do
# Create a new geoloqi session as the application
geoloqi = Geoloqi::Session.new :access_token => CONFIG['geoloqi']['app_access_token']
geoloqi.get('layer/list')[:layers].each do |layer|
puts "\n\ndeleting layer - #{layer[:name]}"
puts geoloqi.post("layer/delete/#{layer[:layer_id]}")
end
end
end
namespace :deals do
desc "Remove all existing deals from all categories"
task :clean do
# Create a new geoloqi session as the application
geoloqi = Geoloqi::Session.new :access_token => CONFIG['geoloqi']['app_access_token']
geoloqi.get('layer/list')[:layers].each do |layer|
puts "Processing layer #{layer[:layer_id]}: #{layer[:name]}"
geoloqi.get('place/list', :layer_id => layer[:layer_id])[:places].each do |place|
puts "\tPlace: #{place[:name]}"
geoloqi.get('trigger/list', :place_id => place[:place_id])[:triggers].each do |trigger|
puts "\t\tTrigger: #{trigger[:trigger_id]} #{trigger[:key]}"
puts "\t\t\t#{trigger[:text]}"
# puts geoloqi.post("trigger/delete/#{trigger[:trigger_id]}", {})
puts geoloqi.post("trigger/update/#{trigger[:trigger_id]}", {
date_to: DateTime.now.iso8601
})
puts "\n"
end
# puts geoloqi.post("place/delete/#{place[:place_id]}", {})
end
end
end
desc "Remove all existing deals from all categories"
task :delete do
# Create a new geoloqi session as the application
geoloqi = Geoloqi::Session.new :access_token => CONFIG['geoloqi']['app_access_token']
geoloqi.get('layer/list')[:layers].each do |layer|
puts "Processing layer #{layer[:layer_id]}: #{layer[:name]}"
geoloqi.get('place/list', :layer_id => layer[:layer_id])[:places].each do |place|
puts "\tPlace: #{place[:name]}"
geoloqi.get('trigger/list', :place_id => place[:place_id])[:triggers].each do |trigger|
puts "\t\tTrigger: #{trigger[:trigger_id]} #{trigger[:key]}"
puts "\t\t\t#{trigger[:text]}"
puts geoloqi.post("trigger/delete/#{trigger[:trigger_id]}", {})
puts "\n"
end
puts geoloqi.post("place/delete/#{place[:place_id]}", {})
end
end
end
desc "Import deals from Sqoot"
task :import do
# Create a new geoloqi session as the application
geoloqi = Geoloqi::Session.new :access_token => CONFIG['geoloqi']['app_access_token']
# Create a new Sqoot client
sqoot = Sqoot::Client.new
# Get all layers created by the application
geoloqi.get('layer/list')[:layers].each do |layer|
# Store location to search for deals nearby
layer_points = []
# Get information about the distribution of users subscribed to each layer
user_clusters_resp = geoloqi.get("layer/user_clusters/#{layer[:layer_id]}")
# For each cluster of users add a point to search for deals nearby
user_clusters_resp[:clusters].each do |c|
layer_points.push({
:latitude => c[:bounds][:center][:latitude], # 'latitude' & 'longitude'
:longitude => c[:bounds][:center][:longitude], # in the bounds object...
:radius => (c[:bounds][:radius] + (DEAL_SEARCH_RADIUS/2))
})
end
# For each single users add a point to search for deals nearby
user_clusters_resp[:singles].each do |s|
layer_points.push({
:latitude => s[:lat], # ...but it's 'lat' & 'lng'in everything else
:longitude => s[:lng],
:radius => DEAL_SEARCH_RADIUS
})
end
# Store deals in an array
sqoot_deals = []
# For each layer_points search for nearby deals
layer_points.each do |lp|
radius_in_miles = lp[:radius] / 1609.3
# Get nearby deals from Sqoot
begin
offers = sqoot.offers(:location => "#{lp[:latitude]},#{lp[:longitude]}",
:radius => radius_in_miles,
:national => false,
:categories => CATEGORIES[layer[:name]],
:order => 'distance',
:per_page => 250)[:offers]
rescue Exception => e
# TODO error out nicer
STDERR.puts "=========="
STDERR.puts "ERROR FETCHING DEALS"
STDERR.puts e.message
STDERR.puts "=========="
next
end
if offers.size
offers.each do |offer|
offer = offer[:offer]
# For each deal create a trigger in Geoloqi on the appropriate layer.
# You can deduplicate triggers by providing a `key` in the parmeters.
# Provide a url to be opened when the push notification is opened.
# In this case `dinodeals://` is defined by the DinoDeals app.
begin
text = (!offer[:short_title].nil? && offer[:short_title].length > 0) ? offer[:short_title] : offer[:title]
base_string = "You found a new deal"
end_string = offer[:locations][0][:name] ? " at #{offer[:locations][0][:name]}" : ""
trigger_data = { :key => offer[:id],
:type => 'message',
:text => "#{base_string}#{end_string}".strip,
:url => "dinodeals://open?url=#{Rack::Utils.escape(offer[:url])}",
:latitude => offer[:locations][0][:latitude],
:longitude => offer[:locations][0][:longitude],
:radius => DEAL_PLACE_RADIUS,
:date_to => offer[:expires_at],
:one_time => 1,
:place_key => Digest::SHA2.hexdigest(offer[:locations][0].to_json),
:place_layer_id => layer[:layer_id],
:place_name => offer[:locations][0][:name].strip,
:extra => {
:deal_text => text
}
}
puts "\n\ncreating - #{text}"
trigger = geoloqi.post 'trigger/create', trigger_data
puts trigger
# If the trigger already existed with that key, then update it
if geoloqi.response.status == 409
# Only send fields that existed in the trigger response, we don't need to send the place_* fields in the update request
puts geoloqi.post "trigger/update/#{trigger[:trigger_id]}", trigger_data.reject {|key,value| !trigger[key]}
end
rescue Geoloqi::ApiError => gae
# TODO error out nicer
STDERR.puts "=========="
STDERR.puts gae.message
STDERR.puts trigger_data.inspect
STDERR.puts offer
STDERR.puts "=========="
# binding.pry
end
end
end
end
end
end
end
desc "Create an alias for rake cron to import deals on heroku"
task "cron" => "deals:import"