Class: SpookAndPay::Result

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(successful, raw, opts = {}) ⇒ Result

Returns a new instance of Result.

Parameters:

  • successful (true, false)
  • Class

    raw

  • Hash

    opts



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_cardObject (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

#errorsObject (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

#rawObject (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

#transactionObject (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.

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


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.

Parameters:

  • Symbol

    target

Returns:

  • Hash



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.

Parameters:

  • Symbol

    target

  • Symbol

    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.

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


24
25
26
# File 'lib/spook_and_pay/result.rb', line 24

def transaction?
  !transaction.nil?
end