trophonius 1.2.6.4 → 1.3.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 +4 -4
- data/lib/trophonius_config.rb +1 -0
- data/lib/trophonius_model.rb +31 -3
- data/lib/trophonius_query.rb +29 -8
- data/lib/trophonius_record.rb +30 -0
- data/lib/trophonius_request.rb +20 -1
- data/lib/trophonius_single.rb +216 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 193ec5a1a1a35715519760565271544fe7d14e56670ff533b30449db17a9739f
|
4
|
+
data.tar.gz: 7b241ecfc24897f44d288fbb912c22fb950b1e645eddb1ed4b2cccaeb6022198
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c4e65f40f0f8e484ebf35418c9ec26b998f0c9cbd4610e449ec5f0c0abf2c49daec033b8c0929b8f15979f785a461218607b4f075a4d877b3dab6637f50c57e
|
7
|
+
data.tar.gz: 7b64990a532ca0944514746d78b56adc39ea5b175c3d683ff284a09a101ad4f15fb94c391e8fee67223baf50bb4d5ad2dc6cb34c593f9cf8abf0a5b0fe3e5395
|
data/lib/trophonius_config.rb
CHANGED
@@ -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) { [] }
|
data/lib/trophonius_model.rb
CHANGED
@@ -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
|
-
|
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
|
|
@@ -166,7 +175,7 @@ module Trophonius
|
|
166
175
|
#
|
167
176
|
# @return [Record] the created record
|
168
177
|
# Model.create(fieldOne: "Data")
|
169
|
-
def self.create(fieldData)
|
178
|
+
def self.create(fieldData, portalData: {})
|
170
179
|
url =
|
171
180
|
URI(
|
172
181
|
URI.escape(
|
@@ -184,7 +193,26 @@ module Trophonius
|
|
184
193
|
new_field_data.merge!({ "#{k}" => fieldData[k] })
|
185
194
|
end
|
186
195
|
end
|
187
|
-
|
196
|
+
|
197
|
+
new_portal_data = {}
|
198
|
+
portalData.each do |portal_name, portal_values|
|
199
|
+
new_portal_data.merge!(
|
200
|
+
portal_name =>
|
201
|
+
portal_values.map do |record|
|
202
|
+
record.inject({}) do |new_hash, (key, value)|
|
203
|
+
new_hash["#{portal_name}::#{key}"] = value
|
204
|
+
new_hash
|
205
|
+
end
|
206
|
+
end
|
207
|
+
)
|
208
|
+
end
|
209
|
+
|
210
|
+
body =
|
211
|
+
if new_portal_data != {}
|
212
|
+
"{\"fieldData\": #{new_field_data.to_json}, \"portalData\": #{new_portal_data.to_json}}"
|
213
|
+
else
|
214
|
+
"{\"fieldData\": #{new_field_data.to_json} }"
|
215
|
+
end
|
188
216
|
response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body)
|
189
217
|
if response['messages'][0]['code'] != '0'
|
190
218
|
if response['messages'][0]['code'] == '102'
|
data/lib/trophonius_query.rb
CHANGED
@@ -42,6 +42,24 @@ module Trophonius
|
|
42
42
|
@current_query
|
43
43
|
end
|
44
44
|
|
45
|
+
##
|
46
|
+
# Returns the current portal limits
|
47
|
+
#
|
48
|
+
# @return [Hash] Hash representing the portal limits
|
49
|
+
def build_portal_limits()
|
50
|
+
@portal_limits ||= {}
|
51
|
+
end
|
52
|
+
|
53
|
+
##
|
54
|
+
# Adds a portal limit to the request
|
55
|
+
#
|
56
|
+
# @param [args] arguments containing a Hash with the format {portalName: requiredLimit}
|
57
|
+
# @return [Trophonius::Model] updated base model
|
58
|
+
def set_portal_limits(args)
|
59
|
+
args[1].current_query.build_portal_limits.merge!(args[0])
|
60
|
+
args[1]
|
61
|
+
end
|
62
|
+
|
45
63
|
##
|
46
64
|
# Adds a find request to the original query, resulting in an "Or" find-request for FileMaker
|
47
65
|
#
|
@@ -107,6 +125,7 @@ module Trophonius
|
|
107
125
|
}/layouts/#{@trophonius_model.layout_name}/_find"
|
108
126
|
)
|
109
127
|
)
|
128
|
+
|
110
129
|
new_field_data = @current_query.map { |_q| {} }
|
111
130
|
|
112
131
|
@trophonius_model.create_translations if @trophonius_model.translations.keys.empty?
|
@@ -120,21 +139,23 @@ module Trophonius
|
|
120
139
|
end
|
121
140
|
end
|
122
141
|
if @offset.nil? || @limit.nil? || @offset == '' || @limit == '' || @offset == 0 || @limit == 0
|
123
|
-
body =
|
124
|
-
if @current_sort.nil?
|
125
|
-
{ query: new_field_data, limit: '100000' }.to_json
|
126
|
-
else
|
127
|
-
{ query: new_field_data, sort: @current_sort, limit: '100000' }.to_json
|
128
|
-
end
|
142
|
+
body = @current_sort.nil? ? { query: new_field_data, limit: '100000' } : { query: new_field_data, sort: @current_sort, limit: '100000' }
|
129
143
|
else
|
130
144
|
body =
|
131
145
|
if @current_sort.nil?
|
132
|
-
{ query: new_field_data, limit: @limit.to_s, offset: @offset.to_s }
|
146
|
+
{ query: new_field_data, limit: @limit.to_s, offset: @offset.to_s }
|
133
147
|
else
|
134
|
-
{ query: new_field_data, sort: @current_sort, limit: @limit.to_s, offset: @offset.to_s }
|
148
|
+
{ query: new_field_data, sort: @current_sort, limit: @limit.to_s, offset: @offset.to_s }
|
135
149
|
end
|
136
150
|
end
|
137
151
|
|
152
|
+
if @portal_limits
|
153
|
+
portal_hash = { portal: @portal_limits.map { |portal_name, limit| "#{portal_name}" } }
|
154
|
+
body.merge!(portal_hash)
|
155
|
+
@portal_limits.each { |portal_name, limit| body.merge!({ "limit.#{portal_name}" => limit.to_s }) }
|
156
|
+
end
|
157
|
+
|
158
|
+
body = body.to_json
|
138
159
|
response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body)
|
139
160
|
if response['messages'][0]['code'] != '0'
|
140
161
|
if response['messages'][0]['code'] == '101' || response['messages'][0]['code'] == '401'
|
data/lib/trophonius_record.rb
CHANGED
@@ -152,6 +152,36 @@ module Trophonius
|
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
155
|
+
##
|
156
|
+
# Runs a FileMaker script from the context of the Model.
|
157
|
+
#
|
158
|
+
# @param [String] script: the FileMaker script to run
|
159
|
+
#
|
160
|
+
# @param [String] scriptparameter: the parameter required by the FileMaker script
|
161
|
+
#
|
162
|
+
# @return [String]: string representing the script result returned by FileMaker
|
163
|
+
def run_script(script: '', scriptparameter: '')
|
164
|
+
url =
|
165
|
+
URI(
|
166
|
+
URI.escape(
|
167
|
+
"http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
|
168
|
+
Trophonius.config.database
|
169
|
+
}/layouts/#{layout_name}/records/#{record_id}?script=#{script}&script.param=#{scriptparameter}"
|
170
|
+
)
|
171
|
+
)
|
172
|
+
|
173
|
+
result = Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}')
|
174
|
+
|
175
|
+
if result['messages'][0]['code'] != '0'
|
176
|
+
Error.throw_error(result['messages'][0]['code'])
|
177
|
+
elsif result['response']['scriptResult'] == '403'
|
178
|
+
Error.throw_error(403)
|
179
|
+
else
|
180
|
+
ret_val = result['response']['scriptResult']
|
181
|
+
return ret_val || true
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
155
185
|
##
|
156
186
|
# Saves the last changes made to the Record to FileMaker.
|
157
187
|
# Throws a FileMaker error if save failed
|
data/lib/trophonius_request.rb
CHANGED
@@ -37,7 +37,6 @@ module Trophonius
|
|
37
37
|
begin
|
38
38
|
JSON.parse(temp.response_body)
|
39
39
|
rescue Exception => e
|
40
|
-
puts "Error was #{e}"
|
41
40
|
Error.throw_error('1631')
|
42
41
|
end
|
43
42
|
end
|
@@ -95,6 +94,26 @@ module Trophonius
|
|
95
94
|
make_request(url, "Bearer #{get_token}", 'get', '{}')
|
96
95
|
end
|
97
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
|
+
|
98
117
|
##
|
99
118
|
# Runs a FileMaker script
|
100
119
|
#
|
@@ -0,0 +1,216 @@
|
|
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
|
+
close_connection(token)
|
34
|
+
Error.throw_error(response['messages'][0]['code'])
|
35
|
+
elsif response['messages'][0]['code'] == '401'
|
36
|
+
close_connection(token)
|
37
|
+
return RecordSet.new(@config[:layout_name], @config[:non_modifiable_fields])
|
38
|
+
else
|
39
|
+
ret_val = RecordSet.new(@config[:layout_name], @config[:non_modifiable_fields])
|
40
|
+
r_results.each do |r|
|
41
|
+
hash = build_result(r)
|
42
|
+
ret_val << hash
|
43
|
+
end
|
44
|
+
end
|
45
|
+
@query = {}
|
46
|
+
close_connection(token)
|
47
|
+
|
48
|
+
return ret_val
|
49
|
+
end
|
50
|
+
|
51
|
+
def run_script(script:, scriptparameter:)
|
52
|
+
url =
|
53
|
+
URI(
|
54
|
+
URI.escape(
|
55
|
+
"http#{@config[:ssl] == true ? 's' : ''}://#{@config[:host]}/fmi/data/v1/databases/#{@config[:database]}/layouts/#{
|
56
|
+
@config[:layout_name]
|
57
|
+
}/records?_limit=1&script=#{script}&script.param=#{scriptparameter}"
|
58
|
+
)
|
59
|
+
)
|
60
|
+
|
61
|
+
token = setup_connection
|
62
|
+
result = make_request(url, "Bearer #{token}", 'get', '{}')
|
63
|
+
ret_val = ''
|
64
|
+
|
65
|
+
if result['messages'][0]['code'] != '0'
|
66
|
+
close_connection(token)
|
67
|
+
Error.throw_error(result['messages'][0]['code'])
|
68
|
+
elsif result['response']['scriptResult'] == '403'
|
69
|
+
close_connection(token)
|
70
|
+
Error.throw_error(403)
|
71
|
+
else
|
72
|
+
ret_val = result['response']['scriptResult']
|
73
|
+
end
|
74
|
+
|
75
|
+
close_connection(token)
|
76
|
+
|
77
|
+
return ret_val
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def build_result(result)
|
83
|
+
hash = Trophonius::Record.new
|
84
|
+
hash.record_id = result['recordId']
|
85
|
+
hash.layout_name = @config[:layout_name]
|
86
|
+
hash.model_name = 'Single'
|
87
|
+
|
88
|
+
result['fieldData'].keys.each do |key|
|
89
|
+
# unless key[/\s/] || key[/\W/]
|
90
|
+
@translations.merge!(
|
91
|
+
{ "#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase}" => "#{key}" }
|
92
|
+
)
|
93
|
+
hash.send(:define_singleton_method, ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')) do
|
94
|
+
hash[key]
|
95
|
+
end
|
96
|
+
unless @config[:non_modifiable_fields]&.include?(key)
|
97
|
+
@all_fields.merge!(
|
98
|
+
ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase =>
|
99
|
+
ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')
|
100
|
+
)
|
101
|
+
hash.send(
|
102
|
+
:define_singleton_method,
|
103
|
+
"#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')}="
|
104
|
+
) do |new_val|
|
105
|
+
hash[key] = new_val
|
106
|
+
hash.modifiable_fields[key] = new_val
|
107
|
+
hash.modified_fields[key] = new_val
|
108
|
+
end
|
109
|
+
end
|
110
|
+
# end
|
111
|
+
hash.merge!({ key => result['fieldData'][key] })
|
112
|
+
hash.modifiable_fields.merge!({ key => result['fieldData'][key] }) unless @config[:non_modifiable_fields]&.include?(key)
|
113
|
+
end
|
114
|
+
result['portalData'].keys.each do |key|
|
115
|
+
unless key[/\s/] || key[/\W/]
|
116
|
+
hash.send(:define_singleton_method, ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')) do
|
117
|
+
hash[key]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
result['portalData'][key].each_with_index do |inner_hash|
|
121
|
+
inner_hash.keys.each do |inner_key|
|
122
|
+
inner_method =
|
123
|
+
ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(inner_key.gsub(/\w+::/, '').to_s), separator: '_')
|
124
|
+
unless inner_method[/\s/] || inner_method[/\W/]
|
125
|
+
inner_hash.send(:define_singleton_method, inner_method.to_s) { inner_hash[inner_key] }
|
126
|
+
inner_hash.send(:define_singleton_method, 'record_id') { inner_hash['recordId'] }
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
hash.merge!({ key => result['portalData'][key] })
|
131
|
+
end
|
132
|
+
return hash
|
133
|
+
end
|
134
|
+
|
135
|
+
def make_request(url_param, token, method, body, params = '')
|
136
|
+
ssl_verifyhost = @config[:local_network] ? 0 : 2
|
137
|
+
ssl_verifypeer = !@config[:local_network]
|
138
|
+
request =
|
139
|
+
Typhoeus::Request.new(
|
140
|
+
url_param,
|
141
|
+
method: method.to_sym,
|
142
|
+
body: body,
|
143
|
+
params: params,
|
144
|
+
ssl_verifyhost: ssl_verifyhost,
|
145
|
+
ssl_verifypeer: ssl_verifypeer,
|
146
|
+
headers: { 'Content-Type' => 'application/json', Authorization: "Bearer #{token}" }
|
147
|
+
)
|
148
|
+
temp = request.run
|
149
|
+
begin
|
150
|
+
JSON.parse(temp.response_body)
|
151
|
+
rescue Exception => e
|
152
|
+
close_connection(token)
|
153
|
+
Error.throw_error('1631')
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def setup_connection
|
158
|
+
ssl_verifyhost = @config[:local_network] ? 0 : 2
|
159
|
+
ssl_verifypeer = !@config[:local_network]
|
160
|
+
url = URI(URI.escape("http#{@config[:ssl] == true ? 's' : ''}://#{@config[:host]}/fmi/data/v1/databases/#{@config[:database]}/sessions"))
|
161
|
+
request =
|
162
|
+
Typhoeus::Request.new(
|
163
|
+
url,
|
164
|
+
method: :post,
|
165
|
+
body:
|
166
|
+
if @config[:external_name].nil? || @config[:external_name].empty?
|
167
|
+
{}
|
168
|
+
else
|
169
|
+
{
|
170
|
+
fmDataSource: [{ database: @config[:external_name], username: @config[:external_username], password: @config[:external_password] }]
|
171
|
+
}.to_json
|
172
|
+
end,
|
173
|
+
params: {},
|
174
|
+
ssl_verifyhost: ssl_verifyhost,
|
175
|
+
ssl_verifypeer: ssl_verifypeer,
|
176
|
+
headers: {
|
177
|
+
'Content-Type' => 'application/json', Authorization: "Basic #{Base64.strict_encode64("#{@config[:username]}:#{@config[:password]}")}"
|
178
|
+
}
|
179
|
+
)
|
180
|
+
temp = request.run
|
181
|
+
begin
|
182
|
+
parsed = JSON.parse(temp.response_body)
|
183
|
+
rescue Exception => e
|
184
|
+
Error.throw_error('1631')
|
185
|
+
end
|
186
|
+
Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
|
187
|
+
return parsed['response']['token']
|
188
|
+
end
|
189
|
+
|
190
|
+
def close_connection(token)
|
191
|
+
url =
|
192
|
+
URI(URI.escape("http#{@config[:ssl] == true ? 's' : ''}://#{@config[:host]}/fmi/data/v1/databases/#{@config[:database]}/sessions/#{token}"))
|
193
|
+
ssl_verifyhost = @config[:local_network] ? 0 : 2
|
194
|
+
ssl_verifypeer = !@config[:local_network]
|
195
|
+
|
196
|
+
request =
|
197
|
+
Typhoeus::Request.new(
|
198
|
+
url,
|
199
|
+
method: :delete,
|
200
|
+
params: {},
|
201
|
+
ssl_verifyhost: ssl_verifyhost,
|
202
|
+
ssl_verifypeer: ssl_verifypeer,
|
203
|
+
headers: { 'Content-Type' => 'application/json' }
|
204
|
+
)
|
205
|
+
temp = request.run
|
206
|
+
|
207
|
+
begin
|
208
|
+
parsed = JSON.parse(temp.response_body)
|
209
|
+
rescue Exception => e
|
210
|
+
Error.throw_error('1631')
|
211
|
+
end
|
212
|
+
Error.throw_error(parsed['messages'][0]['code']) if parsed['messages'][0]['code'] != '0'
|
213
|
+
return true
|
214
|
+
end
|
215
|
+
end
|
216
|
+
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.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kempen Automatisering
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/trophonius_recordset.rb
|
72
72
|
- lib/trophonius_redis_manager.rb
|
73
73
|
- lib/trophonius_request.rb
|
74
|
+
- lib/trophonius_single.rb
|
74
75
|
- lib/trophonius_time.rb
|
75
76
|
homepage: https://github.com/Willem-Jan/Trophonius
|
76
77
|
licenses:
|