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.



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

def initialize(token:)
  @token = token
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Class Method Details

.createObject



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

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

TODO: Check if it’s not the correct one already or catch Data validation failed. [“fieldName”=>nil, “errNo”=>-5213, “errMsg”=>“Same franchise ID supplied.”]



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

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 Vng::Error => e
  # TODO: improve: ignore if the token was already assigned to the franchise
  raise unless e.message.include? 'Same franchise ID supplied'
end

#destroyObject



42
43
44
45
46
47
48
49
# File 'lib/vng/security_token.rb', line 42

def destroy
  query = { securityToken: @token }
  self.class.request path: '/api/v1/security/logout/', query: query
rescue Vng::Error => e
  p "Vng:Error! #{e}"
  # TODO: improve: ignore if the token was already destroyed
  raise unless e.message.include?('Session expired') || e.message.include?('Session does not exist')
end