class ShakeTheCounter::Client

Sets up a client to work with

Attributes

id[RW]
language_code[RW]
refresh_token[RW]
secret[RW]

Public Class Methods

new(args=nil) click to toggle source

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

# 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

Public Instance Methods

access_token() click to toggle source

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

# 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: {}) click to toggle source

Make an API with access_token

# 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) click to toggle source

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

@return True (or error)

# 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
events() click to toggle source

Fetches a list of events for this client

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

@return Array

# 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) click to toggle source

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

@return Reservation

# 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
Also aliased as: reservation
init!() click to toggle source

Set's the default value's @return [Hash] configuration options

# 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
reservation(key)
Alias for: find_reservation
reset!() click to toggle source

Resets the value's to there previous value (instance_variable) @return [Hash] configuration options

# 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) click to toggle source

Send a message to STC that a payment has started.

@return String status

# 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