trophonius 1.2.6.1 → 1.2.7.1

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: 41574e377e8690e833092a9dedd6e7929ae57bdc10d34aa61b76ff89369f8051
4
- data.tar.gz: '0096a1ae118fc10514f68005ad6408710ebc37269e7f2a1ba6fab7395e0f48da'
3
+ metadata.gz: 7b4fa3606dce996196ab6530704a27b93fbbf3e2134add785319b7b7921696cb
4
+ data.tar.gz: 16042983fe5510c9a29986aab3b77018033063980f1191d5fbf4234aa6bd229c
5
5
  SHA512:
6
- metadata.gz: 2ef4fb3e20cb91540f88e391ffd83acc7f54165b2974d7a9ad62a18ebf35ca867829f9d6d09e7e1bcccbabdf1690f6bb0bfb8e083b4c387f598b6e4b31865a38
7
- data.tar.gz: e3f3eab5629b894e7e7a7aebc6dbe1252ed56d2c9a902d0a929f9a513bdc9edad66def8a20dcca697002f107f3016fa6c5151f0d48b838e74e836d2647e2d4f2
6
+ metadata.gz: 36fa65b8130dfe29303627c0727fb8504e832ac50e7b5d74f574f810efeaf9842a55898f68595ba7e2931afefabc4db0f98ec12e0d7aa8d790cde86672b1a614
7
+ data.tar.gz: ebdd54e6f7486fedb7cb03cdf87cf6d1df28ce7c06ab6bc3afaa8320bb9a6f94244cb68a59a158bf4059a578795c1cc932092b7e6c40f5dabc1ca7d71bbffbea
@@ -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
@@ -13,6 +13,7 @@ module Trophonius
13
13
  config_accessor(:username) { 'Admin' }
14
14
  config_accessor(:password) { '' }
15
15
  config_accessor(:ssl) { true }
16
+ config_accessor(:fm_18) { false }
16
17
  config_accessor(:count_result_script) { '' }
17
18
  config_accessor(:layout_name) { '' }
18
19
  config_accessor(:non_modifiable_fields) { [] }
@@ -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(
@@ -83,11 +85,12 @@ module Trophonius
83
85
  def self.disconnect
84
86
  url =
85
87
  URI(
86
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/sessions/#{
87
- Trophonius.config.redis_connection ? Trophonius::RedisManager.get_key(key: 'token') : @token
88
- }"
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
+ )
89
93
  )
90
- puts url
91
94
  ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
92
95
  ssl_verifypeer = !Trophonius.config.local_network
93
96
 
@@ -136,9 +139,11 @@ module Trophonius
136
139
  def self.test_connection
137
140
  url =
138
141
  URI(
139
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
140
- Trophonius.config.layout_name
141
- }/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
+ )
142
147
  )
143
148
  begin
144
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
@@ -112,7 +112,16 @@ module Trophonius
112
112
  #
113
113
  # @return [Hash] translations of the fields Rails -> FileMaker
114
114
  def self.create_translations
115
- self.first
115
+ if Trophonius.config.fm_18
116
+ field_names = Trophonius::Request.get_layout_field_names(layout_name)
117
+ field_names.each do |field|
118
+ @configuration.translations.merge!(
119
+ { "#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(field.to_s), separator: '_').downcase}" => "#{field}" }
120
+ )
121
+ end
122
+ else
123
+ self.first
124
+ end
116
125
  @configuration.translations
117
126
  end
118
127
 
@@ -169,9 +178,11 @@ module Trophonius
169
178
  def self.create(fieldData)
170
179
  url =
171
180
  URI(
172
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
173
- layout_name
174
- }/records"
181
+ URI.escape(
182
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
183
+ Trophonius.config.database
184
+ }/layouts/#{layout_name}/records"
185
+ )
175
186
  )
176
187
  new_field_data = {}
177
188
  create_translations if @configuration.translations.keys.empty?
@@ -199,9 +210,11 @@ module Trophonius
199
210
  else
200
211
  url =
201
212
  URI(
202
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
203
- Trophonius.config.database
204
- }/layouts/#{layout_name}/records/#{response['response']['recordId']}"
213
+ URI.escape(
214
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
215
+ Trophonius.config.database
216
+ }/layouts/#{layout_name}/records/#{response['response']['recordId']}"
217
+ )
205
218
  )
206
219
  ret_val = build_result(Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}')['response']['data'][0])
207
220
  ret_val.send(:define_singleton_method, 'result_count') { 1 }
@@ -219,9 +232,11 @@ module Trophonius
219
232
  def self.find_by(fieldData)
220
233
  url =
221
234
  URI(
222
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
223
- self.layout_name
224
- }/_find?_limit=1"
235
+ URI.escape(
236
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
237
+ Trophonius.config.database
238
+ }/layouts/#{self.layout_name}/_find?_limit=1"
239
+ )
225
240
  )
226
241
  new_field_data = {}
227
242
  create_translations if @configuration.translations.keys.empty?
@@ -260,9 +275,11 @@ module Trophonius
260
275
  def self.find(record_id)
261
276
  url =
262
277
  URI(
263
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
264
- layout_name
265
- }/records/#{record_id}"
278
+ URI.escape(
279
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
280
+ Trophonius.config.database
281
+ }/layouts/#{layout_name}/records/#{record_id}"
282
+ )
266
283
  )
267
284
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}')
268
285
  if response['messages'][0]['code'] != '0'
@@ -283,9 +300,11 @@ module Trophonius
283
300
  def self.delete(record_id)
284
301
  url =
285
302
  URI(
286
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
287
- layout_name
288
- }/records/#{record_id}"
303
+ URI.escape(
304
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
305
+ Trophonius.config.database
306
+ }/layouts/#{layout_name}/records/#{record_id}"
307
+ )
289
308
  )
290
309
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'delete', '{}')
291
310
  if response['messages'][0]['code'] != '0'
@@ -306,9 +325,11 @@ module Trophonius
306
325
  def self.edit(record_id, fieldData)
307
326
  url =
308
327
  URI(
309
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
310
- layout_name
311
- }/records/#{record_id}"
328
+ URI.escape(
329
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
330
+ Trophonius.config.database
331
+ }/layouts/#{layout_name}/records/#{record_id}"
332
+ )
312
333
  )
313
334
  new_field_data = {}
314
335
  create_translations if @configuration.translations.keys.empty?
@@ -431,16 +452,20 @@ module Trophonius
431
452
  unless @limit.empty? || @offset.empty?
432
453
  url =
433
454
  URI(
434
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
435
- Trophonius.config.database
436
- }/layouts/#{layout_name}/records?_offset=#{@offset}&_limit=#{@limit}"
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?_offset=#{@offset}&_limit=#{@limit}"
459
+ )
437
460
  )
438
461
  else
439
462
  url =
440
463
  URI(
441
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
442
- Trophonius.config.database
443
- }/layouts/#{layout_name}/records?_limit=#{count == 0 ? 1_000_000 : count}"
464
+ URI.escape(
465
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
466
+ Trophonius.config.database
467
+ }/layouts/#{layout_name}/records?_limit=#{count == 0 ? 1_000_000 : count}"
468
+ )
444
469
  )
445
470
  end
446
471
  @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
 
@@ -34,9 +34,11 @@ module Trophonius
34
34
 
35
35
  url =
36
36
  URI(
37
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
38
- Trophonius.config.database
39
- }/layouts/#{layout}/_find"
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
+ )
40
42
  )
41
43
 
42
44
  if model.translations.key?(relation[:foreign_key])
@@ -94,9 +96,11 @@ module Trophonius
94
96
 
95
97
  url =
96
98
  URI(
97
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
98
- Trophonius.config.database
99
- }/layouts/#{layout}/_find"
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
+ )
100
104
  )
101
105
 
102
106
  if fk_model.translations.key?(relation[:foreign_key])
@@ -156,9 +160,11 @@ module Trophonius
156
160
  def save
157
161
  url =
158
162
  URI(
159
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
160
- layout_name
161
- }/records/#{record_id}"
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
+ )
162
168
  )
163
169
  body = "{\"fieldData\": #{modified_fields.to_json}}"
164
170
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'patch', body)
@@ -173,9 +179,11 @@ module Trophonius
173
179
  def delete
174
180
  url =
175
181
  URI(
176
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
177
- layout_name
178
- }/records/#{record_id}"
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
+ )
179
187
  )
180
188
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'delete', '{}')
181
189
  response['messages'][0]['code'] != '0' ? Error.throw_error(response['messages'][0]['code']) : true
@@ -191,9 +199,11 @@ module Trophonius
191
199
  def update(fieldData)
192
200
  url =
193
201
  URI(
194
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
195
- layout_name
196
- }/records/#{record_id}"
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
+ )
197
207
  )
198
208
  fieldData.keys.each { |field| modifiable_fields[field] = fieldData[field] }
199
209
  body = "{\"fieldData\": #{fieldData.to_json}}"
@@ -227,9 +237,11 @@ module Trophonius
227
237
  def upload(container_name:, container_repetition: 1, file:)
228
238
  url =
229
239
  URI(
230
- "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{
231
- layout_name
232
- }/records/#{record_id}/containers/#{container_name}/#{container_repetition}"
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
+ )
233
245
  )
234
246
 
235
247
  response = Request.upload_file_request(url, "Bearer #{Request.get_token}", file)
@@ -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,20 @@ 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
40
  Error.throw_error('1631')
41
41
  end
42
42
  end
@@ -52,7 +52,7 @@ module Trophonius
52
52
  #
53
53
  # @return [JSON] parsed json of the response
54
54
  def self.upload_file_request(url_param, auth, file)
55
- url = URI(url_param)
55
+ url = URI(url_param.to_s)
56
56
 
57
57
  https = Net::HTTP.new(url.host, url.port)
58
58
  https.use_ssl = true
@@ -75,11 +75,7 @@ module Trophonius
75
75
  #
76
76
  # @return [String] a valid FileMaker token
77
77
  def self.get_token
78
- if Connection.valid_connection?
79
- Connection.token
80
- else
81
- Connection.connect
82
- end
78
+ Connection.valid_connection? ? Connection.token : Connection.connect
83
79
  end
84
80
 
85
81
  ##
@@ -87,16 +83,50 @@ module Trophonius
87
83
  #
88
84
  # @return [JSON] The first record from FileMaker
89
85
  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")
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
+ }/layouts/#{layout_name}/records?_limit=1"
92
+ )
93
+ )
91
94
  make_request(url, "Bearer #{get_token}", 'get', '{}')
92
95
  end
93
96
 
97
+ ##
98
+ # Retrieves the fieldnames of a layout
99
+ #
100
+ # @return [JSON] The fieldnames of a layout
101
+ def self.get_layout_field_names(layout_name)
102
+ url =
103
+ URI(
104
+ URI.escape(
105
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
106
+ Trophonius.config.database
107
+ }/layouts/#{layout_name}"
108
+ )
109
+ )
110
+ begin
111
+ make_request(url, "Bearer #{get_token}", 'get', '{}')['response']['fieldMetaData'].map { |field| field['name'] }
112
+ rescue Exception
113
+ Error.throw_error('1631')
114
+ end
115
+ end
116
+
94
117
  ##
95
118
  # Runs a FileMaker script
96
119
  #
97
120
  # @return [JSON] The script result from FileMaker
98
121
  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}")
122
+ url =
123
+ URI(
124
+ URI.escape(
125
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
126
+ Trophonius.config.database
127
+ }/layouts/#{layout_name}/records?_limit=1&script=#{script}&script.param=#{scriptparameter}"
128
+ )
129
+ )
100
130
  make_request(url, "Bearer #{get_token}", 'get', '{}')
101
131
  end
102
132
 
@@ -107,9 +137,27 @@ module Trophonius
107
137
  def self.retrieve_all(layout_name, sort)
108
138
  if !sort.empty?
109
139
  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}"}")
140
+ url =
141
+ URI(
142
+ URI.escape(
143
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
144
+ Trophonius.config.database
145
+ }/layouts/#{layout_name}/records?_limit=10000000_sort=#{sort_order}#{
146
+ Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"
147
+ }"
148
+ )
149
+ )
111
150
  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}"}")
151
+ url =
152
+ URI(
153
+ URI.escape(
154
+ "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
155
+ Trophonius.config.database
156
+ }/layouts/#{layout_name}/records?_limit=10000000#{
157
+ Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"
158
+ }"
159
+ )
160
+ )
113
161
  end
114
162
  make_request(url, "Bearer #{get_token}", 'get', '{}')
115
163
  end
@@ -0,0 +1,184 @@
1
+ require 'trophonius_date'
2
+ require 'trophonius_time'
3
+ require 'trophonius_error'
4
+ require 'trophonius_record'
5
+ require 'trophonius_recordset'
6
+
7
+ module Trophonius
8
+ class Trophonius::Single
9
+ attr_reader :query
10
+ def initialize(config:)
11
+ @config = config
12
+ @query = {}
13
+ @translations = {}
14
+ @all_fields = {}
15
+ end
16
+
17
+ def where(fieldData)
18
+ url =
19
+ URI(
20
+ URI.escape(
21
+ "http#{@config[:ssl] == true ? 's' : ''}://#{@config[:host]}/fmi/data/v1/databases/#{@config[:database]}/layouts/#{
22
+ @config[:layout_name]
23
+ }/_find"
24
+ )
25
+ )
26
+ @query.merge!(query: [fieldData])
27
+ @query.merge!(limit: '10000000')
28
+ token = setup_connection
29
+ response = make_request(url, token, 'post', @query.to_json)
30
+
31
+ r_results = response['response']['data']
32
+ if response['messages'][0]['code'] != '0' && response['messages'][0]['code'] != '401'
33
+ Error.throw_error(response['messages'][0]['code'])
34
+ elsif response['messages'][0]['code'] == '401'
35
+ return RecordSet.new(@config[:layout_name], @config[:non_modifiable_fields])
36
+ else
37
+ ret_val = RecordSet.new(@config[:layout_name], @config[:non_modifiable_fields])
38
+ r_results.each do |r|
39
+ hash = build_result(r)
40
+ ret_val << hash
41
+ end
42
+ end
43
+ @query = {}
44
+ close_connection(token)
45
+
46
+ return ret_val
47
+ end
48
+
49
+ private
50
+
51
+ def build_result(result)
52
+ hash = Trophonius::Record.new
53
+ hash.record_id = result['recordId']
54
+ hash.layout_name = @config[:layout_name]
55
+ hash.model_name = 'Single'
56
+
57
+ result['fieldData'].keys.each do |key|
58
+ # unless key[/\s/] || key[/\W/]
59
+ @translations.merge!(
60
+ { "#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase}" => "#{key}" }
61
+ )
62
+ hash.send(:define_singleton_method, ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')) do
63
+ hash[key]
64
+ end
65
+ unless @config[:non_modifiable_fields]&.include?(key)
66
+ @all_fields.merge!(
67
+ ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase =>
68
+ ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')
69
+ )
70
+ hash.send(
71
+ :define_singleton_method,
72
+ "#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')}="
73
+ ) do |new_val|
74
+ hash[key] = new_val
75
+ hash.modifiable_fields[key] = new_val
76
+ hash.modified_fields[key] = new_val
77
+ end
78
+ end
79
+ # end
80
+ hash.merge!({ key => result['fieldData'][key] })
81
+ hash.modifiable_fields.merge!({ key => result['fieldData'][key] }) unless @config[:non_modifiable_fields]&.include?(key)
82
+ end
83
+ result['portalData'].keys.each do |key|
84
+ unless key[/\s/] || key[/\W/]
85
+ hash.send(:define_singleton_method, ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')) do
86
+ hash[key]
87
+ end
88
+ end
89
+ result['portalData'][key].each_with_index do |inner_hash|
90
+ inner_hash.keys.each do |inner_key|
91
+ inner_method =
92
+ ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(inner_key.gsub(/\w+::/, '').to_s), separator: '_')
93
+ unless inner_method[/\s/] || inner_method[/\W/]
94
+ inner_hash.send(:define_singleton_method, inner_method.to_s) { inner_hash[inner_key] }
95
+ inner_hash.send(:define_singleton_method, 'record_id') { inner_hash['recordId'] }
96
+ end
97
+ end
98
+ end
99
+ hash.merge!({ key => result['portalData'][key] })
100
+ end
101
+ return hash
102
+ end
103
+
104
+ def make_request(url_param, token, method, body, params = '')
105
+ ssl_verifyhost = @config[:local_network] ? 0 : 2
106
+ ssl_verifypeer = !@config[:local_network]
107
+ request =
108
+ Typhoeus::Request.new(
109
+ url_param,
110
+ method: method.to_sym,
111
+ body: body,
112
+ params: params,
113
+ ssl_verifyhost: ssl_verifyhost,
114
+ ssl_verifypeer: ssl_verifypeer,
115
+ headers: { 'Content-Type' => 'application/json', Authorization: "Bearer #{token}" }
116
+ )
117
+ temp = request.run
118
+ begin
119
+ JSON.parse(temp.response_body)
120
+ rescue Exception => e
121
+ Error.throw_error('1631')
122
+ end
123
+ end
124
+
125
+ def setup_connection
126
+ ssl_verifyhost = @config[:local_network] ? 0 : 2
127
+ ssl_verifypeer = !@config[:local_network]
128
+ url = URI(URI.escape("http#{@config[:ssl] == true ? 's' : ''}://#{@config[:host]}/fmi/data/v1/databases/#{@config[:database]}/sessions"))
129
+ request =
130
+ Typhoeus::Request.new(
131
+ url,
132
+ method: :post,
133
+ body:
134
+ if @config[:external_name].nil? || @config[:external_name].empty?
135
+ {}
136
+ else
137
+ {
138
+ fmDataSource: [{ database: @config[:external_name], username: @config[:external_username], password: @config[:external_password] }]
139
+ }.to_json
140
+ end,
141
+ params: {},
142
+ ssl_verifyhost: ssl_verifyhost,
143
+ ssl_verifypeer: ssl_verifypeer,
144
+ headers: {
145
+ 'Content-Type' => 'application/json', Authorization: "Basic #{Base64.strict_encode64("#{@config[:username]}:#{@config[:password]}")}"
146
+ }
147
+ )
148
+ temp = request.run
149
+ begin
150
+ parsed = JSON.parse(temp.response_body)
151
+ rescue Exception => e
152
+ Error.throw_error('1631')
153
+ end
154
+ Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
155
+ return parsed['response']['token']
156
+ end
157
+
158
+ def close_connection(token)
159
+ url =
160
+ URI(URI.escape("http#{@config[:ssl] == true ? 's' : ''}://#{@config[:host]}/fmi/data/v1/databases/#{@config[:database]}/sessions/#{token}"))
161
+ ssl_verifyhost = @config[:local_network] ? 0 : 2
162
+ ssl_verifypeer = !@config[:local_network]
163
+
164
+ request =
165
+ Typhoeus::Request.new(
166
+ url,
167
+ method: :delete,
168
+ params: {},
169
+ ssl_verifyhost: ssl_verifyhost,
170
+ ssl_verifypeer: ssl_verifypeer,
171
+ headers: { 'Content-Type' => 'application/json' }
172
+ )
173
+ temp = request.run
174
+
175
+ begin
176
+ parsed = JSON.parse(temp.response_body)
177
+ rescue Exception => e
178
+ Error.throw_error('1631')
179
+ end
180
+ Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
181
+ return true
182
+ end
183
+ end
184
+ 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.6.1
4
+ version: 1.2.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kempen Automatisering
@@ -44,16 +44,16 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 6.0.3.1
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.0.3.1
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,8 @@ files:
69
71
  - lib/trophonius_recordset.rb
70
72
  - lib/trophonius_redis_manager.rb
71
73
  - lib/trophonius_request.rb
74
+ - lib/trophonius_single.rb
75
+ - lib/trophonius_time.rb
72
76
  homepage: https://github.com/Willem-Jan/Trophonius
73
77
  licenses:
74
78
  - MIT
@@ -88,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
92
  - !ruby/object:Gem::Version
89
93
  version: '0'
90
94
  requirements: []
91
- rubygems_version: 3.0.0
95
+ rubygems_version: 3.0.3
92
96
  signing_key:
93
97
  specification_version: 4
94
98
  summary: Link between Ruby (on Rails) and FileMaker.