mapsqueak 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/mapsqueak.rb +129 -60
  2. metadata +2 -2
data/lib/mapsqueak.rb CHANGED
@@ -27,6 +27,7 @@ class MapSqueakSession
27
27
  def initialize(host = 'http://mapsqueak.heroku.com', username=nil,password=nil)
28
28
  @host = host
29
29
  @cookie_file = 'anonymous@anaonymous.com.cookies'
30
+ signin(username,password) unless username.nil?
30
31
  end
31
32
 
32
33
  # sign in by email/password.
@@ -35,19 +36,21 @@ class MapSqueakSession
35
36
  signin_xml = Document.new
36
37
  signin_xml.add_element('session')
37
38
  signin_xml.root << xml_element('email',email)
38
- signin_xml.root << xml_element('password',password)
39
+
40
+ # allow nil passwords right now
41
+ signin_xml.root << xml_element('password',password.to_s)
39
42
  puts signin_xml.to_s
40
-
43
+
41
44
  @cookie_file = "#{email}.cookies"
42
-
45
+
43
46
  curl_str = "curl --data \'#{signin_xml.to_s}\' #{self.host}/sessions.xml -H \"Content-Type: application/xml\" --cookie-jar #{@cookie_file}"
44
-
47
+
45
48
  result = `#{curl_str}`
46
49
  puts result
47
50
  doc = Document.new(result)
48
51
  @user_id = XPath.first(doc,"hash/user-id").text
49
52
  end
50
-
53
+
51
54
  # sign the current user out.
52
55
  # note that the cookie file will be deleted.
53
56
  def sign_out
@@ -60,74 +63,119 @@ class MapSqueakSession
60
63
  # post a new squeak. the squeak can either be whatever the ClientSqueak constructor accepts -
61
64
  # a String of json or xml, or a hash, or it can be a ClientSqueak object. Talk about flexibility!
62
65
  # the send_format must be :xml or :json
63
- def post_squeak(squeak,send_format = :xml)
64
- s = nil
66
+ # facebook params is a Hash with either :facebook_test_user => true, or :access_token=> token
67
+ def squeak(squeak,send_format = :xml, facebook_params = {})
68
+ temp_squeak= nil
65
69
  case squeak
66
- when ClientSqueak
67
- s = squeak
68
- when Hash
69
- s = ClientSqueak.new(squeak)
70
+ when ClientSqueak
71
+ temp_squeak= squeak
72
+ when Hash
73
+ temp_squeak= ClientSqueak.new(squeak)
70
74
  end
71
75
  unless [:json, :xml].include?(send_format)
72
76
  $stderr.puts "Error: send_format must be in json or xml"
73
77
  end
74
78
  format_str = send_format.to_s
75
- data = s.send("to_#{format_str}")
79
+ data = temp_squeak.send("to_#{format_str}")
76
80
  curl_str = "curl --data \'#{data}\' #{self.host}/squeaks.#{format_str} -H \"Content-Type: application/#{format_str}\" --cookie #{@cookie_file}"
77
-
81
+
78
82
  # execute the curl command
79
- `#{curl_str}`
83
+ res = `#{curl_str}`
84
+ actual_squeak = ClientSqueak.new(res)
85
+
86
+ if facebook_params.include?(:access_token)
87
+ user = Koala::Facebook::API.new(facebook_params[:access_token])
88
+ elsif(facebook_params.include?(:facebook_test_user))
89
+ test_users = Koala::Facebook::TestUsers.new(:app_id => '107582139349630', :secret => "ca16bbd5834ab7d4b012ec5e84a0d003")
90
+ user_info = test_users.create(true, "offline_access,read_stream,manage_pages,publish_stream")
91
+ login_url = user_info['login_url']
92
+ user = Koala::Facebook::API.new(user_info['access_token'])
93
+ end
94
+
95
+ unless user.nil?
96
+ puts "Using the following facebook user: #{user.inspect}"
97
+
98
+ picture_url = "http://maps.googleapis.com/maps/api/staticmap?center=#{update[:latitude]},#{update[:longitude]}&zoom=13&size=200x200&maptype=roadmap&markers=color:blue%7Clabel:M%7C#{update[:latitude]},#{update[:longitude]}&sensor=true"
99
+
100
+ puts "Google image url: #{picture_url}"
101
+
102
+ # Use google's static map api to get an image for the squeak
103
+ id = user.put_wall_post("MapSqueak update at #{Time.now.strftime('')}",{:name => 'squeak name',
104
+ :link => "#{opts.host}/squeaks/#{confirmation_return_hash['squeak']['id']}",
105
+ :caption => opts.text,
106
+ :description => "the description of the squeak, TBD",
107
+ :picture => picture_url})
108
+ puts "Updated facebook with id: #{id}"
109
+ puts "Visit #{login_url} to see it ..." unless login_url.nil?
110
+ end
111
+
112
+ # return the Squeak
113
+ return actual_squeak
80
114
  end
81
-
115
+
82
116
  # get a list of no more than max squeaks closest to the given center_latitude/centerlongitude
83
117
  # The format must either be :json or :xml.
84
118
  # TODO: create a list of ClientSqueak objects, or make a get_squeak_objects function
85
- def get_squeaks(center_latitude,center_longitude,max = 100,format=:json)
119
+ def get_squeaks(center_latitude,center_longitude,max = 100,format=:xml)
86
120
  # curl "http://192.168.0.2:3000/squeaks.xml?num_squeaks=3&center_latitude=50.0&center_longitude=-1.8"
87
121
  unless [:json, :xml].include?(format)
88
122
  $stderr.puts "Error: must be in json or xml"
89
123
  end
90
124
 
91
- squeaks = `curl #{self.host}/squeaks.#{format.to_s}?num_squeaks=#{max}&center_latitude=#{center_latitude}&center_longitude=#{center_longitude}`
125
+ squeak_string = `curl #{self.host}/squeaks.#{format.to_s}?num_squeaks=#{max}&center_latitude=#{center_latitude}&center_longitude=#{center_longitude}`
92
126
 
93
- # TODO: parse these appropriately
127
+ return squeak_str_to_objects(squeak_string,format)
94
128
  end
95
-
129
+
96
130
  # return all of my squeaks in a specified format, either :xml or :json
97
- def get_my_squeaks(format=:json)
131
+ def get_my_squeaks(format=:xml)
98
132
  unless [:json, :xml].include?(format)
99
133
  $stderr.puts "Error: must be in json or xml"
100
134
  end
101
135
  # TODO: add a hash based on the parameters requested and use session token
102
136
  # T
103
- `curl #{self.host}/users/#{@user_id}.#{format.to_s} --cookie #{@cookie_file}`
137
+ squeak_string = `curl #{self.host}/users/#{@user_id}.#{format.to_s} --cookie #{@cookie_file}`
138
+ return squeak_str_to_objects(squeak_string,format)
139
+ end
140
+
141
+ #
142
+ def edit_squeak(squeak, update_hash)
143
+ squeak.merge!(update_hash)
144
+ "curl --request PUT --data #{squeak.to_json} #{self.host}/squeaks/#{squeak.id}.json -H \"Content-Type: application/json\""
104
145
  end
105
- end
106
146
 
147
+ :private
148
+ def squeak_str_to_objects(squeak_string,format)
149
+ squeaks = []
150
+ case format
151
+ when :xml
152
+ doc = Document.new(squeak_string)
153
+ doc.elements.each('squeaks/') {|el| squeaks << ClientSqueak.new(el.to_s)}
154
+ when :json
155
+ obj = JSON.parse(squeak_string)
156
+ obj['squeaks'].each {|s| squeaks << ClientSqueak.new(s) }
157
+ end
158
+
159
+ return squeaks
160
+ end
161
+ end
107
162
  # ClientSqueak - a class that encapsulates a client side squeak
108
163
  # Note that if I am not the owner of a squeak, I will not know the user
109
164
  class ClientSqueak
110
165
  include XmlHelpers
111
- attr_accessor :latitude, :longitude, :text, :duration, :expires, :username
112
-
113
- @@mandatory_parameters = %w[latitude longitude text duration]
114
- @@min_latitude = -90.0
115
- @@max_latitude = 90.0
116
- @@min_longitude = -180.0
117
- @@max_longitude = 180.0
118
- @@min_duration = 0.0
119
- @@max_duration = 24.0
120
- @@max_text_length = 140
121
-
122
- # Initialize a new squeak which must be in an allowable format:
166
+
167
+ attr_accessor :latitude, :longitude, :text, :duration, :expires, :username, :id, :time_utc,:expires,:created_at,:updated_at,:user_email, :gmaps
168
+
169
+ # Initialize a new squeak which must be in an allowable format
170
+ # Must set: latitude, longitude, text, and duration
123
171
  # json - a String representation of a JSON object
124
- # e.g. {\"squeak\":{\"latitude\":\"54.1\",\"longitude\":\"-1.7\",\"duration\":\"2\",\"text\":\"Another squeak!\"}}
172
+ # e.g. {\"squeak\":{\"latitude\":\"54\",\"longitude\":\"-1.5\",\"duration\":\"2\",\"text\":\"Another squeak!\"}}
125
173
  # xml - a String representation of an XML blob
126
- # e.g. <squeak><latitude>54.1</latitude><longitude>-1.7</longitude><text>Another squeak!</text><duration>2</duration></squeak>
174
+ # e.g. <squeak><latitude>54</latitude><longitude>-1.5</longitude><text>Another squeak!</text><duration>2</duration></squeak>
127
175
  # hash - a hash representation of a squeak
128
- # e.g. {:squeak => {:latitude=> 54.1, :longitude=>-1.69, :text => "Another squeak!", :duration => 2}}
176
+ # e.g. {:squeak => {:latitude=> 54, :longitude=>-1.69, :text => "Another squeak!", :duration => 2}}
129
177
  # OR if you are lazy (like me) you can just specify the inner hash:
130
- # e.g. {:latitude=> 54.1, :longitude=>-1.69, :text => "Another squeak!", :duration => 2}
178
+ # e.g. {:latitude=> 54, :longitude=>-1.69, :text => "Another squeak!", :duration => 2}
131
179
  def initialize(squeak)
132
180
  @original_squeak = squeak
133
181
  temp_hash = {:squeak => {}}
@@ -149,32 +197,37 @@ class ClientSqueak
149
197
  # a nice flexible way to parse the XML. as long as the XML
150
198
  # is only one level deep
151
199
  doc.elements.each('squeak/') do | el |
152
- el.each do |child|
200
+ el.elements.each do |child|
153
201
  temp_hash[:squeak][child.name.to_sym] = child.text
154
202
  end
155
203
  end
156
204
  rescue Exception => e2
157
- raise "Can't parse squeak text: #{squeak}"
205
+ raise "Can't parse squeak text: #{squeak}\n" + e2.backtrace.join("\n")
158
206
  end
159
207
  end
160
208
  end
161
209
 
162
210
  t = temp_hash['squeak'] || temp_hash[:squeak]
163
- parms_set = []
164
- t.keys.each do | k |
165
- # this will set and validate the parameters
166
- self.send("#{k.to_s}=",t[k])
167
- parms_set << k.to_s
168
- end
211
+ self.merge!(t)
169
212
 
170
213
  # but we need to make sure that all of the mandatory parameters are defined
171
- unless (@@mandatory_parameters - parms_set).empty?
214
+ unless (%w[latitude longitude text duration] - self.to_hash[:squeak].keys.map {|k| k.to_s}).empty?
172
215
  raise "Must have duration, lat and long and text"
173
216
  end
174
217
  end
175
218
 
219
+ # set the id, but only allow it once. we don't necessarily know the id at construction time, so
220
+ # this should be good enough for now.
221
+ def id=(id)
222
+ if @id.nil?
223
+ @id = id
224
+ else
225
+ raise "Can't change a squeak's id"
226
+ end
227
+ end
228
+
176
229
  # convert the squeak to XML format
177
- # e.g. <squeak><latitude>54.1</latitude><longitude>-1.7</longitude><text>Another squeak!</text><duration>2</duration></squeak>
230
+ # e.g. <squeak><latitude>54</latitude><longitude>-1.5</longitude><text>Another squeak!</text><duration>2</duration></squeak>
178
231
  def to_xml
179
232
  params_xml = Document.new
180
233
  params_xml.add_element('squeak')
@@ -186,7 +239,7 @@ class ClientSqueak
186
239
  end
187
240
 
188
241
  # convert the squeak to JSON format
189
- # e.g. {\"squeak\":{\"latitude\":\"54.1\",\"longitude\":\"-1.7\",\"duration\":\"2\",\"text\":\"Another squeak!\"}}
242
+ # e.g. {\"squeak\":{\"latitude\":\"54\",\"longitude\":\"-1.5\",\"duration\":\"2\",\"text\":\"Another squeak!\"}}
190
243
  def to_json
191
244
  self.to_hash.to_json
192
245
  end
@@ -203,37 +256,53 @@ class ClientSqueak
203
256
  }
204
257
  end
205
258
 
259
+ # merge another Squeak's parameters by letting the other's parameters overwrite the current Squeak's parms.
260
+ # Returns a copy Hash
261
+ def merge(other)
262
+ self.to_hash.merge!(other)
263
+ end
264
+
265
+ # merge another Squeak's parameters by letting the other's parameters overwrite the current Squeak's parms.
266
+ # Modifies the current Squeak
267
+ def merge!(other)
268
+ t = other.to_hash
269
+ t.keys.each do | k |
270
+ # this will set and validate the parameters
271
+ self.send("#{k.to_s.gsub('-','_')}=",t[k])
272
+ end
273
+ end
274
+
206
275
  # set the latitude for the squeak
207
- # must be between @@min_latitude and @@max_latitude
276
+ # must be between -90.0 and 90.0
208
277
  def latitude=(latitude)
209
278
  lat = latitude.to_f
210
- raise "Bad latitude" if lat < @@min_latitude
211
- raise "Bad latitude" if lat > @@max_latitude
279
+ raise "Bad latitude" if lat < -90.0
280
+ raise "Bad latitude" if lat > 90.0
212
281
  @latitude = lat
213
282
  end
214
283
 
215
284
  # set the longitude for the squeak
216
- # must be between @@min_longitude and @@max_longitude
285
+ # must be between -180.0 and 180.0
217
286
  def longitude=(longitude)
218
287
  long = longitude.to_f
219
- raise "Bad longitude" if long < @@min_longitude
220
- raise "Bad longitude" if long > @@max_longitude
288
+ raise "Bad longitude" if long < -180.0
289
+ raise "Bad longitude" if long > 180.0
221
290
  @longitude = long
222
291
  end
223
292
 
224
293
  # set the time to live for the squeak
225
- # must be between @@min_duration and @@max_duration
294
+ # must be between 0.0 and 24.0
226
295
  def duration=(duration)
227
296
  dur = duration.to_f
228
- raise "Bad duration" if dur < @@min_duration
229
- raise "Bad duration" if dur > @@max_duration
297
+ raise "Bad duration" if dur < 0.0
298
+ raise "Bad duration" if dur > 24.0
230
299
  @duration = dur
231
300
  end
232
301
 
233
302
  # set the message portion of the squeak
234
- # length can't exceed @@max_text_length
303
+ # length can't exceed 140
235
304
  def text=(txt)
236
- raise "Bad text length" unless txt.length <= @@max_text_length
305
+ raise "Bad text length" unless txt.length <= 140
237
306
  @text = txt.dup
238
307
  end
239
308
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mapsqueak
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ethan Stryker
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-12-03 00:00:00 +00:00
13
+ date: 2011-12-07 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies: []
16
16