class EasyPing::Model::Wrapper

Attributes

config[R]
models[R]
raw[R]
response[R]
type[R]
values[R]

Public Class Methods

new(response, config) click to toggle source
# File lib/easy_ping/model.rb, line 53
def initialize(response, config)
  @config = config
  setup(response)
end
parse!(response, config) click to toggle source
# File lib/easy_ping/model.rb, line 96
def self.parse!(response, config)
  raise EasyPing::APIError.new(response) unless response.success?
  self.new(response, config)
end

Public Instance Methods

all_refund(*args) click to toggle source
# File lib/easy_ping/model.rb, line 118
def all_refund(*args)
  if models.respond_to?(:all_refund)
    models.all_refund(config, *args)
  else
    raise NoMethodError, "undefined method `all_refund' for instance of EasyPing::Model"
  end
end
build_instance(type, values) click to toggle source
# File lib/easy_ping/model.rb, line 101
def build_instance(type, values)
  klass = Model.const_get type.capitalize
  klass.new values
end
list?() click to toggle source
# File lib/easy_ping/model.rb, line 106
def list?
  @list ? true : false
end
refund(*args) click to toggle source
# File lib/easy_ping/model.rb, line 110
def refund(*args)
  if models.respond_to?(:refund)
    models.refund(config, *args)
  else
    raise NoMethodError, "undefined method `refund' for instance of EasyPing::Model"
  end
end
setup(response) click to toggle source
# File lib/easy_ping/model.rb, line 58
def setup(response)
  setup_flag = catch(:halt) do
    if response.respond_to?(:body)
      @response = response
      @raw      = response.body
      @values   = JSON.parse(response.body) rescue nil
    elsif response.kind_of?(Hash)
      @response = nil
      @raw      = nil
      @values   = response
    elsif response.kind_of?(String)
      @response = nil
      @raw      = response
      @values   = JSON.parse(response.body) rescue nil
    end
    throw :halt unless @values

    if @values['object'] == 'list'
      @list, @has_more, @url = true, @values['has_more'], @values['url']
      extend List

      @type = /refunds/ =~ @values['url'] ? 'refund' : 'charge'
      @models = @values['data'].map {|object| build_instance(@type, object)}
    elsif ['charge', 'refund'].include? @values['object']
      @type = @values['object']
      @models = build_instance(@type, @values)
    end
    throw :halt unless @models && @type

    # return true if everything went well
    true
  end

  unless setup_flag
    raise EasyPing::ParametersInvalid, "#{values} is not valid charge or refund object."
  end
end

Private Instance Methods

action_class() click to toggle source
# File lib/easy_ping/model.rb, line 131
def action_class
  type == 'charge' ? EasyPing::Charge : EasyPing::Refund
end
method_missing(name, *args, &block) click to toggle source
# File lib/easy_ping/model.rb, line 135
def method_missing(name, *args, &block)
  models.send name, *args, &block
end