class Tickethub::Resource

Attributes

endpoint[RW]

Public Class Methods

all(params = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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
attributes() click to toggle source
# File lib/tickethub/resource.rb, line 31
def attributes
  @attributes ||= {}
end
call(endpoint, attributes = nil, options = {}, params = {}) click to toggle source
# 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) click to toggle source
# File lib/tickethub/resource.rb, line 187
def self.collection(key, klass, options = {}, &block)
  define_method key do |params = {}|
    Tickethub::Collection.new(@endpoint[key], klass, params, options, @attributes[key.to_sym]).tap do |collection|
      collection.instance_eval &block if block
    end
  end
end
collection_method(key, &block) click to toggle source
# File lib/tickethub/resource.rb, line 44
def self.collection_method(key, &block)
  collection_methods[key] = block
end
collection_methods() click to toggle source
# File lib/tickethub/resource.rb, line 39
def collection_methods
  @collection_methods ||= {}
end
descendants() click to toggle source
# File lib/tickethub/resource.rb, line 35
def descendants
  @descendants ||= []
end
dump_value(key, value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 ISO8601Basic::Date.new(value)
        else raise ArgumentError, 'invalid date value: ' + value
      end
    when :datetime
      case value
        when String then ISO8601Basic::DateTime.new(value)
        else raise ArgumentError, 'invalid datetime value: ' + value
      end
    when :time
      case value
        when String then ISO8601Basic::Time.new(value)
        else raise ArgumentError, 'invalid time value: ' + value
      end
    when :duration
      case value
        when String then ISO8601Basic::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[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
new(endpoint, attributes = nil) click to toggle source
# File lib/tickethub/resource.rb, line 197
def initialize(endpoint, attributes = nil)
  @endpoint = endpoint
  attributes ||= endpoint.get

  self.load attributes
end
path(value = nil, singleton: false) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_types() click to toggle source
# File lib/tickethub/resource.rb, line 23
def registered_types
  @registered_types ||= {}
end
scope(key, proc = -> (params = {}) { self.scope key, params } click to toggle source
# File lib/tickethub/resource.rb, line 74
def self.scope(key, proc = -> (params = {}) { self.scope key, params })
  self.scopes[key] = proc
end
scopes() click to toggle source
# File lib/tickethub/resource.rb, line 27
def scopes
  @scopes ||= {}
end
serialize(attributes) click to toggle source
# 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?() click to toggle source
# File lib/tickethub/resource.rb, line 13
def singleton?
  !! @singleton
end

Public Instance Methods

==(other) click to toggle source
# File lib/tickethub/resource.rb, line 244
def ==(other)
  self.hash == other.hash
end
[](key) click to toggle source
# File lib/tickethub/resource.rb, line 236
def [](key)
  send key
end
[]=(key, value) click to toggle source
# File lib/tickethub/resource.rb, line 240
def []=(key, value)
  send "#{key}=", value
end
destroy() click to toggle source
# File lib/tickethub/resource.rb, line 216
def destroy
  self.load @endpoint.delete.decoded
end
eql?(other) click to toggle source
# File lib/tickethub/resource.rb, line 248
def eql?(other)
  self == other
end
errors() click to toggle source
# File lib/tickethub/resource.rb, line 260
def errors
  @errors ||= Tickethub::Errors.new @attributes[:errors]
end
hash() click to toggle source
Calls superclass method
# File lib/tickethub/resource.rb, line 252
def hash
  id?? [self.class, id].hash : super
end
inspect() click to toggle source
# File lib/tickethub/resource.rb, line 272
def inspect
  "#<#{self.class.name} #{to_h}>"
end
load(attributes) click to toggle source
# File lib/tickethub/resource.rb, line 228
def load(attributes)
  @attributes = {}
  attributes.each do |key, value|
    send "#{key}=", value
  end
  return self
end
reload!() click to toggle source
# File lib/tickethub/resource.rb, line 224
def reload!
  self.load @endpoint.get
end
respond_to?(method, include_priv = false) click to toggle source
Calls superclass method
# File lib/tickethub/resource.rb, line 220
def respond_to?(method, include_priv = false)
  @attributes.key?(method.to_s.remove(/[=\?]\Z/).to_sym) || super
end
to_h() click to toggle source
# File lib/tickethub/resource.rb, line 264
def to_h
  @attributes
end
to_param() click to toggle source
# File lib/tickethub/resource.rb, line 256
def to_param
  self.id
end
to_s() click to toggle source
Calls superclass method
# File lib/tickethub/resource.rb, line 268
def to_s
  self.id?? id : super
end
update(attributes) click to toggle source
# File lib/tickethub/resource.rb, line 208
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?() click to toggle source
# File lib/tickethub/resource.rb, line 204
def valid?
  errors.nil? || errors.valid?
end

Protected Instance Methods

method_missing(method, *arguments) click to toggle source
Calls superclass method
# File lib/tickethub/resource.rb, line 278
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