Module: Trophonius::Connection
- Defined in:
- lib/trophonius_connection.rb
Class Method Summary collapse
-
.connect ⇒ String
Creates a new connection to FileMaker.
-
.disconnect ⇒ Object
Disconnects from the FileMaker server.
-
.last_connection ⇒ Time
Returns the receive time of the last received token.
-
.setup_connection ⇒ String
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.
-
.test_connection ⇒ Boolean
Tests whether the FileMaker token is still valid.
-
.token ⇒ String
Returns the last received token.
-
.valid_connection? ⇒ Boolean
Returns whether the current connection is still valid.
Class Method Details
.connect ⇒ String
Creates a new connection to FileMaker
12 13 14 15 16 17 18 19 20 21 22 |
# 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) Trophonius::RedisManager.get_key(key: 'token') else @token = setup_connection @last_connection = Time.now @token end end |
.disconnect ⇒ Object
Disconnects from the FileMaker server
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 119 120 121 122 123 124 |
# File 'lib/trophonius_connection.rb', line 90 def self.disconnect 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) 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 true end |
.last_connection ⇒ Time
Returns the receive time of the last received token
136 137 138 139 140 |
# File 'lib/trophonius_connection.rb', line 136 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_connection ⇒ String
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
29 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 81 82 83 84 85 |
# File 'lib/trophonius_connection.rb', line 29 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 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" ) ) 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 body = temp.response_body headers = temp.headers code = temp.code begin parsed = JSON.parse(body) rescue Exception => e puts e puts e.backtrace Error.throw_error('1631') end Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0' parsed['response']['token'] end |
.test_connection ⇒ Boolean
Tests whether the FileMaker token is still valid
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/trophonius_connection.rb', line 145 def self.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 }/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_parsed = JSON.parse(temp.response_body) json_parsed['messages'][0]['code'] == '0' rescue StandardError => e puts e puts e.backtrace false end end |
.token ⇒ String
Returns the last received token
129 130 131 |
# File 'lib/trophonius_connection.rb', line 129 def self.token Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: 'token') : @token end |
.valid_connection? ⇒ Boolean
Returns whether the current connection is still valid
175 176 177 178 179 180 181 |
# File 'lib/trophonius_connection.rb', line 175 def self.valid_connection? if Trophonius.config.layout_name != '' && test_connection == false false else last_connection.nil? ? false : test_connection end end |