trophonius 2.1 → 2.1.7

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: 386e8ae48aba1dd8e6898dcbc16dde02e4acd506658676a146ddc646d96d2929
4
- data.tar.gz: 59c8f91b771e3175347f9fa6fc87b522878e3c2cb9f1cb468f3a78b7cf85c616
3
+ metadata.gz: b7e8354023ac2793edf55b987cb32151705e4e36d173e4ce543e85f18cb54cea
4
+ data.tar.gz: b8445ef34040269df2bcb9d6183359fa36107410126d2065707a76b768188125
5
5
  SHA512:
6
- metadata.gz: beca517abee0fd0db44a9e1cc96f16a5014e061101b8818aaa7f368ac597eecff3aaf4968f3ddd063732a2df36f7c8fa841d322b46599062b0a45b57887ae0d3
7
- data.tar.gz: 71b436a45681c37c4bd6b8f3ce34d35e0cb11badb4356a32b2f1be8ebf7eb29af21c88e51a41d5166f2762bee0af4773a628d28f0cd1f7371ab7c45bc4873d24
6
+ metadata.gz: ab653916b39228ed1de20c164d7aa9a1c1fd151ab1b9dac251efd827561e974b832c7ebbb080e2dc18719752ad2a6f2a9684abe32a088499fb4c934ac1797a6b
7
+ data.tar.gz: fcbb644e94eb52245bc2300f4b13307f2f707a451c2d1826fc94c4d1daaa9f02a800a1d40d5ff1d0a935603ee022d59bfbe6b0a1355145bec6175c00b2b9ff67
data/lib/config.rb CHANGED
@@ -1,7 +1,3 @@
1
- # require 'time'
2
- # require 'date_time'
3
- # require 'date'
4
-
5
1
  require 'active_support/configurable'
6
2
  require 'ethon'
7
3
 
@@ -27,7 +23,9 @@ module Trophonius
27
23
  config_accessor(:belongs_to_relations) { {} }
28
24
  config_accessor(:local_network) { false }
29
25
  config_accessor(:redis_connection) { false }
26
+ config_accessor(:redis_no_verify) { false }
30
27
  config_accessor(:pool_size) { 5 }
31
28
  config_accessor(:debug) { false }
29
+ config_accessor(:callbacks) { { before_create: [], before_update: [], before_destroy: [], after_create: [], after_update: [], after_destroy: [] } }
32
30
  end
33
31
  end
@@ -2,9 +2,11 @@ require 'time'
2
2
  require 'base64'
3
3
  require 'securerandom'
4
4
  require 'connectors/redis_manager'
5
+ require 'debug_printer'
5
6
 
6
7
  module Trophonius
7
8
  class Connection
9
+ include DebugPrinter
8
10
  attr_reader :id
9
11
 
10
12
  def initialize
@@ -24,6 +26,49 @@ module Trophonius
24
26
  end
25
27
  end
26
28
 
29
+ ##
30
+ # Disconnects from the FileMaker server
31
+ #
32
+ def disconnect
33
+ uri = URI::RFC2396_Parser.new
34
+ url =
35
+ URI(
36
+ uri.escape(
37
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
38
+ Trophonius.config.database
39
+ }/sessions/#{Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: 'token') : @token}"
40
+ )
41
+ )
42
+ ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
43
+ ssl_verifypeer = !Trophonius.config.local_network
44
+
45
+ request =
46
+ Typhoeus::Request.new(
47
+ url,
48
+ method: :delete,
49
+ params: {},
50
+ ssl_verifyhost: ssl_verifyhost,
51
+ ssl_verifypeer: ssl_verifypeer,
52
+ headers: { 'Content-Type' => 'application/json' }
53
+ )
54
+ temp = request.run
55
+
56
+ begin
57
+ parsed = JSON.parse(temp.response_body)
58
+
59
+ DebugPrinter.print_debug('RECEIVED DISCONNECT', parsed)
60
+ rescue StandardError => e
61
+ puts e
62
+ puts e.backtrace
63
+ Error.throw_error('1631')
64
+ end
65
+ Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
66
+ Trophonius::RedisManager.disconnect! if Trophonius.config.redis_connection
67
+ @token = nil
68
+ @last_connection = nil
69
+ true
70
+ end
71
+
27
72
  private
28
73
 
29
74
  ##
@@ -113,47 +158,6 @@ module Trophonius
113
158
  parsed['response']['token']
114
159
  end
115
160
 
116
- ##
117
- # Disconnects from the FileMaker server
118
- #
119
- def disconnect
120
- uri = URI::RFC2396_Parser.new
121
- url =
122
- URI(
123
- uri.escape(
124
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
125
- Trophonius.config.database
126
- }/sessions/#{Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: 'token') : @token}"
127
- )
128
- )
129
- ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
130
- ssl_verifypeer = !Trophonius.config.local_network
131
-
132
- request =
133
- Typhoeus::Request.new(
134
- url,
135
- method: :delete,
136
- params: {},
137
- ssl_verifyhost: ssl_verifyhost,
138
- ssl_verifypeer: ssl_verifypeer,
139
- headers: { 'Content-Type' => 'application/json' }
140
- )
141
- temp = request.run
142
-
143
- begin
144
- parsed = JSON.parse(temp.response_body)
145
- rescue StandardError => e
146
- puts e
147
- puts e.backtrace
148
- Error.throw_error('1631')
149
- end
150
- Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
151
- Trophonius::RedisManager.disconnect! if Trophonius.config.redis_connection
152
- @token = nil
153
- @last_connection = nil
154
- true
155
- end
156
-
157
161
  ##
158
162
  # Returns the receive time of the last received token
159
163
  # @return [Time] Returns the receive time of the last received token
@@ -167,7 +171,7 @@ module Trophonius
167
171
  # Tests whether the FileMaker token is still valid
168
172
  # @return [Boolean] True if the token is valid False if invalid
169
173
  def test_connection
170
- return last_connection.nil? || last_connection < Time.now - (15 * 60) if Trophonius.config.layout_name == ''
174
+ return !last_connection.nil? && last_connection > Time.now - (15 * 60) if Trophonius.config.layout_name == ''
171
175
 
172
176
  path = "/layouts/#{Trophonius.config.layout_name}/records?_limit=1"
173
177
  response =
@@ -1,10 +1,14 @@
1
+ require 'debug_printer'
1
2
  module Trophonius
2
3
  class ConnectionManager
4
+ include DebugPrinter
3
5
  def initialize
4
6
  @connections = {}
5
7
  Trophonius.config.pool_size.times do
6
8
  connection = Connection.new
7
9
  @connections[connection.id] = { connection: connection, queue: [] }
10
+
11
+ DebugPrinter.print_debug('CONNECTION CREATED', @connections[connection.id].inspect)
8
12
  end
9
13
  end
10
14
 
@@ -22,6 +26,10 @@ module Trophonius
22
26
  nil
23
27
  end
24
28
 
29
+ def disconnect_all
30
+ @connections.each { |_connection_id, connection| connection[:connection].disconnect }
31
+ end
32
+
25
33
  private
26
34
 
27
35
  def auth_header_bearer(id)
@@ -2,14 +2,15 @@ module Trophonius
2
2
  # the RedisManager module is used to create a (single) connection to a redis store.
3
3
  module Trophonius::RedisManager
4
4
  def self.connect
5
- if Trophonius.config.redis_connection
6
- if ENV['REDIS_URL'] && ENV['REDIS_URL'] != ''
7
- @redis ||= Redis.new(url: ENV['REDIS_URL'])
8
- else
9
- @redis ||= Redis.new
10
- end
11
- end
12
- return nil
5
+ return unless Trophonius.config.redis_connection
6
+
7
+ redis_url = ENV.fetch('REDIS_URL')
8
+ options = {}
9
+ options.merge!(url: redis_url) if redis_url && redis_url != ''
10
+ options.merge!(ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }) if Trophonius.config.redis_no_verify
11
+ @redis ||= Redis.new(options)
12
+
13
+ nil
13
14
  end
14
15
 
15
16
  ##
data/lib/error.rb CHANGED
@@ -18,6 +18,7 @@ module Trophonius
18
18
  class FileError < StandardError; end # :nodoc:
19
19
  class CommandError < StandardError; end # :nodoc:
20
20
  class ConnectionError < StandardError; end # :nodoc:
21
+ class FileNotOpenError < StandardError; end # :nodoc:
21
22
  class EmptyFindError < StandardError; end # :nodoc:
22
23
  class ValidationError < StandardError; end # :nodoc:
23
24
  class DateValueError < ValidationError; end # :nodoc:
@@ -223,7 +224,9 @@ module Trophonius
223
224
  # when "738"
224
225
  # when "800"
225
226
  # when "801"
226
- # when "802"
227
+ when '802'
228
+ when '802'
229
+ raise FileNotOpenError.new, 'Could not open FileMaker file on server'
227
230
  # when "803"
228
231
  # when "804"
229
232
  # when "805"
data/lib/fm_date.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'date'
2
+
1
3
  class Date
2
4
  def to_fm
3
5
  strftime('%m/%d/%Y')
data/lib/fm_date_time.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'date'
2
+
1
3
  class DateTime
2
4
  def self.parse_fm_timestamp(timestamp)
3
5
  DateTime.strptime(timestamp, '%m/%d/%Y %H:%M:%S')
data/lib/fm_time.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'date'
2
+ require 'time'
3
+
1
4
  class Time
2
5
  def to_fm
3
6
  strftime('%m-%d-%Y %H:%M:%S')
data/lib/model.rb CHANGED
@@ -37,10 +37,89 @@ module Trophonius
37
37
  @configuration.translations = {}
38
38
  @configuration.has_many_relations = {}
39
39
  @configuration.belongs_to_relations = {}
40
+ @configuration.callbacks = { before_create: [], before_update: [], before_destroy: [], after_create: [], after_update: [], after_destroy: [] }
40
41
  @offset = ''
41
42
  @limit = ''
42
43
  end
43
44
 
45
+ def self.scope(name, procedure, *_args)
46
+ define_singleton_method(name) do |*args|
47
+ procedure.arity.zero? ? procedure.call : procedure.call(*args)
48
+ end
49
+ end
50
+
51
+ def self.after_create(procedure, args)
52
+ @configuration.callbacks[:after_create].push({ name: procedure, args: args })
53
+ end
54
+
55
+ def self.run_after_create
56
+ @configuration.callbacks[:after_create].each do |callback|
57
+ procedure = callback[:name]
58
+ args = callback[:args]
59
+ procedure.is_a?(Proc) ? procedure.call(*args) : send(procedure, *args)
60
+ end
61
+ end
62
+
63
+ def self.after_update(procedure, args)
64
+ @configuration.callbacks[:after_update].push({ name: procedure, args: args })
65
+ end
66
+
67
+ def self.run_after_update
68
+ @configuration.callbacks[:after_update].each do |callback|
69
+ procedure = callback[:name]
70
+ args = callback[:args]
71
+ procedure.is_a?(Proc) ? procedure.call(*args) : send(procedure, *args)
72
+ end
73
+ end
74
+
75
+ def self.after_destroy(procedure, args)
76
+ @configuration.callbacks[:after_destroy].push({ name: procedure, args: args })
77
+ end
78
+
79
+ def self.run_after_destroy
80
+ @configuration.callbacks[:after_destroy].each do |callback|
81
+ procedure = callback[:name]
82
+ args = callback[:args]
83
+ procedure.is_a?(Proc) ? procedure.call(*args) : send(procedure, *args)
84
+ end
85
+ end
86
+
87
+ def self.before_create(procedure, args)
88
+ @configuration.callbacks[:before_create].push({ name: procedure, args: args })
89
+ end
90
+
91
+ def self.run_before_create
92
+ @configuration.callbacks[:before_create].each do |callback|
93
+ procedure = callback[:name]
94
+ args = callback[:args]
95
+ procedure.is_a?(Proc) ? procedure.call(*args) : send(procedure, *args)
96
+ end
97
+ end
98
+
99
+ def self.before_update(procedure, args)
100
+ @configuration.callbacks[:before_update].push({ name: procedure, args: args })
101
+ end
102
+
103
+ def self.run_before_update
104
+ @configuration.callbacks[:before_update].each do |callback|
105
+ procedure = callback[:name]
106
+ args = callback[:args]
107
+ procedure.is_a?(Proc) ? procedure.call(*args) : send(procedure, *args)
108
+ end
109
+ end
110
+
111
+ def self.before_destroy(procedure, args)
112
+ @configuration.callbacks[:before_destroy].push({ name: procedure, args: args })
113
+ end
114
+
115
+ def self.run_before_destroy
116
+ @configuration.callbacks[:before_destroy].each do |callback|
117
+ procedure = callback[:name]
118
+ args = callback[:args]
119
+ procedure.is_a?(Proc) ? procedure.call(*args) : send(procedure, *args)
120
+ end
121
+ end
122
+
44
123
  ##
45
124
  # Add a belongs to relationship.
46
125
  #
@@ -190,6 +269,7 @@ module Trophonius
190
269
  # Model.create(fieldOne: "Data")
191
270
  def self.create(field_data, portal_data: {})
192
271
  create_translations if @configuration.translations.keys.empty?
272
+ run_before_create
193
273
 
194
274
  field_data.transform_keys! { |k| (@configuration.translations[k.to_s] || k).to_s }
195
275
 
@@ -209,6 +289,8 @@ module Trophonius
209
289
  new_record = DatabaseRequest.make_request("/layouts/#{layout_name}/records/#{response['response']['recordId']}", 'get', '{}')
210
290
  record = build_result(new_record['response']['data'][0])
211
291
  record.send(:define_singleton_method, 'result_count') { 1 }
292
+ run_after_create
293
+
212
294
  record
213
295
  end
214
296
 
@@ -268,6 +350,7 @@ module Trophonius
268
350
 
269
351
  url = "layouts/#{layout_name}/records/#{record_id}"
270
352
  response = DatabaseRequest.make_request(url, 'delete', '{}')
353
+
271
354
  if response['messages'][0]['code'] == '0'
272
355
  true
273
356
  else
data/lib/record.rb CHANGED
@@ -22,13 +22,17 @@ module Trophonius
22
22
  @modifiable_fields = {}
23
23
  @modified_fields = {}
24
24
  @model_name = model
25
- @model = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(model_name)))
25
+ @model = model_name.instance_of?(String) ? ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(model_name))) : model_name
26
26
  @layout_name = @model.layout_name
27
27
  define_field_methods(fm_record)
28
28
  define_portal_methods(fm_record)
29
29
  super()
30
30
  end
31
31
 
32
+ def to_param
33
+ record_id.to_s
34
+ end
35
+
32
36
  def []=(field, new_val)
33
37
  modifiable_fields[field] = new_val
34
38
  modified_fields[field] = new_val
@@ -182,8 +186,10 @@ module Trophonius
182
186
  def save
183
187
  url = "layouts/#{layout_name}/records/#{record_id}"
184
188
 
189
+ @model.run_before_update
185
190
  body = "{\"fieldData\": #{modified_fields.to_json}}"
186
191
  response = DatabaseRequest.make_request(url, 'patch', body)
192
+ @model.run_after_update
187
193
  response['messages'][0]['code'] == '0' ? true : Error.throw_error(response['messages'][0]['code'])
188
194
  end
189
195
 
@@ -195,7 +201,9 @@ module Trophonius
195
201
  def delete
196
202
  url = "layouts/#{layout_name}/records/#{record_id}"
197
203
 
204
+ @model.run_before_destroy
198
205
  response = DatabaseRequest.make_request(url, 'delete', '{}')
206
+ @model.run_after_destroy
199
207
  response['messages'][0]['code'] == '0' ? true : Error.throw_error(response['messages'][0]['code'])
200
208
  end
201
209
 
@@ -210,6 +218,7 @@ module Trophonius
210
218
  url = "layouts/#{layout_name}/records/#{record_id}"
211
219
  field_data.each_key { |field| modifiable_fields[field] = field_data[field] }
212
220
  field_data.transform_keys! { |k| (@model.translations[k.to_s] || k).to_s }
221
+ @model.run_before_update
213
222
 
214
223
  portal_data.each do |portal_name, values|
215
224
  values.map do |record|
@@ -232,6 +241,7 @@ module Trophonius
232
241
  return throw_field_missing(field_data) if code == '102'
233
242
  return Error.throw_error(code) if code != '0'
234
243
 
244
+ @model.run_after_update
235
245
  true
236
246
  end
237
247
 
data/lib/single.rb CHANGED
@@ -13,6 +13,14 @@ module Trophonius
13
13
  @all_fields = {}
14
14
  end
15
15
 
16
+ def non_modifiable_fields
17
+ []
18
+ end
19
+
20
+ def layout_name
21
+ @config[:layout_name]
22
+ end
23
+
16
24
  def where(fieldData)
17
25
  uri = URI::RFC2396_Parser.new
18
26
  url =
@@ -114,7 +122,7 @@ module Trophonius
114
122
  private
115
123
 
116
124
  def build_result(result)
117
- hash = Trophonius::Record.new(result, 'Single')
125
+ record = Trophonius::Record.new(result, self)
118
126
  record.layout_name = @config[:layout_name]
119
127
  record
120
128
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trophonius
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.1'
4
+ version: 2.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kempen Automatisering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-21 00:00:00.000000000 Z
11
+ date: 2024-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis