trophonius 1.4.5.5 → 2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/{trophonius_config.rb → config.rb} +9 -3
- data/lib/{trophonius_connection.rb → connectors/connection.rb} +74 -64
- data/lib/connectors/connection_manager.rb +31 -0
- data/lib/connectors/database_request.rb +151 -0
- data/lib/debug_printer.rb +11 -0
- data/lib/{trophonius_error.rb → error.rb} +3 -3
- data/lib/{trophonius_date.rb → fm_date.rb} +1 -1
- data/lib/fm_time.rb +11 -0
- data/lib/generators/trophonius_generator.rb +1 -0
- data/lib/model.rb +394 -0
- data/lib/{trophonius_query.rb → query.rb} +44 -57
- data/lib/{trophonius_record.rb → record.rb} +104 -116
- data/lib/{trophonius_recordset.rb → recordset.rb} +5 -6
- data/lib/{trophonius_single.rb → single.rb} +13 -59
- data/lib/translator.rb +15 -0
- data/lib/trophonius.rb +14 -8
- metadata +19 -16
- data/lib/trophonius_model.rb +0 -511
- data/lib/trophonius_request.rb +0 -173
- data/lib/trophonius_time.rb +0 -6
- /data/lib/{trophonius_redis_manager.rb → connectors/redis_manager.rb} +0 -0
- /data/lib/{trophonius_date_time.rb → fm_date_time.rb} +0 -0
data/lib/trophonius_model.rb
DELETED
@@ -1,511 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
require 'trophonius_config'
|
3
|
-
require 'trophonius_record'
|
4
|
-
require 'trophonius_recordset'
|
5
|
-
require 'trophonius_query'
|
6
|
-
require 'trophonius_error'
|
7
|
-
|
8
|
-
module Trophonius
|
9
|
-
# This class will retrieve the records from the FileMaker database and build a RecordSet filled with Record objects. One Record object represents a record in FileMaker.
|
10
|
-
class Trophonius::Model
|
11
|
-
attr_reader :configuration
|
12
|
-
attr_accessor :current_query
|
13
|
-
|
14
|
-
def initialize(config:)
|
15
|
-
@configuration = config
|
16
|
-
@offset = ''
|
17
|
-
@limit = ''
|
18
|
-
end
|
19
|
-
|
20
|
-
##
|
21
|
-
# Sets up the configuration for the model.
|
22
|
-
#
|
23
|
-
# @param [Hash] configuration: the hash containing the config to setup the model correctly.
|
24
|
-
# configuration = {layout_name: "theFileMakerLayoutForThisModel", non_modifiable_fields: ["an", "array", "containing", "calculation_fields", "etc."]}
|
25
|
-
def self.config(configuration)
|
26
|
-
@configuration ||= Configuration.new
|
27
|
-
@configuration.layout_name = configuration[:layout_name]
|
28
|
-
@configuration.non_modifiable_fields = configuration[:non_modifiable_fields]
|
29
|
-
@configuration.all_fields = {}
|
30
|
-
@configuration.translations = {}
|
31
|
-
@configuration.has_many_relations = {}
|
32
|
-
@configuration.belongs_to_relations = {}
|
33
|
-
@offset = ''
|
34
|
-
@limit = ''
|
35
|
-
end
|
36
|
-
|
37
|
-
##
|
38
|
-
# Add a belongs to relationship.
|
39
|
-
#
|
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 } })
|
45
|
-
end
|
46
|
-
|
47
|
-
##
|
48
|
-
# Add a has many relationship.
|
49
|
-
#
|
50
|
-
# @param [Symbol] model_name: the name of the model to build a relation with
|
51
|
-
# @param [String] primary_key: the name of the field containing the primary to build the relation over
|
52
|
-
# @param [String] foreign_key: the name of the field containing the primary to build the relation over
|
53
|
-
def self.has_many(model_name, primary_key:, foreign_key:)
|
54
|
-
@configuration.has_many_relations.merge!({ model_name => { primary_key: primary_key, foreign_key: foreign_key } })
|
55
|
-
end
|
56
|
-
|
57
|
-
##
|
58
|
-
# Limits the found record set.
|
59
|
-
#
|
60
|
-
# @param [Integer] page: number of current page
|
61
|
-
# @param [Integer] limit: number of records retreived
|
62
|
-
#
|
63
|
-
# @return [Trophonius::Model] Self
|
64
|
-
def self.paginate(page, limit)
|
65
|
-
@offset = (((page * limit) - limit) + 1).to_s
|
66
|
-
@limit = limit.to_s
|
67
|
-
self
|
68
|
-
end
|
69
|
-
|
70
|
-
##
|
71
|
-
# Returns the FileMaker layout this Model corresponds to
|
72
|
-
#
|
73
|
-
# @return [String] layout name of the model
|
74
|
-
def self.layout_name
|
75
|
-
@configuration.layout_name
|
76
|
-
end
|
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
|
-
|
94
|
-
##
|
95
|
-
# Returns the fields that FileMaker won't allow us to modify
|
96
|
-
#
|
97
|
-
# @return [[Array]] fields that FileMaker won't allow us to modify
|
98
|
-
def self.non_modifiable_fields
|
99
|
-
@configuration.non_modifiable_fields
|
100
|
-
end
|
101
|
-
|
102
|
-
##
|
103
|
-
# Returns the translations of the fields
|
104
|
-
#
|
105
|
-
# @return [Hash] translations of the fields Rails -> FileMaker
|
106
|
-
def self.translations
|
107
|
-
@configuration.translations
|
108
|
-
end
|
109
|
-
|
110
|
-
##
|
111
|
-
# creates Rails -> FileMaker field translations by requesting the first record
|
112
|
-
#
|
113
|
-
# @return [Hash] translations of the fields Rails -> FileMaker
|
114
|
-
def self.create_translations
|
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.to_s => field.to_s }
|
120
|
-
)
|
121
|
-
end
|
122
|
-
else
|
123
|
-
first
|
124
|
-
end
|
125
|
-
@configuration.translations
|
126
|
-
end
|
127
|
-
|
128
|
-
def self.method_missing(method, *args)
|
129
|
-
new_instance = Trophonius::Model.new(config: @configuration)
|
130
|
-
new_instance.current_query = Trophonius::Query.new(trophonius_model: self, limit: @limit, offset: @offset)
|
131
|
-
args << new_instance
|
132
|
-
new_instance.current_query.send(method, args) if new_instance.current_query.respond_to?(method)
|
133
|
-
end
|
134
|
-
|
135
|
-
def method_missing(method, *args, &block)
|
136
|
-
if @current_query.respond_to?(method)
|
137
|
-
args << self
|
138
|
-
@current_query.send(method, args)
|
139
|
-
elsif @current_query.response.respond_to?(method)
|
140
|
-
ret_val = @current_query.run_query(method, *args, &block)
|
141
|
-
@limit = ''
|
142
|
-
@offset = ''
|
143
|
-
ret_val
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
##
|
148
|
-
# Finds all records in FileMaker corresponding to the requested query
|
149
|
-
# @param [Hash] fieldData: the data to find
|
150
|
-
#
|
151
|
-
# @return [Trophonius::Model] new instance of the model
|
152
|
-
def self.where(field_data)
|
153
|
-
new_instance = Trophonius::Model.new(config: @configuration)
|
154
|
-
new_instance.current_query = Trophonius::Query.new(trophonius_model: self, limit: @limit, offset: @offset)
|
155
|
-
new_instance.current_query.build_query[0].merge!(field_data)
|
156
|
-
new_instance
|
157
|
-
end
|
158
|
-
|
159
|
-
##
|
160
|
-
# Finds all records in FileMaker corresponding to the requested query
|
161
|
-
# This method is created to enable where chaining
|
162
|
-
#
|
163
|
-
# @param [Hash] fieldData: the data to find
|
164
|
-
#
|
165
|
-
# @return [Trophonius::Model] new instance of the model
|
166
|
-
def where(field_data)
|
167
|
-
@current_query.build_query[0].merge!(field_data)
|
168
|
-
self
|
169
|
-
end
|
170
|
-
|
171
|
-
##
|
172
|
-
# Creates and saves a record in FileMaker
|
173
|
-
#
|
174
|
-
# @param [Hash] fieldData: the fields to fill with the data
|
175
|
-
#
|
176
|
-
# @return [Record] the created record
|
177
|
-
# Model.create(fieldOne: "Data")
|
178
|
-
def self.create(field_data, portalData: {})
|
179
|
-
uri = URI::RFC2396_Parser.new
|
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"
|
186
|
-
)
|
187
|
-
)
|
188
|
-
new_field_data = {}
|
189
|
-
create_translations if @configuration.translations.keys.empty?
|
190
|
-
field_data.keys.each do |k|
|
191
|
-
if @configuration.translations.keys.include?(k.to_s)
|
192
|
-
new_field_data.merge!({ @configuration.translations[k.to_s].to_s => field_data[k] })
|
193
|
-
else
|
194
|
-
new_field_data.merge!({ k.to_s => field_data[k] })
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
new_portal_data = {}
|
199
|
-
portalData.each do |portal_name, portal_values|
|
200
|
-
new_portal_data.merge!(
|
201
|
-
portal_name =>
|
202
|
-
portal_values.map do |record|
|
203
|
-
record.each_with_object({}) do |(key, value), new_hash|
|
204
|
-
new_hash["#{portal_name}::#{key}"] = value
|
205
|
-
end
|
206
|
-
end
|
207
|
-
)
|
208
|
-
end
|
209
|
-
|
210
|
-
body =
|
211
|
-
if new_portal_data == {}
|
212
|
-
"{\"fieldData\": #{new_field_data.to_json} }"
|
213
|
-
else
|
214
|
-
"{\"fieldData\": #{new_field_data.to_json}, \"portalData\": #{new_portal_data.to_json}}"
|
215
|
-
end
|
216
|
-
response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body)
|
217
|
-
if response['messages'][0]['code'] == '0'
|
218
|
-
url =
|
219
|
-
URI(
|
220
|
-
uri.escape(
|
221
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
222
|
-
Trophonius.config.database
|
223
|
-
}/layouts/#{layout_name}/records/#{response['response']['recordId']}"
|
224
|
-
)
|
225
|
-
)
|
226
|
-
ret_val = build_result(Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}')['response']['data'][0])
|
227
|
-
ret_val.send(:define_singleton_method, 'result_count') { 1 }
|
228
|
-
ret_val
|
229
|
-
else
|
230
|
-
if response['messages'][0]['code'] == '102'
|
231
|
-
results = Request.retrieve_first(layout_name)
|
232
|
-
if results['messages'][0]['code'] == '0'
|
233
|
-
r_results = results['response']['data']
|
234
|
-
ret_val = r_results.empty? ? Error.throw_error('102') : r_results[0]['fieldData']
|
235
|
-
Error.throw_error('102', (new_field_data.keys.map(&:downcase) - ret_val.keys.map(&:downcase)).flatten.join(', '), layout_name)
|
236
|
-
else
|
237
|
-
Error.throw_error('102')
|
238
|
-
end
|
239
|
-
end
|
240
|
-
Error.throw_error(response['messages'][0]['code'])
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
##
|
245
|
-
# Finds and returns the first Record containing fitting the find request
|
246
|
-
#
|
247
|
-
# @param [Hash] fieldData: the data to find
|
248
|
-
#
|
249
|
-
# @return [Record] a Record object that correspond to FileMaker record fitting the find request
|
250
|
-
# Model.find_by(fieldOne: "Data")
|
251
|
-
def self.find_by(field_data)
|
252
|
-
uri = URI::RFC2396_Parser.new
|
253
|
-
url =
|
254
|
-
URI(
|
255
|
-
uri.escape(
|
256
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
257
|
-
Trophonius.config.database
|
258
|
-
}/layouts/#{layout_name}/_find?_limit=1"
|
259
|
-
)
|
260
|
-
)
|
261
|
-
new_field_data = {}
|
262
|
-
create_translations if @configuration.translations.keys.empty?
|
263
|
-
field_data.keys.each do |k|
|
264
|
-
if @configuration.translations.keys.include?(k.to_s)
|
265
|
-
new_field_data.merge!({ @configuration.translations[k.to_s].to_s => field_data[k] })
|
266
|
-
else
|
267
|
-
new_field_data.merge!({ k.to_s => field_data[k] })
|
268
|
-
end
|
269
|
-
end
|
270
|
-
body = { query: [new_field_data], limit: '100000' }.to_json
|
271
|
-
response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body)
|
272
|
-
|
273
|
-
if response['messages'][0]['code'] == '0'
|
274
|
-
r_results = response['response']['data']
|
275
|
-
ret_val = RecordSet.new(layout_name, non_modifiable_fields)
|
276
|
-
r_results.each do |r|
|
277
|
-
hash = build_result(r)
|
278
|
-
ret_val << hash
|
279
|
-
end
|
280
|
-
ret_val.first
|
281
|
-
else
|
282
|
-
if response['messages'][0]['code'] == '101' || response['messages'][0]['code'] == '401'
|
283
|
-
return RecordSet.new(layout_name, non_modifiable_fields)
|
284
|
-
end
|
285
|
-
|
286
|
-
Error.throw_error(response['messages'][0]['code'])
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
290
|
-
##
|
291
|
-
# Finds and returns a Record corresponding to the record_id
|
292
|
-
#
|
293
|
-
# @param [Integer] record_id: the record id to retrieve from FileMaker
|
294
|
-
#
|
295
|
-
# @return [Record] the record
|
296
|
-
def self.find(record_id)
|
297
|
-
uri = URI::RFC2396_Parser.new
|
298
|
-
url =
|
299
|
-
URI(
|
300
|
-
uri.escape(
|
301
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
302
|
-
Trophonius.config.database
|
303
|
-
}/layouts/#{layout_name}/records/#{record_id}"
|
304
|
-
)
|
305
|
-
)
|
306
|
-
response = Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}')
|
307
|
-
if response['messages'][0]['code'] == '0'
|
308
|
-
ret_val = build_result(response['response']['data'][0])
|
309
|
-
ret_val.send(:define_singleton_method, 'result_count') { 1 }
|
310
|
-
ret_val
|
311
|
-
else
|
312
|
-
Error.throw_error(response['messages'][0]['code'], record_id)
|
313
|
-
end
|
314
|
-
end
|
315
|
-
|
316
|
-
##
|
317
|
-
# Deletes a record from FileMaker
|
318
|
-
#
|
319
|
-
# @param [Integer] record_id: the record id to retrieve from FileMaker
|
320
|
-
#
|
321
|
-
# @return [Boolean] True if the delete was successful
|
322
|
-
def self.delete(record_id)
|
323
|
-
uri = URI::RFC2396_Parser.new
|
324
|
-
url =
|
325
|
-
URI(
|
326
|
-
uri.escape(
|
327
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
328
|
-
Trophonius.config.database
|
329
|
-
}/layouts/#{layout_name}/records/#{record_id}"
|
330
|
-
)
|
331
|
-
)
|
332
|
-
response = Request.make_request(url, "Bearer #{Request.get_token}", 'delete', '{}')
|
333
|
-
if response['messages'][0]['code'] == '0'
|
334
|
-
true
|
335
|
-
else
|
336
|
-
Error.throw_error(response['messages'][0]['code'])
|
337
|
-
end
|
338
|
-
end
|
339
|
-
|
340
|
-
##
|
341
|
-
# Edits a record in FileMaker
|
342
|
-
#
|
343
|
-
# @param [Integer] record_id: the record id to edit in FileMaker
|
344
|
-
#
|
345
|
-
# @param [Hash] fieldData: A hash containing the fields to edit and the new data to fill them with
|
346
|
-
#
|
347
|
-
# @return [Boolean] True if the delete was successful
|
348
|
-
def self.edit(record_id, field_data)
|
349
|
-
uri = URI::RFC2396_Parser.new
|
350
|
-
url =
|
351
|
-
URI(
|
352
|
-
uri.escape(
|
353
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
354
|
-
Trophonius.config.database
|
355
|
-
}/layouts/#{layout_name}/records/#{record_id}"
|
356
|
-
)
|
357
|
-
)
|
358
|
-
new_field_data = {}
|
359
|
-
create_translations if @configuration.translations.keys.empty?
|
360
|
-
field_data.keys.each do |k|
|
361
|
-
if @configuration.translations.keys.include?(k.to_s)
|
362
|
-
new_field_data.merge!({ @configuration.translations[k.to_s].to_s => field_data[k] })
|
363
|
-
else
|
364
|
-
new_field_data.merge!({ k.to_s => field_data[k] })
|
365
|
-
end
|
366
|
-
end
|
367
|
-
body = "{\"fieldData\": #{new_field_data.to_json}}"
|
368
|
-
response = Request.make_request(url, "Bearer #{Request.get_token}", 'patch', body)
|
369
|
-
response['messages'][0]['code'] == '0' ? true : Error.throw_error(response['messages'][0]['code'])
|
370
|
-
end
|
371
|
-
|
372
|
-
##
|
373
|
-
# Builds the resulting Record
|
374
|
-
#
|
375
|
-
# @param [JSON] result: the HTTP result from FileMaker
|
376
|
-
#
|
377
|
-
# @return [Record] A Record with singleton_methods for the fields where possible
|
378
|
-
def self.build_result(result)
|
379
|
-
hash = Trophonius::Record.new(name)
|
380
|
-
hash.record_id = result['recordId']
|
381
|
-
hash.layout_name = layout_name
|
382
|
-
hash.model_name = name
|
383
|
-
|
384
|
-
result['fieldData'].keys.each do |key|
|
385
|
-
# unless key[/\s/] || key[/\W/]
|
386
|
-
@configuration.translations.merge!(
|
387
|
-
{ ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase.to_s => key.to_s }
|
388
|
-
)
|
389
|
-
hash.send(:define_singleton_method, ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')) do
|
390
|
-
hash[key]
|
391
|
-
end
|
392
|
-
unless non_modifiable_fields&.include?(key)
|
393
|
-
@configuration.all_fields.merge!(
|
394
|
-
ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase =>
|
395
|
-
ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')
|
396
|
-
)
|
397
|
-
hash.send(
|
398
|
-
:define_singleton_method,
|
399
|
-
"#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')}="
|
400
|
-
) do |new_val|
|
401
|
-
hash[key] = new_val
|
402
|
-
hash.modifiable_fields[key] = new_val
|
403
|
-
hash.modified_fields[key] = new_val
|
404
|
-
end
|
405
|
-
end
|
406
|
-
# end
|
407
|
-
hash.merge!({ key => result['fieldData'][key] })
|
408
|
-
hash.modifiable_fields.merge!({ key => result['fieldData'][key] }) unless non_modifiable_fields&.include?(key)
|
409
|
-
end
|
410
|
-
result['portalData'].keys.each do |key|
|
411
|
-
unless key[/\s/] || key[/\W/]
|
412
|
-
hash.send(:define_singleton_method, ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')) do
|
413
|
-
hash[key]
|
414
|
-
end
|
415
|
-
end
|
416
|
-
result['portalData'][key].each do |inner_hash|
|
417
|
-
inner_hash.keys.each do |inner_key|
|
418
|
-
inner_method =
|
419
|
-
ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(inner_key.gsub(/\w+::/, '').to_s), separator: '_')
|
420
|
-
unless inner_method[/\s/] || inner_method[/\W/]
|
421
|
-
inner_hash.send(:define_singleton_method, inner_method.to_s) { inner_hash[inner_key] }
|
422
|
-
inner_hash.send(:define_singleton_method, 'record_id') { inner_hash['recordId'] }
|
423
|
-
end
|
424
|
-
end
|
425
|
-
end
|
426
|
-
hash.merge!({ key => result['portalData'][key] })
|
427
|
-
end
|
428
|
-
hash
|
429
|
-
end
|
430
|
-
|
431
|
-
##
|
432
|
-
# Retrieve the first record from FileMaker from the context of the Model.
|
433
|
-
#
|
434
|
-
# @return [Record]: a Record corresponding to the FileMaker record.
|
435
|
-
def self.first
|
436
|
-
results = Request.retrieve_first(layout_name)
|
437
|
-
if results['messages'][0]['code'] == '0'
|
438
|
-
r_results = results['response']['data']
|
439
|
-
ret_val = r_results.empty? ? Trophonius::Record.new : build_result(r_results[0])
|
440
|
-
ret_val.send(:define_singleton_method, 'result_count') { r_results.empty? ? 0 : 1 }
|
441
|
-
ret_val
|
442
|
-
else
|
443
|
-
Error.throw_error(results['messages'][0]['code'])
|
444
|
-
end
|
445
|
-
end
|
446
|
-
|
447
|
-
##
|
448
|
-
# Runs a FileMaker script from the context of the Model.
|
449
|
-
#
|
450
|
-
# @param [String] script: the FileMaker script to run
|
451
|
-
#
|
452
|
-
# @param [String] scriptparameter: the parameter required by the FileMaker script
|
453
|
-
#
|
454
|
-
# @return [String]: string representing the script result returned by FileMaker
|
455
|
-
def self.run_script(script: '', scriptparameter: '')
|
456
|
-
result = Request.run_script(script, scriptparameter, layout_name)
|
457
|
-
if result['messages'][0]['code'] != '0'
|
458
|
-
Error.throw_error(result['messages'][0]['code'])
|
459
|
-
elsif result['response']['scriptResult'] == '403'
|
460
|
-
Error.throw_error(403)
|
461
|
-
else
|
462
|
-
result['response']['scriptResult']
|
463
|
-
|
464
|
-
end
|
465
|
-
end
|
466
|
-
|
467
|
-
##
|
468
|
-
# Retrieve the first 10000000 records from FileMaker from the context of the Model.
|
469
|
-
#
|
470
|
-
# @param [Hash] sort: a hash containing the fields to sort by and the direction to sort in (optional)
|
471
|
-
#
|
472
|
-
# @return [RecordSet]: a RecordSet containing all the Record objects that correspond to the FileMaker records.
|
473
|
-
def self.all(sort: {})
|
474
|
-
uri = URI::RFC2396_Parser.new
|
475
|
-
results = Request.retrieve_all(layout_name, sort)
|
476
|
-
count = results['response']['scriptResult'].to_i
|
477
|
-
url = if @limit.empty? || @offset.empty?
|
478
|
-
URI(
|
479
|
-
uri.escape(
|
480
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
481
|
-
Trophonius.config.database
|
482
|
-
}/layouts/#{layout_name}/records?_limit=#{count == 0 ? 1_000_000 : count}"
|
483
|
-
)
|
484
|
-
)
|
485
|
-
else
|
486
|
-
URI(
|
487
|
-
uri.escape(
|
488
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
489
|
-
Trophonius.config.database
|
490
|
-
}/layouts/#{layout_name}/records?_offset=#{@offset}&_limit=#{@limit}"
|
491
|
-
)
|
492
|
-
)
|
493
|
-
end
|
494
|
-
@limit = ''
|
495
|
-
@offset = ''
|
496
|
-
results = Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}')
|
497
|
-
if results['messages'][0]['code'] == '0'
|
498
|
-
r_results = results['response']['data']
|
499
|
-
ret_val = RecordSet.new(layout_name, non_modifiable_fields)
|
500
|
-
r_results.each do |r|
|
501
|
-
hash = build_result(r)
|
502
|
-
ret_val << hash
|
503
|
-
end
|
504
|
-
ret_val.result_count = count
|
505
|
-
ret_val
|
506
|
-
else
|
507
|
-
Error.throw_error(results['messages'][0]['code'])
|
508
|
-
end
|
509
|
-
end
|
510
|
-
end
|
511
|
-
end
|
data/lib/trophonius_request.rb
DELETED
@@ -1,173 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'base64'
|
4
|
-
require 'trophonius_connection'
|
5
|
-
require 'uri'
|
6
|
-
require 'net/http'
|
7
|
-
module Trophonius
|
8
|
-
module Trophonius::Request
|
9
|
-
##
|
10
|
-
# Crafts and runs a HTTP request of any type
|
11
|
-
#
|
12
|
-
# @param [URI] urlparam: the url to make the request to
|
13
|
-
#
|
14
|
-
# @param [String] auth: the authentication required for the request
|
15
|
-
#
|
16
|
-
# @param [String] method: the type of HTTP request to make (i.e. get)
|
17
|
-
#
|
18
|
-
# @param [JSONString] body: the body of the HTTP request
|
19
|
-
#
|
20
|
-
# @param [String] params: optional parameters added to the request
|
21
|
-
#
|
22
|
-
# @return [JSON] parsed json of the response
|
23
|
-
def self.make_request(url_param, auth, method, body, params = '')
|
24
|
-
ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
|
25
|
-
ssl_verifypeer = !Trophonius.config.local_network
|
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
|
-
temp = request.run
|
37
|
-
begin
|
38
|
-
JSON.parse(temp.response_body)
|
39
|
-
rescue Exception => e
|
40
|
-
puts e
|
41
|
-
puts e.backtrace
|
42
|
-
Error.throw_error('1631')
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
##
|
47
|
-
# Crafts and runs a HTTP request for uploading a file to a container
|
48
|
-
#
|
49
|
-
# @param [URI] urlparam: the url to make the request to
|
50
|
-
#
|
51
|
-
# @param [String] auth: the authentication required for the request
|
52
|
-
#
|
53
|
-
# @param [Tempfile or File] file: file to upload
|
54
|
-
#
|
55
|
-
# @return [JSON] parsed json of the response
|
56
|
-
def self.upload_file_request(url_param, auth, file)
|
57
|
-
url = URI(url_param.to_s)
|
58
|
-
|
59
|
-
https = Net::HTTP.new(url.host, url.port)
|
60
|
-
https.use_ssl = true
|
61
|
-
|
62
|
-
request = Net::HTTP::Post.new(url)
|
63
|
-
request['Authorization'] = auth.to_s
|
64
|
-
request['Content-Type'] = 'multipart/form-data;'
|
65
|
-
form_data = [['upload', file]]
|
66
|
-
request.set_form form_data, 'multipart/form-data'
|
67
|
-
response = https.request(request)
|
68
|
-
begin
|
69
|
-
JSON.parse(response.read_body)
|
70
|
-
rescue Exception
|
71
|
-
Error.throw_error('1631')
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
##
|
76
|
-
# Gets the current FileMaker token
|
77
|
-
#
|
78
|
-
# @return [String] a valid FileMaker token
|
79
|
-
def self.get_token
|
80
|
-
Connection.valid_connection? ? Connection.token : Connection.connect
|
81
|
-
end
|
82
|
-
|
83
|
-
##
|
84
|
-
# Retrieves the first record from FileMaker
|
85
|
-
#
|
86
|
-
# @return [JSON] The first record from FileMaker
|
87
|
-
def self.retrieve_first(layout_name)
|
88
|
-
uri = URI::RFC2396_Parser.new
|
89
|
-
url =
|
90
|
-
URI(
|
91
|
-
uri.escape(
|
92
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
93
|
-
Trophonius.config.database
|
94
|
-
}/layouts/#{layout_name}/records?_limit=1"
|
95
|
-
)
|
96
|
-
)
|
97
|
-
make_request(url, "Bearer #{get_token}", 'get', '{}')
|
98
|
-
end
|
99
|
-
|
100
|
-
##
|
101
|
-
# Retrieves the fieldnames of a layout
|
102
|
-
#
|
103
|
-
# @return [JSON] The fieldnames of a layout
|
104
|
-
def self.get_layout_field_names(layout_name)
|
105
|
-
uri = URI::RFC2396_Parser.new
|
106
|
-
url =
|
107
|
-
URI(
|
108
|
-
uri.escape(
|
109
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
110
|
-
Trophonius.config.database
|
111
|
-
}/layouts/#{layout_name}"
|
112
|
-
)
|
113
|
-
)
|
114
|
-
begin
|
115
|
-
make_request(url, "Bearer #{get_token}", 'get', '{}')['response']['fieldMetaData'].map { |field| field['name'] }
|
116
|
-
rescue StandardError => e
|
117
|
-
puts e
|
118
|
-
puts e.backtrace
|
119
|
-
Error.throw_error('1631')
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
##
|
124
|
-
# Runs a FileMaker script
|
125
|
-
#
|
126
|
-
# @return [JSON] The script result from FileMaker
|
127
|
-
def self.run_script(script, scriptparameter, layout_name)
|
128
|
-
uri = URI::RFC2396_Parser.new
|
129
|
-
url =
|
130
|
-
URI(
|
131
|
-
uri.escape(
|
132
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
133
|
-
Trophonius.config.database
|
134
|
-
}/layouts/#{layout_name}/records?_limit=1&script=#{script}&script.param=#{scriptparameter}"
|
135
|
-
)
|
136
|
-
)
|
137
|
-
make_request(url, "Bearer #{get_token}", 'get', '{}')
|
138
|
-
end
|
139
|
-
|
140
|
-
##
|
141
|
-
# Retrieves the 10000000 records from FileMaker
|
142
|
-
#
|
143
|
-
# @return [JSON] The first 10000000 records from FileMaker
|
144
|
-
def self.retrieve_all(layout_name, sort)
|
145
|
-
uri = URI::RFC2396_Parser.new
|
146
|
-
if sort.empty?
|
147
|
-
url =
|
148
|
-
URI(
|
149
|
-
uri.escape(
|
150
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
151
|
-
Trophonius.config.database
|
152
|
-
}/layouts/#{layout_name}/records?_limit=10000000#{
|
153
|
-
Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"
|
154
|
-
}"
|
155
|
-
)
|
156
|
-
)
|
157
|
-
else
|
158
|
-
sort_order = sort.to_json.to_s
|
159
|
-
url =
|
160
|
-
URI(
|
161
|
-
uri.escape(
|
162
|
-
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
163
|
-
Trophonius.config.database
|
164
|
-
}/layouts/#{layout_name}/records?_limit=10000000&_sort=#{sort_order}#{
|
165
|
-
Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"
|
166
|
-
}"
|
167
|
-
)
|
168
|
-
)
|
169
|
-
end
|
170
|
-
make_request(url, "Bearer #{get_token}", 'get', '{}')
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
data/lib/trophonius_time.rb
DELETED
File without changes
|
File without changes
|