Class: EasyPing::Model::Wrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/easy_ping/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, config) ⇒ Wrapper

Returns a new instance of Wrapper.



51
52
53
54
# File 'lib/easy_ping/model.rb', line 51

def initialize(response, config)
  @config = config
  setup(response)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



133
134
135
# File 'lib/easy_ping/model.rb', line 133

def method_missing(name, *args, &block)
  models.send name, *args, &block
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



49
50
51
# File 'lib/easy_ping/model.rb', line 49

def config
  @config
end

#modelsObject (readonly)

Returns the value of attribute models.



49
50
51
# File 'lib/easy_ping/model.rb', line 49

def models
  @models
end

#rawObject (readonly)

Returns the value of attribute raw.



49
50
51
# File 'lib/easy_ping/model.rb', line 49

def raw
  @raw
end

#responseObject (readonly)

Returns the value of attribute response.



49
50
51
# File 'lib/easy_ping/model.rb', line 49

def response
  @response
end

#typeObject (readonly)

Returns the value of attribute type.



49
50
51
# File 'lib/easy_ping/model.rb', line 49

def type
  @type
end

#valuesObject (readonly)

Returns the value of attribute values.



49
50
51
# File 'lib/easy_ping/model.rb', line 49

def values
  @values
end

Class Method Details

.parse!(response, config) ⇒ Object

Raises:



94
95
96
97
# File 'lib/easy_ping/model.rb', line 94

def self.parse!(response, config)
  raise EasyPing::APIError.new(response) unless response.success?
  self.new(response, config)
end

Instance Method Details

#all_refund(*args) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/easy_ping/model.rb', line 116

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) ⇒ Object



99
100
101
102
# File 'lib/easy_ping/model.rb', line 99

def build_instance(type, values)
  klass = Model.const_get type.capitalize
  klass.new values
end

#list?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/easy_ping/model.rb', line 104

def list?
  @list ? true : false
end

#refund(*args) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/easy_ping/model.rb', line 108

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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/easy_ping/model.rb', line 56

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