trophonius 1.2.7 → 1.4.0

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: 36a94743894d1780d9502732ea12d63065c0d45df4553d84dc37a766600a7dc4
4
- data.tar.gz: d3aa3192857584c40fefe7596998039cf3c9497f88532985ee7d0b36cf4dc1db
3
+ metadata.gz: 5cd1b995810aa5284fcf855c4e4398d232c8c89ddccc7198ae52b96141dd7d5f
4
+ data.tar.gz: d63843e3b6c432e52ae9b94b49a0e75307a8680be92f997b4a34b045728334f1
5
5
  SHA512:
6
- metadata.gz: c7fefc47d7faaadc70d2dbe0ff406164eee1e8994dfaf8dd32991c35223ffe545d815585b42aeb6a121046dbc0ba472e4600ab84f62fa114ffd953f9c91d1598
7
- data.tar.gz: 7623409b3827a3e5fee64302a593d232761ef3a84f6212371121e7100b8d3fa70bcacf12eaca95d4f85fd542da255a547c256b8fece821471061f9eba323a4d3
6
+ metadata.gz: 75eafee6437ab8b647abf164f80cdf144a0a21dca37963e94b3cdea1ed6ebb48c470b332544a96dc0c085b16a737e3a0e15a178ca6d3ac5e15bfef1f0bd51c7b
7
+ data.tar.gz: 4a6b0d5eb0d552855e087b5d2388fe1b58da5f425cd251805c95adc7863e5c53c52fbf1a2796877a0c1814e9bd9df10c4fd2f7450692941d3c22e2439cbaef3a
@@ -0,0 +1,30 @@
1
+ require 'rails'
2
+
3
+ module Trophonius
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+ namespace 'trophonius'
6
+
7
+ class_option :host, type: :string, default: 'location_to.your_filemakerserver.com'
8
+ class_option :database, type: :string, default: 'Name_of_your_database'
9
+
10
+ source_root File.expand_path('../templates', __FILE__)
11
+
12
+ desc 'add the config file'
13
+
14
+ def copy_initializer_file
15
+ @host = options['host']
16
+ @database = options['database']
17
+ create_file 'config/initializers/trophonius.rb',
18
+ "Trophonius.configure do |config|
19
+ config.host = '#{@host}'
20
+ config.database = '#{@database}'
21
+ config.username = Rails.application.credentials.dig(:username) # (requires >= Rails 5.2) otherwise use old secrets
22
+ config.password = Rails.application.credentials.dig(:password) # (requires >= Rails 5.2) otherwise use old secrets
23
+ config.redis_connection = false # default false, true if you want to store the token in redis
24
+ config.ssl = true # or false depending on whether https or http should be used
25
+ # USE THE NEXT OPTION WITH CAUTION
26
+ config.local_network = false # if true the ssl certificate will not be verified to allow for self-signed certificates
27
+ end"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails'
2
+
3
+ module Trophonius
4
+ class ModelGenerator < ::Rails::Generators::Base
5
+ namespace 'trophonius_model'
6
+
7
+ class_option :model, type: :string, default: 'MyModel'
8
+ class_option :layout, type: :string, default: 'MyModelsLayout'
9
+
10
+ source_root File.expand_path('../templates', __FILE__)
11
+
12
+ desc 'add the config file'
13
+
14
+ def copy_model_file
15
+ @model = options['model']
16
+ @layout = options['layout']
17
+ create_file "app/models/#{@model.downcase}.rb",
18
+ "class #{@model.humanize} < Trophonius::Model
19
+ config layout_name: '#{@layout}'
20
+ end"
21
+ end
22
+ end
23
+ end
@@ -175,7 +175,7 @@ module Trophonius
175
175
  #
176
176
  # @return [Record] the created record
177
177
  # Model.create(fieldOne: "Data")
178
- def self.create(fieldData)
178
+ def self.create(fieldData, portalData: {})
179
179
  url =
180
180
  URI(
181
181
  URI.escape(
@@ -193,7 +193,26 @@ module Trophonius
193
193
  new_field_data.merge!({ "#{k}" => fieldData[k] })
194
194
  end
195
195
  end
196
- body = "{\"fieldData\": #{new_field_data.to_json}}"
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
197
216
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body)
198
217
  if response['messages'][0]['code'] != '0'
199
218
  if response['messages'][0]['code'] == '102'
@@ -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 }.to_json
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 }.to_json
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'
@@ -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
@@ -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,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trophonius
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.7
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kempen Automatisering
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2019-12-16 00:00:00.000000000 Z
@@ -54,11 +54,13 @@ dependencies:
54
54
  version: '5.0'
55
55
  description: A lightweight, easy to use link between Ruby (on Rails) and FileMaker
56
56
  using the FileMaker Data-API.
57
- email:
57
+ email:
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - lib/generators/trophonius_generator.rb
63
+ - lib/generators/trophonius_model_generator.rb
62
64
  - lib/trophonius.rb
63
65
  - lib/trophonius_config.rb
64
66
  - lib/trophonius_connection.rb
@@ -71,12 +73,13 @@ files:
71
73
  - lib/trophonius_recordset.rb
72
74
  - lib/trophonius_redis_manager.rb
73
75
  - lib/trophonius_request.rb
76
+ - lib/trophonius_single.rb
74
77
  - lib/trophonius_time.rb
75
78
  homepage: https://github.com/Willem-Jan/Trophonius
76
79
  licenses:
77
80
  - MIT
78
81
  metadata: {}
79
- post_install_message:
82
+ post_install_message:
80
83
  rdoc_options: []
81
84
  require_paths:
82
85
  - lib
@@ -92,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
95
  version: '0'
93
96
  requirements: []
94
97
  rubygems_version: 3.0.3
95
- signing_key:
98
+ signing_key:
96
99
  specification_version: 4
97
100
  summary: Link between Ruby (on Rails) and FileMaker.
98
101
  test_files: []