Module: Trophonius::Connection

Defined in:
lib/trophonius_connection.rb

Class Method Summary collapse

Class Method Details

.connectString

Creates a new connection to FileMaker

Returns:

  • (String)

    the token used to connect with the FileMaker data api



11
12
13
14
15
# File 'lib/trophonius_connection.rb', line 11

def self.connect
  @token = setup_connection
  @last_connection = Time.now
  @token
end

.last_connectionTime

Returns the receive time of the last received token

Returns:

  • (Time)

    Returns the receive time of the last received token



59
60
61
# File 'lib/trophonius_connection.rb', line 59

def self.last_connection
  @last_connection
end

.setup_connectionString

Creates and runs a HTTP request to create a new data api connection This method throws an error when the request returns with a HTTP error or a FileMaker error

Returns:

  • (String)

    the token used to connect with the FileMaker data api if successful



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/trophonius_connection.rb', line 22

def self.setup_connection
  @token = ''
  ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
  ssl_verifypeer = !Trophonius.config.local_network
  url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/sessions")
  request = Typhoeus::Request.new(
    url,
    method: :post,
    body: {},
    params: {},
    ssl_verifyhost: ssl_verifyhost,
    ssl_verifypeer: ssl_verifypeer,
    headers: { 'Content-Type' => 'application/json', Authorization: "Basic #{Base64.strict_encode64("#{Trophonius.config.username}:#{Trophonius.config.password}")}" }
  )
  temp = request.run

  begin
    parsed = JSON.parse(temp.response_body)
    if parsed['messages'][0]['code'] != '0'
      Error.throw_error(response['messages'][0]['code'])
    end
    return parsed['response']['token']
  rescue Exception
    Error.throw_error('1631')
  end
end

.test_connectionBoolean

Tests whether the FileMaker token is still valid

Returns:

  • (Boolean)

    True if the token is valid False if invalid



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/trophonius_connection.rb', line 66

def self.test_connection
  url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{Trophonius.config.layout_name}/records?_limit=1")
  begin
    request = Typhoeus::Request.new(
      url,
      method: :get,
      body: {},
      params: {},
      headers: { 'Content-Type' => 'application/json', Authorization: "Bearer #{@token}" }
    )
    temp = request.run
    JSON.parse(temp.response_body)['messages'][0]['code'] == '0'
  rescue StandardError
    return false
  end
end

.tokenString

Returns the last received token

Returns:

  • (String)

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



52
53
54
# File 'lib/trophonius_connection.rb', line 52

def self.token
  @token
end

.valid_connection?Boolean

Returns whether the current connection is still valid

Returns:

  • (Boolean)

    True if the connection is valid False if invalid



86
87
88
# File 'lib/trophonius_connection.rb', line 86

def self.valid_connection?
  @last_connection.nil? ? false : (((Time.now - last_connection) / 60).round <= 15 || test_connection)
end