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.



53
54
55
56
# File 'lib/easy_ping/model.rb', line 53

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)



135
136
137
# File 'lib/easy_ping/model.rb', line 135

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#modelsObject (readonly)

Returns the value of attribute models.



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

def models
  @models
end

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Class Method Details

.parse!(response, config) ⇒ Object

Raises:



96
97
98
99
# 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

Instance Method Details

#all_refund(*args) ⇒ Object



118
119
120
121
122
123
124
# 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) ⇒ Object



101
102
103
104
# File 'lib/easy_ping/model.rb', line 101

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

#list?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/easy_ping/model.rb', line 106

def list?
  @list ? true : false
end

#refund(*args) ⇒ Object



110
111
112
113
114
115
116
# 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) ⇒ Object



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
93
94
# 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