Class: ShakeTheCounter::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/shake_the_counter/client.rb

Overview

Sets up a client to work with

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Client

There are two ways to setup a new client:

  1. Initialize with options ‘(Client.new(client_id: x)`)

  2. Initilize without options, get options from config



16
17
18
19
20
21
22
23
24
25
# File 'lib/shake_the_counter/client.rb', line 16

def initialize(args=nil)
  if args.nil?
    init! 
    reset!
  else
    args.each do |key,value|
      instance_variable_set("@#{key}", value)
    end
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/shake_the_counter/client.rb', line 6

def id
  @id
end

#language_codeObject

Returns the value of attribute language_code.



8
9
10
# File 'lib/shake_the_counter/client.rb', line 8

def language_code
  @language_code
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



5
6
7
# File 'lib/shake_the_counter/client.rb', line 5

def refresh_token
  @refresh_token
end

#secretObject

Returns the value of attribute secret.



7
8
9
# File 'lib/shake_the_counter/client.rb', line 7

def secret
  @secret
end

Instance Method Details

#access_tokenObject

Retrieves a new authentication token to use for this client or reuse the same one from memory.



48
49
50
# File 'lib/shake_the_counter/client.rb', line 48

def access_token
  @access_token ||= ShakeTheCounter::Authentication.renew_access_token(client_id: id, client_secret: secret, refresh_token: refresh_token)["access_token"]
end

#call(path, http_method: :get, body: {}, header: {}) ⇒ Object

Make an API with access_token



56
57
58
59
60
# File 'lib/shake_the_counter/client.rb', line 56

def call(path, http_method: :get, body: {}, header: {})
  # add bearer token to header
  header[:authorization] = "Bearer #{access_token}"
  return ShakeTheCounter::API.call(path, http_method: http_method, body: body, header: header)
end

#confirm_reservation(reservation_key: '', payment_method: '', amount_paid: nil) ⇒ Object

Confirm the reservation and receive tickets /api/v1/reservation/reservationKey/confirm



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/shake_the_counter/client.rb', line 111

def confirm_reservation(reservation_key: '', payment_method: '', amount_paid: nil)

  # step 1: confirm
  path = "reservation/#{reservation_key}/confirm"
  body = {
    PaymentMethod: payment_method,
    AmountPaid: amount_paid
  }
  call(path, http_method: :post, body: body.to_json)

  # step 2: get tickets
  # GET /api/v1/reservation/{reservationKey}/tickets
  path = "reservation/#{reservation_key}/tickets"
  result = call(path, http_method: :get)

  # define new list
  list = []

  for ticket in result
    list << ShakeTheCounter::Ticket.new(ticket)
  end
  return list
end

#eventsObject

Fetches a list of events for this client

/api/v1/events/available/{languageCode}

Returns:

  • Array



67
68
69
70
71
72
73
74
75
76
# File 'lib/shake_the_counter/client.rb', line 67

def events
  return @events if @events
  @events = []
  path = "events/available/#{language_code}"
  result = call(path, http_method: :get)
  for event in result
    @events << ShakeTheCounter::Event.new(event, client: self)
  end
  return @events
end

#find_reservation(key) ⇒ Object Also known as: reservation

Find a reservation GET /api/v1/reservation/reservationKey

Returns:

  • Reservation



83
84
85
86
87
88
# File 'lib/shake_the_counter/client.rb', line 83

def find_reservation(key)
  path = "reservation/#{key}"
  result = call(path, http_method: :get)
  reservation = ShakeTheCounter::Reservation.new(result)
  return reservation
end

#init!Hash

Set’s the default value’s

Returns:

  • (Hash)

    configuration options



29
30
31
32
33
34
35
36
# File 'lib/shake_the_counter/client.rb', line 29

def init!
  @defaults = {
    :@refresh_token => ShakeTheCounter::Config.refresh_token,
    :@id            => ShakeTheCounter::Config.client_id,
    :@secret        => ShakeTheCounter::Config.client_secret,
    :@language_code => ShakeTheCounter::Config.language_code,
  }
end

#reset!Hash

Resets the value’s to there previous value (instance_variable)

Returns:

  • (Hash)

    configuration options



40
41
42
# File 'lib/shake_the_counter/client.rb', line 40

def reset!
  @defaults.each { |key, value| instance_variable_set(key, value) }
end

#start_payment(reservation_key) ⇒ Object

Send a message to STC that a payment has started.

Returns:

  • String status



96
97
98
99
100
101
102
103
104
# File 'lib/shake_the_counter/client.rb', line 96

def start_payment(reservation_key)
  path = "reservation/#{reservation_key}/payment"
  result = call(path, http_method: :post)
  if result.code.to_i == 200
    return true
  else
    raise ShakeTheCounterError.new "Payment failed"
  end
end