Class: Vng::SecurityToken

Inherits:
Resource show all
Defined in:
lib/vng/security_token.rb

Overview

Provides methods to interact with Vonigo work security tokens.

Constant Summary collapse

PATH =
'/api/v1/security/login/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:) ⇒ SecurityToken

Returns a new instance of SecurityToken.



11
12
13
# File 'lib/vng/security_token.rb', line 11

def initialize(token:)
  @token = token
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/vng/security_token.rb', line 9

def token
  @token
end

Class Method Details

.createObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vng/security_token.rb', line 15

def self.create
  body = {
    app_version: '1',
    company: 'Vonigo',
    username: Vng.configuration.username,
    password: Digest::MD5.hexdigest(Vng.configuration.password),
  }

  data = request path: PATH, body: body, include_security_token: false

  new token: data['securityToken']
end

Instance Method Details

#assign_to(franchise_id:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vng/security_token.rb', line 28

def assign_to(franchise_id:)
  body = {
    securityToken: @token,
    method: "2",
    franchiseID: franchise_id,
  }

  self.class.request path: '/api/v1/security/session/', body: body, include_security_token: false
rescue Error => e
  raise unless e.message.include? 'Same franchise ID supplied'
end

#destroyObject



40
41
42
43
44
45
# File 'lib/vng/security_token.rb', line 40

def destroy
  query = { securityToken: @token }
  self.class.request path: '/api/v1/security/logout/', query: query
rescue Error => e
  raise unless e.message.include?('Session expired') || e.message.include?('Session does not exist')
end