tickethub 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e87494a40da501ed5049f42be78ad18c060c598
4
- data.tar.gz: 17da18e8657285fce922f03b835cc2409d942e27
3
+ metadata.gz: 75b7f07212e2ad1e522dbd7aad6d12f182a741dd
4
+ data.tar.gz: 801f6f6e85e89700f476424154fd922fd6948e94
5
5
  SHA512:
6
- metadata.gz: 9ca70e607c4b5f654a77ec62cffe889b97d875cfcf57b599095a29d222ce0e750417213a8636ed8e47129def26b92d7186dbd67dfb7d1dbd7fd1a855f1b5b593
7
- data.tar.gz: 0434a2ddc9ef885cfd7b1037e558ca05f36b3f1216983eaf92da2ce8c15c54e37dcd375027e19e63ce71f8e9e46d514d23a06a08b54f71d61878027c1e0ee5ca
6
+ metadata.gz: 84b02aa3524c5bc2d21a344179a9719a70c88436b6567eb82cea112708af6984b3f8a9d375c8e04cc122d3ce52e9f9bb901608214d531bb10863952164d64758
7
+ data.tar.gz: 108ddb446e8a39ce8513ebbf63c359a82ae34ca59c92c65db8e3811b574797313fe82ee500b53475a4382e65c0f104a5f19187ab5042f163690cd0c1205c6791
data/lib/tickethub.rb CHANGED
@@ -4,9 +4,9 @@ module Tickethub
4
4
  @path = path
5
5
  end
6
6
 
7
- def self.endpoint
8
- Endpoint.new @path || 'https://api.tickethub.io', format: :json,
9
- headers: { 'Accept-Version' => 'v1', 'Accept' => 'application/json' }
7
+ def self.endpoint(options = {})
8
+ Endpoint.new @path || 'https://api.tickethub.io', options.merge(format: :json,
9
+ headers: { 'Accept-Version' => 'v1', 'Accept' => 'application/json' })
10
10
  end
11
11
 
12
12
  require_relative 'tickethub/helpers'
data/lib/tickethub/app.rb CHANGED
@@ -24,8 +24,8 @@ module Tickethub
24
24
  attributes ||= endpoint.get
25
25
 
26
26
  if attributes['token']
27
- endpoint = Tickethub.endpoint['/app', {
28
- auth_type: :bearer, password: attributes['token']['access_token'] }]
27
+ endpoint = Tickethub.endpoint(auth_type: :bearer,
28
+ password: attributes['token']['access_token'])[self.class.path]
29
29
  end
30
30
 
31
31
  super(endpoint, attributes)
@@ -1,9 +1,10 @@
1
+ require 'cgi'
1
2
  require_relative 'helpers'
2
3
 
3
4
  module Tickethub
4
5
  class Collection < Enumerator
5
6
  attr_accessor :cache
6
- attr_reader :count, :endpoint
7
+ attr_reader :count, :endpoint, :params
7
8
 
8
9
  def initialize(endpoint, klass, params = {})
9
10
  @params = params.dup
@@ -13,7 +14,7 @@ module Tickethub
13
14
  klass.registered_types.each do |type, options|
14
15
  define_singleton_method type do
15
16
  instance_variable_defined?("@#{type}") ? instance_variable_get("@#{type}") :
16
- instance_variable_set("@#{type}", Tickethub::Collection.new(@endpoint[type], options[:klass]))
17
+ instance_variable_set("@#{type}", Tickethub::Collection.new(endpoint[type], options[:klass]))
17
18
  end
18
19
  end
19
20
 
@@ -22,27 +23,28 @@ module Tickethub
22
23
  end
23
24
 
24
25
  super() do |yielder|
25
- self.reload! if @cache.nil?
26
+ self.reload! if cache.nil?
26
27
 
27
- @cache.each do |row|
28
- yielder << @klass.load(@endpoint, row)
28
+ cache.each do |row|
29
+ yielder << @klass.load(endpoint, row)
29
30
  end
30
31
 
31
- while (@offset + @cache.length) < @count
32
- response = @endpoint.get @params.merge(offset: @cache.length)
33
- response.decoded.each do |row| @cache << row
34
- yielder << @klass.load(@endpoint, row)
32
+ while (offset + cache.length) < count
33
+ response = endpoint.get params.merge(offset: cache.length)
34
+ response.decoded.each do |row| cache << row
35
+ yielder << @klass.load(endpoint, row)
35
36
  end
36
37
  end
37
38
  end
38
39
  end
39
40
 
40
41
  def reload!
41
- @cache = (response = @endpoint.get @params).decoded
42
+ @cache = (response = endpoint.get params).decoded
42
43
  @count, @offset, @limit =
43
44
  response.status == 206 ?
44
45
  response.headers.values_at(*%w(x-total-count x-offset x-limit))
45
46
  .collect { |value| value[0].to_i } : [@cache.length, 0, @cache.length]
47
+ return self
46
48
  end
47
49
 
48
50
  def last
@@ -54,8 +56,8 @@ module Tickethub
54
56
  reload! if @limit.nil?
55
57
  return @limit
56
58
  else
57
- self.class.new @endpoint, @klass,
58
- @params.merge(limit: value)
59
+ self.class.new endpoint, @klass,
60
+ params.merge(limit: value)
59
61
  end
60
62
  end
61
63
 
@@ -64,23 +66,23 @@ module Tickethub
64
66
  reload! if @offset.nil?
65
67
  return @offset
66
68
  else
67
- self.class.new @endpoint, @klass,
68
- @params.merge(offset: value)
69
+ self.class.new endpoint, @klass,
70
+ params.merge(offset: value)
69
71
  end
70
72
  end
71
73
 
72
74
  def filter(value)
73
- self.class.new @endpoint, @klass,
74
- @params.merge(filters: (@params[:filters] || {}).merge(value))
75
+ self.class.new endpoint, @klass,
76
+ params.merge(filters: filters.merge(value))
75
77
  end
76
78
 
77
79
  def filters
78
- @params[:filters].dup
80
+ (params[:filters] || {}).dup
79
81
  end
80
82
 
81
83
  def order(value = nil)
82
- return @params[:order].dup if value.nil?
83
- order = (@params[:order] || []) + (case value
84
+ return params[:order].dup if value.nil?
85
+ order = (params[:order] || []) + (case value
84
86
  when Symbol, String then [value.to_s]
85
87
  when Hash
86
88
  value.collect do |key, direction|
@@ -88,18 +90,23 @@ module Tickethub
88
90
  end
89
91
  end)
90
92
 
91
- self.class.new @endpoint, @klass,
92
- @params.merge(order: order)
93
+ self.class.new endpoint, @klass,
94
+ params.merge(order: order)
95
+ end
96
+
97
+ def scope(path, params = {})
98
+ self.class.new endpoint[path], @klass,
99
+ self.params.merge(params)
93
100
  end
94
101
 
95
102
  [:get, :post, :patch, :delete].each do |key|
96
- define_method key do |path, params = {}, options = {}|
103
+ define_method key do |path, params = {}|
97
104
  endpoint = (if path.is_a? Hash
98
105
  params, options = [path, params]
99
106
  self.endpoint
100
107
  else self.endpoint[path] end)
101
108
 
102
- if (response = endpoint.send(key, @params.merge(params), options)).body.length > 2
109
+ if (response = endpoint.send(key, self.params.merge(params))).body.length > 2
103
110
  return response.decoded
104
111
  end
105
112
  end
@@ -123,31 +130,26 @@ module Tickethub
123
130
  return @count
124
131
  end
125
132
 
126
- def [](*args)
127
- case (key = args[0])
133
+ def [](search)
134
+ case search
128
135
  when Fixnum
129
- self.offset(key).first
136
+ self.offset(search).first
130
137
  when Hash
131
- self.filter(key).first
138
+ self.filter(search).first
132
139
  when Range
133
- self.offset(key.min).first(key.max)
140
+ self.offset(search.min).first(search.max)
134
141
  when String
135
- options = { params: args[1] || {} }.merge args[2] || {}
136
- @klass.load @klass.endpoint[key, @endpoint[nil, options].options]
142
+ endpoint = self.endpoint[@klass.path, CGI::escape(search)]
143
+ @klass.load endpoint, endpoint.get(params)
137
144
  else
138
145
  raise ArgumentError, 'invalid search value type'
139
146
  end
140
147
  end
141
148
 
142
149
  def create(attributes = {})
143
- @klass.load @endpoint, @endpoint.post(attributes).decoded
150
+ @klass.load endpoint, post(attributes)
144
151
  rescue Tickethub::ResourceInvalid => err
145
- @klass.load @endpoint, Tickethub::Response.new(err.response).decoded
146
- end
147
-
148
- def scope(key, params = {}, options = {})
149
- Tickethub::Collection.new @endpoint[key, options.merge(params: params)], @klass,
150
- filters: @filters, order: @order
152
+ @klass.load endpoint, Tickethub::Response.new(err.response).decoded
151
153
  end
152
154
  end
153
155
  end
@@ -1,3 +1,5 @@
1
+ require 'uri'
2
+
1
3
  require_relative 'request'
2
4
  require_relative 'response'
3
5
  require_relative 'helpers'
@@ -6,31 +8,29 @@ module Tickethub
6
8
  class Endpoint
7
9
  attr_reader :options, :url
8
10
 
9
- def initialize(url, options = {})
10
- @url = url
11
+ def initialize(url, options)
12
+ @url = _normalize_path url
11
13
  @options = options
12
14
  end
13
15
 
14
- def [](suburl, options = {})
15
- url = (if suburl.nil? then self.url
16
- else
17
- suburl = suburl.to_s
18
- base = self.url
19
- base += "/" unless base =~ /\/$/
20
- URI.join(base, suburl).to_s
21
- end)
22
-
23
- self.class.new url, Tickethub::Helpers.deep_merge(@options, options)
16
+ def [](*parts)
17
+ parts = parts.compact.map { |part| _normalize_path part.to_s }
18
+ self.class.new URI.join(url, *parts).to_s, @options
24
19
  end
25
20
 
26
21
  [:get, :post, :delete, :patch].each do |method|
27
- define_method method do |params = {}, options = {}|
28
- request options.merge(method: method, params: params)
22
+ define_method method do |params = {}|
23
+ request params, options.merge(method: method)
29
24
  end
30
25
  end
31
26
 
32
- def request(options = {})
33
- Tickethub::Request.new(url, Tickethub::Helpers.deep_merge(@options, options)).execute
27
+ def request(params, options)
28
+ raise 'this endpoint is readonly' if frozen?
29
+ Tickethub::Request.new(url, options.merge(params: params)).execute
30
+ end
31
+
32
+ def _normalize_path(str)
33
+ str.match(/\/$/) ? str : "#{str}/"
34
34
  end
35
35
  end
36
36
  end
@@ -10,14 +10,6 @@ module Tickethub
10
10
  value.to_s.split('_').map {|w| w.capitalize }.join
11
11
  end
12
12
 
13
- def deep_merge(hash, other_hash)
14
- hash.merge(other_hash) do |key, oldval, newval|
15
- oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
16
- newval = newval.to_hash if newval.respond_to?(:to_hash)
17
- oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? deep_merge(oldval, newval) : newval
18
- end
19
- end
20
-
21
13
  # Stolen from Rack:
22
14
 
23
15
  DEFAULT_SEP = /[&;] */n
@@ -10,14 +10,14 @@ module Tickethub
10
10
 
11
11
  class << self
12
12
 
13
- def path(value, options = {})
14
- @path = value
15
- @options = options
13
+ def singleton?
14
+ !! @singleton
16
15
  end
17
16
 
18
- def endpoint
19
- return self.superclass.endpoint unless @path
20
- Tickethub.endpoint[@path, @options]
17
+ def path(value = nil, singleton: false)
18
+ return @path || (superclass.respond_to?(:path) ? superclass.path : nil) if value.nil?
19
+ @singleton = singleton
20
+ @path = value
21
21
  end
22
22
 
23
23
  def registered_types
@@ -151,9 +151,12 @@ module Tickethub
151
151
  end
152
152
 
153
153
  klass = klass ? klass[1][:klass] : self
154
-
155
- if id = attributes['id']
156
- endpoint = klass.endpoint[id, endpoint.options]
154
+ endpoint = if klass.singleton?
155
+ endpoint[klass.path]
156
+ elsif id = attributes['id']
157
+ endpoint[klass.path, id]
158
+ else # readonly
159
+ endpoint[klass.path].freeze
157
160
  end
158
161
 
159
162
  klass.new endpoint, attributes
@@ -161,30 +164,18 @@ module Tickethub
161
164
 
162
165
  def self.association(key, klass, path: key)
163
166
  define_method key do
164
- instance_variable_defined?("@#{key}") ? instance_variable_get("@#{key}") :
165
- instance_variable_set("@#{key}", klass.load(@endpoint[path]))
166
- end
167
-
168
- define_method "#{key}=" do |value|
169
- instance_variable_set "@#{key}", ((value.nil? || value.is_a?(klass)) ? value :
170
- klass.load(@endpoint[path], value))
167
+ @attributes.key?(key.to_sym) ?
168
+ (attrs = @attributes[key.to_sym]) &&
169
+ klass.load(@endpoint[path], attrs) :
170
+ klass.load(@endpoint[path])
171
171
  end
172
172
  end
173
173
 
174
174
  def self.collection(key, klass, path: key, &block)
175
175
  define_method key do |params = {}|
176
- (instance_variable_defined?("@#{key}") ? instance_variable_get("@#{key}") :
177
- (sinleton? || id?) ? instance_variable_set("@#{key}",
178
- Tickethub::Collection.new(@endpoint[path], klass, params)) : []).tap do |collection|
179
- collection.instance_eval &block if block
180
- end
181
- end
182
-
183
- define_method "#{key}=" do |values|
184
- instance_variable_set "@#{key}", id??
185
- (Tickethub::Collection.new(@endpoint[path], klass).tap do |collection|
186
- collection.cache = values
187
- end) : values.collect { |value| klass.load @endpoint, value }
176
+ Tickethub::Collection.new(@endpoint[path], klass, params).tap do |collection|
177
+ collection.instance_eval &block if block
178
+ end
188
179
  end
189
180
  end
190
181
 
@@ -246,7 +237,7 @@ module Tickethub
246
237
  end
247
238
 
248
239
  def hash
249
- id?? id.hash : super
240
+ id?? [self.class, id].hash : super
250
241
  end
251
242
 
252
243
  def to_param
@@ -1,6 +1,6 @@
1
1
  module Tickethub
2
2
  class Supplier < Resource
3
- path '/supplier'
3
+ path '/supplier', singleton: true
4
4
 
5
5
  require_relative 'supplier/bill'
6
6
  require_relative 'supplier/customer'
@@ -66,15 +66,15 @@ module Tickethub
66
66
  def self.[](attributes)
67
67
  token = attributes[:token].is_a?(String) ? attributes[:token]
68
68
  : attributes[:token][:access_token]
69
- self.new Tickethub.endpoint['/supplier', auth_type: :bearer, password: token]
69
+ self.load Tickethub.endpoint(auth_type: :bearer, password: token)[path]
70
70
  end
71
71
 
72
72
  def initialize(endpoint, attributes = nil)
73
73
  attributes ||= endpoint.get
74
74
 
75
75
  if attributes['token']
76
- endpoint = Tickethub.endpoint['/supplier', {
77
- auth_type: :bearer, password: attributes['token']['access_token'] }]
76
+ endpoint = Tickethub.endpoint(auth_type: :bearer,
77
+ password: attributes['token']['access_token'])[self.class.path]
78
78
  end
79
79
 
80
80
  super(endpoint, attributes)
@@ -4,7 +4,7 @@ module Tickethub
4
4
  class Supplier::Option < Resource
5
5
  path '/supplier/options'
6
6
 
7
- scope :dates, -> (params) { @endpoint[:dates, params: params].get.decoded }
7
+ scope :dates, -> (params) { @endpoint[:dates].get(params).decoded }
8
8
  scope :active
9
9
 
10
10
  require_relative 'season'
@@ -1,6 +1,6 @@
1
1
  module Tickethub
2
2
  class Token < Resource
3
- path '/token'
3
+ path '/token', singleton: true
4
4
 
5
5
  def can?(scope)
6
6
  self.scope.member? scope.to_s
@@ -1,6 +1,6 @@
1
1
  module Tickethub
2
2
  class User < Resource
3
- path '/user'
3
+ path '/user', singleton: true
4
4
 
5
5
  require_relative 'user/app'
6
6
  require_relative 'user/supplier'
@@ -27,7 +27,7 @@ module Tickethub
27
27
  : attributes[:token][:access_token]
28
28
  end
29
29
 
30
- endpoint = Tickethub.endpoint['/user', auth_type: :bearer, password: token]
30
+ endpoint = Tickethub.endpoint(auth_type: :bearer, password: token)['/user']
31
31
  self.new endpoint
32
32
  end
33
33
 
@@ -1,3 +1,3 @@
1
1
  module Tickethub
2
- VERSION = '0.1.4'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tickethub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Morgan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-21 00:00:00.000000000 Z
11
+ date: 2014-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler