ticketinghub 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ticketing_hub.rb +3 -3
- data/lib/ticketing_hub/client.rb +66 -13
- data/lib/ticketing_hub/configuration.rb +21 -22
- data/lib/ticketing_hub/connection.rb +20 -17
- data/lib/ticketing_hub/error.rb +12 -39
- data/lib/ticketing_hub/request.rb +5 -7
- data/lib/ticketing_hub/version.rb +1 -1
- data/ticketinghub.gemspec +5 -5
- metadata +12 -35
- data/lib/ticketing_hub/authentication.rb +0 -11
- data/lib/ticketing_hub/collection.rb +0 -52
- data/lib/ticketing_hub/consumer.rb +0 -27
- data/lib/ticketing_hub/order.rb +0 -67
- data/lib/ticketing_hub/resource.rb +0 -168
- data/lib/ticketing_hub/schema.rb +0 -30
- data/lib/ticketing_hub/ticket.rb +0 -23
- data/lib/ticketing_hub/tier.rb +0 -24
- data/lib/ticketing_hub/venue.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c25d671d6f876fd869a08093f5a778d7e1f58833
|
4
|
+
data.tar.gz: 0edd4860eb9d3a8b53154cd8cd6c6517fa87fcc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f898db0ee7e0e0a096b5f6dc8c5761d3659414be64032b29de69946e1e3b73458c45ff4fbe2ddd63f8926f36b6a1d9c6120a9b67f1bbaa528cafc65c10c7bb1
|
7
|
+
data.tar.gz: b974d5e29536fac7313d1fd2c99aca2a98c4cbe06f6ae8b9748bc56fc56cbe14843dbed8780fb09d17dfbefd6d421840301fd5d491af818c5062c406d1451527
|
data/lib/ticketing_hub.rb
CHANGED
@@ -6,16 +6,16 @@ module TicketingHub
|
|
6
6
|
extend Configuration
|
7
7
|
|
8
8
|
class << self
|
9
|
-
def new
|
9
|
+
def new options={}
|
10
10
|
TicketingHub::Client.new options
|
11
11
|
end
|
12
12
|
|
13
|
-
def method_missing
|
13
|
+
def method_missing method, *args, &block
|
14
14
|
return super unless new.respond_to? method
|
15
15
|
new.send method, *args, &block
|
16
16
|
end
|
17
17
|
|
18
|
-
def respond_to?
|
18
|
+
def respond_to? method, include_private=false
|
19
19
|
new.respond_to?(method, include_private) || super(method, include_private)
|
20
20
|
end
|
21
21
|
end
|
data/lib/ticketing_hub/client.rb
CHANGED
@@ -1,27 +1,80 @@
|
|
1
|
-
require_relative 'authentication'
|
2
1
|
require_relative 'connection'
|
3
2
|
require_relative 'request'
|
4
|
-
require_relative 'resource'
|
5
|
-
|
6
|
-
require_relative 'venue'
|
7
|
-
require_relative 'order'
|
8
|
-
require_relative 'consumer'
|
9
|
-
require_relative 'tier'
|
10
|
-
require_relative 'ticket'
|
11
3
|
|
12
4
|
module TicketingHub
|
13
5
|
class Client
|
14
|
-
include TicketingHub::Authentication
|
15
6
|
include TicketingHub::Connection
|
16
7
|
include TicketingHub::Request
|
17
8
|
|
18
|
-
attr_accessor
|
9
|
+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
|
19
10
|
|
20
|
-
def initialize
|
21
|
-
options = TicketingHub.options.merge
|
11
|
+
def initialize options={}
|
12
|
+
options = TicketingHub.options.merge options
|
22
13
|
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
23
|
-
send
|
14
|
+
send "#{key}=", options[key]
|
24
15
|
end
|
25
16
|
end
|
17
|
+
|
18
|
+
def get_token code
|
19
|
+
post('token', grant_type: 'code', client_id: client_id,
|
20
|
+
client_secret: client_secret, code: code, endpoint: oauth_endpoint)['access_token']
|
21
|
+
end
|
22
|
+
|
23
|
+
def venue
|
24
|
+
get 'venue'
|
25
|
+
end
|
26
|
+
|
27
|
+
def user
|
28
|
+
get 'user'
|
29
|
+
end
|
30
|
+
|
31
|
+
def orders
|
32
|
+
get 'orders'
|
33
|
+
end
|
34
|
+
|
35
|
+
def order order_id
|
36
|
+
get "orders/#{_normalize_object_id order_id}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def tiers option_id=nil
|
40
|
+
get 'tiers', { option_id: option_id }
|
41
|
+
end
|
42
|
+
|
43
|
+
def options date=nil
|
44
|
+
date = Date.parse(date.to_s) if date
|
45
|
+
get 'options', { date: date }
|
46
|
+
end
|
47
|
+
|
48
|
+
def option option_id
|
49
|
+
get "options/#{_normalize_object_id option_id}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def consumer_fields order_id, ticket_id=nil
|
53
|
+
ticket_id.nil?? get("orders/#{_normalize_object_id order_id}/fields") :
|
54
|
+
get("tickets/#{_normalize_object_id ticket_id}/fields")
|
55
|
+
end
|
56
|
+
|
57
|
+
def create_order order_attributes
|
58
|
+
post 'orders', order_attributes
|
59
|
+
end
|
60
|
+
|
61
|
+
def cancel_order order_id
|
62
|
+
delete "orders/#{_normalize_object_id order_id}"
|
63
|
+
end
|
64
|
+
|
65
|
+
def update_order order_id, order_attributes
|
66
|
+
patch "orders/#{_normalize_object_id order_id}", order_attributes
|
67
|
+
end
|
68
|
+
|
69
|
+
def confirm_order order_id, order_attributes=nil
|
70
|
+
update_order order_id, order_attributes if order_attributes
|
71
|
+
post "orders/#{_normalize_object_id order_id}/confirm"
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def _normalize_object_id object_id
|
77
|
+
(object_id.is_a?(Hash) ? object_id['id'] : object_id).to_i
|
78
|
+
end
|
26
79
|
end
|
27
80
|
end
|
@@ -9,10 +9,10 @@ module TicketingHub
|
|
9
9
|
:faraday_config_block,
|
10
10
|
:api_version,
|
11
11
|
:api_endpoint,
|
12
|
-
:
|
13
|
-
:
|
14
|
-
:
|
15
|
-
:
|
12
|
+
:oauth_endpoint,
|
13
|
+
:client_id,
|
14
|
+
:client_secret,
|
15
|
+
:oauth_token,
|
16
16
|
:proxy,
|
17
17
|
:user_agent,
|
18
18
|
:request_host,
|
@@ -20,13 +20,14 @@ module TicketingHub
|
|
20
20
|
|
21
21
|
DEFAULT_ADAPTER = Faraday.default_adapter
|
22
22
|
DEFAULT_API_VERSION = 1
|
23
|
-
DEFAULT_API_ENDPOINT = ENV['TH_API_ENDPOINT'] || 'https://api.ticketinghub.com/
|
23
|
+
DEFAULT_API_ENDPOINT = ENV['TH_API_ENDPOINT'] || 'https://api.ticketinghub.com/v1/'
|
24
|
+
DEFAULT_OAUTH_ENDPOINT = ENV['TH_OAUTH_ENDPOINT'] || 'https://www.ticketinghub.com/oauth/'
|
24
25
|
DEFAULT_USER_AGENT = "TicketingHub Ruby Gem #{TicketingHub::VERSION}".freeze
|
25
26
|
DEFAULT_AUTO_TRAVERSAL = false
|
26
27
|
|
27
|
-
attr_accessor
|
28
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
28
29
|
|
29
|
-
def self.extended
|
30
|
+
def self.extended base
|
30
31
|
base.reset
|
31
32
|
end
|
32
33
|
|
@@ -40,28 +41,26 @@ module TicketingHub
|
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
43
|
-
def api_endpoint=
|
44
|
+
def api_endpoint= value
|
44
45
|
@api_endpoint = File.join value, ''
|
45
46
|
end
|
46
47
|
|
47
|
-
def
|
48
|
-
@web_endpoint = File.join value, ''
|
49
|
-
end
|
50
|
-
|
51
|
-
def faraday_config(&block)
|
48
|
+
def faraday_config &block
|
52
49
|
@faraday_config_block = block
|
53
50
|
end
|
54
51
|
|
55
52
|
def reset
|
56
|
-
self.adapter
|
57
|
-
self.api_version
|
58
|
-
self.api_endpoint
|
59
|
-
self.
|
60
|
-
self.
|
61
|
-
self.
|
62
|
-
self.
|
63
|
-
self.
|
64
|
-
self.
|
53
|
+
self.adapter = DEFAULT_ADAPTER
|
54
|
+
self.api_version = DEFAULT_API_VERSION
|
55
|
+
self.api_endpoint = DEFAULT_API_ENDPOINT
|
56
|
+
self.oauth_endpoint = DEFAULT_OAUTH_ENDPOINT
|
57
|
+
self.client_id = nil
|
58
|
+
self.client_secret = nil
|
59
|
+
self.oauth_token = nil
|
60
|
+
self.proxy = nil
|
61
|
+
self.request_host = nil
|
62
|
+
self.user_agent = DEFAULT_USER_AGENT
|
63
|
+
self.auto_traversal = DEFAULT_AUTO_TRAVERSAL
|
65
64
|
end
|
66
65
|
end
|
67
66
|
end
|
@@ -16,36 +16,39 @@ module TicketingHub
|
|
16
16
|
503 => TicketingHub::ServiceUnavailable
|
17
17
|
}
|
18
18
|
|
19
|
-
def on_complete
|
19
|
+
def on_complete response
|
20
20
|
key = response[:status].to_i
|
21
21
|
raise ERROR_MAP[key].new(response) if ERROR_MAP.has_key? key
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
# @private
|
26
25
|
module Connection
|
27
26
|
private
|
28
|
-
|
27
|
+
|
28
|
+
def connection options={}
|
29
|
+
token = options.delete(:access_token) || options.delete(:oauth_token) || oauth_token
|
30
|
+
|
29
31
|
options = {
|
30
|
-
authenticate:
|
32
|
+
authenticate: !token.nil?,
|
31
33
|
force_urlencoded: false,
|
32
|
-
raw:
|
33
|
-
|
34
|
+
raw: false,
|
35
|
+
accept: 'application/json',
|
36
|
+
user_agent: user_agent
|
34
37
|
}.merge(options)
|
35
38
|
|
36
39
|
options.merge! proxy: proxy unless proxy.nil?
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
41
|
+
Faraday.new(options) do |conn|
|
42
|
+
conn.request :oauth2, token unless token.nil?
|
43
|
+
conn.request options[:force_urlencoded] ? :url_encoded : :json
|
44
|
+
|
45
|
+
conn.use ErrorHandler
|
46
|
+
conn.response :follow_redirects
|
47
|
+
conn.response :mashify
|
48
|
+
conn.response :json, content_type: /\bjson$/
|
49
|
+
|
50
|
+
faraday_config_block.call conn if faraday_config_block
|
51
|
+
conn.adapter *adapter
|
49
52
|
end
|
50
53
|
end
|
51
54
|
end
|
data/lib/ticketing_hub/error.rb
CHANGED
@@ -1,70 +1,43 @@
|
|
1
1
|
module TicketingHub
|
2
|
-
# Custom error class for rescuing from all GitHub errors
|
3
2
|
class Error < StandardError
|
4
3
|
attr_accessor :response
|
5
4
|
|
6
|
-
def initialize
|
5
|
+
def initialize response=nil
|
7
6
|
@response = response
|
8
|
-
super build_error_message
|
9
7
|
end
|
10
8
|
|
11
9
|
def response_body
|
12
|
-
@
|
13
|
-
if (body = @response[:body]) && !body.empty?
|
14
|
-
if body.is_a?(String)
|
15
|
-
MultiJson.load(body, :symbolize_keys => true)
|
16
|
-
else
|
17
|
-
body
|
18
|
-
end
|
19
|
-
else
|
20
|
-
nil
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def build_error_message
|
27
|
-
return nil if @response.nil?
|
28
|
-
|
29
|
-
message = if response_body
|
30
|
-
": #{response_body[:error] || response_body[:message] || ''}"
|
31
|
-
else
|
32
|
-
''
|
33
|
-
end
|
34
|
-
errors = unless message.empty?
|
35
|
-
response_body[:errors] ? ": #{response_body[:errors].map{|e|e[:message]}.join(', ')}" : ''
|
36
|
-
end
|
37
|
-
"#{@response[:method].to_s.upcase} #{@response[:url].to_s}: #{@response[:status]}#{message}#{errors}"
|
10
|
+
@response[:body]
|
38
11
|
end
|
39
12
|
end
|
40
13
|
|
41
|
-
# Raised when
|
14
|
+
# Raised when TicketingHub returns a 400 HTTP status code
|
42
15
|
class BadRequest < Error; end
|
43
16
|
|
44
|
-
# Raised when
|
17
|
+
# Raised when TicketingHub returns a 401 HTTP status code
|
45
18
|
class Unauthorized < Error; end
|
46
19
|
|
47
|
-
# Raised when
|
20
|
+
# Raised when TicketingHub returns a 403 HTTP status code
|
48
21
|
class Forbidden < Error; end
|
49
22
|
|
50
|
-
# Raised when
|
23
|
+
# Raised when TicketingHub returns a 404 HTTP status code
|
51
24
|
class NotFound < Error; end
|
52
25
|
|
53
|
-
# Raised when
|
26
|
+
# Raised when TicketingHub returns a 406 HTTP status code
|
54
27
|
class NotAcceptable < Error; end
|
55
28
|
|
56
|
-
# Raised when
|
29
|
+
# Raised when TicketingHub returns a 422 HTTP status code
|
57
30
|
class UnprocessableEntity < Error; end
|
58
31
|
|
59
|
-
# Raised when
|
32
|
+
# Raised when TicketingHub returns a 500 HTTP status code
|
60
33
|
class InternalServerError < Error; end
|
61
34
|
|
62
|
-
# Raised when
|
35
|
+
# Raised when TicketingHub returns a 501 HTTP status code
|
63
36
|
class NotImplemented < Error; end
|
64
37
|
|
65
|
-
# Raised when
|
38
|
+
# Raised when TicketingHub returns a 502 HTTP status code
|
66
39
|
class BadGateway < Error; end
|
67
40
|
|
68
|
-
# Raised when
|
41
|
+
# Raised when TicketingHub returns a 503 HTTP status code
|
69
42
|
class ServiceUnavailable < Error; end
|
70
43
|
end
|
@@ -3,11 +3,11 @@ require 'multi_json'
|
|
3
3
|
module TicketingHub
|
4
4
|
module Request
|
5
5
|
|
6
|
-
def delete
|
6
|
+
def delete path, options={}
|
7
7
|
request(:delete, path, options).body
|
8
8
|
end
|
9
9
|
|
10
|
-
def get
|
10
|
+
def get path, options={}
|
11
11
|
response = request(:get, path, options)
|
12
12
|
body = response.body
|
13
13
|
|
@@ -21,15 +21,15 @@ module TicketingHub
|
|
21
21
|
body
|
22
22
|
end
|
23
23
|
|
24
|
-
def patch
|
24
|
+
def patch path, options={}
|
25
25
|
request(:patch, path, options).body
|
26
26
|
end
|
27
27
|
|
28
|
-
def post
|
28
|
+
def post path, options={}
|
29
29
|
request(:post, path, options).body
|
30
30
|
end
|
31
31
|
|
32
|
-
def put
|
32
|
+
def put path, options={}
|
33
33
|
request(:put, path, options).body
|
34
34
|
end
|
35
35
|
|
@@ -50,8 +50,6 @@ module TicketingHub
|
|
50
50
|
conn_options = { force_urlencoded: force_urlencoded, url: url }
|
51
51
|
|
52
52
|
connection(conn_options).send(method) do |request|
|
53
|
-
request.headers['Accept'] = options.delete(:accept) || 'application/json'
|
54
|
-
|
55
53
|
case method
|
56
54
|
when :get, :delete, :head
|
57
55
|
request.url(path, options)
|
data/ticketinghub.gemspec
CHANGED
@@ -5,12 +5,12 @@ require 'ticketing_hub/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.add_development_dependency 'bundler', '~> 1.0'
|
8
|
-
spec.add_dependency 'addressable', '~> 2.3.
|
9
|
-
spec.add_dependency 'faraday', '~> 0.8.
|
8
|
+
spec.add_dependency 'addressable', '~> 2.3.4'
|
9
|
+
spec.add_dependency 'faraday', '~> 0.8.7'
|
10
10
|
spec.add_dependency 'faraday_middleware', '~> 0.9.0'
|
11
|
-
spec.add_dependency 'multi_json', '~> 1.
|
12
|
-
spec.add_dependency '
|
13
|
-
|
11
|
+
spec.add_dependency 'multi_json', '~> 1.7.2'
|
12
|
+
spec.add_dependency 'hashie', '~> 2.0'
|
13
|
+
|
14
14
|
spec.authors = ['Oliver Morgan']
|
15
15
|
spec.description = %q{Wrapper for TicketingHub API}
|
16
16
|
spec.email = ['olly@ticketinghub.com']
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ticketinghub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oliver Morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -30,28 +30,28 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 2.3.
|
33
|
+
version: 2.3.4
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 2.3.
|
40
|
+
version: 2.3.4
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: faraday
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.8.
|
47
|
+
version: 0.8.7
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.8.
|
54
|
+
version: 0.8.7
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: faraday_middleware
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,42 +72,28 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 1.
|
75
|
+
version: 1.7.2
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 1.
|
82
|
+
version: 1.7.2
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: hashie
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ~>
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
89
|
+
version: '2.0'
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ~>
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: activemodel
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - '>='
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: 3.2.12
|
104
|
-
type: :runtime
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - '>='
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: 3.2.12
|
96
|
+
version: '2.0'
|
111
97
|
description: Wrapper for TicketingHub API
|
112
98
|
email:
|
113
99
|
- olly@ticketinghub.com
|
@@ -120,20 +106,11 @@ files:
|
|
120
106
|
- LICENSE.md
|
121
107
|
- Rakefile
|
122
108
|
- ticketinghub.gemspec
|
123
|
-
- lib/ticketing_hub/authentication.rb
|
124
109
|
- lib/ticketing_hub/client.rb
|
125
|
-
- lib/ticketing_hub/collection.rb
|
126
110
|
- lib/ticketing_hub/configuration.rb
|
127
111
|
- lib/ticketing_hub/connection.rb
|
128
|
-
- lib/ticketing_hub/consumer.rb
|
129
112
|
- lib/ticketing_hub/error.rb
|
130
|
-
- lib/ticketing_hub/order.rb
|
131
113
|
- lib/ticketing_hub/request.rb
|
132
|
-
- lib/ticketing_hub/resource.rb
|
133
|
-
- lib/ticketing_hub/schema.rb
|
134
|
-
- lib/ticketing_hub/ticket.rb
|
135
|
-
- lib/ticketing_hub/tier.rb
|
136
|
-
- lib/ticketing_hub/venue.rb
|
137
114
|
- lib/ticketing_hub/version.rb
|
138
115
|
- lib/ticketing_hub.rb
|
139
116
|
- spec/helper.rb
|
@@ -158,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
135
|
version: 1.3.6
|
159
136
|
requirements: []
|
160
137
|
rubyforge_project:
|
161
|
-
rubygems_version: 2.0.
|
138
|
+
rubygems_version: 2.0.3
|
162
139
|
signing_key:
|
163
140
|
specification_version: 4
|
164
141
|
summary: Wrapper for TicketingHub API
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'active_support'
|
2
|
-
|
3
|
-
module TicketingHub
|
4
|
-
class Collection
|
5
|
-
include Enumerable
|
6
|
-
|
7
|
-
def self.from_association parent, child_class, options={}
|
8
|
-
path = [ parent.model_name.route_key, parent.id,
|
9
|
-
child_class.model_name.route_key ].join '/'
|
10
|
-
new path, parent, child_class
|
11
|
-
end
|
12
|
-
|
13
|
-
attr_accessor :elements, :parent, :child_class
|
14
|
-
|
15
|
-
def initialize(elements, parent, child_class)
|
16
|
-
self.elements = elements
|
17
|
-
self.parent = parent
|
18
|
-
self.child_class = child_class
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_a
|
22
|
-
elements.dup
|
23
|
-
end
|
24
|
-
|
25
|
-
def elements
|
26
|
-
unless @elements.is_a? Array
|
27
|
-
@elements = parent.client.get(path, options)
|
28
|
-
.map { |attrs| child_class.new attrs, parent.client }
|
29
|
-
end
|
30
|
-
|
31
|
-
@elements
|
32
|
-
end
|
33
|
-
|
34
|
-
def method_missing(method, *args, &block)
|
35
|
-
return super unless [].respond_to? method
|
36
|
-
elements.send method, *args, &block
|
37
|
-
end
|
38
|
-
|
39
|
-
def build(attributes = {})
|
40
|
-
child_class.new.tap do |resource|
|
41
|
-
{ parent.foreign_key => parent.id,
|
42
|
-
parent.model_name.singular_route_key => parent }.merge(attributes).each do |key, value|
|
43
|
-
resource.send "#{key}=", value if resource.respond_to? "#{key}="
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def create(*args)
|
49
|
-
build.create *args
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module TicketingHub
|
2
|
-
class Consumer < Resource
|
3
|
-
schema do
|
4
|
-
integer :id
|
5
|
-
string :first_name, :last_name, :email, :country
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.from_fields fields
|
9
|
-
new.tap do |consumer|
|
10
|
-
consumer.instance_eval do
|
11
|
-
@schema ||= singleton_class.schema do
|
12
|
-
fields.each do |field|
|
13
|
-
type = Schema::KNOWN_ATTRIBUTE_TYPES.member?(field['type']) ? field['type'] : 'string'
|
14
|
-
send type, field['name']
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
fields.each do |field|
|
19
|
-
singleton_class.validates_presence_of field['name'] if field['required']
|
20
|
-
send "#{field['name']}=", field['value']
|
21
|
-
field['errors'].each { |error| errors.add field['name'], error }
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
data/lib/ticketing_hub/order.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'money'
|
2
|
-
require 'active_support'
|
3
|
-
|
4
|
-
module TicketingHub
|
5
|
-
class Order < Resource
|
6
|
-
schema do
|
7
|
-
integer :id, :venue_id, :consumer_id
|
8
|
-
float :commission, :total, default: 0
|
9
|
-
string :currency
|
10
|
-
datetime :confirmed_at
|
11
|
-
end
|
12
|
-
|
13
|
-
attr_accessor :consumer_fields
|
14
|
-
|
15
|
-
has_many :tickets
|
16
|
-
|
17
|
-
belongs_to :venue
|
18
|
-
belongs_to :consumer
|
19
|
-
|
20
|
-
def total
|
21
|
-
attributes[:total].to_money currency
|
22
|
-
end
|
23
|
-
|
24
|
-
def commission
|
25
|
-
attributes[:commission].to_money currency
|
26
|
-
end
|
27
|
-
|
28
|
-
def consumer_fields
|
29
|
-
@consumer_fields ||= client.get("venues/#{venue_id}/orders/new")['consumer_fields'].map do |field|
|
30
|
-
HashWithIndifferentAccess.new field
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def tickets=(tickets)
|
35
|
-
@tickets = tickets.map { |ticket| Ticket.new ticket, client }
|
36
|
-
end
|
37
|
-
|
38
|
-
def consumer_fields=(fields)
|
39
|
-
@consumer_fields = fields
|
40
|
-
self.consumer = Consumer.from_fields consumer_fields
|
41
|
-
end
|
42
|
-
|
43
|
-
def pdf
|
44
|
-
client.get("orders/#{id}", accept: 'application/pdf')
|
45
|
-
end
|
46
|
-
|
47
|
-
def create attributes={}
|
48
|
-
self.attributes = attributes
|
49
|
-
if tiers = attributes.delete(:tiers)
|
50
|
-
tickets = attributes[:tickets_attributes] ||= []
|
51
|
-
tiers.each do |tier_id, qty|
|
52
|
-
qty.to_i.times { tickets.push tier_id: tier_id.to_i }
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
if (consumer = attributes.delete(:consumer)).try :persisted?
|
57
|
-
(attributes[:consumer_attributes] ||= {})[:email] = consumer.email
|
58
|
-
end
|
59
|
-
|
60
|
-
venue.orders.build client.post("venues/#{venue_id}/orders", attributes)
|
61
|
-
rescue TicketingHub::BadRequest => error
|
62
|
-
self.tap { errors.add :tickets, error.response_body['message'] }
|
63
|
-
rescue TicketingHub::UnprocessableEntity => error
|
64
|
-
venue.orders.build error.response_body
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,168 +0,0 @@
|
|
1
|
-
require 'active_model'
|
2
|
-
require 'active_support'
|
3
|
-
require_relative 'schema'
|
4
|
-
require_relative 'collection'
|
5
|
-
|
6
|
-
module TicketingHub
|
7
|
-
class Resource
|
8
|
-
extend ActiveModel::Callbacks
|
9
|
-
extend ActiveModel::Naming
|
10
|
-
|
11
|
-
include ActiveModel::AttributeMethods
|
12
|
-
include ActiveModel::Serialization
|
13
|
-
include ActiveModel::Serializers::JSON
|
14
|
-
include ActiveModel::Validations
|
15
|
-
include ActiveModel::Dirty
|
16
|
-
|
17
|
-
class << self
|
18
|
-
def has_many key
|
19
|
-
attr_accessor key
|
20
|
-
define_method key do
|
21
|
-
klass = "TicketingHub::#{key.to_s.singularize.classify}".constantize
|
22
|
-
value = instance_variable_get "@#{key}"
|
23
|
-
value || Collection.from_association(self, klass).tap do |collection|
|
24
|
-
instance_variable_set "@#{key}", collection
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
define_method "#{key}=" do |values|
|
29
|
-
klass = "TicketingHub::#{key.to_s.singularize.classify}".constantize
|
30
|
-
instance_variable_set "@#{key}", Collection.new(values.map do |value|
|
31
|
-
value.is_a?(klass) ? value : send(key).build(value)
|
32
|
-
end, self, klass)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def belongs_to key
|
37
|
-
attr_accessor key
|
38
|
-
|
39
|
-
define_method "#{key}=" do |value|
|
40
|
-
klass = "TicketingHub::#{key.to_s.classify}".constantize
|
41
|
-
value = value.is_a?(Hash) ? klass.new(value.merge({
|
42
|
-
foreign_key => id,
|
43
|
-
model_name.singular_route_key => self
|
44
|
-
})) : value
|
45
|
-
send "#{klass.foreign_key}=", value.id
|
46
|
-
instance_variable_set "@#{key}", value
|
47
|
-
end
|
48
|
-
|
49
|
-
define_method key do
|
50
|
-
klass = "TicketingHub::#{key.to_s.classify}".constantize
|
51
|
-
value = instance_variable_get "@#{key}"
|
52
|
-
if value.nil? && (fk = send(klass.foreign_key)).present?
|
53
|
-
klass.find(fk).tap do |record|
|
54
|
-
instance_variable_set "@#{key}", record
|
55
|
-
end
|
56
|
-
else value end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def create attributes={}
|
61
|
-
new.create attributes
|
62
|
-
end
|
63
|
-
|
64
|
-
def model_name
|
65
|
-
@_model_name ||= ActiveModel::Name.new self, TicketingHub
|
66
|
-
end
|
67
|
-
|
68
|
-
def foreign_key
|
69
|
-
name.to_s.foreign_key
|
70
|
-
end
|
71
|
-
|
72
|
-
def default_client
|
73
|
-
TicketingHub::Client.new
|
74
|
-
end
|
75
|
-
|
76
|
-
def all
|
77
|
-
default_client.get(model_name.route_key).map { |attributes| new attributes }
|
78
|
-
end
|
79
|
-
|
80
|
-
def find id
|
81
|
-
new default_client.get("#{model_name.route_key}/#{id}")
|
82
|
-
end
|
83
|
-
|
84
|
-
def schema(&block)
|
85
|
-
if block_given?
|
86
|
-
undefine_attribute_methods @schema.keys if @schema.present?
|
87
|
-
@schema = TicketingHub::Schema.new(&block).tap do |schema|
|
88
|
-
attr_accessor *schema.keys
|
89
|
-
define_attribute_methods schema.keys
|
90
|
-
end
|
91
|
-
else
|
92
|
-
@schema ||= TicketingHub::Schema.new
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def endpoint=(value)
|
97
|
-
@endpoint = value unless value.empty?
|
98
|
-
end
|
99
|
-
|
100
|
-
def endpoint
|
101
|
-
@endpoint || model_name.plural
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
delegate :foreign_key, :model_name, :schema, to: :class
|
106
|
-
|
107
|
-
attr_accessor :client, :errors, :attributes
|
108
|
-
define_model_callbacks :create, :save, :update, :destroy
|
109
|
-
|
110
|
-
def initialize(attributes = {}, client = self.class.default_client)
|
111
|
-
self.client = client
|
112
|
-
self.attributes = attributes
|
113
|
-
@errors = ActiveModel::Errors.new self
|
114
|
-
end
|
115
|
-
|
116
|
-
def ==(other)
|
117
|
-
other.equal?(self) ||
|
118
|
-
(other.instance_of?(self.class) && other.id == id && ! other.new_record?)
|
119
|
-
end
|
120
|
-
|
121
|
-
def ===(other)
|
122
|
-
object.is_a? self
|
123
|
-
end
|
124
|
-
|
125
|
-
def eql?(other)
|
126
|
-
self == other
|
127
|
-
end
|
128
|
-
|
129
|
-
def hash
|
130
|
-
id.hash
|
131
|
-
end
|
132
|
-
|
133
|
-
def endpoint
|
134
|
-
new_record?? model_name.plural : [model_name.singular, id].join('/')
|
135
|
-
end
|
136
|
-
|
137
|
-
def create attributes={}
|
138
|
-
throw 'Cannot create existing record.' if persisted?
|
139
|
-
self.attributes = client.post(endpoint, self.attributes.merge(attributes))
|
140
|
-
end
|
141
|
-
|
142
|
-
def new_record?
|
143
|
-
id.nil?
|
144
|
-
end
|
145
|
-
|
146
|
-
def persisted?
|
147
|
-
! new_record?
|
148
|
-
end
|
149
|
-
|
150
|
-
def attributes
|
151
|
-
schema.keys.each_with_object Hash.new do |key, memo|
|
152
|
-
memo[key.to_sym] = instance_variable_get "@#{key}"
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
def attributes=(values)
|
157
|
-
values.each do |key, value|
|
158
|
-
send("#{key}=", value) if respond_to? "#{key}="
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
def column_for_attribute(name)
|
163
|
-
Class.new do
|
164
|
-
def type; schema[name] end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
data/lib/ticketing_hub/schema.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
module TicketingHub
|
2
|
-
class Schema
|
3
|
-
KNOWN_ATTRIBUTE_TYPES = %w(string text integer float decimal datetime timestamp time date binary boolean)
|
4
|
-
|
5
|
-
def [] key
|
6
|
-
@attrs[key.to_s].to_sym
|
7
|
-
end
|
8
|
-
|
9
|
-
def initialize &block
|
10
|
-
@attrs = Hash.new
|
11
|
-
instance_eval &block if block_given?
|
12
|
-
end
|
13
|
-
|
14
|
-
def keys
|
15
|
-
@attrs.keys
|
16
|
-
end
|
17
|
-
|
18
|
-
def attribute name, type, options = {}
|
19
|
-
raise ArgumentError, "Unknown Attribute type: #{type.inspect} for key: #{name.inspect}" unless type.nil? || Schema::KNOWN_ATTRIBUTE_TYPES.include?(type.to_s)
|
20
|
-
tap { @attrs[name.to_s] = [type.to_s, options] }
|
21
|
-
end
|
22
|
-
|
23
|
-
KNOWN_ATTRIBUTE_TYPES.each do |type|
|
24
|
-
define_method type do |*args|
|
25
|
-
options = args.last.is_a?(::Hash) ? args.pop : {}
|
26
|
-
args.each { |name| attribute name, type, options }
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
data/lib/ticketing_hub/ticket.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'money'
|
2
|
-
|
3
|
-
module TicketingHub
|
4
|
-
class Ticket < Resource
|
5
|
-
schema do
|
6
|
-
integer :id, :tier_id, :order_id
|
7
|
-
float :commission, :price, default: 0
|
8
|
-
string :code
|
9
|
-
end
|
10
|
-
|
11
|
-
belongs_to :tier
|
12
|
-
belongs_to :order
|
13
|
-
delegate :currency, to: :order
|
14
|
-
|
15
|
-
def price
|
16
|
-
attributes[:price].to_money currency
|
17
|
-
end
|
18
|
-
|
19
|
-
def commission
|
20
|
-
attributes[:commission].to_money currency
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/ticketing_hub/tier.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'money'
|
2
|
-
|
3
|
-
module TicketingHub
|
4
|
-
class Tier < Resource
|
5
|
-
schema do
|
6
|
-
integer :id, :venue_id
|
7
|
-
string :name
|
8
|
-
text :description
|
9
|
-
float :commission, :price, default: 0
|
10
|
-
end
|
11
|
-
|
12
|
-
attr_accessor :venue
|
13
|
-
delegate :currency, to: :venue
|
14
|
-
belongs_to :venue
|
15
|
-
|
16
|
-
def price
|
17
|
-
attributes[:price].to_money currency
|
18
|
-
end
|
19
|
-
|
20
|
-
def commission
|
21
|
-
attributes[:commission].to_money currency
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
data/lib/ticketing_hub/venue.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
module TicketingHub
|
2
|
-
class Venue < Resource
|
3
|
-
attr_accessor :tiers, :orders
|
4
|
-
|
5
|
-
schema do
|
6
|
-
integer :id
|
7
|
-
string :name, :street_1, :street_2, :city, :region, :postcode, :country, :category, :currency
|
8
|
-
float :lat, :lng
|
9
|
-
end
|
10
|
-
|
11
|
-
has_many :tiers
|
12
|
-
has_many :orders
|
13
|
-
end
|
14
|
-
end
|