john-mayer 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # John Mayer
2
2
 
3
- It's a Foursquare API wrapper.
3
+ It's a Foursquare API wrapper, designed to "just work", just like all of John Mayer's hooks and
4
+ sweet guitar licks. It uses objects instead of hashes, and tries to be smart about when to load things.
4
5
 
5
6
  ## Usage
6
7
 
@@ -8,25 +9,65 @@ Get a foursquare:
8
9
 
9
10
  foursquare = Foursquare::Base.new("ACCESS_TOKEN")
10
11
 
12
+ ### Users
13
+
11
14
  Find a user:
12
15
 
13
- foursquare.users.find("USER_ID")
16
+ user = foursquare.users.find("USER_ID")
17
+
18
+ Now we've got a `Foursquare::User` object. You can call sweet methods like `user.name` and even
19
+ `user.last_checkin`. **In general, John Mayer's Foursquare object methods are just snake-cased
20
+ versions of the attributes returned in the JSON.** Now let's accidentally that user's friends:
21
+
22
+ user.friends
23
+
24
+ This will return an array of `Foursquare::User` objects. Don't worry about the fact that they're
25
+ populated by limited JSON. John Mayer will fetch the extra JSON if you need it. For example:
26
+
27
+ friend = user.friends.first
28
+ friend.name # Will not trigger a network call, since we already have it
29
+ friend.twitter # Will trigger a network to load the user's contact information
30
+
31
+ ### Checkins
32
+
33
+ But wait, Foursquare isn't just users! It's checkins too! So let's find some checkins:
14
34
 
15
- Find a checkin:
35
+ user.checkins
16
36
 
17
- foursquare.checkins.find("CHECKIN_ID")
37
+ Now we have an array of `Foursquare::Checkin` objects. We can also grab a specific checkin:
18
38
 
19
- Find a venue:
39
+ checkin = foursquare.checkins.find("CHECKIN_ID")
40
+
41
+ ### Venues
42
+
43
+ We can get at a checkin's venue by calling `checkin.venue`. Pretty easy, RIGHT? Right. If you want to
44
+ find a venue directly, here ya go:
20
45
 
21
46
  foursquare.venues.find("VENUE_ID")
22
47
 
48
+ You can also search venues:
49
+
50
+ foursquare.venues.search(:ll => "40.7236307,-73.9999479") # Returns all resulting groups
51
+ foursquare.venues.nearby(:ll => "40.7236307,-73.9999479") # Returns only nearby venues
52
+ foursquare.venues.trending(:ll => "40.7236307,-73.9999479") # Returns only trending venues
53
+ foursquare.venues.favorites(:ll => "40.7236307,-73.9999479") # Returns only favorite venues
54
+
55
+ The `:ll` option is required for venue searches. You can also feel free to pass any of the other
56
+ available Foursquare API options, as specified in the docs.
57
+
23
58
  ### Logging
24
59
 
25
60
  If you want to see what's going on up in there, you can set `Foursquare.verbose` to `true`
26
61
 
27
62
  Foursquare.verbose = true
28
63
 
29
- Right now it'll log to `STDOUT`. Maybe I'll add nicer logging later. If you're lucky.
64
+ Right now it'll log to `STDOUT`. Maybe I'll add nicer logging later. If you're lucky. In the meantime,
65
+ if you want to use your own logger, and you're kind of a jerk like me, you can do something like this:
66
+
67
+ Foursquare.verbose = true
68
+ def Foursquare.log(message)
69
+ Rails.logger.info("[foursquare] #{message}") # HAX, SORRY BRANDON
70
+ end
30
71
 
31
72
  ## TODO
32
73
 
@@ -3,6 +3,7 @@ $LOAD_PATH << File.dirname(__FILE__)
3
3
  require "rubygems"
4
4
  require "typhoeus"
5
5
  require "json"
6
+ require "cgi"
6
7
  require "foursquare/base"
7
8
  require "foursquare/checkin_proxy"
8
9
  require "foursquare/checkin"
@@ -11,6 +12,8 @@ require "foursquare/user"
11
12
  require "foursquare/venue_proxy"
12
13
  require "foursquare/venue"
13
14
  require "foursquare/settings"
15
+ require "foursquare/tip"
16
+ require "foursquare/photo"
14
17
 
15
18
  module Foursquare
16
19
  class Error < StandardError ; end
@@ -29,16 +29,17 @@ module Foursquare
29
29
  params.merge!(:oauth_token => @access_token)
30
30
  response = JSON.parse(Typhoeus::Request.get(API + path, :params => params).body)
31
31
  Foursquare.log(response.inspect)
32
- response["meta"]["errorType"] ? error(response) : response["response"]
32
+ error(response) || response["response"]
33
33
  end
34
34
 
35
35
  def post(path, params={})
36
36
  params = camelize(params)
37
37
  Foursquare.log("POST #{API + path}")
38
+ Foursquare.log("PARAMS: #{params.inspect}")
38
39
  params.merge!(:oauth_token => @access_token)
39
40
  response = JSON.parse(Typhoeus::Request.post(API + path, :params => params).body)
40
41
  Foursquare.log(response.inspect)
41
- response["meta"]["errorType"] ? error(response) : response["response"]
42
+ error(response) || response["response"]
42
43
  end
43
44
 
44
45
  private
@@ -51,7 +52,15 @@ module Foursquare
51
52
  end
52
53
 
53
54
  def error(response)
54
- raise Foursquare::Error.new(Foursquare::ERRORS[response['meta']['errorType']])
55
+ case response["meta"]["errorType"]
56
+ when nil
57
+ # It's all good.
58
+ when "deprecated"
59
+ Foursquare.log(Foursquare::ERRORS[response['meta']['errorType']])
60
+ nil
61
+ else
62
+ raise Foursquare::Error.new(Foursquare::ERRORS[response['meta']['errorType']])
63
+ end
55
64
  end
56
65
  end
57
66
  end
@@ -27,5 +27,6 @@ module Foursquare
27
27
  nil
28
28
  end
29
29
  end
30
+ alias_method :add, :create
30
31
  end
31
32
  end
@@ -0,0 +1,38 @@
1
+ # Here's how I want this to work:
2
+ #
3
+ # users, venues = foursquare.multi do |request|
4
+ # request.users.search :twitter => "nakajima"
5
+ # request.venues.search :ll => "12,-71"
6
+ # end
7
+ #
8
+ # It's just difficult to implement. So it's not implemented yet.
9
+ module Foursquare
10
+ class Multi
11
+ def initialize(foursquare, block)
12
+ @foursquare = foursquare
13
+ @requests = []
14
+ @responses = []
15
+ end
16
+
17
+ def get(path, options={})
18
+ @requests << path + query(params)
19
+ end
20
+
21
+ def perform
22
+ responses = @foursquare.get("multi", :requests => @requests.join(','))
23
+ end
24
+
25
+ private
26
+
27
+ def query(params)
28
+ camelized = params.inject({}) { |o, (k, v)|
29
+ o[k.to_s.gsub(/(_[a-z])/) { |m| m[1..1].upcase }] = v
30
+ o
31
+ }
32
+ camelized.inject([]) { |o, (k, v)|
33
+ o << CGI.escape(k) + "=" CGI.escape(v)
34
+ o
35
+ }.join('&')
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,27 @@
1
+ module Foursquare
2
+ class Photo
3
+ def initialize(foursquare, json)
4
+ @foursquare, @json = foursquare, json
5
+ end
6
+
7
+ def id
8
+ @json["id"]
9
+ end
10
+
11
+ def name
12
+ @json["name"]
13
+ end
14
+
15
+ def created_at
16
+ @json["createdAt"]
17
+ end
18
+
19
+ def url
20
+ @json["url"]
21
+ end
22
+
23
+ def sizes
24
+ @json["sizes"]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ module Foursquare
2
+ class Tip
3
+ def initialize(foursquare, json)
4
+ @foursquare, @json = foursquare, json
5
+ end
6
+
7
+ def id
8
+ @json["id"]
9
+ end
10
+
11
+ def text
12
+ @json["text"]
13
+ end
14
+
15
+ def created_at
16
+ @json["createdAt"]
17
+ end
18
+
19
+ def status
20
+ @json["status"]
21
+ end
22
+
23
+ def photo
24
+ @json["photo"]
25
+ end
26
+
27
+ def user
28
+ @json["user"] && Foursquare::User.new(@foursquare, @json["user"])
29
+ end
30
+
31
+ def venue
32
+ @json["venue"] && Foursquare::Venue.new(@foursquare, @json["venue"])
33
+ end
34
+ end
35
+ end
@@ -102,5 +102,11 @@ module Foursquare
102
102
  Foursquare::User.new(@foursquare, item)
103
103
  end
104
104
  end
105
+
106
+ def tips(options={})
107
+ @foursquare.get("users/#{id}/tips", options)["tips"]["items"].map do |item|
108
+ Foursquare::Tip.new(@foursquare, item)
109
+ end
110
+ end
105
111
  end
106
112
  end
@@ -4,8 +4,18 @@ module Foursquare
4
4
  @foursquare = foursquare
5
5
  end
6
6
 
7
+ def self.search(foursquare, options={})
8
+
9
+ end
10
+
7
11
  def find(id)
8
12
  Foursquare::User.new(@foursquare, @foursquare.get("users/#{id}")["user"])
9
13
  end
14
+
15
+ def search(options={})
16
+ @foursquare.get("users/search", options)["results"].map do |json|
17
+ Foursquare::User.new(@foursquare, json)
18
+ end
19
+ end
10
20
  end
11
21
  end
@@ -39,5 +39,11 @@ module Foursquare
39
39
  def todos_count
40
40
  @json["todos"]["count"]
41
41
  end
42
+
43
+ def photos(options={:group => "checkin"})
44
+ @foursquare.get("venues/#{id}/photos", options)["photos"]["items"].map do |item|
45
+ Foursquare::Photo.new(@foursquare, item)
46
+ end
47
+ end
42
48
  end
43
49
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: john-mayer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 4
10
- version: 0.1.4
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pat Nakajima
@@ -59,14 +59,16 @@ files:
59
59
  - lib/foursquare.rb
60
60
  - lib/foursquare/base.rb
61
61
  - lib/foursquare/settings.rb
62
+ - lib/foursquare/tip.rb
63
+ - lib/foursquare/multi.rb
62
64
  - lib/foursquare/checkin.rb
65
+ - lib/foursquare/photo.rb
63
66
  - lib/foursquare/checkin_proxy.rb
64
67
  - lib/foursquare/user.rb
65
68
  - lib/foursquare/user_proxy.rb
66
69
  - lib/foursquare/venue.rb
67
70
  - lib/foursquare/venue_proxy.rb
68
71
  - spec/THERE_ARENT_ANY
69
- - test.rb
70
72
  has_rdoc: true
71
73
  homepage: https://github.com/groupme/john-mayer
72
74
  licenses: []
data/test.rb DELETED
@@ -1,4 +0,0 @@
1
- require 'lib/foursquare'
2
-
3
- FOURSQUARE = Foursquare::Base.new('YSQQ2G4HOATU5515IFH4ODCKUQYYKZ2M4DVBNPI1ZCXKL3QS')
4
- # FOURSQUARE = Foursquare::Base.new('T2UPWWBXJP2UFBRTBXKDM1UQAH3A3TUDELQ3YQRFAWGLKTTT')