trophonius 1.2.4.10 → 1.2.6.4

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: c79101c445307adbbb319a9cf07fed8e2b0fb3ae6d17a213833452ff9252f6cd
4
- data.tar.gz: 21e0bc3a18a443f3576699100c48cf4f1db6b9efe4a031d5c52a26c95a4cb464
3
+ metadata.gz: f64c4285e902e34390d654a1766180aebae9a4c3bb483286d442b02284813390
4
+ data.tar.gz: c85fc16b8bd2179865d3d75709be1b44a15d17aded4ae5ee6a2820bbf16e0488
5
5
  SHA512:
6
- metadata.gz: cfef37d5c0ddca6ac545d76ed5b62df8b8f1514f247cdf6749ab0925ac33a0d889fe6f11ba9a75985164c81e7b8ffb91c24c13df79ba53e4475aa53f3e27ec37
7
- data.tar.gz: 6afb02d5bc0d8d8a33853ffc1eff5f1f7cceb4026a665e74f6ff01d3b7189a616e9cc127b8e9962ea8fa23abb381661d5c8dbb4602588668abf8168d2784be7d
6
+ metadata.gz: 59ba939131882022370de145757b09fc5c3b03775314ee08e298d383d0cf6a2d7796ae98706cf98b806dcd58ed7df5d6fd07656ec811df3b0d136775f5abf926
7
+ data.tar.gz: 2df0e98f4da6fc84658faf26632b65e52574efea36dea08b11131dfd5234b43f3612693efbf5d741e11c7c2fe0f924989ea7496db57e39c01a89c8d57756292a
@@ -1,6 +1,8 @@
1
1
  require 'trophonius_request'
2
2
  require 'trophonius_model'
3
3
  require 'trophonius_config'
4
+ require 'trophonius_date'
5
+ require 'trophonius_time'
4
6
 
5
7
  module Trophonius # :nodoc:
6
8
  def self.configuration
@@ -18,6 +18,8 @@ module Trophonius
18
18
  config_accessor(:non_modifiable_fields) { [] }
19
19
  config_accessor(:all_fields) { {} }
20
20
  config_accessor(:translations) { {} }
21
+ config_accessor(:has_many_relations) { {} }
22
+ config_accessor(:belongs_to_relations) { {} }
21
23
  config_accessor(:local_network) { false }
22
24
  config_accessor(:redis_connection) { false }
23
25
  end
@@ -38,7 +38,9 @@ module Trophonius
38
38
  ssl_verifypeer = !Trophonius.config.local_network
39
39
  url =
40
40
  URI(
41
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/sessions"
41
+ URI.escape(
42
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/sessions"
43
+ )
42
44
  )
43
45
  request =
44
46
  Typhoeus::Request.new(
@@ -77,6 +79,44 @@ module Trophonius
77
79
  return parsed['response']['token']
78
80
  end
79
81
 
82
+ ##
83
+ # Disconnects from the FileMaker server
84
+ #
85
+ def self.disconnect
86
+ url =
87
+ URI(
88
+ URI.escape(
89
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
90
+ Trophonius.config.database
91
+ }/sessions/#{Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: 'token') : @token}"
92
+ )
93
+ )
94
+ ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
95
+ ssl_verifypeer = !Trophonius.config.local_network
96
+
97
+ request =
98
+ Typhoeus::Request.new(
99
+ url,
100
+ method: :delete,
101
+ params: {},
102
+ ssl_verifyhost: ssl_verifyhost,
103
+ ssl_verifypeer: ssl_verifypeer,
104
+ headers: { 'Content-Type' => 'application/json' }
105
+ )
106
+ temp = request.run
107
+
108
+ begin
109
+ parsed = JSON.parse(temp.response_body)
110
+ rescue Exception => e
111
+ Error.throw_error('1631')
112
+ end
113
+ Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
114
+ Trophonius::RedisManager.disconnect! if Trophonius.config.redis_connection
115
+ @token = nil
116
+ @last_connection = nil
117
+ return true
118
+ end
119
+
80
120
  ##
81
121
  # Returns the last received token
82
122
  # @return [String] the last valid *token* used to connect with the FileMaker data api
@@ -99,9 +139,11 @@ module Trophonius
99
139
  def self.test_connection
100
140
  url =
101
141
  URI(
102
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
103
- Trophonius.config.layout_name
104
- }/records?_limit=1"
142
+ URI.escape(
143
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
144
+ Trophonius.config.database
145
+ }/layouts/#{Trophonius.config.layout_name}/records?_limit=1"
146
+ )
105
147
  )
106
148
  begin
107
149
  request =
@@ -0,0 +1,7 @@
1
+ class Date
2
+ def to_fm
3
+ self.strftime('%m/%d/%Y')
4
+ end
5
+
6
+ alias convert_to_fm to_fm
7
+ end
@@ -0,0 +1,5 @@
1
+ class DateTime
2
+ def self.parse_fm_timestamp(timestamp)
3
+ DateTime.strptime(timestamp, '%m/%d/%Y %H:%M:%S')
4
+ end
5
+ end
@@ -19,6 +19,14 @@ module Trophonius
19
19
  class ConnectionError < StandardError; end # :nodoc:
20
20
  class EmptyFindError < StandardError; end # :nodoc:
21
21
  class ValidationError < StandardError; end # :nodoc:
22
+ class DateValueError < ValidationError; end # :nodoc:
23
+ class TimeValueError < ValidationError; end # :nodoc:
24
+ class NumberValueError < ValidationError; end # :nodoc:
25
+ class ValueOutOfRangeError < ValidationError; end # :nodoc:
26
+ class ValueNotUniqueError < ValidationError; end # :nodoc:
27
+ class ValueNotExistingError < ValidationError; end # :nodoc:
28
+ class ValueNotInValuelistError < ValidationError; end # :nodoc:
29
+ class ValueFailedCalculationError < ValidationError; end # :nodoc:
22
30
 
23
31
  ##
24
32
  # Throws an error corresponding to the error number
@@ -140,14 +148,21 @@ module Trophonius
140
148
  # when "417"
141
149
  # when "418"
142
150
  when '500'
143
- raise ValidationError.new, 'Data value does not meet validation entry options (hint make sure your date values are formatted as MM/DD/YYYY)'
144
- # when "501"
145
- # when "502"
146
- # when "503"
147
- # when "504"
148
- # when "505"
149
- # when "506"
150
- # when "507"
151
+ raise DateValueError.new, 'Date value does not meet validation entry options (hint make sure your date values are formatted as MM/DD/YYYY)'
152
+ when '501'
153
+ raise TimeValueError.new, 'Time value does not meet validation entry options'
154
+ when '502'
155
+ raise NumberValueError.new, 'Number value does not meet validation entry options'
156
+ when '503'
157
+ raise ValueOutOfRangeError.new, 'Value in field is not within the range specified in validation entry options'
158
+ when '504'
159
+ raise ValueNotUniqueError.new, 'Value in field is not unique, as required in validation entry options'
160
+ when '505'
161
+ raise ValueNotExistingError.new, 'Value in field is not an existing value in the file, as required in validation entry options'
162
+ when '506'
163
+ raise ValueNotInValuelistError.new, 'Value in field is not listed in the value list specified in validation entry options'
164
+ when '507'
165
+ raise ValueFailedCalculationError.new, 'Value in field failed calculation test of validation entry options'
151
166
  # when "508"
152
167
  # when "509"
153
168
  # when "510"
@@ -28,23 +28,20 @@ module Trophonius
28
28
  @configuration.non_modifiable_fields = configuration[:non_modifiable_fields]
29
29
  @configuration.all_fields = {}
30
30
  @configuration.translations = {}
31
+ @configuration.has_many_relations = {}
32
+ @configuration.belongs_to_relations = {}
31
33
  @offset = ''
32
34
  @limit = ''
33
35
  end
34
36
 
35
37
  ##
36
- # Sets up the configuration for the model.
38
+ # Add a belongs to relationship.
37
39
  #
38
- # @param [Hash] configuration: the hash containing the config to setup the model correctly.
39
- # configuration = {layout_name: "theFileMakerLayoutForThisModel", non_modifiable_fields: ["an", "array", "containing", "calculation_fields", "etc."]}
40
- def self.config(configuration)
41
- @configuration ||= Configuration.new
42
- @configuration.layout_name = configuration[:layout_name]
43
- @configuration.non_modifiable_fields = configuration[:non_modifiable_fields]
44
- @configuration.all_fields = {}
45
- @configuration.translations = {}
46
- @offset = ''
47
- @limit = ''
40
+ # @param [Symbol] model_name: the name of the model to build a relation with
41
+ # @param [String] primary_key: the name of the field containing the primary to build the relation over
42
+ # @param [String] foreign_key: the name of the field containing the primary to build the relation over
43
+ def self.belongs_to(model_name, primary_key:, foreign_key:)
44
+ @configuration.belongs_to_relations.merge!({ model_name => { primary_key: primary_key, foreign_key: foreign_key } })
48
45
  end
49
46
 
50
47
  ##
@@ -53,17 +50,8 @@ module Trophonius
53
50
  # @param [Symbol] model_name: the name of the model to build a relation with
54
51
  # @param [String] primary_key: the name of the field containing the primary to build the relation over
55
52
  # @param [String] foreign_key: the name of the field containing the primary to build the relation over
56
- #
57
- # @return [Trophonius::Model] Self
58
53
  def self.has_many(model_name, primary_key:, foreign_key:)
59
- @configuration ||= Configuration.new
60
- @configuration.layout_name = configuration[:layout_name]
61
- @configuration.non_modifiable_fields = configuration[:non_modifiable_fields]
62
- @configuration.all_fields = {}
63
- @configuration.translations = {}
64
- @offset = ''
65
- @limit = ''
66
- self
54
+ @configuration.has_many_relations.merge!({ model_name => { primary_key: primary_key, foreign_key: foreign_key } })
67
55
  end
68
56
 
69
57
  ##
@@ -87,6 +75,22 @@ module Trophonius
87
75
  @configuration.layout_name
88
76
  end
89
77
 
78
+ ##
79
+ # Returns the Hash containing the related parent models
80
+ #
81
+ # @return [Hash] child models
82
+ def self.has_many_relations
83
+ @configuration.has_many_relations
84
+ end
85
+
86
+ ##
87
+ # Returns the Hash containing the related parent models
88
+ #
89
+ # @return [Hash] parent models
90
+ def self.belongs_to_relations
91
+ @configuration.belongs_to_relations
92
+ end
93
+
90
94
  ##
91
95
  # Returns the fields that FileMaker won't allow us to modify
92
96
  #
@@ -165,9 +169,11 @@ module Trophonius
165
169
  def self.create(fieldData)
166
170
  url =
167
171
  URI(
168
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
169
- layout_name
170
- }/records"
172
+ URI.escape(
173
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
174
+ Trophonius.config.database
175
+ }/layouts/#{layout_name}/records"
176
+ )
171
177
  )
172
178
  new_field_data = {}
173
179
  create_translations if @configuration.translations.keys.empty?
@@ -195,9 +201,11 @@ module Trophonius
195
201
  else
196
202
  url =
197
203
  URI(
198
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
199
- Trophonius.config.database
200
- }/layouts/#{layout_name}/records/#{response['response']['recordId']}"
204
+ URI.escape(
205
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
206
+ Trophonius.config.database
207
+ }/layouts/#{layout_name}/records/#{response['response']['recordId']}"
208
+ )
201
209
  )
202
210
  ret_val = build_result(Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}')['response']['data'][0])
203
211
  ret_val.send(:define_singleton_method, 'result_count') { 1 }
@@ -215,9 +223,11 @@ module Trophonius
215
223
  def self.find_by(fieldData)
216
224
  url =
217
225
  URI(
218
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
219
- self.layout_name
220
- }/_find?_limit=1"
226
+ URI.escape(
227
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
228
+ Trophonius.config.database
229
+ }/layouts/#{self.layout_name}/_find?_limit=1"
230
+ )
221
231
  )
222
232
  new_field_data = {}
223
233
  create_translations if @configuration.translations.keys.empty?
@@ -256,9 +266,11 @@ module Trophonius
256
266
  def self.find(record_id)
257
267
  url =
258
268
  URI(
259
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
260
- layout_name
261
- }/records/#{record_id}"
269
+ URI.escape(
270
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
271
+ Trophonius.config.database
272
+ }/layouts/#{layout_name}/records/#{record_id}"
273
+ )
262
274
  )
263
275
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}')
264
276
  if response['messages'][0]['code'] != '0'
@@ -279,9 +291,11 @@ module Trophonius
279
291
  def self.delete(record_id)
280
292
  url =
281
293
  URI(
282
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
283
- layout_name
284
- }/records/#{record_id}"
294
+ URI.escape(
295
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
296
+ Trophonius.config.database
297
+ }/layouts/#{layout_name}/records/#{record_id}"
298
+ )
285
299
  )
286
300
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'delete', '{}')
287
301
  if response['messages'][0]['code'] != '0'
@@ -302,9 +316,11 @@ module Trophonius
302
316
  def self.edit(record_id, fieldData)
303
317
  url =
304
318
  URI(
305
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
306
- layout_name
307
- }/records/#{record_id}"
319
+ URI.escape(
320
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
321
+ Trophonius.config.database
322
+ }/layouts/#{layout_name}/records/#{record_id}"
323
+ )
308
324
  )
309
325
  new_field_data = {}
310
326
  create_translations if @configuration.translations.keys.empty?
@@ -330,6 +346,8 @@ module Trophonius
330
346
  hash = Trophonius::Record.new
331
347
  hash.record_id = result['recordId']
332
348
  hash.layout_name = layout_name
349
+ hash.model_name = name
350
+
333
351
  result['fieldData'].keys.each do |key|
334
352
  # unless key[/\s/] || key[/\W/]
335
353
  @configuration.translations.merge!(
@@ -425,16 +443,20 @@ module Trophonius
425
443
  unless @limit.empty? || @offset.empty?
426
444
  url =
427
445
  URI(
428
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
429
- Trophonius.config.database
430
- }/layouts/#{layout_name}/records?_offset=#{@offset}&_limit=#{@limit}"
446
+ URI.escape(
447
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
448
+ Trophonius.config.database
449
+ }/layouts/#{layout_name}/records?_offset=#{@offset}&_limit=#{@limit}"
450
+ )
431
451
  )
432
452
  else
433
453
  url =
434
454
  URI(
435
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
436
- Trophonius.config.database
437
- }/layouts/#{layout_name}/records?_limit=#{count == 0 ? 1_000_000 : count}"
455
+ URI.escape(
456
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
457
+ Trophonius.config.database
458
+ }/layouts/#{layout_name}/records?_limit=#{count == 0 ? 1_000_000 : count}"
459
+ )
438
460
  )
439
461
  end
440
462
  @limit = ''
@@ -101,9 +101,11 @@ module Trophonius
101
101
  def run_query(method, *args, &block)
102
102
  url =
103
103
  URI(
104
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
105
- @trophonius_model.layout_name
106
- }/_find"
104
+ URI.escape(
105
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
106
+ Trophonius.config.database
107
+ }/layouts/#{@trophonius_model.layout_name}/_find"
108
+ )
107
109
  )
108
110
  new_field_data = @current_query.map { |_q| {} }
109
111
 
@@ -8,7 +8,7 @@ module Trophonius
8
8
  #
9
9
  # A Record is contained in a RecordSet and has methods to retrieve data from the fields inside the Record-hash
10
10
  class Trophonius::Record < Hash
11
- attr_accessor :record_id, :layout_name, :modifiable_fields, :modified_fields
11
+ attr_accessor :record_id, :model_name, :layout_name, :modifiable_fields, :modified_fields
12
12
 
13
13
  ##
14
14
  # Initializes a new Record
@@ -22,20 +22,153 @@ module Trophonius
22
22
  super
23
23
  end
24
24
 
25
+ def method_missing(method, *args, &block)
26
+ if ActiveSupport::Inflector.pluralize(method).to_s == method.to_s
27
+ model = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(method)))
28
+ pk_model = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(model_name)))
29
+
30
+ if model.belongs_to_relations[ActiveSupport::Inflector.parameterize(model_name).to_sym]
31
+ relation = model.belongs_to_relations[ActiveSupport::Inflector.parameterize(model_name).to_sym]
32
+ layout = model.layout_name
33
+ model.create_translations if model.translations.keys.empty?
34
+
35
+ url =
36
+ URI(
37
+ URI.escape(
38
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
39
+ Trophonius.config.database
40
+ }/layouts/#{layout}/_find"
41
+ )
42
+ )
43
+
44
+ if model.translations.key?(relation[:foreign_key])
45
+ foreign_key_field = model.translations[relation[:foreign_key]].to_s
46
+ else
47
+ foreign_key_field = relation[:foreign_key].to_s
48
+ end
49
+
50
+ if pk_model.translations.key?(relation[:primary_key])
51
+ primary_key_field = pk_model.translations[relation[:primary_key]].to_s
52
+ else
53
+ primary_key_field = relation[:primary_key].to_s
54
+ end
55
+
56
+ body = { query: [{ foreign_key_field => self[primary_key_field].to_s }], limit: 100_000 }.to_json
57
+ response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body)
58
+
59
+ if response['messages'][0]['code'] != '0'
60
+ if response['messages'][0]['code'] == '101' || response['messages'][0]['code'] == '401'
61
+ resp = RecordSet.new(layout, model.non_modifiable_fields)
62
+ return resp
63
+ else
64
+ if response['messages'][0]['code'] == '102'
65
+ results = Request.retrieve_first(layout)
66
+ if results['messages'][0]['code'] != '0'
67
+ Error.throw_error('102')
68
+ else
69
+ r_results = results['response']['data']
70
+ ret_val = r_results.empty? ? Error.throw_error('102') : r_results[0]['fieldData']
71
+ query_keys = [foreign_key_field]
72
+ Error.throw_error('102', (query_keys - ret_val.keys.map(&:downcase)).flatten.join(', '), layout)
73
+ end
74
+ end
75
+ Error.throw_error(response['messages'][0]['code'])
76
+ end
77
+ else
78
+ r_results = response['response']['data']
79
+ ret_val = RecordSet.new(layout, model.non_modifiable_fields)
80
+ r_results.each do |r|
81
+ hash = model.build_result(r)
82
+ ret_val << hash
83
+ end
84
+ @response = ret_val
85
+ return @response
86
+ end
87
+ end
88
+ elsif ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(method))).respond_to?('first')
89
+ fk_model = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(model_name)))
90
+ pk_model = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(method)))
91
+
92
+ if pk_model.has_many_relations[ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.pluralize(model_name)).to_sym]
93
+ relation = pk_model.has_many_relations[ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.pluralize(model_name)).to_sym]
94
+ layout = pk_model.layout_name
95
+ pk_model.create_translations if pk_model.translations.keys.empty?
96
+
97
+ url =
98
+ URI(
99
+ URI.escape(
100
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
101
+ Trophonius.config.database
102
+ }/layouts/#{layout}/_find"
103
+ )
104
+ )
105
+
106
+ if fk_model.translations.key?(relation[:foreign_key])
107
+ foreign_key_field = fk_model.translations[relation[:foreign_key]].to_s
108
+ else
109
+ foreign_key_field = relation[:foreign_key].to_s
110
+ end
111
+
112
+ if pk_model.translations.key?(relation[:primary_key])
113
+ primary_key_field = pk_model.translations[relation[:primary_key]].to_s
114
+ else
115
+ primary_key_field = relation[:primary_key].to_s
116
+ end
117
+
118
+ body = { query: [{ primary_key_field => self[foreign_key_field].to_s }], limit: 1 }.to_json
119
+
120
+ response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body)
121
+ if response['messages'][0]['code'] != '0'
122
+ if response['messages'][0]['code'] == '101' || response['messages'][0]['code'] == '401'
123
+ resp = RecordSet.new(layout, pk_model.non_modifiable_fields)
124
+ return resp
125
+ else
126
+ if response['messages'][0]['code'] == '102'
127
+ results = Request.retrieve_first(layout)
128
+ if results['messages'][0]['code'] != '0'
129
+ Error.throw_error('102')
130
+ else
131
+ r_results = results['response']['data']
132
+ ret_val = r_results.empty? ? Error.throw_error('102') : r_results[0]['fieldData']
133
+ query_keys = [primary_key_field]
134
+ Error.throw_error('102', (query_keys - ret_val.keys.map(&:downcase)).flatten.join(', '), layout)
135
+ end
136
+ end
137
+ Error.throw_error(response['messages'][0]['code'])
138
+ end
139
+ else
140
+ r_results = response['response']['data']
141
+ ret_val = RecordSet.new(layout, pk_model.non_modifiable_fields)
142
+ r_results.each do |r|
143
+ hash = pk_model.build_result(r)
144
+ ret_val << hash
145
+ end
146
+ @response = ret_val
147
+ return @response.first
148
+ end
149
+ end
150
+ else
151
+ super
152
+ end
153
+ end
154
+
25
155
  ##
26
156
  # Saves the last changes made to the Record to FileMaker.
27
157
  # Throws a FileMaker error if save failed
28
158
  #
29
159
  # @return [True] if successful
30
160
  def save
31
- url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}")
161
+ url =
162
+ URI(
163
+ URI.escape(
164
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
165
+ Trophonius.config.database
166
+ }/layouts/#{layout_name}/records/#{record_id}"
167
+ )
168
+ )
32
169
  body = "{\"fieldData\": #{modified_fields.to_json}}"
33
170
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'patch', body)
34
- if response['messages'][0]['code'] != '0'
35
- Error.throw_error(response['messages'][0]['code'])
36
- else
37
- true
38
- end
171
+ response['messages'][0]['code'] != '0' ? Error.throw_error(response['messages'][0]['code']) : true
39
172
  end
40
173
 
41
174
  ##
@@ -44,13 +177,16 @@ module Trophonius
44
177
  #
45
178
  # @return [True] if successful
46
179
  def delete
47
- url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}")
180
+ url =
181
+ URI(
182
+ URI.escape(
183
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
184
+ Trophonius.config.database
185
+ }/layouts/#{layout_name}/records/#{record_id}"
186
+ )
187
+ )
48
188
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'delete', '{}')
49
- if response['messages'][0]['code'] != '0'
50
- Error.throw_error(response['messages'][0]['code'])
51
- else
52
- true
53
- end
189
+ response['messages'][0]['code'] != '0' ? Error.throw_error(response['messages'][0]['code']) : true
54
190
  end
55
191
 
56
192
  ##
@@ -61,24 +197,29 @@ module Trophonius
61
197
  #
62
198
  # @return [True] if successful
63
199
  def update(fieldData)
64
- url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}")
65
- fieldData.keys.each do |field|
66
- modifiable_fields[field] = fieldData[field]
67
- end
200
+ url =
201
+ URI(
202
+ URI.escape(
203
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
204
+ Trophonius.config.database
205
+ }/layouts/#{layout_name}/records/#{record_id}"
206
+ )
207
+ )
208
+ fieldData.keys.each { |field| modifiable_fields[field] = fieldData[field] }
68
209
  body = "{\"fieldData\": #{fieldData.to_json}}"
69
210
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'patch', body)
70
211
  if response['messages'][0]['code'] != '0'
71
- if response["messages"][0]["code"] == "102"
212
+ if response['messages'][0]['code'] == '102'
72
213
  results = Request.retrieve_first(layout_name)
73
- if results["messages"][0]["code"] != "0"
74
- Error.throw_error("102")
214
+ if results['messages'][0]['code'] != '0'
215
+ Error.throw_error('102')
75
216
  else
76
- r_results = results["response"]["data"]
77
- ret_val = r_results.empty? ? Error.throw_error("102") : r_results[0]['fieldData']
78
- Error.throw_error("102", (fieldData.keys.map(&:downcase) - ret_val.keys.map(&:downcase)).flatten.join(', '), layout_name)
217
+ r_results = results['response']['data']
218
+ ret_val = r_results.empty? ? Error.throw_error('102') : r_results[0]['fieldData']
219
+ Error.throw_error('102', (fieldData.keys.map(&:downcase) - ret_val.keys.map(&:downcase)).flatten.join(', '), layout_name)
79
220
  end
80
221
  end
81
- Error.throw_error(response["messages"][0]["code"])
222
+ Error.throw_error(response['messages'][0]['code'])
82
223
  else
83
224
  true
84
225
  end
@@ -94,14 +235,17 @@ module Trophonius
94
235
  #
95
236
  # @return [True] if successful
96
237
  def upload(container_name:, container_repetition: 1, file:)
97
- url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}/containers/#{container_name}/#{container_repetition}")
238
+ url =
239
+ URI(
240
+ URI.escape(
241
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
242
+ Trophonius.config.database
243
+ }/layouts/#{layout_name}/records/#{record_id}/containers/#{container_name}/#{container_repetition}"
244
+ )
245
+ )
98
246
 
99
247
  response = Request.upload_file_request(url, "Bearer #{Request.get_token}", file)
100
- if response['messages'][0]['code'] != '0'
101
- Error.throw_error(response['messages'][0]['code'])
102
- else
103
- true
104
- end
248
+ response['messages'][0]['code'] != '0' ? Error.throw_error(response['messages'][0]['code']) : true
105
249
  end
106
250
  end
107
251
  end
@@ -4,7 +4,6 @@ require 'base64'
4
4
  require 'trophonius_connection'
5
5
  require 'uri'
6
6
  require 'net/http'
7
-
8
7
  module Trophonius
9
8
  module Trophonius::Request
10
9
  ##
@@ -24,19 +23,21 @@ module Trophonius
24
23
  def self.make_request(url_param, auth, method, body, params = '')
25
24
  ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
26
25
  ssl_verifypeer = !Trophonius.config.local_network
27
- request = Typhoeus::Request.new(
28
- url_param,
29
- method: method.to_sym,
30
- body: body,
31
- params: params,
32
- ssl_verifyhost: ssl_verifyhost,
33
- ssl_verifypeer: ssl_verifypeer,
34
- headers: { 'Content-Type' => 'application/json', Authorization: auth.to_s }
35
- )
26
+ request =
27
+ Typhoeus::Request.new(
28
+ url_param,
29
+ method: method.to_sym,
30
+ body: body,
31
+ params: params,
32
+ ssl_verifyhost: ssl_verifyhost,
33
+ ssl_verifypeer: ssl_verifypeer,
34
+ headers: { 'Content-Type' => 'application/json', Authorization: auth.to_s }
35
+ )
36
36
  temp = request.run
37
37
  begin
38
38
  JSON.parse(temp.response_body)
39
- rescue Exception
39
+ rescue Exception => e
40
+ puts "Error was #{e}"
40
41
  Error.throw_error('1631')
41
42
  end
42
43
  end
@@ -52,7 +53,7 @@ module Trophonius
52
53
  #
53
54
  # @return [JSON] parsed json of the response
54
55
  def self.upload_file_request(url_param, auth, file)
55
- url = URI(url_param)
56
+ url = URI(url_param.to_s)
56
57
 
57
58
  https = Net::HTTP.new(url.host, url.port)
58
59
  https.use_ssl = true
@@ -75,11 +76,7 @@ module Trophonius
75
76
  #
76
77
  # @return [String] a valid FileMaker token
77
78
  def self.get_token
78
- if Connection.valid_connection?
79
- Connection.token
80
- else
81
- Connection.connect
82
- end
79
+ Connection.valid_connection? ? Connection.token : Connection.connect
83
80
  end
84
81
 
85
82
  ##
@@ -87,7 +84,14 @@ module Trophonius
87
84
  #
88
85
  # @return [JSON] The first record from FileMaker
89
86
  def self.retrieve_first(layout_name)
90
- url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records?_limit=1")
87
+ url =
88
+ URI(
89
+ URI.escape(
90
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
91
+ Trophonius.config.database
92
+ }/layouts/#{layout_name}/records?_limit=1"
93
+ )
94
+ )
91
95
  make_request(url, "Bearer #{get_token}", 'get', '{}')
92
96
  end
93
97
 
@@ -96,7 +100,14 @@ module Trophonius
96
100
  #
97
101
  # @return [JSON] The script result from FileMaker
98
102
  def self.run_script(script, scriptparameter, layout_name)
99
- url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records?_limit=1&script=#{script}&script.param=#{scriptparameter}")
103
+ url =
104
+ URI(
105
+ URI.escape(
106
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
107
+ Trophonius.config.database
108
+ }/layouts/#{layout_name}/records?_limit=1&script=#{script}&script.param=#{scriptparameter}"
109
+ )
110
+ )
100
111
  make_request(url, "Bearer #{get_token}", 'get', '{}')
101
112
  end
102
113
 
@@ -107,9 +118,27 @@ module Trophonius
107
118
  def self.retrieve_all(layout_name, sort)
108
119
  if !sort.empty?
109
120
  sort_order = sort.to_json.to_s
110
- url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records?_limit=10000000_sort=#{sort_order}#{Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"}")
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
+ }/layouts/#{layout_name}/records?_limit=10000000_sort=#{sort_order}#{
127
+ Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"
128
+ }"
129
+ )
130
+ )
111
131
  else
112
- url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records?_limit=10000000#{Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"}")
132
+ url =
133
+ URI(
134
+ URI.escape(
135
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
136
+ Trophonius.config.database
137
+ }/layouts/#{layout_name}/records?_limit=10000000#{
138
+ Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"
139
+ }"
140
+ )
141
+ )
113
142
  end
114
143
  make_request(url, "Bearer #{get_token}", 'get', '{}')
115
144
  end
@@ -0,0 +1,6 @@
1
+ class Time
2
+ def to_fm
3
+ self.strftime('%m-%d-%Y %H:%M:%S')
4
+ end
5
+ alias convert_to_fm to_fm
6
+ 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.2.4.10
4
+ version: 1.2.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kempen Automatisering
@@ -42,18 +42,18 @@ dependencies:
42
42
  name: activesupport
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 6.x
47
+ version: '5.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 6.x
55
- description: An easy to use link between Ruby (on Rails) and FileMaker using the FileMaker
56
- Data-API.
54
+ version: '5.0'
55
+ description: A lightweight, easy to use link between Ruby (on Rails) and FileMaker
56
+ using the FileMaker Data-API.
57
57
  email:
58
58
  executables: []
59
59
  extensions: []
@@ -62,6 +62,8 @@ files:
62
62
  - lib/trophonius.rb
63
63
  - lib/trophonius_config.rb
64
64
  - lib/trophonius_connection.rb
65
+ - lib/trophonius_date.rb
66
+ - lib/trophonius_date_time.rb
65
67
  - lib/trophonius_error.rb
66
68
  - lib/trophonius_model.rb
67
69
  - lib/trophonius_query.rb
@@ -69,6 +71,7 @@ files:
69
71
  - lib/trophonius_recordset.rb
70
72
  - lib/trophonius_redis_manager.rb
71
73
  - lib/trophonius_request.rb
74
+ - lib/trophonius_time.rb
72
75
  homepage: https://github.com/Willem-Jan/Trophonius
73
76
  licenses:
74
77
  - MIT
@@ -88,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
91
  - !ruby/object:Gem::Version
89
92
  version: '0'
90
93
  requirements: []
91
- rubygems_version: 3.0.0
94
+ rubygems_version: 3.0.3
92
95
  signing_key:
93
96
  specification_version: 4
94
97
  summary: Link between Ruby (on Rails) and FileMaker.