Class: Trophonius::Connection

Inherits:
Object
  • Object
show all
Includes:
DebugPrinter
Defined in:
lib/connectors/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DebugPrinter

print_debug

Constructor Details

#initializeConnection

Returns a new instance of Connection.



12
13
14
15
16
# File 'lib/connectors/connection.rb', line 12

def initialize
  @id = SecureRandom.uuid
  @token = ''
  connect
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/connectors/connection.rb', line 10

def id
  @id
end

Instance Method Details

#disconnectObject

Disconnects from the FileMaker server



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/connectors/connection.rb', line 32

def disconnect
  return unless test_connection

  uri = URI::RFC2396_Parser.new
  url =
    URI(
      uri.escape(
        "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
          Trophonius.config.database
        }/sessions/#{Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: 'token') : @token}"
      )
    )
  ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
  ssl_verifypeer = !Trophonius.config.local_network

  request =
    Typhoeus::Request.new(
      url,
      method: :delete,
      params: {},
      ssl_verifyhost: ssl_verifyhost,
      ssl_verifypeer: ssl_verifypeer,
      headers: { 'Content-Type' => 'application/json' }
    )
  temp = request.run

  begin
    parsed = JSON.parse(temp.response_body)

    DebugPrinter.print_debug('RECEIVED DISCONNECT', parsed)
  rescue StandardError => e
    puts e
    puts e.backtrace
    Error.throw_error('1631')
  end
  Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
  Trophonius::RedisManager.disconnect! if Trophonius.config.redis_connection
  @token = nil
  @last_connection = nil
  true
end

#tokenString

Returns the last received token

Returns:

  • (String)

    the last valid token used to connect with the FileMaker data api



21
22
23
24
25
26
27
# File 'lib/connectors/connection.rb', line 21

def token
  if valid_connection?
    Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: 'token') : @token
  else
    connect
  end
end