Class: QuickTravel::Checkout

Inherits:
Adapter
  • Object
show all
Defined in:
lib/quick_travel/checkout.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adapter

all, belongs_to, find, has_many, #to_s

Methods included from InitFromHash

#define_readers, #initialize, #to_hash

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



13
14
15
# File 'lib/quick_travel/checkout.rb', line 13

def error
  @error
end

#progressObject (readonly)

Slow checkouts will require a poll – use #status(checkout_id)



11
12
13
# File 'lib/quick_travel/checkout.rb', line 11

def progress
  @progress
end

#redirect_urlObject (readonly)

Provides the url to redirect the browser to when payment is via an external gateway



8
9
10
# File 'lib/quick_travel/checkout.rb', line 8

def redirect_url
  @redirect_url
end

#requires_pollObject (readonly)

Slow checkouts will require a poll – use #status(checkout_id)



11
12
13
# File 'lib/quick_travel/checkout.rb', line 11

def requires_poll
  @requires_poll
end

Class Method Details

.attributes_forObject

TODO Move to an external builder?



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/quick_travel/checkout.rb', line 48

def self.attributes_for
  attrs = yield
  attrs[:completed] = attrs['progress'] == 'completed'
  attrs[:successful] = attrs[:completed]
  attrs
rescue AdapterError => e
  {
    completed:  true,
    successful: false,
    error:      e.message
  }
end

.build_checkout_for(&block) ⇒ Object



43
44
45
# File 'lib/quick_travel/checkout.rb', line 43

def self.build_checkout_for(&block)
  new(attributes_for(&block))
end

.client_token(data) ⇒ Object



15
16
17
# File 'lib/quick_travel/checkout.rb', line 15

def self.client_token(data)
  get_and_validate('/api/checkouts/client_token.json', data).symbolize_keys
end

.create(data) ⇒ Object

Create a checkout in QuickTravel



20
21
22
# File 'lib/quick_travel/checkout.rb', line 20

def self.create(data)
  build_checkout_for { post_and_validate('/api/checkouts.json', data) }
end

.status(id) ⇒ Object

Poll for status of checkout

While in progress returns:

progress: 'processing'

When complete:

progress: 'completed'

When failed, status :ok

successful: false
error: 'Reason for failure'


35
36
37
# File 'lib/quick_travel/checkout.rb', line 35

def self.status(id)
  build_checkout_for { get_and_validate("/api/checkouts/#{id}.json?v=1") }
end

.update(id, data) ⇒ Object



39
40
41
# File 'lib/quick_travel/checkout.rb', line 39

def self.update(id, data)
  build_checkout_for { put_and_validate("/api/checkouts/#{id}.json", data) }
end

Instance Method Details

#completed?Boolean

These methods are a bit ewwwwy… We really have a tri-state: FAILED, SUCCEEDED, PROCESSING

…in process of fixing up users of this class

Returns:

  • (Boolean)


69
70
71
# File 'lib/quick_travel/checkout.rb', line 69

def completed?
  @completed
end

#idObject



61
62
63
# File 'lib/quick_travel/checkout.rb', line 61

def id
  @checkout_id # This is the key QT returns
end

#successful?Boolean

Successful OR processing… OMG.. :(

Returns:

  • (Boolean)


74
75
76
# File 'lib/quick_travel/checkout.rb', line 74

def successful?
  @successful
end

#to_hObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/quick_travel/checkout.rb', line 82

def to_h
  {
    checkout_id:   id,
    progress:      progress,
    completed:     completed?,
    successful:    successful?,
    requires_poll: requires_poll,
    redirect_url:  redirect_url,
    error:         error
  }
end

#unsuccessful?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/quick_travel/checkout.rb', line 78

def unsuccessful?
  completed? && !successful?
end