Class: Antaeus::GuestAPIClient

Inherits:
APIClient show all
Defined in:
lib/antaeus-sdk/guest_api_client.rb

Instance Method Summary collapse

Methods inherited from APIClient

#connected?, #delete, #get, #initialize, instance, #patch, #post, #put, #raw, #refresh_token

Constructor Details

This class inherits a constructor from Antaeus::APIClient

Instance Method Details

#authenticate(guest, pin) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/antaeus-sdk/guest_api_client.rb', line 3

def authenticate(guest, pin)
  @guest ||= guest
  begin
    raw_token_data = RestClient.post(
      "#{Antaeus.config.base_url}/guests/login",
      { email: @guest, pin: pin }.to_json,
      content_type: :json,
      accept: :json
    )
    token_data = JSON.load(raw_token_data)
    set_token(@guest, token_data['guest_token'])
    true
  rescue RestClient::Exception => e
    raise Exceptions::AuthenticationFailure, e.response
  end
end

#authenticated?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/antaeus-sdk/guest_api_client.rb', line 38

def authenticated?
  if @guest && @guest_data[@guest] && @guest_data[@guest][:token]
    true
  else
    false
  end
end

#connectObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/antaeus-sdk/guest_api_client.rb', line 46

def connect
  return true if connected?

  if authenticated?
    @rest_client = RestClient::Resource.new(
      Antaeus.config.base_url,
      content_type: :json,
      accept: :json,
      headers: {
        'X-Guest-Token:': @guest_data[@guest][:token]
      }
    )
  else
    raise Exceptions::LoginRequired
  end
end

#get_token(guest) ⇒ Object

Retrieve the token for a given guest



30
31
32
33
34
35
36
# File 'lib/antaeus-sdk/guest_api_client.rb', line 30

def get_token(guest)
  return nil unless @guest
  return nil unless @guest == guest
  return nil unless @guest_data
  return nil unless @guest_data[guest]
  @guest_data[guest][:token] ? @guest_data[guest][:token].dup : nil
end

#set_token(guest, token) ⇒ Object

Set the token in memory for the given guest



21
22
23
24
25
26
27
# File 'lib/antaeus-sdk/guest_api_client.rb', line 21

def set_token(guest, token)
  # this should probably be improved to handle race conditions
  @guest ||= guest
  @guest_data ||= {}
  @guest_data[guest] ||= {}
  @guest_data[guest][:token] = token
end