Class: Foursquare::Base
- Inherits:
-
Object
- Object
- Foursquare::Base
- Defined in:
- lib/foursquare.rb
Constant Summary collapse
- BASE_URL =
'http://api.foursquare.com/v1'
- FORMAT =
'json'
Instance Attribute Summary collapse
-
#oauth ⇒ Object
Returns the value of attribute oauth.
Instance Method Summary collapse
- #add_tip(params = {}) ⇒ Object
- #add_venue(params = {}) ⇒ Object
-
#api_url(method_name, options = nil) ⇒ Object
Foursquare API: groups.google.com/group/foursquare-api/web/api-documentation.
- #categories ⇒ Object
- #checkin(params = {}) ⇒ Object
-
#checkins(params = {}) ⇒ Object
Check in methods.
- #friend_approve(params = {}) ⇒ Object
- #friend_deny(params = {}) ⇒ Object
-
#friend_requests ⇒ Object
Friend methods.
- #friend_send_request(params = {}) ⇒ Object
- #friends(params = {}) ⇒ Object
- #friends_by_name(params = {}) ⇒ Object
- #friends_by_phone(params = {}) ⇒ Object
- #friends_by_twitter(params = {}) ⇒ Object
- #get(url, params) ⇒ Object
- #history(params = {}) ⇒ Object
-
#initialize(oauth) ⇒ Base
constructor
A new instance of Base.
- #parse_response(response) ⇒ Object
- #post(url, body) ⇒ Object
- #settings_set_pings(params = {}) ⇒ Object
- #test(params = {}) ⇒ Object
- #tip_mark_done(params = {}) ⇒ Object
- #tip_mark_todo(params = {}) ⇒ Object
-
#tips(params = {}) ⇒ Object
Tip methods.
- #to_query_params(options) ⇒ Object
-
#user(params = {}) ⇒ Object
User methods.
- #venue(params = P) ⇒ Object
- #venue_flag_closed(params = {}) ⇒ Object
- #venue_propose_edit(params = {}) ⇒ Object
-
#venues(params = {}) ⇒ Object
Venue methods.
Constructor Details
#initialize(oauth) ⇒ Base
Returns a new instance of Base.
61 62 63 |
# File 'lib/foursquare.rb', line 61 def initialize(oauth) @oauth = oauth end |
Instance Attribute Details
#oauth ⇒ Object
Returns the value of attribute oauth.
59 60 61 |
# File 'lib/foursquare.rb', line 59 def oauth @oauth end |
Instance Method Details
#add_tip(params = {}) ⇒ Object
185 186 187 |
# File 'lib/foursquare.rb', line 185 def add_tip(params = {}) post('/addtip', params).tip end |
#add_venue(params = {}) ⇒ Object
167 168 169 |
# File 'lib/foursquare.rb', line 167 def add_venue(params = {}) post('/addvenue', params).venue end |
#api_url(method_name, options = nil) ⇒ Object
Foursquare API: groups.google.com/group/foursquare-api/web/api-documentation
.test # api test method
=> {'response': 'ok'}
.checkin = => ‘At home. Writing code’ # post new check in
=> {...checkin hash...}
.history # authenticated user’s checkin history
> [hashie…, checkin hashie…]
.send(‘venue.flagclosed=’, => 12345) # flag venue 12345 as closed
> ‘ok’
.venue_flagclosed = => 12345
> ‘ok’
Assignment methods(POSTs) always return a hash. Annoyingly Ruby always returns what’s on the right side of the assignment operator. So there are some wrapper methods below for POSTs that make sure it gets turned into a hashie
def method_missing(method_symbol, params = {})
method_name = method_symbol.to_s.split(/\.|_/).join('/')
if (method_name[-1,1]) == '='
method = method_name[0..-2]
result = post(api_url(method), params)
params.replace(result[method] || result)
else
result = get(api_url(method_name, params))
result[method_name] || result
end
end
def api(method_symbol, params = {})
Hashie::Mash.new(method_missing(method_symbol, params))
end
100 101 102 103 104 105 106 107 |
# File 'lib/foursquare.rb', line 100 def api_url(method_name, = nil) params = .is_a?(Hash) ? to_query_params() : params = nil if params and params.blank? url = BASE_URL + '/' + method_name.split('.').join('/') url += ".#{FORMAT}" url += "?#{params}" if params url end |
#categories ⇒ Object
163 164 165 |
# File 'lib/foursquare.rb', line 163 def categories get('/categories', params).categories end |
#checkin(params = {}) ⇒ Object
135 136 137 |
# File 'lib/foursquare.rb', line 135 def checkin(params = {}) post('/checkin', params).checkin end |
#checkins(params = {}) ⇒ Object
Check in methods
131 132 133 |
# File 'lib/foursquare.rb', line 131 def checkins(params = {}) get('/checkins', params).checkins end |
#friend_approve(params = {}) ⇒ Object
203 204 205 |
# File 'lib/foursquare.rb', line 203 def friend_approve(params = {}) post('/friend/approve', params).user end |
#friend_deny(params = {}) ⇒ Object
207 208 209 |
# File 'lib/foursquare.rb', line 207 def friend_deny(params = {}) post('/friend/deny', params).user end |
#friend_requests ⇒ Object
Friend methods
199 200 201 |
# File 'lib/foursquare.rb', line 199 def friend_requests get('/friend/requests').requests end |
#friend_send_request(params = {}) ⇒ Object
211 212 213 |
# File 'lib/foursquare.rb', line 211 def friend_send_request(params = {}) post('/friend/sendrequest', params).user end |
#friends(params = {}) ⇒ Object
149 150 151 |
# File 'lib/foursquare.rb', line 149 def friends(params = {}) get('/friends', params).friends end |
#friends_by_name(params = {}) ⇒ Object
215 216 217 |
# File 'lib/foursquare.rb', line 215 def friends_by_name(params = {}) post('/findfriends/byname', params).users end |
#friends_by_phone(params = {}) ⇒ Object
219 220 221 |
# File 'lib/foursquare.rb', line 219 def friends_by_phone(params = {}) get('/findfriends/byphone', params).users end |
#friends_by_twitter(params = {}) ⇒ Object
223 224 225 |
# File 'lib/foursquare.rb', line 223 def friends_by_twitter(params = {}) get('/findfriends/bytwitter', params).users end |
#get(url, params) ⇒ Object
119 120 121 |
# File 'lib/foursquare.rb', line 119 def get(url, params) parse_response(@oauth.access_token.get(api_url(url, params))) end |
#history(params = {}) ⇒ Object
139 140 141 |
# File 'lib/foursquare.rb', line 139 def history(params = {}) get('/history', params).checkins end |
#parse_response(response) ⇒ Object
109 110 111 112 113 |
# File 'lib/foursquare.rb', line 109 def parse_response(response) raise_errors(response) Hashie::Mash.new(Yajl::Parser.parse(response.body)) end |
#post(url, body) ⇒ Object
123 124 125 |
# File 'lib/foursquare.rb', line 123 def post(url, body) parse_response(@oauth.access_token.post(api_url(url), body)) end |
#settings_set_pings(params = {}) ⇒ Object
227 228 229 |
# File 'lib/foursquare.rb', line 227 def settings_set_pings(params = {}) post('/settings/setpings', params).settings end |
#test(params = {}) ⇒ Object
231 232 233 |
# File 'lib/foursquare.rb', line 231 def test(params = {}) get('/test', params) end |
#tip_mark_done(params = {}) ⇒ Object
193 194 195 |
# File 'lib/foursquare.rb', line 193 def tip_mark_done(params = {}) post('/tip/markdone', params).tip end |
#tip_mark_todo(params = {}) ⇒ Object
189 190 191 |
# File 'lib/foursquare.rb', line 189 def tip_mark_todo(params = {}) post('/tip/marktodo', params).tip end |
#tips(params = {}) ⇒ Object
Tip methods
181 182 183 |
# File 'lib/foursquare.rb', line 181 def tips(params = {}) get('/tips', params).tips end |
#to_query_params(options) ⇒ Object
115 116 117 |
# File 'lib/foursquare.rb', line 115 def to_query_params() .map{|key, value| "#{key}=#{value}"}.join('&') end |
#user(params = {}) ⇒ Object
User methods
145 146 147 |
# File 'lib/foursquare.rb', line 145 def user(params = {}) get('/user', params).user end |
#venue(params = P) ⇒ Object
159 160 161 |
# File 'lib/foursquare.rb', line 159 def venue(params = P) get('/venue', params).venue end |
#venue_flag_closed(params = {}) ⇒ Object
175 176 177 |
# File 'lib/foursquare.rb', line 175 def venue_flag_closed(params = {}) post('/venue/flagclosed', params) end |
#venue_propose_edit(params = {}) ⇒ Object
171 172 173 |
# File 'lib/foursquare.rb', line 171 def venue_propose_edit(params = {}) post('/venue/proposeedit', params) end |
#venues(params = {}) ⇒ Object
Venue methods
155 156 157 |
# File 'lib/foursquare.rb', line 155 def venues(params = {}) get('/venues', params).venues end |