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

Adding Checkin#comments and Checkin#overlaps #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ It's a Foursquare API wrapper. It uses objects instead of hashes, and tries to b
Install it as a gem (in your `Gemfile`) and its dependencies:

gem "json"
gem "typhoeus"
gem "httparty"
gem "quimby"

## Usage
@@ -114,4 +114,4 @@ if you want to use your own logger, and you're kind of a jerk like me, you can d

* Creating checkins works, but it should really return notifications. Also, if the
checkin can't be created, it should return errors.
* I don't know, so much other stuff.
* I don't know, so much other stuff.
5 changes: 3 additions & 2 deletions lib/foursquare.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$LOAD_PATH << File.dirname(__FILE__)

require "rubygems"
require "typhoeus"
require "httparty"
require "json"
require "cgi"
require "foursquare/base"
@@ -16,6 +16,7 @@
require "foursquare/photo"
require "foursquare/location"
require "foursquare/category"
require "foursquare/checkin_comment"

module Foursquare
class Error < StandardError ; end
@@ -42,7 +43,7 @@ def self.log(msg)
"not_authorized" => "Although authentication succeeded, the acting user is not allowed to see this information due to privacy restrictions.",
"rate_limit_exceeded" => "Rate limit for this hour exceeded.",
"deprecated" => "Something about this request is using deprecated functionality, or the response format may be about to change.",
"server_error" => "Server is currently experiencing issues. Check status.foursquare.com for udpates.",
"server_error" => "Server is currently experiencing issues. Check status.foursquare.com for updates.",
"other" => "Some other type of error occurred."
}
end
26 changes: 13 additions & 13 deletions lib/foursquare/base.rb
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ def get(path, params={})
Foursquare.log("GET #{API + path}")
Foursquare.log("PARAMS: #{params.inspect}")
merge_auth_params(params)
response = JSON.parse(Typhoeus::Request.get(API + path, :params => params).body)
response = JSON.parse(HTTParty.get(API + path, :query => params).body)
Foursquare.log(response.inspect)
error(response) || response["response"]
end
@@ -44,56 +44,56 @@ def post(path, params={})
Foursquare.log("POST #{API + path}")
Foursquare.log("PARAMS: #{params.inspect}")
merge_auth_params(params)
response = JSON.parse(Typhoeus::Request.post(API + path, :params => params).body)
response = JSON.parse(HTTParty.post(API + path, :query => params).body)
Foursquare.log(response.inspect)
error(response) || response["response"]
end

def authorize_url(redirect_uri)
# http://developer.foursquare.com/docs/oauth.html

# check params
raise "you need to define a client id before" if @client_id.blank?
raise "no callback url provided" if redirect_uri.blank?

# params
params = {}
params["client_id"] = @client_id
params["response_type"] = "code"
params["redirect_uri"] = redirect_uri

# url
oauth2_url('authenticate', params)
end

def access_token(code, redirect_uri)
# http://developer.foursquare.com/docs/oauth.html

# check params
raise "you need to define a client id before" if @client_id.blank?
raise "you need to define a client secret before" if @client_secret.blank?
raise "no code provided" if code.blank?
raise "no redirect_uri provided" if redirect_uri.blank?

# params
params = {}
params["client_id"] = @client_id
params["client_secret"] = @client_secret
params["grant_type"] = "authorization_code"
params["redirect_uri"] = redirect_uri
params["code"] = code

# url
url = oauth2_url('access_token', params)

# response
# http://developer.foursquare.com/docs/oauth.html
response = JSON.parse(Typhoeus::Request.get(url).body)
response = JSON.parse(HTTParty.get(url).body)
response["access_token"]
end

private

def oauth2_url(method_name, params)
"https://foursquare.com/oauth2/#{method_name}?#{params.to_query}"
end
14 changes: 14 additions & 0 deletions lib/foursquare/checkin.rb
Original file line number Diff line number Diff line change
@@ -35,6 +35,20 @@ def mayor?
@json["isMayor"]
end

def comments
fetch if @json["comments"].nil? || (@json["comments"]["count"] > 0 && @json["comments"]["items"].length == 0)
@json["comments"]["items"].map { |comment| Foursquare::CheckinComment.new(@foursquare, comment) }
end

def overlaps
fetch unless @json["overlaps"]
if @json["overlaps"] && @json["overlaps"]["items"]
@json["overlaps"]["items"].map { |checkin| Foursquare::Checkin.new(@foursquare, checkin) }
else
[]
end
end

def timezone
@json["timeZone"]
end
29 changes: 29 additions & 0 deletions lib/foursquare/checkin_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Foursquare
class CheckinComment
attr_reader :json

def initialize(foursquare, json)
@foursquare, @json = foursquare, json
end

def id
@json["id"]
end

def created_at
Time.at(@json["createdAt"].to_i)
end

def text
@json["text"]
end

def user(full=false)
if full
@foursquare.users.find(@json["user"]["id"])
else
Foursquare::User.new(@foursquare, @json["user"])
end
end
end
end
6 changes: 3 additions & 3 deletions quimby.gemspec
Original file line number Diff line number Diff line change
@@ -37,14 +37,14 @@ Gem::Specification.new do |s|
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<typhoeus>, [">= 0"])
s.add_runtime_dependency(%q<httparty>, [">= 0"])
s.add_runtime_dependency(%q<json>, [">= 0"])
else
s.add_dependency(%q<typhoeus>, [">= 0"])
s.add_dependency(%q<httparty>, [">= 0"])
s.add_dependency(%q<json>, [">= 0"])
end
else
s.add_dependency(%q<typhoeus>, [">= 0"])
s.add_dependency(%q<httparty>, [">= 0"])
s.add_dependency(%q<json>, [">= 0"])
end
end