trophonius 1.1.6 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d1c4de653b4f48f81cf9ea3b8ced6ffce09cd7583d2b23b0fe17de97cfa2e9d
4
- data.tar.gz: 0c41d739871de40caabf04ed8fd2575a39320b839fe0064b6bdc616722ef0eef
3
+ metadata.gz: b838d18be991c51c6c3190b0a09c552852778726a803ea0cb5a9186e542fb63a
4
+ data.tar.gz: 36f06cb81b17c474e73684d9f42260a31ff3658c9607e6dbb3a1412ef2749079
5
5
  SHA512:
6
- metadata.gz: f232aaf25f6a72e0e8e2779eae555f4e467ff013e436d0cc18e2cd9510e523ecace64e0e93b9a93d14b121d3ef071954410e9649ee88434274a3e8a39eba7dc1
7
- data.tar.gz: 2c90e1062b92195726377d4a7a07efb1a365a5916d8a35c90dbfddd12f8aaf49c2c84bf8c3954a34708d93a3abf920d9c3138be8acd32b3a749726fd2fcf7589
6
+ metadata.gz: 043f1c4dc5584aa2627c0a0a6c438f5519b9797223025e5997fc7d0f6f4e1adf0889a9d5d4e41dcecab0049b48f468978f7edaa249ef4ebc7007e16426cb8543
7
+ data.tar.gz: c7493bb44cf5a9d87ad78d4b82fa61d61272aa8dc6eed200d9e20d82bbf0079265f86c2000e4585ff09259253e47802476c14595cf878739933c9a220a7f7b49
@@ -19,5 +19,6 @@ module Trophonius
19
19
  config_accessor(:all_fields) { {} }
20
20
  config_accessor(:translations) { {} }
21
21
  config_accessor(:local_network) { false }
22
+ config_accessor(:redis_connection) { false }
22
23
  end
23
24
  end
@@ -1,6 +1,7 @@
1
+ require 'time'
1
2
  require 'base64'
2
3
  require 'typhoeus'
3
-
4
+ require 'trophonius_redis_manager'
4
5
  module Trophonius
5
6
  module Trophonius::Connection
6
7
  ##
@@ -9,9 +10,16 @@ module Trophonius
9
10
  # @return [String] the *token* used to connect with the FileMaker data api
10
11
 
11
12
  def self.connect
12
- @token = setup_connection
13
- @last_connection = Time.now
14
- @token
13
+ if Trophonius.config.redis_connection
14
+ Trophonius::RedisManager.set_key(key: 'token', value: setup_connection)
15
+ Trophonius::RedisManager.set_key(key: 'last_connection', value: Time.now)
16
+ token = Trophonius::RedisManager.get_key(key: 'token')
17
+ token
18
+ else
19
+ @token = setup_connection
20
+ @last_connection = Time.now
21
+ @token
22
+ end
15
23
  end
16
24
 
17
25
  ##
@@ -20,28 +28,51 @@ module Trophonius
20
28
  # @return [String] the *token* used to connect with the FileMaker data api if successful
21
29
 
22
30
  def self.setup_connection
23
- @token = ''
31
+ if Trophonius.config.redis_connection
32
+ Trophonius::RedisManager.set_key(key: 'token', value: '')
33
+ Trophonius::RedisManager.set_key(key: 'last_connection', value: nil)
34
+ else
35
+ @token = ''
36
+ end
24
37
  ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
25
38
  ssl_verifypeer = !Trophonius.config.local_network
26
- url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/sessions")
27
- request = Typhoeus::Request.new(
28
- url,
29
- method: :post,
30
- body: Trophonius.config.external_name.empty? ? {} : { fmDataSource: [{ database: Trophonius.config.external_name, username: Trophonius.config.external_username, password: Trophonius.config.external_password }] }.to_json,
31
- params: {},
32
- ssl_verifyhost: ssl_verifyhost,
33
- ssl_verifypeer: ssl_verifypeer,
34
- headers: { 'Content-Type' => 'application/json', Authorization: "Basic #{Base64.strict_encode64("#{Trophonius.config.username}:#{Trophonius.config.password}")}" }
35
- )
39
+ url =
40
+ URI(
41
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/sessions"
42
+ )
43
+ request =
44
+ Typhoeus::Request.new(
45
+ url,
46
+ method: :post,
47
+ body:
48
+ if Trophonius.config.external_name.empty?
49
+ {}
50
+ else
51
+ {
52
+ fmDataSource: [
53
+ {
54
+ database: Trophonius.config.external_name,
55
+ username: Trophonius.config.external_username,
56
+ password: Trophonius.config.external_password
57
+ }
58
+ ]
59
+ }.to_json
60
+ end,
61
+ params: {},
62
+ ssl_verifyhost: ssl_verifyhost,
63
+ ssl_verifypeer: ssl_verifypeer,
64
+ headers: {
65
+ 'Content-Type' => 'application/json',
66
+ Authorization: "Basic #{Base64.strict_encode64("#{Trophonius.config.username}:#{Trophonius.config.password}")}"
67
+ }
68
+ )
36
69
  temp = request.run
37
70
 
38
71
  begin
39
72
  parsed = JSON.parse(temp.response_body)
40
- if parsed['messages'][0]['code'] != '0'
41
- Error.throw_error(response['messages'][0]['code'])
42
- end
73
+ Error.throw_error(response['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
43
74
  return parsed['response']['token']
44
- rescue Exception
75
+ rescue Exception => e
45
76
  Error.throw_error('1631')
46
77
  end
47
78
  end
@@ -50,29 +81,35 @@ module Trophonius
50
81
  # Returns the last received token
51
82
  # @return [String] the last valid *token* used to connect with the FileMaker data api
52
83
  def self.token
53
- @token
84
+ token = Trophonius::RedisManager.get_key(key: 'token')
85
+ return Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: 'token') : @token
54
86
  end
55
87
 
56
88
  ##
57
89
  # Returns the receive time of the last received token
58
90
  # @return [Time] Returns the receive time of the last received token
59
91
  def self.last_connection
60
- @last_connection
92
+ last = Trophonius::RedisManager.get_key(key: 'last_connection')
93
+ last = last.nil? ? nil : Time.parse(last)
94
+ Trophonius.config.redis_connection ? last : @last_connection
61
95
  end
62
96
 
63
97
  ##
64
98
  # Tests whether the FileMaker token is still valid
65
99
  # @return [Boolean] True if the token is valid False if invalid
66
100
  def self.test_connection
67
- 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")
68
- begin
69
- request = Typhoeus::Request.new(
70
- url,
71
- method: :get,
72
- body: {},
73
- params: {},
74
- headers: { 'Content-Type' => 'application/json', Authorization: "Bearer #{@token}" }
101
+ url =
102
+ URI(
103
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
104
+ Trophonius.config.layout_name
105
+ }/records?_limit=1"
75
106
  )
107
+ begin
108
+ request =
109
+ Typhoeus::Request.new(
110
+ url,
111
+ method: :get, body: {}, params: {}, headers: { 'Content-Type' => 'application/json', Authorization: "Bearer #{@token}" }
112
+ )
76
113
  temp = request.run
77
114
  JSON.parse(temp.response_body)['messages'][0]['code'] == '0'
78
115
  rescue StandardError
@@ -84,10 +121,10 @@ module Trophonius
84
121
  # Returns whether the current connection is still valid
85
122
  # @return [Boolean] True if the connection is valid False if invalid
86
123
  def self.valid_connection?
87
- if test_connection == false
124
+ if Trophonius.config.layout_name != '' && test_connection == false
88
125
  false
89
126
  else
90
- @last_connection.nil? ? false : (((Time.now - last_connection) / 60).round <= 15 || test_connection)
127
+ last_connection.nil? ? false : (((Time.now - last_connection) / 60).round <= 15 || test_connection)
91
128
  end
92
129
  end
93
130
  end
@@ -0,0 +1,34 @@
1
+ module Trophonius
2
+ module Trophonius::RedisManager
3
+ def self.connect
4
+ if ENV['REDIS_URL'] && ENV['REDIS_URL'] != ''
5
+ @redis ||= Redis.new(url: ENV['REDIS_URL'])
6
+ else
7
+ @redis ||= Redis.new
8
+ end
9
+ end
10
+
11
+ def self.key_exists?(key:)
12
+ connect unless connected?
13
+ !(@redis.get(key).nil? || @redis.get(key).empty?)
14
+ end
15
+
16
+ def self.get_key(key:)
17
+ connect unless connected?
18
+ @redis.get(key)
19
+ end
20
+
21
+ def self.set_key(key:, value:)
22
+ connect unless connected?
23
+ @redis.set(key, value)
24
+ end
25
+
26
+ def self.connected?
27
+ @redis.nil? == false && @redis.connected?
28
+ end
29
+
30
+ def self.disconnect
31
+ @redis.disconnect!
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trophonius
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kempen Automatisering
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '3.0'
33
+ version: '4.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '3.0'
40
+ version: '4.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activesupport
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +67,7 @@ files:
67
67
  - lib/trophonius_query.rb
68
68
  - lib/trophonius_record.rb
69
69
  - lib/trophonius_recordset.rb
70
+ - lib/trophonius_redis_manager.rb
70
71
  - lib/trophonius_request.rb
71
72
  homepage: https://github.com/Willem-Jan/Trophonius
72
73
  licenses:
@@ -87,8 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  - !ruby/object:Gem::Version
88
89
  version: '0'
89
90
  requirements: []
90
- rubyforge_project:
91
- rubygems_version: 2.7.9
91
+ rubygems_version: 3.1.2
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Link between Ruby (on Rails) and FileMaker.