mojodna-fireeagle 0.7.0.1 → 0.8.0.0

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.
data/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.8.0.0
2
+
3
+ * Update recent method
4
+
5
+ == 0.7.1.0
6
+
7
+ * Fix OAuth Dependency Issue
8
+
1
9
  == 0.6.3
2
10
 
3
11
  * Bugfix for Ruby 1.9
@@ -2,7 +2,7 @@ require 'fileutils'
2
2
  include FileUtils
3
3
 
4
4
  require 'rubygems'
5
- %w[rake hoe newgem rubigen oauth GeoRuby].each do |req_gem|
5
+ %w[rake hoe newgem rubigen oauth geo_ruby].each do |req_gem|
6
6
  begin
7
7
  require req_gem
8
8
  rescue LoadError
@@ -15,4 +15,4 @@ end
15
15
 
16
16
  $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
17
17
 
18
- require 'fireeagle'
18
+ require 'fireeagle'
@@ -1,4 +1,4 @@
1
- class FireEagle
1
+ module FireEagle
2
2
  class Client
3
3
  # TODO add access_token=() and request_token=() methods that check whether the tokens are usable
4
4
 
@@ -114,7 +114,7 @@ class FireEagle
114
114
  "#{FireEagle::MOBILE_AUTH_URL}#{@app_id}"
115
115
  end
116
116
 
117
- # The URL the user must access to authorize this token. request_token must be called first. For use by web-based and desktop-based applications.
117
+ # The URL the user must access to authorize this token. get_request_token must be called first. For use by web-based and desktop-based applications.
118
118
  def authorization_url
119
119
  raise FireEagle::ArgumentError, "call #get_request_token first" if @request_token.nil?
120
120
  request_token.authorize_url
@@ -154,7 +154,7 @@ class FireEagle
154
154
  # * <tt>plazes_id</tt>
155
155
  def lookup(params)
156
156
  raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
157
- response = get(FireEagle::LOOKUP_API_PATH + ".#{format}", :params => params)
157
+ response = get(FireEagle::LOOKUP_API_PATH, :params => params)
158
158
  FireEagle::Response.new(response.body).locations
159
159
  end
160
160
 
@@ -186,14 +186,14 @@ class FireEagle
186
186
  def update(location = {})
187
187
  raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
188
188
  location = sanitize_location_hash(location)
189
- response = post(FireEagle::UPDATE_API_PATH + ".#{format}", :params => location)
189
+ response = post(FireEagle::UPDATE_API_PATH, :params => location)
190
190
  FireEagle::Response.new(response.body)
191
191
  end
192
192
 
193
193
  # Returns the Location of a User.
194
194
  def user
195
195
  raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
196
- response = get(FireEagle::USER_API_PATH + ".#{format}")
196
+ response = get(FireEagle::USER_API_PATH)
197
197
  FireEagle::Response.new(response.body).users.first
198
198
  end
199
199
  alias_method :location, :user
@@ -203,13 +203,13 @@ class FireEagle
203
203
  #
204
204
  # == Optional parameters:
205
205
  #
206
- # <tt>count</tt> Number of users to return per page. (default: 10)
207
- # <tt>start</tt> The page number at which to start returning the list of users. Pages are 0-indexed, each page contains the per_page number of users. (default: 0)
208
206
  # <tt>time</tt> The time to start looking at recent updates from. Value is flexible, supported forms are 'now', 'yesterday', '12:00', '13:00', '1:00pm' and '2008-03-12 12:34:56'. (default: 'now')
209
- def recent(count = 10, start = 0, time = 'now')
207
+ # <tt>count</tt> Number of users to return per page. (default: 10)
208
+ # <tt>start</tt> The page number at which to start returning the list of users.
209
+ def recent(time = 'now', count = 10, start = 1)
210
210
  raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
211
211
  params = { :count => count, :start => start, :time => time }
212
- response = get(FireEagle::RECENT_API_PATH + ".#{format}", :params => params)
212
+ response = get(FireEagle::RECENT_API_PATH, :params => params)
213
213
  FireEagle::Response.new(response.body).users
214
214
  end
215
215
 
@@ -233,11 +233,11 @@ class FireEagle
233
233
  # * <tt>upcoming_venue_id</tt>
234
234
  # * <tt>yahoo_local_id</tt>
235
235
  # * <tt>plazes_id</tt>
236
- def within(location = {}, count = 10, start = 0)
236
+ def within(location = {}, count = 10, start = 1)
237
237
  raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
238
238
  location = sanitize_location_hash(location)
239
239
  params = { :count => count, :start => start }.merge(location)
240
- response = get(FireEagle::WITHIN_API_PATH + ".#{format}", :params => params)
240
+ response = get(FireEagle::WITHIN_API_PATH, :params => params)
241
241
  FireEagle::Response.new(response.body).users
242
242
  end
243
243
 
@@ -265,17 +265,17 @@ class FireEagle
265
265
  def request(method, url, options) #:nodoc:
266
266
  response = case method
267
267
  when :post
268
- access_token.request(:post, url, options[:params])
268
+ access_token.request(:post, "#{url}.#{format}", options[:params])
269
269
  when :get
270
270
  qs = options[:params].collect { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&") if options[:params]
271
- access_token.request(:get, "#{url}?#{qs}")
271
+ access_token.request(:get, "#{url}.#{format}?#{qs}")
272
272
  else
273
273
  raise ArgumentError, "method #{method} not supported"
274
274
  end
275
275
 
276
276
  case response.code
277
+ when '404'; then raise FireEagle::FireEagleException, "Not Found"
277
278
  when '500'; then raise FireEagle::FireEagleException, "Internal Server Error"
278
- when '400'; then raise FireEagle::FireEagleException, "Method Not Implemented Yet"
279
279
  else response
280
280
  end
281
281
  end
@@ -1,5 +1,5 @@
1
1
  #Describes a location
2
- class FireEagle
2
+ module FireEagle
3
3
  class Location
4
4
 
5
5
  #Initialize a Location from an XML response
@@ -49,6 +49,21 @@ class FireEagle
49
49
  @lower_corner ||= @georss[0..1] rescue nil
50
50
  end
51
51
 
52
+ # The actual query that the user used so that it can be geocoded by the
53
+ # consumer since the Yahoo! geocoder is a little flaky, especially when it
54
+ # comes to intersections etc.
55
+ #
56
+ # Turns something like this:
57
+ #
58
+ # <query> "q=333%20W%20Harbor%20Dr,%20San%20Diego,%20CA" </query>
59
+ #
60
+ # into
61
+ #
62
+ # 333 W Harbor Dr, San Diego, CA
63
+ def query
64
+ @query ||= CGI::unescape((@doc.at("/location/query").innerText).gsub('"', '').split('=')[1]).strip rescue nil
65
+ end
66
+
52
67
  # The GeoRuby[http://georuby.rubyforge.org/] representation of this location
53
68
  def geom
54
69
  if @doc.at("/location//georss:box")
@@ -67,4 +82,4 @@ class FireEagle
67
82
  end
68
83
 
69
84
  end
70
- end
85
+ end
@@ -1,4 +1,4 @@
1
- class FireEagle
1
+ module FireEagle
2
2
  class Response
3
3
 
4
4
  #Parses the XML response from FireEagle
@@ -1,6 +1,5 @@
1
- class FireEagle
1
+ module FireEagle
2
2
  class User
3
-
4
3
  #Parses the XML response from FireEagle.
5
4
  def initialize(doc)
6
5
  doc = Hpricot(doc) unless doc.is_a?(Hpricot::Doc || Hpricot::Elem)
@@ -12,6 +11,11 @@ class FireEagle
12
11
  @token ||= @doc.at("/user").attributes["token"] rescue nil
13
12
  end
14
13
 
14
+ #The time at which this user was last located
15
+ def located_at
16
+ @located_at ||= Time.parse(@doc.at("/user").attributes["located-at"]) rescue nil
17
+ end
18
+
15
19
  #FireEagle's "best guess" form this User's Location. This best guess is derived as the most accurate
16
20
  #level of the hierarchy with a timestamp in the last half an hour <b>or</b> as the most accurate
17
21
  #level of the hierarchy with the most recent timestamp.
@@ -1,7 +1,7 @@
1
- class FireEagle #:nodoc:
1
+ module FireEagle #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 7
4
+ MINOR = 8
5
5
  TINY = 0
6
6
  TEENY = 0
7
7
 
data/lib/fireeagle.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  require 'time'
2
2
  require 'net/https'
3
3
  require 'rubygems'
4
- gem 'oauth', ">= 0.2.1"
4
+ gem 'oauth', ">= 0.2.4"
5
+ require 'oauth/helper'
5
6
  require 'oauth/client/helper'
6
7
  require 'oauth/request_proxy/net_http'
7
8
  require 'hpricot'
8
9
  require 'geo_ruby'
9
10
 
10
- class FireEagle
11
+ module FireEagle
11
12
  API_SERVER = "https://fireeagle.yahooapis.com"
12
13
  AUTH_SERVER = "https://fireeagle.yahoo.net"
13
14
  REQUEST_TOKEN_PATH = "/oauth/request_token"
@@ -5,6 +5,8 @@ describe "FireEagle Location" do
5
5
  before(:each) do
6
6
  location = Hpricot.XML(XML_LOCATION_CHUNK)
7
7
  @location = FireEagle::Location.new(location)
8
+ location_with_query = Hpricot.XML(XML_QUERY_LOCATION_CHUNK)
9
+ @location_with_query = FireEagle::Location.new(location_with_query)
8
10
  end
9
11
 
10
12
  it "should know if this is a best guess" do
@@ -35,6 +37,10 @@ describe "FireEagle Location" do
35
37
  @location.to_s.should == @location.name
36
38
  end
37
39
 
40
+ it "should return the actual query string" do
41
+ @location_with_query.query.should == "333 W Harbor Dr, San Diego, CA"
42
+ end
43
+
38
44
  describe "GeoRuby support" do
39
45
 
40
46
  it "should represent a bounding box as a GeoRuby Envelope" do
@@ -57,4 +63,4 @@ describe "FireEagle Location" do
57
63
 
58
64
  end
59
65
 
60
- end
66
+ end
@@ -57,7 +57,7 @@ describe "FireEagle Response" do
57
57
  describe "error handling" do
58
58
 
59
59
  it "should raise an exception when returned xml with a status of fail" do
60
- lambda { FireEagle::Response.new(XML_ERROR_RESPONSE) }.should raise_error(FireEagle::FireEagleException)
60
+ lambda { FireEagle::Response.new(XML_ERROR_RESPONSE) }.should raise_error(FireEagle::FireEagleException, "Something bad happened")
61
61
  end
62
62
 
63
63
  end
@@ -134,11 +134,12 @@ describe "FireEagle" do
134
134
  end
135
135
 
136
136
  describe "lookup method" do
137
-
138
137
  before(:each) do
139
- @client = FireEagle::Client.new(:consumer_key => 'key', :consumer_secret => 'sekret', :access_token => 'toke', :access_token_secret => 'sekret')
140
- @response = stub('response', :body => XML_LOOKUP_RESPONSE)
141
- @client.stub!(:request).and_return(@response)
138
+ @client = FireEagle::Client.new(:consumer_key => 'key', :consumer_secret => 'sekret', :access_token => 'toke', :access_token_secret => 'sekret')
139
+ response = stub('response', :body => XML_LOOKUP_RESPONSE)
140
+ fail_response = stub('fail response', :body => XML_FAIL_LOOKUP_RESPONSE)
141
+ @client.stub!(:request).with(:get, FireEagle::LOOKUP_API_PATH, :params => {:q => "30022"}).and_return(response)
142
+ @client.stub!(:request).with(:get, FireEagle::LOOKUP_API_PATH, :params => {:mnc => 12, :mcc => 502, :lac => 2051, :cellid => 39091}).and_return(fail_response)
142
143
  end
143
144
 
144
145
  it "should return an array of Locations" do
@@ -153,6 +154,11 @@ describe "FireEagle" do
153
154
  @client.lookup(:q => "30022").first.name.should == "Alpharetta, GA 30022"
154
155
  end
155
156
 
157
+ it "should raise an exception if the lookup failed" do
158
+ lambda {
159
+ @client.lookup(:mnc => 12, :mcc => 502, :lac => 2051, :cellid => 39091)
160
+ }.should raise_error(FireEagle::FireEagleException, "Place can't be identified.")
161
+ end
156
162
  end
157
163
 
158
164
  describe "within method" do
@@ -179,12 +185,45 @@ describe "FireEagle" do
179
185
  end
180
186
 
181
187
  it "should return an array of Users" do
182
- @client.recent(2, 1, 'yesterday').should have(2).users
188
+ @client.recent('yesterday', 3, 1).should have(3).users
183
189
  end
184
190
 
185
- it "should return an array of Locations for each" do
186
- @client.recent(2, 1, 'yesterday').first.should have(5).locations
191
+ it "should have an 'located_at' timestamp for each user" do
192
+ @client.recent('yesterday', 3, 1).first.located_at.should == Time.parse('2008-07-31T22:31:37+12:00')
187
193
  end
188
194
  end
189
195
 
196
+ describe "making a request" do
197
+ before do
198
+ @client = FireEagle::Client.new(:consumer_key => 'key', :consumer_secret => 'sekret', :access_token => 'toke', :access_token_secret => 'sekret')
199
+ @access_token = stub('access token')
200
+ @client.stub!(:access_token).and_return(@access_token)
201
+ end
202
+
203
+ it "should not raise any exception when response is OK" do
204
+ response = stub('response', :code => '200', :body => XML_RECENT_RESPONSE)
205
+ @access_token.stub!(:request).and_return(response)
206
+ lambda { @client.recent }.should_not raise_error
207
+ end
208
+
209
+ it "should raise an exception when requesting to a resource that doesn't exist (404)" do
210
+ response = stub('response', :code => '404', :body => '')
211
+ @access_token.stub!(:request).and_return(response)
212
+ lambda { @client.recent }.should raise_error(FireEagle::FireEagleException, 'Not Found')
213
+ end
214
+
215
+ it "should raise an exception when requesting to a resource that hit an internal server error (500)" do
216
+ response = stub('response', :code => '500', :body => '')
217
+ @access_token.stub!(:request).and_return(response)
218
+ lambda { @client.recent }.should raise_error(FireEagle::FireEagleException, 'Internal Server Error')
219
+ end
220
+
221
+ it "should raise an exception when response is apart from 200, 404 and 500" do
222
+ %w{401 403 405}.each do |code|
223
+ response = stub('response', :code => code, :body => XML_ERROR_RESPONSE)
224
+ @access_token.stub!(:request).and_return(response)
225
+ lambda { @client.recent }.should raise_error(FireEagle::FireEagleException, 'Something bad happened')
226
+ end
227
+ end
228
+ end
190
229
  end
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@ rescue LoadError
5
5
  gem 'rspec'
6
6
  require 'spec'
7
7
  end
8
+ require 'time'
8
9
 
9
10
  XML_ERROR_RESPONSE = <<-RESPONSE
10
11
  <?xml version="1.0" encoding="utf-8"?>
@@ -73,6 +74,19 @@ XML_LOCATION_CHUNK = <<-RESPONSE
73
74
  </location>
74
75
  RESPONSE
75
76
 
77
+ XML_QUERY_LOCATION_CHUNK = <<-RESPONSE
78
+ <location best-guess="true">
79
+ <id>111541</id>
80
+ <georss:point>32.7093315125 -117.1650772095</georss:point>
81
+ <level>0</level>
82
+ <level-name>exact</level-name>
83
+ <located-at>2008-03-03T09:05:16-08:00</located-at>
84
+ <name>333 W Harbor Dr, San Diego, CA</name>
85
+ <query> "q=333%20W%20Harbor%20Dr,%20San%20Diego,%20CA" </query>
86
+ </location>
87
+ RESPONSE
88
+
89
+
76
90
  XML_EXACT_LOCATION_CHUNK = <<-RESPONSE
77
91
  <location>
78
92
  <georss:point>38.5351715088 -121.7948684692</georss:box>
@@ -124,6 +138,13 @@ XML_LOOKUP_RESPONSE = <<-RESPONSE
124
138
  </rsp>
125
139
  RESPONSE
126
140
 
141
+ XML_FAIL_LOOKUP_RESPONSE = <<-RESPONSE
142
+ <?xml version="1.0" encoding="utf-8"?>
143
+ <rsp stat="fail">
144
+ <err msg="Place can't be identified." code="6"/>
145
+ </rsp>
146
+ RESPONSE
147
+
127
148
  XML_WITHIN_RESPONSE = <<-RESPONSE
128
149
  <?xml version="1.0" encoding="UTF-8"?>
129
150
  <rsp stat="ok">
@@ -254,106 +275,13 @@ RESPONSE
254
275
 
255
276
  XML_RECENT_RESPONSE = <<-RESPONSE
256
277
  <?xml version="1.0" encoding="UTF-8"?>
257
- <rsp stat="ok" xmlns:georss="http://www.georss.org/georss">
278
+ <rsp xmlns:georss="http://www.georss.org/georss" stat="ok">
258
279
  <users>
259
- <user token="MQdDrJgXMNJi">
260
- <location-hierarchy>
261
- <location best-guess="true">
262
- <id>111541</id>
263
- <georss:point>32.7093315125 -117.1650772095</georss:point>
264
- <level>0</level>
265
- <level-name>exact</level-name>
266
- <located-at>2008-03-03T09:05:16-08:00</located-at>
267
- <name>333 W Harbor Dr, San Diego, CA</name>
268
- </location>
269
- <location best-guess="false">
270
- <id>111551</id>
271
- <georss:box>32.6916618347 -117.2174377441 32.744140625 -117.1458892822</georss:box>
272
- <level>1</level>
273
- <level-name>postal</level-name>
274
- <located-at>2008-03-03T09:05:16-08:00</located-at>
275
- <name>San Diego, CA 92101</name>
276
- <place-id>NpiXqwmYA5viX3K3Ew</place-id>
277
- <woeid>12796255</woeid>
278
- </location>
279
- <location best-guess="false">
280
- <id>111561</id>
281
- <georss:box>32.5349388123 -117.2884292603 33.1128082275 -116.9142913818</georss:box>
282
- <level>3</level>
283
- <level-name>city</level-name>
284
- <located-at>2008-03-03T09:05:16-08:00</located-at>
285
- <name>San Diego, CA</name>
286
- <place-id>Nm4O.DebBZTYKUsu</place-id>
287
- <woeid>2487889</woeid>
288
- </location>
289
- <location best-guess="false">
290
- <id>111571</id>
291
- <georss:box>32.5342788696 -124.4150238037 42.0093803406 -114.1308135986</georss:box>
292
- <level>5</level>
293
- <level-name>state</level-name>
294
- <located-at>2008-03-03T09:05:16-08:00</located-at>
295
- <name>California</name>
296
- <place-id>SVrAMtCbAphCLAtP</place-id>
297
- <woeid>2347563</woeid>
298
- </location>
299
- <location best-guess="false">
300
- <id>111581</id>
301
- <georss:box>18.9108390808 -167.2764129639 72.8960571289 -66.6879425049</georss:box>
302
- <level>6</level>
303
- <level-name>country</level-name>
304
- <located-at>2008-03-03T09:05:16-08:00</located-at>
305
- <name>United States</name>
306
- <place-id>4KO02SibApitvSBieQ</place-id>
307
- <woeid>23424977</woeid>
308
- </location>
309
- </location-hierarchy>
310
- </user>
311
- <user token="XMoZaTjZJiOQ">
312
- <location-hierarchy>
313
- <location best-guess="true">
314
- <id>113221</id>
315
- <georss:box>32.8155899048 -96.8162918091 32.8511695862 -96.7717132568</georss:box>
316
- <level>1</level>
317
- <level-name>postal</level-name>
318
- <located-at>2008-03-03T10:24:24-08:00</located-at>
319
- <name>Dallas, TX 75205</name>
320
- <place-id>1KTPxFCYA5vlcics6A</place-id>
321
- <woeid>12790441</woeid>
322
- </location>
323
- <location best-guess="false">
324
- <id>113231</id>
325
- <georss:box>32.6209487915 -96.9988708496 33.0258712769 -96.4660263062</georss:box>
326
- <level>3</level>
327
- <level-name>city</level-name>
328
- <located-at>2008-03-03T10:24:24-08:00</located-at>
329
- <name>Dallas, TX</name>
330
- <place-id>bgWooz.bApRFBRNK</place-id>
331
- <woeid>2388929</woeid>
332
- </location>
333
- <location best-guess="false">
334
- <id>113241</id>
335
- <georss:box>25.8372898102 -106.645652771 36.5006904602 -93.5079269409</georss:box>
336
- <level>5</level>
337
- <level-name>state</level-name>
338
- <located-at>2008-03-03T10:24:24-08:00</located-at>
339
- <name>Texas</name>
340
- <place-id>b1Yi6qubApjz6jWW</place-id>
341
- <woeid>2347602</woeid>
342
- </location>
343
- <location best-guess="false">
344
- <id>113251</id>
345
- <georss:box>18.9108390808 -167.2764129639 72.8960571289 -66.6879425049</georss:box>
346
- <level>6</level>
347
- <level-name>country</level-name>
348
- <located-at>2008-03-03T10:24:24-08:00</located-at>
349
- <name>United States</name>
350
- <place-id>4KO02SibApitvSBieQ</place-id>
351
- <woeid>23424977</woeid>
352
- </location>
353
- </location-hierarchy>
354
- </user>
280
+ <user located-at="2008-07-31T22:31:37+12:00" token="5pyl1xip0uh6"/>
281
+ <user located-at="2008-07-31T21:49:03+12:00" token="i71yc3myixg3"/>
282
+ <user located-at="2008-07-30T21:40:54+12:00" token="q1jm8nubnpsi"/>
355
283
  </users>
356
284
  </rsp>
357
285
  RESPONSE
358
286
 
359
- require File.dirname(__FILE__) + '/../lib/fireeagle'
287
+ require File.dirname(__FILE__) + '/../lib/fireeagle'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mojodna-fireeagle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0.1
4
+ version: 0.8.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Newland
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirements:
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.2.1
22
+ version: 0.2.4
23
23
  version:
24
24
  - !ruby/object:Gem::Dependency
25
25
  name: hpricot
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  requirements: []
97
97
 
98
98
  rubyforge_project: fireeagle
99
- rubygems_version: 1.0.1
99
+ rubygems_version: 1.2.0
100
100
  signing_key:
101
101
  specification_version: 2
102
102
  summary: Ruby wrapper for Yahoo!'s FireEagle