Skip to content
This repository was archived by the owner on Dec 16, 2024. It is now read-only.

Commit 633ca84

Browse files
author
Lawson Kurtz
committed
Add API version param to all API requests
1 parent 7b50c4b commit 633ca84

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

lib/foursquare/base.rb

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ module Foursquare
22
class Base
33
API = "https://api.foursquare.com/v2/"
44

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)
1212
raise ArgumentError, "You need to pass either an access_token or client_id and client_secret"
1313
end
1414
end
@@ -34,6 +34,7 @@ def get(path, params={})
3434
Foursquare.log("GET #{API + path}")
3535
Foursquare.log("PARAMS: #{params.inspect}")
3636
merge_auth_params(params)
37+
merge_version_params(params)
3738
response = JSON.parse(Typhoeus::Request.get(API + path, :params => params).body)
3839
Foursquare.log(response.inspect)
3940
error(response) || response["response"]
@@ -44,56 +45,57 @@ def post(path, params={})
4445
Foursquare.log("POST #{API + path}")
4546
Foursquare.log("PARAMS: #{params.inspect}")
4647
merge_auth_params(params)
48+
merge_version_params(params)
4749
response = JSON.parse(Typhoeus::Request.post(API + path, :params => params).body)
4850
Foursquare.log(response.inspect)
4951
error(response) || response["response"]
5052
end
51-
53+
5254
def authorize_url(redirect_uri)
5355
# http://developer.foursquare.com/docs/oauth.html
54-
56+
5557
# check params
5658
raise "you need to define a client id before" if @client_id.blank?
5759
raise "no callback url provided" if redirect_uri.blank?
58-
60+
5961
# params
6062
params = {}
6163
params["client_id"] = @client_id
6264
params["response_type"] = "code"
6365
params["redirect_uri"] = redirect_uri
64-
66+
6567
# url
6668
oauth2_url('authenticate', params)
6769
end
68-
70+
6971
def access_token(code, redirect_uri)
7072
# http://developer.foursquare.com/docs/oauth.html
71-
73+
7274
# check params
7375
raise "you need to define a client id before" if @client_id.blank?
7476
raise "you need to define a client secret before" if @client_secret.blank?
7577
raise "no code provided" if code.blank?
7678
raise "no redirect_uri provided" if redirect_uri.blank?
77-
79+
7880
# params
7981
params = {}
8082
params["client_id"] = @client_id
8183
params["client_secret"] = @client_secret
8284
params["grant_type"] = "authorization_code"
8385
params["redirect_uri"] = redirect_uri
8486
params["code"] = code
85-
87+
8688
# url
8789
url = oauth2_url('access_token', params)
88-
90+
8991
# response
9092
# http://developer.foursquare.com/docs/oauth.html
9193
response = JSON.parse(Typhoeus::Request.get(url).body)
9294
response["access_token"]
9395
end
9496

9597
private
96-
98+
9799
def oauth2_url(method_name, params)
98100
"https://foursquare.com/oauth2/#{method_name}?#{params.to_query}"
99101
end
@@ -132,5 +134,9 @@ def merge_auth_params(params)
132134
params.merge!(:client_id => @client_id, :client_secret => @client_secret)
133135
end
134136
end
137+
138+
def merge_version_params(params)
139+
params.merge!(:v => @api_version)
140+
end
135141
end
136142
end

0 commit comments

Comments
 (0)