Class: Vng::SecurityToken

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

Overview

Provides methods to interact with Vonigo work security tokens.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, host:) ⇒ SecurityToken

Returns a new instance of SecurityToken.



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

def initialize(token:, host:)
  @token = token
  @host = host
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/vng/security_token.rb', line 4

def token
  @token
end

Class Method Details

.create(host:, username:, password:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vng/security_token.rb', line 11

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

  uri = URI::HTTPS.build host: host, path: '/api/v1/security/login/'

  request = Net::HTTP::Get.new(uri.request_uri)
  request.initialize_http_header 'Content-Type' => 'application/json; charset=utf-8'
  request.body = body.to_json

  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    http.request request
  end

  token = JSON(response.body)['securityToken']

  new token: token, host: host
end

Instance Method Details

#destroyObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vng/security_token.rb', line 35

def destroy
  body = {
    securityToken: @token,
  }

  uri = URI::HTTPS.build host: @host, path: '/api/v1/security/login/'

  request = Net::HTTP::Post.new(uri.request_uri)
  request.initialize_http_header 'Content-Type' => 'application/json'
  request.body = body.to_json

  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    http.request request
  end
end