Class: Tickethub::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/tickethub/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, attributes = nil) ⇒ Resource

Returns a new instance of Resource.



203
204
205
206
207
208
# File 'lib/tickethub/resource.rb', line 203

def initialize(endpoint, attributes = nil)
  @endpoint = endpoint
  attributes ||= endpoint.get

  self.load attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object (protected)



284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/tickethub/resource.rb', line 284

def method_missing(method, *arguments)
  if match = method.to_s.match(/^(.+)(=|\?)$/)
    key = match[1].to_sym

    case match[2]
      when '='
        @attributes[key] = self.class.load_value(key, arguments.first, self)
      when "?"
        !! @attributes[key]
    end
  else
    @attributes.key?(method) ? @attributes[method] : super
  end
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



201
202
203
# File 'lib/tickethub/resource.rb', line 201

def endpoint
  @endpoint
end

Class Method Details

.all(params = {}) ⇒ Object



54
55
56
# File 'lib/tickethub/resource.rb', line 54

def self.all(params = {})
  Tickethub::Collection.new Tickethub.endpoint[self.path], self, params
end

.association(key, klass, options = {}) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/tickethub/resource.rb', line 178

def self.association(key, klass, options = {})
  define_method key do
    @attributes.key?(key.to_sym) ? 
      (attrs = @attributes[key.to_sym]) &&
        klass.call(@endpoint[key], attrs, options) :
          klass.call(@endpoint[key], nil, options)
  end
end

.attribute(key, options) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/tickethub/resource.rb', line 66

def self.attribute(key, options)
  self.attributes[key] = options

  self.descendants.each do |descendant|
    descendant.attributes[key] = options
  end
end

.attributesObject



31
32
33
# File 'lib/tickethub/resource.rb', line 31

def attributes
  @attributes ||= {}
end

.call(endpoint, attributes = nil, options = {}, params = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/tickethub/resource.rb', line 151

def self.call(endpoint, attributes = nil, options = {}, params = {})
  if attributes.is_a? String
    attributes = (options[:shallow] == false ?
      endpoint[CGI::escape(attributes)] :
      endpoint[self.path, CGI::escape(attributes)]).get params
  end

  attributes ||= endpoint.get params

  klass = registered_types.find do |type, options|
    attributes[options[:attribute].to_s] == options[:type]
  end

  klass = klass ? klass[1][:klass] : self
  path = options[:shallow] == false ? endpoint.uri.path + klass.path : klass.path

  endpoint = if klass.singleton?
    endpoint[klass.path]
  elsif id = attributes['id']
    options[:shallow] == false ? endpoint[id] : endpoint[klass.path, id]
  else # readonly
    endpoint[klass.path].freeze
  end

  klass.new endpoint, attributes
end

.collection(key, klass, options = {}, &block) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/tickethub/resource.rb', line 187

def self.collection(key, klass, options = {}, &block)
  define_method key do |params = {}|
    if @attributes[key.to_sym].is_a? Array
      @attributes[key.to_sym].collect do |attrs|
        klass.call(@endpoint[key], attrs, options)
      end
    else
      Tickethub::Collection.new(@endpoint[key], klass, params, options).tap do |collection|
        collection.instance_eval &block if block
      end
    end
  end
end

.collection_method(key, &block) ⇒ Object



44
45
46
# File 'lib/tickethub/resource.rb', line 44

def self.collection_method(key, &block)
  collection_methods[key] = block
end

.collection_methodsObject



39
40
41
# File 'lib/tickethub/resource.rb', line 39

def collection_methods
  @collection_methods ||= {}
end

.descendantsObject



35
36
37
# File 'lib/tickethub/resource.rb', line 35

def descendants
  @descendants ||= []
end

.dump_value(key, value) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/tickethub/resource.rb', line 130

def self.dump_value(key, value)
  return value unless self.attributes.key? key
  case self.attributes[key][:type]
    when :date      then value.iso8601
    when :datetime  then value.iso8601
    when :time      then value.iso8601[10..-1]
    when :duration  then value.iso8601
    when :money     then value.fractional
    when :timezone  then value.zone
    when :country   then value.alpha2
    when :currency  then value.iso_code
    else value
  end
end

.inherited(descendant) ⇒ Object



48
49
50
51
52
# File 'lib/tickethub/resource.rb', line 48

def self.inherited(descendant)
  if descendant.ancestors.member? Tickethub::Resource
    self.descendants.push descendant
  end
end

.load_value(key, value, object) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/tickethub/resource.rb', line 78

def self.load_value(key, value, object)
  return nil if value.nil? || (value.is_a?(String) && value.empty?)
  return value unless self.attributes.key? key

  case self.attributes[key][:type]
    when :date
      case value
        when String then ISO8601::Date.new(value)
        else raise ArgumentError, 'invalid date value: ' + value
      end
    when :datetime
      case value
        when String then ISO8601::DateTime.new(value)
        else raise ArgumentError, 'invalid datetime value: ' + value
      end
    when :time
      case value
        when String then ISO8601::Time.new(value)
        else raise ArgumentError, 'invalid time value: ' + value
      end
    when :duration
      case value
        when String then ISO8601::Duration.new(value)
        else raise ArgumentError, 'invalid time value: ' + value
      end
    when :money
      case value
        when String
          currency, value = value.split ' '
          currency = Money::Currency.wrap(currency)
          Money.new(value.to_d * currency.subunit_to_unit, currency)
        else raise ArgumentError, 'invalid money value: ' + value
      end
    when :currency
      case value
        when String then Money::Currency.new(value)
        else raise ArgumentError, 'invalid currency value: ' + value
      end            
    when :timezone
      case value
        when String then Timezone::Zone.new(zone: value)
        else raise ArgumentError, 'invalid timezone value: ' + value
      end
    when :country
      case value
        when String then ISO3166::Country.new(value)
        else raise ArgumentError, 'invalid country value: ' + value
      end
    else value
  end
end

.path(value = nil, singleton: false) ⇒ Object



17
18
19
20
21
# File 'lib/tickethub/resource.rb', line 17

def path(value = nil, singleton: false)        
  return @path || (superclass.respond_to?(:path) ? superclass.path : nil) if value.nil?
  @singleton = singleton
  @path = value
end

.polymorphic(type, method, attribute = :type) ⇒ Object



58
59
60
# File 'lib/tickethub/resource.rb', line 58

def self.polymorphic(type, method, attribute = :type)
  self.superclass.register_type type, method, self, attribute
end

.register_type(type, method, klass, attribute = :type) ⇒ Object

klass, attribute



62
63
64
# File 'lib/tickethub/resource.rb', line 62

def self.register_type(type, method, klass, attribute = :type) # klass, attribute
  self.registered_types[method] = { type: type, klass: klass, attribute: attribute }
end

.registered_typesObject



23
24
25
# File 'lib/tickethub/resource.rb', line 23

def registered_types
  @registered_types ||= {}
end

.scope(key, proc = -> (params = {}) { self.scope key, params }) ⇒ Object



74
75
76
# File 'lib/tickethub/resource.rb', line 74

def self.scope(key, proc = -> (params = {}) { self.scope key, params })
  self.scopes[key] = proc
end

.scopesObject



27
28
29
# File 'lib/tickethub/resource.rb', line 27

def scopes
  @scopes ||= {}
end

.serialize(attributes) ⇒ Object



145
146
147
148
149
# File 'lib/tickethub/resource.rb', line 145

def self.serialize(attributes)
  attributes.collect do |key, value|
    [key, dump_value(key, value)]
  end.to_h
end

.singleton?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/tickethub/resource.rb', line 13

def singleton?
  !! @singleton
end

Instance Method Details

#==(other) ⇒ Object



250
251
252
# File 'lib/tickethub/resource.rb', line 250

def ==(other)
  self.hash == other.hash
end

#[](key) ⇒ Object



242
243
244
# File 'lib/tickethub/resource.rb', line 242

def [](key)
  send key
end

#[]=(key, value) ⇒ Object



246
247
248
# File 'lib/tickethub/resource.rb', line 246

def []=(key, value)
  send "#{key}=", value
end

#destroyObject



222
223
224
# File 'lib/tickethub/resource.rb', line 222

def destroy
  self.load @endpoint.delete.decoded
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/tickethub/resource.rb', line 254

def eql?(other)
  self == other
end

#errorsObject



266
267
268
# File 'lib/tickethub/resource.rb', line 266

def errors
  @errors ||= Tickethub::Errors.new @attributes[:errors]
end

#hashObject



258
259
260
# File 'lib/tickethub/resource.rb', line 258

def hash
  id?? [self.class, id].hash : super
end

#inspectObject



278
279
280
# File 'lib/tickethub/resource.rb', line 278

def inspect
  "#<#{self.class.name} #{to_h}>"
end

#load(attributes) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/tickethub/resource.rb', line 234

def load(attributes)
  @attributes = {}
  attributes.each do |key, value|
    send "#{key}=", value
  end
  return self
end

#reload!Object



230
231
232
# File 'lib/tickethub/resource.rb', line 230

def reload!
  self.load @endpoint.get
end

#respond_to?(method, include_priv = false) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/tickethub/resource.rb', line 226

def respond_to?(method, include_priv = false)
  @attributes.key?(method.to_s.remove(/[=\?]\Z/).to_sym) || super
end

#to_hObject



270
271
272
# File 'lib/tickethub/resource.rb', line 270

def to_h
  @attributes
end

#to_paramObject



262
263
264
# File 'lib/tickethub/resource.rb', line 262

def to_param
  self.id
end

#to_sObject



274
275
276
# File 'lib/tickethub/resource.rb', line 274

def to_s
  self.id?? id : super
end

#update(attributes) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/tickethub/resource.rb', line 214

def update(attributes)
  self.load @endpoint.patch(attributes).decoded
  return true
rescue Tickethub::ResourceInvalid => err
  self.load Tickethub::Response.new(err.response).decoded
  return false
end

#valid?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/tickethub/resource.rb', line 210

def valid?
  errors.nil? || errors.valid?
end