trophonius 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/trophonius_model.rb +52 -9
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6fdab7a86338046934a0fe85d3745072bf6c039ada8846b463662e2bba64001
4
- data.tar.gz: fafd0b28308fa6cba72ae44c4c5b09fe2530f1128546850e7e7e9467f68102d0
3
+ metadata.gz: 8b2cf487b05747dd90e7c4e0bd6b98f0b8ab2175c7c11da8ea69acd06c11eaab
4
+ data.tar.gz: 0ea9bfe33c04b6ef4a26966afd7baaee43c874b89ac1a6ca3b5aa9576a5693d6
5
5
  SHA512:
6
- metadata.gz: a62b3fa93dd8262b5cc06a6565e1feda26aa7ae9cf31907f94a6f023c38dbf9f7e2aa6936fce77e4618e0eb3abbbf9f6b596f0c786f129460bc69a1cea6683e2
7
- data.tar.gz: 79485e8a01856c16afde75cd8a5fac25bdf6f3846c89d8099d9444cb34fe7d1fed3b2957e2c4c543b42b4dd599abe071e62c2c2b0b5acdd747938e39dd6d1605
6
+ metadata.gz: a566aaf863a1d39dbd5496a6321c34ec8c5536afb0f5d735b9ad3a829584e81a6c58a7238cc96d5a3317c40f2efeac92c76039e5731d5244afc8ffae5934f1a7
7
+ data.tar.gz: e0ba771d1ee7f466eb5eee6660b06e48c10516ca4e9c12e74a35149ca578e35a8c7da63fcb2d6d3ad2be27c2c96a60d1f747be36c6ee799c41f620c6b05708f6
@@ -7,8 +7,6 @@ require "trophonius_error"
7
7
  module Trophonius
8
8
  # 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.
9
9
  class Trophonius::Model
10
- # Contains all the fields on the model (modifiable and non_modifiable)
11
- attr_reader :all_fields
12
10
 
13
11
  ##
14
12
  # Sets up the configuration for the model.
@@ -19,7 +17,8 @@ module Trophonius
19
17
  @configuration ||= Configuration.new
20
18
  @configuration.layout_name = configuration[:layout_name]
21
19
  @configuration.non_modifiable_fields = configuration[:non_modifiable_fields]
22
- @all_fields = {}
20
+ @configuration.all_fields = {}
21
+ @configuration.translations = {}
23
22
  end
24
23
 
25
24
  ##
@@ -34,6 +33,16 @@ module Trophonius
34
33
  @configuration.non_modifiable_fields
35
34
  end
36
35
 
36
+ ##
37
+ # Returns the translations of the fields
38
+ def self.translations
39
+ @configuration.translations
40
+ end
41
+
42
+ def self.create_translations
43
+ self.first
44
+ end
45
+
37
46
  ##
38
47
  # Creates and saves a record in FileMaker
39
48
  #
@@ -43,7 +52,18 @@ module Trophonius
43
52
  # Model.create(fieldOne: "Data")
44
53
  def self.create(fieldData)
45
54
  url = URI("http#{Trophonius.config.ssl == true ? "s" : ""}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records")
46
- body = "{\"fieldData\": #{fieldData.to_json}}"
55
+ new_field_data = {}
56
+ if @configuration.translations.keys.empty?
57
+ create_translations
58
+ end
59
+ fieldData.keys.each do |k|
60
+ if @configuration.translations.keys.include?(k.to_s)
61
+ new_field_data.merge!({"#{@configuration.translations[k.to_s]}" => fieldData[k]})
62
+ else
63
+ new_field_data.merge!({"#{k}" => fieldData[k]})
64
+ end
65
+ end
66
+ body = "{\"fieldData\": #{new_field_data.to_json}}"
47
67
  response = Request.make_request(url, "Bearer #{Request.get_token}", "post", body)
48
68
  if response["messages"][0]["code"] != "0"
49
69
  Error.throw_error(response["messages"][0]["code"])
@@ -66,7 +86,18 @@ module Trophonius
66
86
  # Model.where(fieldOne: "Data")
67
87
  def self.where(fieldData)
68
88
  url = URI("http#{Trophonius.config.ssl == true ? "s" : ""}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{self.layout_name}/_find")
69
- body = {query: [fieldData], limit:"100000"}.to_json
89
+ new_field_data = {}
90
+ if @configuration.translations.keys.empty?
91
+ create_translations
92
+ end
93
+ fieldData.keys.each do |k|
94
+ if @configuration.translations.keys.include?(k.to_s)
95
+ new_field_data.merge!({"#{@configuration.translations[k.to_s]}" => fieldData[k]})
96
+ else
97
+ new_field_data.merge!({"#{k}" => fieldData[k]})
98
+ end
99
+ end
100
+ body = {query: [new_field_data], limit:"100000"}.to_json
70
101
  response = Request.make_request(url, "Bearer #{Request.get_token}", "post", body)
71
102
  if response["messages"][0]["code"] != "0"
72
103
  return RecordSet.new(self.layout_name, self.non_modifiable_fields) if response["messages"][0]["code"] == "101" || response["messages"][0]["code"] == "401"
@@ -125,10 +156,21 @@ module Trophonius
125
156
  #
126
157
  # @param [Hash] fieldData: A hash containing the fields to edit and the new data to fill them with
127
158
  #
128
- # @return [Boolean] True if the delete was successful
159
+ # @return [Boolean] True if the delete was successful
129
160
  def self.edit(record_id, fieldData)
130
161
  url = URI("http#{Trophonius.config.ssl == true ? "s" : ""}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}")
131
- body = "{\"fieldData\": #{fieldData.to_json}}"
162
+ new_field_data = {}
163
+ if @configuration.translations.keys.empty?
164
+ create_translations
165
+ end
166
+ fieldData.keys.each do |k|
167
+ if @configuration.translations.keys.include?(k.to_s)
168
+ new_field_data.merge!({"#{@configuration.translations[k.to_s]}" => fieldData[k]})
169
+ else
170
+ new_field_data.merge!({"#{k}" => fieldData[k]})
171
+ end
172
+ end
173
+ body = "{\"fieldData\": #{new_field_data.to_json}}"
132
174
  response = Request.make_request(url, "Bearer #{Request.get_token}", "patch", body)
133
175
  if response["messages"][0]["code"] != "0"
134
176
  Error.throw_error(response["messages"][0]["code"])
@@ -149,11 +191,12 @@ module Trophonius
149
191
  hash.layout_name = layout_name
150
192
  result["fieldData"].keys.each do |key|
151
193
  # unless key[/\s/] || key[/\W/]
194
+ @configuration.translations.merge!({ "#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase}" => "#{key}" })
152
195
  hash.send(:define_singleton_method, ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')) do
153
196
  hash[key]
154
197
  end
155
198
  unless non_modifiable_fields&.include?(key)
156
- @all_fields.merge!(ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase => ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_'))
199
+ @configuration.all_fields.merge!(ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase => ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_'))
157
200
  hash.send(:define_singleton_method, "#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')}=") do |new_val|
158
201
  hash[key] = new_val
159
202
  hash.modifiable_fields[key] = new_val
@@ -196,7 +239,7 @@ module Trophonius
196
239
  Error.throw_error(results["messages"][0]["code"])
197
240
  else
198
241
  r_results = results["response"]["data"]
199
- ret_val = r_results.empty? ? Trophonius::Record.new({}) : build_result(r_results[0])
242
+ ret_val = r_results.empty? ? Trophonius::Record.new() : build_result(r_results[0])
200
243
  ret_val.send(:define_singleton_method, "result_count") do
201
244
  r_results.empty? ? 0 : 1
202
245
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trophonius
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kempen Automatisering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-13 00:00:00.000000000 Z
11
+ date: 2019-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus