Class: Foursquare::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/foursquare.rb

Constant Summary collapse

BASE_URL =
'http://api.foursquare.com/v1'
FORMAT =
'json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth) ⇒ Base

Returns a new instance of Base.



65
66
67
# File 'lib/foursquare.rb', line 65

def initialize(oauth)
  @oauth = oauth
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, params = {}) ⇒ 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



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/foursquare.rb', line 87

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

Instance Attribute Details

#oauthObject

Returns the value of attribute oauth.



63
64
65
# File 'lib/foursquare.rb', line 63

def oauth
  @oauth
end

Instance Method Details

#addtip(params = {}) ⇒ Object



153
154
155
# File 'lib/foursquare.rb', line 153

def addtip(params = {})
  api(:addtip=, params).tip
end

#addvenue(params = {}) ⇒ Object



141
142
143
# File 'lib/foursquare.rb', line 141

def addvenue(params = {})
  api(:addvenue=, params).venue
end

#api(method_symbol, params = {}) ⇒ Object



100
101
102
# File 'lib/foursquare.rb', line 100

def api(method_symbol, params = {})
  Hashie::Mash.new(method_missing(method_symbol, params))
end

#api_url(method_name, options = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/foursquare.rb', line 104

def api_url(method_name, options = nil)
  params = options.is_a?(Hash) ? to_query_params(options) : options
  params = nil if params and params.blank?
  url = BASE_URL + '/' + method_name.split('.').join('/')
  url += ".#{FORMAT}"
  url += "?#{params}" if params
  url = URI.escape(url)
  url
end

#checkin(params = {}) ⇒ Object

API method wrappers



133
134
135
# File 'lib/foursquare.rb', line 133

def checkin(params = {})
  api(:checkin=, params).checkin
end

#findfriends_byname(params = {}) ⇒ Object



181
182
183
# File 'lib/foursquare.rb', line 181

def findfriends_byname(params = {})
  api(:findfriends_byname, params).users
end

#findfriends_byphone(params = {}) ⇒ Object



185
186
187
# File 'lib/foursquare.rb', line 185

def findfriends_byphone(params = {})
  api(:findfriends_byphone, params).users
end

#findfriends_bytwitter(params = {}) ⇒ Object



189
190
191
# File 'lib/foursquare.rb', line 189

def findfriends_bytwitter(params = {})
  api(:findfriends_bytwitter, params).users
end

#friend_approve(params = {}) ⇒ Object



169
170
171
# File 'lib/foursquare.rb', line 169

def friend_approve(params = {})
  api(:friend_approve=, params).user
end

#friend_deny(params = {}) ⇒ Object



173
174
175
# File 'lib/foursquare.rb', line 173

def friend_deny(params = {})
  api(:friend_deny=, params).user
end

#friend_requestsObject



165
166
167
# File 'lib/foursquare.rb', line 165

def friend_requests
  api(:friend_requests).requests
end

#friend_sendrequest(params = {}) ⇒ Object



177
178
179
# File 'lib/foursquare.rb', line 177

def friend_sendrequest(params = {})
  api(:friend_sendrequest=, params).user
end

#get(url) ⇒ Object



123
124
125
# File 'lib/foursquare.rb', line 123

def get(url)
  parse_response(@oauth.access_token.get(url))
end

#history(params = {}) ⇒ Object



137
138
139
# File 'lib/foursquare.rb', line 137

def history(params = {})
  api(:history, params).checkins
end

#parse_response(response) ⇒ Object



114
115
116
117
# File 'lib/foursquare.rb', line 114

def parse_response(response)
  raise_errors(response)
  Crack::JSON.parse(response.body)
end

#post(url, body) ⇒ Object



127
128
129
# File 'lib/foursquare.rb', line 127

def post(url, body)
  parse_response(@oauth.access_token.post(url, body))
end

#settings_setpings(params = {}) ⇒ Object



193
194
195
# File 'lib/foursquare.rb', line 193

def settings_setpings(params = {})
  api(:settings_setpings=, params).settings
end

#tip_markdone(params = {}) ⇒ Object



161
162
163
# File 'lib/foursquare.rb', line 161

def tip_markdone(params = {})
  api(:tip_markdone=, params).tip
end

#tip_marktodo(params = {}) ⇒ Object



157
158
159
# File 'lib/foursquare.rb', line 157

def tip_marktodo(params = {})
  api(:tip_marktodo=, params).tip
end

#to_query_params(options) ⇒ Object



119
120
121
# File 'lib/foursquare.rb', line 119

def to_query_params(options)
  options.collect { |key, value| "#{key}=#{value}" }.join('&')
end

#venue_flagclosed(params = {}) ⇒ Object



149
150
151
# File 'lib/foursquare.rb', line 149

def venue_flagclosed(params = {})
  api(:venue_flagclosed=, params)
end

#venue_proposeedit(params = {}) ⇒ Object



145
146
147
# File 'lib/foursquare.rb', line 145

def venue_proposeedit(params = {})
  api(:venue_proposeedit=, params)
end