quimby 0.3.0 → 0.4.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/README.md +26 -0
- data/lib/foursquare/base.rb +47 -0
- data/lib/foursquare/photo.rb +25 -0
- data/lib/foursquare/user.rb +21 -0
- data/lib/foursquare/venue.rb +34 -4
- metadata +3 -3
data/README.md
CHANGED
@@ -13,6 +13,28 @@ Get a foursquare:
|
|
13
13
|
You can also user `client_id` and `client_secret`
|
14
14
|
|
15
15
|
foursquare = Foursquare::Base.new("CLIENT_ID", "CLIENT_SECRET")
|
16
|
+
|
17
|
+
### Authentication
|
18
|
+
|
19
|
+
First, you need to [register your application](https://foursquare.com/oauth).
|
20
|
+
|
21
|
+
#### Web server application
|
22
|
+
|
23
|
+
Get a foursquare with your `client_id` and `client_secret`
|
24
|
+
|
25
|
+
foursquare = Foursquare::Base.new("CLIENT_ID", "CLIENT_SECRET")
|
26
|
+
|
27
|
+
Redirect users to the Foursquare authentication page. You need to pass your `callback_url`. Get the url to redirect to with:
|
28
|
+
|
29
|
+
foursquare.authorize_url("CALLBACK_SESSION_URL")
|
30
|
+
|
31
|
+
Then Foursquare will redirect the user to your callback url with a code parameter in the url. Exchange this code for an access token using:
|
32
|
+
|
33
|
+
access_token = foursquare.access_token(params["code"], "CALLBACK_SESSION_URL")
|
34
|
+
|
35
|
+
Now you can get a foursquare using only an access token and make requests on user's behalf:
|
36
|
+
|
37
|
+
foursquare = Foursquare::Base.new("ACCESS_TOKEN")
|
16
38
|
|
17
39
|
### Users
|
18
40
|
|
@@ -42,6 +64,10 @@ But wait, Foursquare isn't just users! It's checkins too! So let's find some che
|
|
42
64
|
Now we have an array of `Foursquare::Checkin` objects. We can also grab a specific checkin:
|
43
65
|
|
44
66
|
checkin = foursquare.checkins.find("CHECKIN_ID")
|
67
|
+
|
68
|
+
You also have a convenient method if you want to get all the user checkins:
|
69
|
+
|
70
|
+
user.all_checkins
|
45
71
|
|
46
72
|
### Venues
|
47
73
|
|
data/lib/foursquare/base.rb
CHANGED
@@ -48,8 +48,55 @@ module Foursquare
|
|
48
48
|
Foursquare.log(response.inspect)
|
49
49
|
error(response) || response["response"]
|
50
50
|
end
|
51
|
+
|
52
|
+
def authorize_url(redirect_uri)
|
53
|
+
# http://developer.foursquare.com/docs/oauth.html
|
54
|
+
|
55
|
+
# check params
|
56
|
+
raise "you need to define a client id before" if @client_id.blank?
|
57
|
+
raise "no callback url provided" if redirect_uri.blank?
|
58
|
+
|
59
|
+
# params
|
60
|
+
params = {}
|
61
|
+
params["client_id"] = @client_id
|
62
|
+
params["response_type"] = "code"
|
63
|
+
params["redirect_uri"] = redirect_uri
|
64
|
+
|
65
|
+
# url
|
66
|
+
oauth2_url('authenticate', params)
|
67
|
+
end
|
68
|
+
|
69
|
+
def access_token(code, redirect_uri)
|
70
|
+
# http://developer.foursquare.com/docs/oauth.html
|
71
|
+
|
72
|
+
# check params
|
73
|
+
raise "you need to define a client id before" if @client_id.blank?
|
74
|
+
raise "you need to define a client secret before" if @client_secret.blank?
|
75
|
+
raise "no code provided" if code.blank?
|
76
|
+
raise "no redirect_uri provided" if redirect_uri.blank?
|
77
|
+
|
78
|
+
# params
|
79
|
+
params = {}
|
80
|
+
params["client_id"] = @client_id
|
81
|
+
params["client_secret"] = @client_secret
|
82
|
+
params["grant_type"] = "authorization_code"
|
83
|
+
params["redirect_uri"] = redirect_uri
|
84
|
+
params["code"] = code
|
85
|
+
|
86
|
+
# url
|
87
|
+
url = oauth2_url('access_token', params)
|
88
|
+
|
89
|
+
# response
|
90
|
+
# http://developer.foursquare.com/docs/oauth.html
|
91
|
+
response = JSON.parse(Typhoeus::Request.get(url).body)
|
92
|
+
response["access_token"]
|
93
|
+
end
|
51
94
|
|
52
95
|
private
|
96
|
+
|
97
|
+
def oauth2_url(method_name, params)
|
98
|
+
"https://foursquare.com/oauth2/#{method_name}?#{params.to_query}"
|
99
|
+
end
|
53
100
|
|
54
101
|
def camelize(params)
|
55
102
|
params.inject({}) { |o, (k, v)|
|
data/lib/foursquare/photo.rb
CHANGED
@@ -23,5 +23,30 @@ module Foursquare
|
|
23
23
|
def sizes
|
24
24
|
@json["sizes"]
|
25
25
|
end
|
26
|
+
|
27
|
+
def square_300
|
28
|
+
@square_300 ||= @json["sizes"]["items"].select { |i| i["width"] == 300 }.first
|
29
|
+
end
|
30
|
+
|
31
|
+
def square_300_url
|
32
|
+
square_300["url"]
|
33
|
+
end
|
34
|
+
|
35
|
+
def square_100
|
36
|
+
@square_100 ||= @json["sizes"]["items"].select { |i| i["width"] == 100 }.first
|
37
|
+
end
|
38
|
+
|
39
|
+
def square_100_url
|
40
|
+
square_100["url"]
|
41
|
+
end
|
42
|
+
|
43
|
+
def square_36
|
44
|
+
@square_36 ||= @json["sizes"]["items"].select { |i| i["width"] == 36 }.first
|
45
|
+
end
|
46
|
+
|
47
|
+
def square_36_url
|
48
|
+
square_36["url"]
|
49
|
+
end
|
50
|
+
|
26
51
|
end
|
27
52
|
end
|
data/lib/foursquare/user.rb
CHANGED
@@ -74,6 +74,27 @@ module Foursquare
|
|
74
74
|
fetch unless @json.has_key?("mayorships")
|
75
75
|
@json["mayorships"]["items"]
|
76
76
|
end
|
77
|
+
|
78
|
+
# https://developer.foursquare.com/docs/users/checkins.html
|
79
|
+
# https://developer.foursquare.com/docs/explore.html#req=users/self/checkins
|
80
|
+
def checkins(options={})
|
81
|
+
@foursquare.get("users/#{id}/checkins", options)["checkins"]["items"].map do |item|
|
82
|
+
Foursquare::Checkin.new(@foursquare, item)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def all_checkins
|
87
|
+
count = 250
|
88
|
+
offset = 0
|
89
|
+
array = []
|
90
|
+
while count == 250
|
91
|
+
checkins = checkins(:limit => count, :offset => offset)
|
92
|
+
array += checkins
|
93
|
+
count = checkins.count
|
94
|
+
offset = offset + count
|
95
|
+
end
|
96
|
+
array
|
97
|
+
end
|
77
98
|
|
78
99
|
def checkin_count
|
79
100
|
fetch unless @json.has_key?("checkins")
|
data/lib/foursquare/venue.rb
CHANGED
@@ -19,11 +19,11 @@ module Foursquare
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def location
|
22
|
-
@json["location"]
|
22
|
+
Foursquare::Location.new(@json["location"])
|
23
23
|
end
|
24
24
|
|
25
25
|
def categories
|
26
|
-
@json["categories"]
|
26
|
+
@categories ||= @json["categories"].map { |hash| Foursquare::Category.new(hash) }
|
27
27
|
end
|
28
28
|
|
29
29
|
def verified?
|
@@ -41,11 +41,41 @@ module Foursquare
|
|
41
41
|
def todos_count
|
42
42
|
@json["todos"]["count"]
|
43
43
|
end
|
44
|
-
|
45
|
-
def
|
44
|
+
|
45
|
+
def stats
|
46
|
+
@json["stats"]
|
47
|
+
end
|
48
|
+
|
49
|
+
def primary_category
|
50
|
+
return nil if categories.blank?
|
51
|
+
@primary_category ||= categories.select { |category| category.primary? }.try(:first)
|
52
|
+
end
|
53
|
+
|
54
|
+
# return the url to the icon of the primary category
|
55
|
+
# if no primary is available, then return a default icon
|
56
|
+
def icon
|
57
|
+
primary_category ? primary_category["icon"] : "https://foursquare.com/img/categories/none.png"
|
58
|
+
end
|
59
|
+
|
60
|
+
def photos_count
|
61
|
+
@json["photos"]["count"]
|
62
|
+
end
|
63
|
+
|
64
|
+
# not all photos may be present here (but we try to avoid one extra API call)
|
65
|
+
# if you want to get all the photos, try all_photos
|
66
|
+
def photos
|
67
|
+
return all_photos if @json["photos"].blank?
|
68
|
+
@json["photos"]["groups"].select { |g| g["type"] == "venue" }.first["items"].map do |item|
|
69
|
+
Foursquare::Photo.new(@foursquare, item)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# https://developer.foursquare.com/docs/venues/photos.html
|
74
|
+
def all_photos(options={:group => "venue"})
|
46
75
|
@foursquare.get("venues/#{id}/photos", options)["photos"]["items"].map do |item|
|
47
76
|
Foursquare::Photo.new(@foursquare, item)
|
48
77
|
end
|
49
78
|
end
|
79
|
+
|
50
80
|
end
|
51
81
|
end
|
metadata
CHANGED