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



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/trophonius_connection.rb', line 12

def self.connect
  if Trophonius.config.redis_connection
    Trophonius::RedisManager.set_key(key: 'token', value: setup_connection)
    Trophonius::RedisManager.set_key(key: 'last_connection', value: Time.now)
    token = Trophonius::RedisManager.get_key(key: 'token')
    token
  else
    @token = setup_connection
    @last_connection = Time.now
    @token
  end
end

.last_connectionTime

Returns the receive time of the last received token

Returns:

  • (Time)

    Returns the receive time of the last received token



90
91
92
93
94
# File 'lib/trophonius_connection.rb', line 90

def self.last_connection
  last = Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: 'last_connection') : nil
  last = last.nil? ? nil : Time.parse(last)
  Trophonius.config.redis_connection ? last : @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



30
31
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
73
74
75
76
77
78
# File 'lib/trophonius_connection.rb', line 30

def self.setup_connection
  if Trophonius.config.redis_connection
    Trophonius::RedisManager.set_key(key: 'token', value: '')
    Trophonius::RedisManager.set_key(key: 'last_connection', value: nil)
  else
    @token = ''
  end
  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:
        if Trophonius.config.external_name.empty?
          {}
        else
          {
            fmDataSource: [
              {
                database: Trophonius.config.external_name,
                username: Trophonius.config.external_username,
                password: Trophonius.config.external_password
              }
            ]
          }.to_json
        end,
      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)
  rescue Exception => e
    Error.throw_error('1631')
  end
  Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
  return parsed['response']['token']
end

.test_connectionBoolean

Tests whether the FileMaker token is still valid

Returns:

  • (Boolean)

    True if the token is valid False if invalid



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/trophonius_connection.rb', line 99

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



83
84
85
# File 'lib/trophonius_connection.rb', line 83

def self.token
  return Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: '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



122
123
124
125
126
127
128
# File 'lib/trophonius_connection.rb', line 122

def self.valid_connection?
  if Trophonius.config.layout_name != '' && test_connection == false
    false
  else
    last_connection.nil? ? false : (((Time.now - last_connection) / 60).round <= 15 || test_connection)
  end
end