Class: SpookAndPay::Result
- Inherits:
-
Object
- Object
- SpookAndPay::Result
- Defined in:
- lib/spook_and_pay/result.rb
Overview
A small convenience class which wraps any results coming back from a provider. This is never instanced directly, but instead instances are created by the Provider classes.
Instance Attribute Summary collapse
-
#credit_card ⇒ Object
readonly
Readers for the various portions of a result’s payload.
-
#errors ⇒ Object
readonly
Readers for the various portions of a result’s payload.
-
#raw ⇒ Object
readonly
Readers for the various portions of a result’s payload.
-
#transaction ⇒ Object
readonly
Readers for the various portions of a result’s payload.
Instance Method Summary collapse
-
#credit_card? ⇒ true, false
Checks to see if a credit card is present.
-
#errors? ⇒ true, false
Checks to see if any errors are present.
-
#errors_for(target) ⇒ Object
Collects errors for a specific target, keyed by field.
-
#errors_for_field(target, field) ⇒ Object
Returns the errors for a specific target and field.
-
#failure? ⇒ true, false
A nice helper for checking for failure.
-
#initialize(successful, raw, opts = {}) ⇒ Result
constructor
A new instance of Result.
-
#successful? ⇒ true, false
A nice alias for checking for success.
-
#transaction? ⇒ true, false
Checks to see if a transaction is present.
Constructor Details
#initialize(successful, raw, opts = {}) ⇒ Result
Returns a new instance of Result.
13 14 15 16 17 18 19 |
# File 'lib/spook_and_pay/result.rb', line 13 def initialize(successful, raw, opts = {}) @successful = successful @raw = raw @transaction = opts[:transaction] if opts.has_key?(:transaction) @credit_card = opts[:credit_card] if opts.has_key?(:credit_card) @errors = opts[:errors] || [] end |
Instance Attribute Details
#credit_card ⇒ Object (readonly)
Readers for the various portions of a result’s payload. Depending on the type of request any of these may be nil.
8 9 10 |
# File 'lib/spook_and_pay/result.rb', line 8 def credit_card @credit_card end |
#errors ⇒ Object (readonly)
Readers for the various portions of a result’s payload. Depending on the type of request any of these may be nil.
8 9 10 |
# File 'lib/spook_and_pay/result.rb', line 8 def errors @errors end |
#raw ⇒ Object (readonly)
Readers for the various portions of a result’s payload. Depending on the type of request any of these may be nil.
8 9 10 |
# File 'lib/spook_and_pay/result.rb', line 8 def raw @raw end |
#transaction ⇒ Object (readonly)
Readers for the various portions of a result’s payload. Depending on the type of request any of these may be nil.
8 9 10 |
# File 'lib/spook_and_pay/result.rb', line 8 def transaction @transaction end |
Instance Method Details
#credit_card? ⇒ true, false
Checks to see if a credit card is present.
31 32 33 |
# File 'lib/spook_and_pay/result.rb', line 31 def credit_card? !credit_card.nil? end |
#errors? ⇒ true, false
Checks to see if any errors are present.
38 39 40 |
# File 'lib/spook_and_pay/result.rb', line 38 def errors? !errors.empty? end |
#errors_for(target) ⇒ Object
Collects errors for a specific target, keyed by field.
47 48 49 50 51 52 53 |
# File 'lib/spook_and_pay/result.rb', line 47 def errors_for(target) errors.select{|e| e.target == target}.reduce({}) do |h, e| h[e.field] ||= [] h[e.field] << e h end end |
#errors_for_field(target, field) ⇒ Object
Returns the errors for a specific target and field.
60 61 62 |
# File 'lib/spook_and_pay/result.rb', line 60 def errors_for_field(target, field) errors.select {|e| e.target == target and e.field == field} end |
#failure? ⇒ true, false
A nice helper for checking for failure.
74 75 76 |
# File 'lib/spook_and_pay/result.rb', line 74 def failure? !@successful end |
#successful? ⇒ true, false
A nice alias for checking for success.
67 68 69 |
# File 'lib/spook_and_pay/result.rb', line 67 def successful? @successful end |
#transaction? ⇒ true, false
Checks to see if a transaction is present.
24 25 26 |
# File 'lib/spook_and_pay/result.rb', line 24 def transaction? !transaction.nil? end |