@@ -2,13 +2,13 @@ module Foursquare
2
2
class Base
3
3
API = "https://api.foursquare.com/v2/"
4
4
5
- def initialize ( * args )
6
- case args . size
7
- when 1
8
- @access_token = args . first
9
- when 2
10
- @client_id , @client_secret = args
11
- else
5
+ def initialize ( args = { } )
6
+ @access_token = args . fetch ( :access_token , nil )
7
+ @client_id = args . fetch ( :client_id , nil )
8
+ @client_secret = args . fetch ( :client_secret , nil )
9
+ @api_version = args . fetch ( :api_version , Date . new . strftime ( '%Y%m%d' ) )
10
+
11
+ unless @access_token || ( @client_id && @client_secret )
12
12
raise ArgumentError , "You need to pass either an access_token or client_id and client_secret"
13
13
end
14
14
end
@@ -34,6 +34,7 @@ def get(path, params={})
34
34
Foursquare . log ( "GET #{ API + path } " )
35
35
Foursquare . log ( "PARAMS: #{ params . inspect } " )
36
36
merge_auth_params ( params )
37
+ merge_version_params ( params )
37
38
response = JSON . parse ( Typhoeus ::Request . get ( API + path , :params => params ) . body )
38
39
Foursquare . log ( response . inspect )
39
40
error ( response ) || response [ "response" ]
@@ -44,56 +45,57 @@ def post(path, params={})
44
45
Foursquare . log ( "POST #{ API + path } " )
45
46
Foursquare . log ( "PARAMS: #{ params . inspect } " )
46
47
merge_auth_params ( params )
48
+ merge_version_params ( params )
47
49
response = JSON . parse ( Typhoeus ::Request . post ( API + path , :params => params ) . body )
48
50
Foursquare . log ( response . inspect )
49
51
error ( response ) || response [ "response" ]
50
52
end
51
-
53
+
52
54
def authorize_url ( redirect_uri )
53
55
# http://developer.foursquare.com/docs/oauth.html
54
-
56
+
55
57
# check params
56
58
raise "you need to define a client id before" if @client_id . blank?
57
59
raise "no callback url provided" if redirect_uri . blank?
58
-
60
+
59
61
# params
60
62
params = { }
61
63
params [ "client_id" ] = @client_id
62
64
params [ "response_type" ] = "code"
63
65
params [ "redirect_uri" ] = redirect_uri
64
-
66
+
65
67
# url
66
68
oauth2_url ( 'authenticate' , params )
67
69
end
68
-
70
+
69
71
def access_token ( code , redirect_uri )
70
72
# http://developer.foursquare.com/docs/oauth.html
71
-
73
+
72
74
# check params
73
75
raise "you need to define a client id before" if @client_id . blank?
74
76
raise "you need to define a client secret before" if @client_secret . blank?
75
77
raise "no code provided" if code . blank?
76
78
raise "no redirect_uri provided" if redirect_uri . blank?
77
-
79
+
78
80
# params
79
81
params = { }
80
82
params [ "client_id" ] = @client_id
81
83
params [ "client_secret" ] = @client_secret
82
84
params [ "grant_type" ] = "authorization_code"
83
85
params [ "redirect_uri" ] = redirect_uri
84
86
params [ "code" ] = code
85
-
87
+
86
88
# url
87
89
url = oauth2_url ( 'access_token' , params )
88
-
90
+
89
91
# response
90
92
# http://developer.foursquare.com/docs/oauth.html
91
93
response = JSON . parse ( Typhoeus ::Request . get ( url ) . body )
92
94
response [ "access_token" ]
93
95
end
94
96
95
97
private
96
-
98
+
97
99
def oauth2_url ( method_name , params )
98
100
"https://foursquare.com/oauth2/#{ method_name } ?#{ params . to_query } "
99
101
end
@@ -132,5 +134,9 @@ def merge_auth_params(params)
132
134
params . merge! ( :client_id => @client_id , :client_secret => @client_secret )
133
135
end
134
136
end
137
+
138
+ def merge_version_params ( params )
139
+ params . merge! ( :v => @api_version )
140
+ end
135
141
end
136
142
end
0 commit comments