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

.disconnectObject

Disconnects from the FileMaker server



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/trophonius_connection.rb', line 85

def self.disconnect
  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)
  rescue Exception => e
    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
  return true
end

.last_connectionTime

Returns the receive time of the last received token

Returns:

  • (Time)

    Returns the receive time of the last received token



130
131
132
133
134
# File 'lib/trophonius_connection.rb', line 130

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
79
80
# 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(
      URI.escape(
        "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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/trophonius_connection.rb', line 139

def self.test_connection
  url =
    URI(
      URI.escape(
        "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



123
124
125
# File 'lib/trophonius_connection.rb', line 123

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



164
165
166
167
168
169
170
# File 'lib/trophonius_connection.rb', line 164

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