Class: EasyPing::Model::Wrapper
- Inherits:
-
Object
- Object
- EasyPing::Model::Wrapper
- Extended by:
- Forwardable
- Defined in:
- lib/easy_ping/model.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
- #all_refund(*args) ⇒ Object
- #build_instance(type, values) ⇒ Object
-
#initialize(response, config) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #list? ⇒ Boolean
- #refund(*args) ⇒ Object
- #setup(response) ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
51 52 53 |
# File 'lib/easy_ping/model.rb', line 51 def config @config end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
51 52 53 |
# File 'lib/easy_ping/model.rb', line 51 def models @models end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
51 52 53 |
# File 'lib/easy_ping/model.rb', line 51 def raw @raw end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
51 52 53 |
# File 'lib/easy_ping/model.rb', line 51 def response @response end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
51 52 53 |
# File 'lib/easy_ping/model.rb', line 51 def type @type end |
#values ⇒ Object (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
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
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 |