foursquare_venues 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in foursquare_venues.gemspec
4
+ gemspec
data/README ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "foursquare_venues/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "foursquare_venues"
7
+ s.version = FoursquareVenues::VERSION
8
+ s.authors = ["Tres Trantham"]
9
+ s.email = ["tres@trestrantham.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{A Foursquare Venues API Wrapper}
12
+ s.description = %q{A Foursquare Venues API Wrapper}
13
+
14
+ s.rubyforge_project = "foursquare_venues"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.licenses = ["MIT"]
21
+ end
@@ -0,0 +1,57 @@
1
+ module FoursquareVenues
2
+ class Base
3
+ API = "https://api.foursquare.com/v2/venues/"
4
+
5
+ def initialize(*args)
6
+ case args.size
7
+ when 2
8
+ @client_id, @client_secret = args
9
+ else
10
+ raise ArgumentError, "You need to pass a client_id and client_secret"
11
+ end
12
+ end
13
+
14
+ def venues
15
+ FoursquareVenues::VenueProxy.new(self)
16
+ end
17
+
18
+ def get(path, params={})
19
+ params = camelize(params)
20
+ FoursquareVenues.log("GET #{API + path}")
21
+ FoursquareVenues.log("PARAMS: #{params.inspect}")
22
+ params.merge!(:client_id => @client_id, :client_secret => @client_secret)
23
+ response = JSON.parse(Typhoeus::Request.get(API + path, :params => params).body)
24
+ FoursquareVenues.log(response.inspect)
25
+ error(response) || response["response"]
26
+ end
27
+
28
+ private
29
+
30
+ def camelize(params)
31
+ params.inject({}) { |o, (k, v)|
32
+ o[k.to_s.gsub(/(_[a-z])/) { |m| m[1..1].upcase }] = v
33
+ o
34
+ }
35
+ end
36
+
37
+ def error(response)
38
+ case response["meta"]["errorType"]
39
+ when nil
40
+ # It's all good.
41
+ when "deprecated"
42
+ FoursquareVenues.log(FoursquareVenues::ERRORS[response['meta']['errorType']])
43
+ nil
44
+ else
45
+ error_type = response['meta']['errorType']
46
+ case error_type
47
+ when "invalid_auth"
48
+ raise FoursquareVenues::InvalidAuth.new(FoursquareVenues::ERRORS[error_type])
49
+ when "server_error"
50
+ raise FoursquareVenues::ServiceUnavailable.new(FoursquareVenues::ERRORS[error_type])
51
+ else
52
+ raise FoursquareVenues::Error.new(FoursquareVenues::ERRORS[error_type])
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,30 @@
1
+ module FoursquareVenues
2
+ class Category
3
+
4
+ def initialize(json)
5
+ @json = json
6
+ end
7
+
8
+ def name
9
+ @json["name"]
10
+ end
11
+
12
+ def plural_name
13
+ @json["pluralName"]
14
+ end
15
+
16
+ def icon
17
+ @json["icon"]
18
+ end
19
+
20
+ # array
21
+ def parents
22
+ @json["parents"]
23
+ end
24
+
25
+ def primary?
26
+ @json["primary"] == true
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,47 @@
1
+ # https://developer.foursquare.com/docs/responses/venue.html
2
+
3
+ module FoursquareVenues
4
+ class Location
5
+
6
+ def initialize(json)
7
+ @json = json
8
+ end
9
+
10
+ def address
11
+ @json["address"]
12
+ end
13
+
14
+ def cross_street
15
+ @json["crossStreet"]
16
+ end
17
+
18
+ def city
19
+ @json["city"]
20
+ end
21
+
22
+ def state
23
+ @json["state"]
24
+ end
25
+
26
+ def postal_code
27
+ @json["postalCode"]
28
+ end
29
+
30
+ def country
31
+ @json["country"]
32
+ end
33
+
34
+ def lat
35
+ @json["lat"]
36
+ end
37
+
38
+ def lng
39
+ @json["lng"]
40
+ end
41
+
42
+ def distance
43
+ @json["distance"]
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,52 @@
1
+ module FoursquareVenues
2
+ class Photo
3
+ def initialize(foursquare, json)
4
+ @foursquare, @json = foursquare, json
5
+ end
6
+
7
+ def id
8
+ @json["id"]
9
+ end
10
+
11
+ def name
12
+ @json["name"]
13
+ end
14
+
15
+ def created_at
16
+ @json["createdAt"]
17
+ end
18
+
19
+ def url
20
+ @json["url"]
21
+ end
22
+
23
+ def sizes
24
+ @json["sizes"]
25
+ end
26
+
27
+ def square_300
28
+ @square_300 ||= @json["sizes"]["items"].select { |i| i["width"] == 300 }.first
29
+ end
30
+
31
+ def square_300_url
32
+ square_300["url"]
33
+ end
34
+
35
+ def square_100
36
+ @square_100 ||= @json["sizes"]["items"].select { |i| i["width"] == 100 }.first
37
+ end
38
+
39
+ def square_100_url
40
+ square_100["url"]
41
+ end
42
+
43
+ def square_36
44
+ @square_36 ||= @json["sizes"]["items"].select { |i| i["width"] == 36 }.first
45
+ end
46
+
47
+ def square_36_url
48
+ square_36["url"]
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,102 @@
1
+ module FoursquareVenues
2
+ class Venue
3
+ attr_reader :json
4
+
5
+ def initialize(foursquare, json)
6
+ @foursquare, @json = foursquare, json
7
+ end
8
+
9
+ def fetch
10
+ @json = @foursquare.get("#{id}")["venue"]
11
+ self
12
+ end
13
+
14
+ def id
15
+ @json["id"]
16
+ end
17
+
18
+ def name
19
+ @json["name"]
20
+ end
21
+
22
+ def contact
23
+ @json["contact"]
24
+ end
25
+
26
+ def location
27
+ FoursquareVenues::Location.new(@json["location"])
28
+ end
29
+
30
+ def categories
31
+ @categories ||= @json["categories"].map { |hash| FoursquareVenues::Category.new(hash) }
32
+ end
33
+
34
+ def verified?
35
+ @json["verified"]
36
+ end
37
+
38
+ def checkins_count
39
+ @json["stats"]["checkinsCount"]
40
+ end
41
+
42
+ def users_count
43
+ @json["stats"]["usersCount"]
44
+ end
45
+
46
+ def todos_count
47
+ @json["todos"]["count"]
48
+ end
49
+
50
+ def stats
51
+ @json["stats"]
52
+ end
53
+
54
+ def primary_category
55
+ return nil if categories.blank?
56
+ @primary_category ||= categories.select { |category| category.primary? }.try(:first)
57
+ end
58
+
59
+ # return the url to the icon of the primary category
60
+ # if no primary is available, then return a default icon
61
+ # optionally accepts a size to return 64px or 256px icon
62
+ def icon(size = '')
63
+ icon_url = primary_category ? primary_category.icon : "https://foursquare.com/img/categories/none.png"
64
+ # return "https://foursquare.com/img/categories/none.png"
65
+ if ['64','256'].include?(size)
66
+ icon_url = icon_url.split('.png').first + '_' + size + '.png'
67
+ end
68
+ icon_url
69
+ end
70
+
71
+ def short_url
72
+ @json["shortUrl"]
73
+ end
74
+
75
+ def photos_count
76
+ @json["photos"]["count"]
77
+ end
78
+
79
+ # not all photos may be present here (but we try to avoid one extra API call)
80
+ # if you want to get all the photos, try all_photos
81
+ def photos
82
+ return all_photos if @json["photos"].blank?
83
+ @json["photos"]["groups"].select { |g| g["type"] == "venue" }.first["items"].map do |item|
84
+ FoursquareVenues::Photo.new(@foursquare, item)
85
+ end
86
+ end
87
+
88
+ # https://developer.foursquare.com/docs/venues/photos.html
89
+ def all_photos(options={:group => "venue"})
90
+ @foursquare.get("#{id}/photos", options)["photos"]["items"].map do |item|
91
+ FoursquareVenues::Photo.new(@foursquare, item)
92
+ end
93
+ end
94
+
95
+ # count the people who have checked-in at the venue in the last two hours
96
+ def here_now_count
97
+ fetch unless @json.has_key?("hereNow")
98
+ @json["hereNow"]["count"]
99
+ end
100
+
101
+ end
102
+ end
@@ -0,0 +1,50 @@
1
+ module FoursquareVenues
2
+ class Venues
3
+ def initialize(*args)
4
+ case args.size
5
+ when 2
6
+ @client_id, @client_secret = args
7
+ @foursquare = FoursquareVenues::Base.new(@client_id, @client_secret)
8
+ else
9
+ raise ArgumentError, "You need to pass a client_id and client_secret"
10
+ end
11
+ end
12
+
13
+ def find(id)
14
+ FoursquareVenues::Venue.new(@foursquare, @foursquare.get("#{id}")["venue"])
15
+ end
16
+
17
+ def search(options={})
18
+ raise ArgumentError, "You must include :ll" unless options[:ll]
19
+ response = @foursquare.get('search', options)["groups"].inject({}) do |venues, group|
20
+ venues[group["type"]] ||= []
21
+ venues[group["type"]] += group["items"].map do |json|
22
+ FoursquareVenues::Venue.new(@foursquare, json)
23
+ end
24
+ venues
25
+ end
26
+ end
27
+
28
+ def trending(options={})
29
+ search_group("trending", options)
30
+ end
31
+
32
+ def favorites(options={})
33
+ search_group("favorites", options)
34
+ end
35
+
36
+ def nearby(options={})
37
+ search_group("nearby", options)
38
+ end
39
+
40
+ private
41
+
42
+ def search_group(name, options)
43
+ raise ArgumentError, "You must include :ll" unless options[:ll]
44
+ response = @foursquare.get('search', options)["groups"].detect { |group| group["type"] == name }
45
+ response ? response["items"].map do |json|
46
+ FoursquareVenues::Venue.new(@foursquare, json)
47
+ end : []
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module FoursquareVenues
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,39 @@
1
+ require "typhoeus"
2
+ require "json"
3
+ require "foursquare_venues/version"
4
+ require "foursquare_venues/base"
5
+ require "foursquare_venues/venues" # proxy for Venue
6
+ require "foursquare_venues/venue"
7
+ require "foursquare_venues/location"
8
+ require "foursquare_venues/category"
9
+ require "foursquare_venues/photo"
10
+
11
+ module FoursquareVenues
12
+ class Error < StandardError ; end
13
+ class InvalidAuth < FoursquareVenues::Error; end
14
+ class ServiceUnavailable < FoursquareVenues::Error; end
15
+
16
+ def self.verbose=(setting)
17
+ @verbose = setting
18
+ end
19
+
20
+ def self.verbose?
21
+ @verbose
22
+ end
23
+
24
+ def self.log(msg)
25
+ return unless verbose?
26
+ puts "[foursquare_venues] #{msg}"
27
+ end
28
+
29
+ ERRORS = {
30
+ "invalid_auth" => "OAuth token was not provided or was invalid.",
31
+ "param_error" => "A required parameter was missing or a parameter was malformed. This is also used if the resource ID in the path is incorrect.",
32
+ "endpoint_error" => "The requested path does not exist.",
33
+ "not_authorized" => "Although authentication succeeded, the acting user is not allowed to see this information due to privacy restrictions.",
34
+ "rate_limit_exceeded" => "Rate limit for this hour exceeded.",
35
+ "deprecated" => "Something about this request is using deprecated functionality, or the response format may be about to change.",
36
+ "server_error" => "Server is currently experiencing issues. Check status.foursquare.com for udpates.",
37
+ "other" => "Some other type of error occurred."
38
+ }
39
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foursquare_venues
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Tres Trantham
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-08 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: A Foursquare Venues API Wrapper
17
+ email:
18
+ - tres@trestrantham.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - .gitignore
27
+ - Gemfile
28
+ - README
29
+ - Rakefile
30
+ - foursquare_venues.gemspec
31
+ - lib/foursquare_venues.rb
32
+ - lib/foursquare_venues/base.rb
33
+ - lib/foursquare_venues/category.rb
34
+ - lib/foursquare_venues/location.rb
35
+ - lib/foursquare_venues/photo.rb
36
+ - lib/foursquare_venues/venue.rb
37
+ - lib/foursquare_venues/venues.rb
38
+ - lib/foursquare_venues/version.rb
39
+ homepage: ""
40
+ licenses:
41
+ - MIT
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project: foursquare_venues
62
+ rubygems_version: 1.8.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: A Foursquare Venues API Wrapper
66
+ test_files: []
67
+