event_inventory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ require 'active_support/core_ext'
2
+ require 'patron'
3
+ require 'sax-machine'
4
+
5
+ module EventInventory
6
+ ROOT = 'http://services.eventinventory.com/webservices/'.freeze
7
+ CATALOG = File.join(ROOT, 'ticketsearch.asmx').freeze
8
+ EDIT_ORDER = File.join(ROOT, 'editorder.asmx').freeze
9
+ VIEW_ORDER = File.join(ROOT, 'vieworder.asmx').freeze
10
+
11
+ OLD_OPERATIONS = %w[
12
+ ListEvents_Active
13
+ GetEventList
14
+ ListVenues_Active
15
+ GetVenueList
16
+ ListBrokers_Active
17
+ ListShipping_Active
18
+ GetTicketInfo
19
+ CheckTicketAvailability
20
+ GetVenueMapURL
21
+ ListProductions_Active
22
+ ListTickets
23
+ ]
24
+
25
+ Error = Class.new(StandardError)
26
+ AuthorizationError = Class.new(Error)
27
+ ParameterError = Class.new(Error)
28
+ NetworkError = Class.new(Error)
29
+
30
+ mattr_accessor :security_token
31
+ mattr_accessor :proxy
32
+
33
+ autoload :Base, 'event_inventory/base'
34
+ autoload :Catalog, 'event_inventory/catalog'
35
+ autoload :FormatHelper, 'event_inventory/format_helper'
36
+ autoload :ParseHelper, 'event_inventory/parse_helper'
37
+ end
@@ -0,0 +1,150 @@
1
+ module EventInventory
2
+ class Base
3
+ class Action
4
+ include EventInventory::FormatHelper
5
+
6
+ attr_reader :name, :operation, :method, :service, :parser, :parameters
7
+
8
+ def initialize(operation, options={}, &block)
9
+ @name = options[:as] || operation
10
+ @operation = format_key(operation || options[:as])
11
+ @method = options[:method]
12
+ @service = options[:service]
13
+ @parser = options[:parser]
14
+ @parameters = {}
15
+
16
+ instance_eval(&block) if block_given?
17
+ end
18
+
19
+ # Adds a parameter that the web service accepts.
20
+ def parameter(name, options={})
21
+ @parameters[name] = options
22
+ end
23
+
24
+ def execute(*args)
25
+ parameters = args.first || {}
26
+ Request.new(self, parameters).perform
27
+ end
28
+ end
29
+
30
+ class Request
31
+ include EventInventory::FormatHelper
32
+
33
+ def initialize(action, parameters)
34
+ @action = action
35
+ @parameters = HashWithIndifferentAccess.new(parameters)
36
+ end
37
+
38
+ # Executes the call to the web service.
39
+ def perform
40
+ uri = URI.parse(url)
41
+
42
+ session = Patron::Session.new
43
+ session.timeout = 60
44
+ session.base_url = "#{uri.scheme}://#{uri.host}:#{uri.port}/"
45
+ session.proxy = EventInventory.proxy if EventInventory.proxy
46
+
47
+ retries = 5
48
+
49
+ begin
50
+ response = case @action.method
51
+ when :get
52
+ session.get([uri.path, uri.query].compact.join('?'))
53
+ when :post
54
+ session.post(uri.path, uri.query)
55
+ end
56
+ rescue Patron::PartialFileError, Patron::ConnectionFailed, Patron::HostResolutionError, Patron::TimeoutError
57
+ if (retries -= 1) > 0
58
+ retry
59
+ else
60
+ raise NetworkError.new($!.to_s)
61
+ end
62
+ end
63
+
64
+ case body = response.body
65
+ when /Security Token: (\w+) \(APPCLIENT_ID\) is not registered/
66
+ raise AuthorizationError, "Invalid security token (#{$1})"
67
+ when /IP Address: ([\d\.]+) is not enabled/
68
+ raise AuthorizationError, "Unauthorized IP (#{$1})"
69
+ when /^Missing parameter: (.*)\./
70
+ raise ParameterError, "Missing #{$1}"
71
+ when /There was an error parsing (\w+) parameter \[(\w+)=(.*?)\]/
72
+ raise ParameterError, "Invalid #{$2} (#{$1}, \"#{$3}\")"
73
+ else
74
+ @action.parser.parse(body) if body
75
+ end
76
+ end
77
+
78
+ def endpoint(service)
79
+ case service
80
+ when :catalog then CATALOG
81
+ else raise Error.new("Unknown endpoint for service (#{service})")
82
+ end
83
+ end
84
+ private :endpoint
85
+
86
+ # Builds the base URL from the endpoint and operation. If the method is
87
+ # GET, the query values are appended.
88
+ def url
89
+ base_url = "#{endpoint(@action.service)}/#{@action.operation}"
90
+ base_url << "?#{query(@action.operation)}" if @action.method == :get
91
+ base_url
92
+ end
93
+ private :url
94
+
95
+ # Builds the query string from the specified parameter values.
96
+ def query(operation)
97
+ old_format = OLD_OPERATIONS.include?(operation)
98
+
99
+ @action.parameters.collect do |key, options|
100
+ value = @parameters[options[:as] || key]
101
+ value ||= options[:default] if options.include?(:default)
102
+
103
+ "#{format_key(key, old_format)}=#{format_query_value(value)}"
104
+ end.tap do |parameters|
105
+ parameters << unless old_format
106
+ "SecurityToken=#{format_query_value(EventInventory.security_token)}"
107
+ else
108
+ "APPCLIENT_ID=#{format_query_value(EventInventory.security_token)}"
109
+ end
110
+ end.join('&')
111
+ end
112
+ private :query
113
+ end
114
+
115
+ class << self
116
+ # Sets or returns the service.
117
+ def service(service=nil)
118
+ unless service
119
+ read_inheritable_attribute(:service)
120
+ else
121
+ write_inheritable_attribute(:service, service)
122
+ end
123
+ end
124
+
125
+ # Creates a new GET operation.
126
+ def get(operation, options={}, &block)
127
+ action(operation, options.merge(:method => :get), &block)
128
+ end
129
+
130
+ # Creates a new POST operation.
131
+ def post(operation, options={}, &block)
132
+ action(operation, options.merge(:method => :post), &block)
133
+ end
134
+
135
+ # Creates a new operation.
136
+ def action(operation, options={}, &block)
137
+ options[:service] ||= service
138
+ options[:parser] ||= self::Parser
139
+ action = Action.new(operation, options, &block)
140
+
141
+ self.singleton_class.class_eval do
142
+ define_method(action.name) do |*args|
143
+ action.execute(*args)
144
+ end
145
+ end
146
+ end
147
+ private :action
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,12 @@
1
+ module EventInventory
2
+ module Catalog
3
+ autoload :Base, 'event_inventory/catalog/base'
4
+ autoload :Category, 'event_inventory/catalog/category'
5
+ autoload :Configuration, 'event_inventory/catalog/configuration'
6
+ autoload :Event, 'event_inventory/catalog/event'
7
+ autoload :Performance, 'event_inventory/catalog/performance'
8
+ autoload :Performer, 'event_inventory/catalog/performer'
9
+ autoload :Ticket, 'event_inventory/catalog/ticket'
10
+ autoload :Venue, 'event_inventory/catalog/venue'
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module EventInventory::Catalog
2
+ class Base < EventInventory::Base
3
+ service :catalog
4
+ end
5
+
6
+ ParseHelper = EventInventory::ParseHelper
7
+ end
@@ -0,0 +1,25 @@
1
+ module EventInventory::Catalog
2
+ class Category < Base
3
+ class Parser
4
+ include ParseHelper
5
+
6
+ class Category
7
+ include ParseHelper
8
+
9
+ element :row, :value => :category_id, :as => :id
10
+ element :row, :value => :category_name, :as => :name
11
+ element :row, :value => :parent_category_id, :as => :parent_id
12
+ end
13
+
14
+ elements :row, :as => :categories, :class => Category
15
+ end
16
+
17
+ get :get_all_categories, :as => :fetch
18
+
19
+ class << self
20
+ def all(parameters={})
21
+ fetch(parameters).categories
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ module EventInventory::Catalog
2
+ class Configuration < Base
3
+ class Parser
4
+ include ParseHelper
5
+
6
+ element :row, :value => :venuemap, :force => true, :as => :map_url
7
+ end
8
+
9
+ get :get_venue_map_url, :as => :fetch do
10
+ parameter :event_id, :as => :performer_id
11
+ parameter :venue_id
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ module EventInventory::Catalog
2
+ class Event < Base
3
+ class Parser
4
+ include ParseHelper
5
+
6
+ class Event
7
+ include ParseHelper
8
+
9
+ element :row, :value => :production_id, :as => :id
10
+ element :row, :value => :event_date, :as => :occurs_at
11
+ element :row, :value => :event_id, :as => :performer_id
12
+ element :row, :value => :opponent_event_id, :as => :opponent_id
13
+ element :row, :value => :venue_id, :as => :venue_id
14
+ element :row, :value => :short_note, :as => :note
15
+ element :row, :value => :is_long_note_available, :as => :has_note
16
+ element :row, :value => :min_cost, :as => :minimum_price
17
+ element :row, :value => :max_cost, :as => :maximum_price
18
+ element :row, :value => :content_standardization_type_id, :as => :content_standardization_type_id
19
+ end
20
+
21
+ elements :row, :as => :events, :class => Event
22
+ end
23
+
24
+ get :get_all_productions, :as => :fetch
25
+
26
+ class << self
27
+ def all(parameters={})
28
+ fetch(parameters).events
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ module EventInventory::Catalog
2
+ class Performer < Base
3
+ class Parser
4
+ include ParseHelper
5
+
6
+ class Performer
7
+ include ParseHelper
8
+
9
+ element :row, :value => :event_id, :as => :id
10
+ element :row, :value => :event_name, :as => :name
11
+ element :row, :value => :event_type_id, :as => :event_type_id
12
+ element :row, :value => :category_id, :as => :category_id
13
+ end
14
+
15
+ elements :row, :as => :performers, :class => Performer
16
+ end
17
+
18
+ get :get_all_events, :as => :fetch
19
+
20
+ class << self
21
+ def all(parameters={})
22
+ fetch(parameters).performers
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ module EventInventory::Catalog
2
+ class Venue < Base
3
+ class Parser
4
+ include ParseHelper
5
+
6
+ class Venue
7
+ include ParseHelper
8
+
9
+ element :row, :value => :venue_id, :as => :id
10
+ element :row, :value => :venue_name, :as => :name
11
+ element :row, :value => :address1, :as => :street_address
12
+ element :row, :value => :address2, :as => :extended_address
13
+ element :row, :value => :city, :as => :locality
14
+ element :row, :value => :region_code, :as => :region
15
+ element :row, :value => :postal_code, :as => :postal_code
16
+ element :row, :value => :country_code, :as => :country_code
17
+ element :row, :value => :phone, :as => :phone
18
+ element :row, :value => :market_area_id, :as => :market_area_id
19
+ end
20
+
21
+ elements :row, :as => :venues, :class => Venue
22
+ end
23
+
24
+ get :get_all_venues, :as => :fetch
25
+
26
+ class << self
27
+ def all(parameters={})
28
+ fetch(parameters).venues
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,22 @@
1
+ module EventInventory
2
+ module FormatHelper
3
+ # Formats keys for use in parsing responses and building query strings.
4
+ def format_key(key, old_format=false)
5
+ key = key.to_s
6
+ key.upcase! if key == 'id'
7
+ key.gsub!(/_id$/, '_ID')
8
+ key.gsub!(/_url$/, '_URL')
9
+ old_format ? key.upcase : key.camelize
10
+ end
11
+
12
+ # Formats and escapes query values for use in a query string.
13
+ def format_query_value(value)
14
+ value = case value
15
+ when Time, Date then value.to_s(:db)
16
+ else value.to_s
17
+ end
18
+
19
+ CGI.escape(value)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ module EventInventory
2
+ module ParseHelper
3
+ def self.included(base)
4
+ base.class_eval do
5
+ include SAXMachine
6
+ end
7
+
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ include FormatHelper
13
+
14
+ def element(name, options={})
15
+ if options[:value] && !options[:force]
16
+ options[:value] = format_key(options[:value])
17
+ end
18
+
19
+ super(name, options)
20
+ end
21
+
22
+ def elements(name, options={})
23
+ if options[:value] && !options[:force]
24
+ options[:value] = format_key(options[:value])
25
+ end
26
+
27
+ super(name, options)
28
+ end
29
+ end
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_inventory
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Tyler Hunt
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-24 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ - beta4
33
+ version: 3.0.0.beta4
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: patron
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - "="
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 0
46
+ - 4
47
+ - 6
48
+ version: 0.4.6
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: sax-machine
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - "="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ - 0
62
+ - 15
63
+ version: 0.0.15
64
+ type: :runtime
65
+ version_requirements: *id003
66
+ description:
67
+ email:
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - lib/event_inventory/base.rb
76
+ - lib/event_inventory/catalog/base.rb
77
+ - lib/event_inventory/catalog/category.rb
78
+ - lib/event_inventory/catalog/configuration.rb
79
+ - lib/event_inventory/catalog/event.rb
80
+ - lib/event_inventory/catalog/performer.rb
81
+ - lib/event_inventory/catalog/venue.rb
82
+ - lib/event_inventory/catalog.rb
83
+ - lib/event_inventory/format_helper.rb
84
+ - lib/event_inventory/parse_helper.rb
85
+ - lib/event_inventory.rb
86
+ has_rdoc: true
87
+ homepage: http://github.com/tylerhunt/event_inventory
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.7
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: An interface library for the Event Inventory web service.
118
+ test_files: []
119
+