zvent 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +42 -0
- data/lib/core/ext.rb +18 -0
- data/lib/zvent.rb +11 -0
- data/lib/zvent/base.rb +36 -0
- data/lib/zvent/category.rb +14 -0
- data/lib/zvent/event.rb +121 -0
- data/lib/zvent/session.rb +115 -0
- data/lib/zvent/venue.rb +24 -0
- metadata +77 -0
data/README
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
= Zvent 0.0.3
|
2
|
+
|
3
|
+
An interface with zvents API without all the mess.
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
An interface with zvents API without all the mess.
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
give it your API key when you initialize the session object.
|
12
|
+
@zvent = Zvent::Sesion.new('API_KEY')
|
13
|
+
|
14
|
+
To get an event you give it an event id.
|
15
|
+
This call returns an event object.
|
16
|
+
event = @zvent.find_event('12345')
|
17
|
+
|
18
|
+
The event can get images of all different types and sizes
|
19
|
+
event.image('tiny') # returns an image (44x44)
|
20
|
+
|
21
|
+
The event can have categories of sorts
|
22
|
+
event.categories # returns the category object
|
23
|
+
|
24
|
+
The event has support for timezones. Please read the rdoc for more examples
|
25
|
+
event.startTime # Returns the UTC of the start time
|
26
|
+
event.endTime(false) # Returns the end time of the event in local time to the venue.
|
27
|
+
# If the venue is in US/Pacific the time of the event will come back
|
28
|
+
# in US/Pacific.
|
29
|
+
|
30
|
+
Each event has a venue object
|
31
|
+
venue = event.venue # returns the venue object
|
32
|
+
|
33
|
+
To search for events give it a location
|
34
|
+
events = @zvent.find_events('Simi Valley, CA')
|
35
|
+
events[:event_count] # how many events total are in this search
|
36
|
+
events[:events] # access to the array of events
|
37
|
+
|
38
|
+
== TODO
|
39
|
+
|
40
|
+
- Get a list of categories
|
41
|
+
- Implement tzinfo timezones of events
|
42
|
+
- Have it not blow up if event can not be found with find_event call
|
data/lib/core/ext.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# backported from rails
|
2
|
+
class Hash
|
3
|
+
def to_query(namespace = nil)
|
4
|
+
collect do |key, value|
|
5
|
+
value.to_query(namespace ? "#{namespace}[#{key}]" : key)
|
6
|
+
end.sort * '&'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Object
|
11
|
+
def to_query(key)
|
12
|
+
"#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_param
|
16
|
+
to_s
|
17
|
+
end
|
18
|
+
end
|
data/lib/zvent.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'json'
|
7
|
+
require 'tzinfo'
|
8
|
+
require 'cgi'
|
9
|
+
require 'net/http'
|
10
|
+
|
11
|
+
Dir[File.dirname(__FILE__) + "/zvent/*.rb"].each { |file| require(file) }
|
data/lib/zvent/base.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Zvent
|
2
|
+
BASE_URL = "http://www.zvents.com/rest"
|
3
|
+
|
4
|
+
# raised when no location is given when it is required
|
5
|
+
class NoLocationError < StandardError; end
|
6
|
+
|
7
|
+
# raised when no id proveded when it is required
|
8
|
+
class NoIdError < StandardError; end
|
9
|
+
|
10
|
+
# raise when not given an API key
|
11
|
+
class NoApiKeyError < StandardError; end
|
12
|
+
|
13
|
+
# raise when the API doesn't like the request
|
14
|
+
class ZventApiError < StandardError; end
|
15
|
+
|
16
|
+
class InvalidImageSize < StandardError; end
|
17
|
+
|
18
|
+
class Base
|
19
|
+
|
20
|
+
# Get Json and parse it
|
21
|
+
def get_resources(url)
|
22
|
+
url = URI.parse(url)
|
23
|
+
|
24
|
+
res = Net::HTTP.start(url.host, url.port) {|http|
|
25
|
+
http.get(url.request_uri+"&format=json&key=#{@api_key}")
|
26
|
+
}
|
27
|
+
resources = JSON.parse(res.body)
|
28
|
+
|
29
|
+
if resources['rsp']['status'] == 'error'
|
30
|
+
raise Zvent::ZventApiError.new(resources['rsp']['msg'])
|
31
|
+
end
|
32
|
+
|
33
|
+
return resources
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Zvent
|
2
|
+
# An object that represents a single venue from zvents
|
3
|
+
class Category < Base
|
4
|
+
attr_accessor :name, :pid, :id, :count
|
5
|
+
|
6
|
+
def initialize(category_hash)
|
7
|
+
begin
|
8
|
+
category_hash.each_pair{|key, value| self.send("#{key}=", value) }
|
9
|
+
rescue NoMethodError => e
|
10
|
+
# Do nothing!
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/zvent/event.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
module Zvent
|
2
|
+
# An object that represents a single event from zvents
|
3
|
+
class Event < Base
|
4
|
+
IMAGE_SIZES = ['tiny', 'medium', 'featured', 'primary', 'original']
|
5
|
+
|
6
|
+
attr_accessor :name, :artists, :price, :private, :editors_pick, :url, :approved,
|
7
|
+
:sc, :id, :images, :description, :vid, :color, :phone, :startTime,
|
8
|
+
:endTime, :zurl, :venue, :categories
|
9
|
+
|
10
|
+
# expects the json hash that is given from zvents
|
11
|
+
def initialize(event_hash)
|
12
|
+
begin
|
13
|
+
event_hash.each_pair{|key, value| self.send("#{key}=", value) }
|
14
|
+
rescue NoMethodError => e
|
15
|
+
# Do nothing!
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Get the start time of the event. Events are guaranteed an start time.
|
20
|
+
# The function will return a DateTime object of the time.
|
21
|
+
#
|
22
|
+
# <b>options</b>
|
23
|
+
# <tt>utc</tt> - Want the time in local (to the venue) or in UTC? Defaults to UTC
|
24
|
+
def startTime(utc=true)
|
25
|
+
# if there is no startTime return nil
|
26
|
+
return nil if @startTime.nil?
|
27
|
+
|
28
|
+
# Parse the startTime
|
29
|
+
start_time = DateTime.parse(@startTime)
|
30
|
+
|
31
|
+
# Decide if we need to return UTC or local time
|
32
|
+
utc ? DateTime.parse(self.tz_timezone.local_to_utc(start_time).to_s) : start_time
|
33
|
+
end
|
34
|
+
|
35
|
+
# Get the end time of the event. Events are not guaranteed an end time.
|
36
|
+
# The function will return a DateTime object of the time. If there isn't an endtime it will return nil.
|
37
|
+
#
|
38
|
+
# <b>options</b>
|
39
|
+
# <tt>utc</tt> - Want the time in local (to the venue) or in UTC? Defaults to UTC
|
40
|
+
def endTime(utc=true)
|
41
|
+
# if there is no startTime return nil
|
42
|
+
return nil if @endTime.nil?
|
43
|
+
|
44
|
+
# Parse the startTime
|
45
|
+
end_time = DateTime.parse(@endTime)
|
46
|
+
|
47
|
+
# Decide if we need to return UTC or local time
|
48
|
+
utc ? DateTime.parse(self.tz_timezone.local_to_utc(end_time).to_s) : end_time
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns the tz timezone object from the venue
|
52
|
+
def tz_timezone ; @venue.tz_timezone; end
|
53
|
+
|
54
|
+
# Does the event have any images
|
55
|
+
def images? ; !(@images.nil? || @images.empty?); end
|
56
|
+
|
57
|
+
# Does the event or venue have any images
|
58
|
+
def deep_images?
|
59
|
+
self.images? || (@venue.nil? ? false : @venue.images?)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Categories takes in some json
|
63
|
+
def categories=(categories_json)
|
64
|
+
categories_json.each do |category|
|
65
|
+
if @categories.nil?
|
66
|
+
@categories = [Zvent::Category.new(category)]
|
67
|
+
else
|
68
|
+
@categories << Zvent::Category.new(category)
|
69
|
+
end
|
70
|
+
end if categories_json
|
71
|
+
end
|
72
|
+
|
73
|
+
def category? ; !(@categories.nil? || @categories.empty?) ; end
|
74
|
+
|
75
|
+
# Returns the first image it sees from event. If none is found it will return nil
|
76
|
+
# <b>size</b>
|
77
|
+
# * <tt>tiny</tt> - 44x44
|
78
|
+
# * <tt>medium</tt> - 66x66
|
79
|
+
# * <tt>featured</tt> - 150x150
|
80
|
+
# * <tt>primary</tt> - 184x184
|
81
|
+
# * <tt>original</tt> Will just grab the original image from zvents (default)
|
82
|
+
def image(size='original')
|
83
|
+
self.images? ? convert_image(@images.first, size) : nil
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns the first image it sees. First it checks the event for images thent the venue for images.
|
87
|
+
# If none is found it will return nil
|
88
|
+
#
|
89
|
+
# <b>size</b>
|
90
|
+
# * <tt>tiny</tt> - 44x44
|
91
|
+
# * <tt>medium</tt> - 66x66
|
92
|
+
# * <tt>featured</tt> - 150x150
|
93
|
+
# * <tt>primary</tt> - 184x184
|
94
|
+
# * <tt>original</tt> Will just grab the original image from zvents (default)
|
95
|
+
def deep_image(size='original')
|
96
|
+
image = nil
|
97
|
+
if self.images?
|
98
|
+
image = @images.first
|
99
|
+
elsif self.venue
|
100
|
+
image = @venue.images? ? @venue.images.first : nil
|
101
|
+
else
|
102
|
+
image = nil
|
103
|
+
end
|
104
|
+
|
105
|
+
(image.nil?) ? image : convert_image(image, size)
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
# grab the size of the image requested
|
110
|
+
def convert_image(image, size)
|
111
|
+
# Apparently venue returns a different kind of image than event.
|
112
|
+
image_url = image.class == Hash ? image['url'] : image
|
113
|
+
|
114
|
+
# if the size is original just return the image
|
115
|
+
return image_url if size == 'original'
|
116
|
+
|
117
|
+
# else grab the specific size
|
118
|
+
IMAGE_SIZES.include?(size) ? image_url.insert(image_url.index('.jpg'), "_#{size}") : (raise Zvent::InvalidImageSize.new)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module Zvent
|
2
|
+
# A zvent session used to search and everything
|
3
|
+
class Session < Base
|
4
|
+
# Initializes the session object. It requires an API key
|
5
|
+
def initialize(api_key)
|
6
|
+
raise Zvent::NoApiKeyError.new if !api_key || api_key.strip.empty?
|
7
|
+
@api_key = api_key
|
8
|
+
end
|
9
|
+
|
10
|
+
# Use this method to find events from zvents.
|
11
|
+
#
|
12
|
+
# <b>Return</b>
|
13
|
+
# returns a hash that contains an array of events and an event count
|
14
|
+
# {:event_count => 10, :events => [<# event>, ...]}
|
15
|
+
#
|
16
|
+
# <b>Arguments</b>
|
17
|
+
#
|
18
|
+
# location
|
19
|
+
# * <tt>location</tt> - A string describing a location around which the search results will be restricted. (e.g., san francisco, ca or 94131). You can also specify a location using a longitude/latitude coordinate pair using the notation -74.0:BY:40.9
|
20
|
+
#
|
21
|
+
# zvent_options
|
22
|
+
# * <tt>what</tt> - The string against which events are matched. (e.g., parade). (<tt>default</tt> = nil, which searches for everything)
|
23
|
+
# * <tt>when</tt> - A string specifying a date range for the search (e.g., today, this week, next week, friday, etc.). Explicit date ranges can be specified by separating two dates with the word "to" (e.g., monday to thursday, 10/30/2007 to 11/4/2007). Leave this string blank to search all future events.
|
24
|
+
# * <tt>radius</tt> - The number of miles around the location (specified in the where field) to search. If this field is left blank, a default radius is supplied. The default radius varies according to the location specified in the where field.
|
25
|
+
# * <tt>limit</tt> - The maximum number of matching events to return. The default is 10 and maximum is 10. Zvents partners can exceed the maximum.(<tt>Default</tt> = 10. <tt>Max</tt> = 25)
|
26
|
+
# * <tt>offset</tt> - The number of events to skip from the beginning of the search results. If a search matches 1000 events, returning 25 events starting at offset 100 will return event numbers 100-125 that match the search. (<tt>Default</tt> = 0)
|
27
|
+
# * <tt>trim</tt> - Set to 1 if repeating events should be trimmed from the search results. Set to 0 to return repeating events. If the trim option is enabled, only 1 event will be returned from each repeating series that matches the search. The number of events within the series that match the search is returned in the series_count response parameter. Defaults to 1.
|
28
|
+
# * <tt>sort</tt> - Set to 1 to sort search results by start time. Set to 0 to sort search results by relevance. Defaults to 0.
|
29
|
+
# * <tt>cat</tt> - Restrict your search to items that belong to a specific category. You must provide a category identifier which can be determined using the categories API call.
|
30
|
+
# * <tt>catex</tt> - Exclude items from a specific category from the search. You must provide a category identifier which can be determined using the categories API call.
|
31
|
+
#
|
32
|
+
# options
|
33
|
+
# * <tt>as_json</tt> - If set to true method will return the json from zvents without any transformation. (<tt>false</tt> by default).
|
34
|
+
#
|
35
|
+
# Examples:
|
36
|
+
# find_events('93063')
|
37
|
+
# => Finds any 10 events near the 93063 zip code area.
|
38
|
+
#
|
39
|
+
# find_events('611 N. Brand Blvd. Glendale, Ca', {:what => 'dancing', :limit => 25})
|
40
|
+
# => Finds 25 events near the address that consists of dancing
|
41
|
+
#
|
42
|
+
# find_events('611 N. Brand Blvd. Glendale, Ca', {:what => 'dancing'}, {:as_json => true})
|
43
|
+
# => Should return the json straight from zvents
|
44
|
+
#
|
45
|
+
def find_events(location, zvent_options = {}, options = {})
|
46
|
+
#location is required
|
47
|
+
raise Zvent::NoLocationError.new if !location || location.strip.empty?
|
48
|
+
|
49
|
+
#grab the json from zvents
|
50
|
+
json_ret = get_resources(BASE_URL+"/search?#{zvent_options.merge(:where => location).to_query}")
|
51
|
+
|
52
|
+
#return the json or objectified json
|
53
|
+
options[:as_json] ? json_ret : objectify_zvents_json(json_ret)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Use this method to return a single event from zvents
|
57
|
+
#
|
58
|
+
# <b>Return</b>
|
59
|
+
# returns an single event. If an event can not be found it will return nil
|
60
|
+
# <# event>
|
61
|
+
#
|
62
|
+
# <b>Arguments</b>
|
63
|
+
# event_id
|
64
|
+
# * <tt>id</tt> - ID of the event
|
65
|
+
#
|
66
|
+
# options
|
67
|
+
# * <tt>as_json</tt> - If set to true method will return the json from zvents without any transformation. (<tt>false</tt> by default).
|
68
|
+
#
|
69
|
+
# Examples:
|
70
|
+
# find_event('1234')
|
71
|
+
# => Finds an event with the id of 1234
|
72
|
+
#
|
73
|
+
# find_event('1234', {:as_json => true})
|
74
|
+
# => Finds an event with the id of 1234 and returns the json as it comes in from zvents
|
75
|
+
def find_event(event_id, zvent_options={}, options = {})
|
76
|
+
# event_id is required
|
77
|
+
raise Zvent::NoIdError if event_id.strip.empty?
|
78
|
+
|
79
|
+
#grab the json from zvents
|
80
|
+
json_ret = get_resources(BASE_URL+"/event?#{zvent_options.merge(:id => event_id).to_query}")
|
81
|
+
|
82
|
+
#return the json or objectified json
|
83
|
+
options[:as_json] ? json_ret : objectify_zvents_json(json_ret)[:events].first
|
84
|
+
end
|
85
|
+
|
86
|
+
protected
|
87
|
+
def objectify_zvents_json(json)
|
88
|
+
venues = objectify_venues(json['rsp']['content']['venues'])
|
89
|
+
{:events => objectify_events(json['rsp']['content']['events'], venues),
|
90
|
+
:event_count => json['rsp']['content']['event_count']||0}
|
91
|
+
end
|
92
|
+
|
93
|
+
# returns a hash of venues
|
94
|
+
# {venue_id => <# venue >, ...}
|
95
|
+
def objectify_venues(venues)
|
96
|
+
venue_hash = {}
|
97
|
+
venues.each do |venue|
|
98
|
+
v = objectify_venue(venue)
|
99
|
+
venue_hash[v.id] = v
|
100
|
+
end if venues && !venues.empty?
|
101
|
+
venue_hash
|
102
|
+
end
|
103
|
+
|
104
|
+
# returns an array of events
|
105
|
+
def objectify_events(events, venues_hash)
|
106
|
+
(events && !events.empty?) ? events.collect{|e| objectify_event(e, venues_hash[e['vid']])} : []
|
107
|
+
end
|
108
|
+
|
109
|
+
# Turns the event json into an event object
|
110
|
+
def objectify_event(event_hash, venue) ; Event.new(event_hash.merge(:venue => venue)) ; end
|
111
|
+
|
112
|
+
# Turns the venue json into a venue object
|
113
|
+
def objectify_venue(venue) ; Venue.new(venue) ; end
|
114
|
+
end
|
115
|
+
end
|
data/lib/zvent/venue.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Zvent
|
2
|
+
# An object that represents a single venue from zvents
|
3
|
+
class Venue < Base
|
4
|
+
attr_accessor :address, :city, :state, :country, :name, :latitude,
|
5
|
+
:longitude, :zip, :private, :id, :timezone, :zurl,
|
6
|
+
:types, :images, :phone, :url, :description, :parent_id
|
7
|
+
|
8
|
+
def initialize(venue_hash)
|
9
|
+
begin
|
10
|
+
venue_hash.each_pair{|key, value| self.send("#{key}=", value) }
|
11
|
+
rescue NoMethodError => e
|
12
|
+
# Do nothing!
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Does the venue have any images
|
17
|
+
def images? ; !@images.empty? ; end
|
18
|
+
|
19
|
+
def timezone? ; !(@timezone.nil? || @timezone.empty?) ; end
|
20
|
+
|
21
|
+
# Returns the tz timezone object
|
22
|
+
def tz_timezone ; timezone? ? TZInfo::Timezone.get(@timezone) : nil ; end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zvent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Austin Fonacier
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-07-23 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.1.2
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: tzinfo
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 0.3.9
|
32
|
+
version:
|
33
|
+
description: Interface for the zvents API without all the mess.
|
34
|
+
email: afonacier@yellowpages.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- README
|
41
|
+
files:
|
42
|
+
- lib/zvent.rb
|
43
|
+
- lib/zvent/event.rb
|
44
|
+
- lib/zvent/venue.rb
|
45
|
+
- lib/zvent/base.rb
|
46
|
+
- lib/zvent/category.rb
|
47
|
+
- lib/zvent/session.rb
|
48
|
+
- lib/core/ext.rb
|
49
|
+
- README
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/austinrfnd/zvent-gem/
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.0.1
|
73
|
+
signing_key:
|
74
|
+
specification_version: 2
|
75
|
+
summary: Interface for the zvents API without all the mess.
|
76
|
+
test_files: []
|
77
|
+
|