trophonius 1.0.10 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39d58cd73fd9aa4eb16eb71bae7ee950169af5f1d67909d1ad872b1836e21701
4
- data.tar.gz: facd02e4e8d4e60953ebb3fc94591ac9e5c67b964c572afa78c5a2e4f19acadc
3
+ metadata.gz: 21d0e929509f72eea54b627ad8a221d067799a50a0018cabfe1822f81af59e98
4
+ data.tar.gz: 8f3b25aa6e74b6d8de7aa79df84eb309644fd5cffc5546e12286cbad6adfcb37
5
5
  SHA512:
6
- metadata.gz: c3068d5fa17f5d8f88baf1a08f4b9e82da6fbc0e9c21ea938c6b6e8b49c93774a3f15c708af6e3a2a7fd413737bed9ae95e1d5c9554daa7a3170e17a37e967b3
7
- data.tar.gz: 6bfdb0a45ccca549a011bebb0b0ed738e46f67a6e315f2c79a29cc1348685c91dd83b2212aa75f9486caec9b8d0a2099366486bab6907b64d8fe02f2d2785325
6
+ metadata.gz: 2cd21fb288d2148692f5b4173fca976a65a4d4a1ab8768fd08c38aae7bb917b70941c3efd5e4d10e8dc1362d0519135d7811cbd81ac334dc970f55bf34e8f167
7
+ data.tar.gz: dad64504f016d6ffc426f38fcec744530810b9e4d8dfc33c725820f737ea33230fdaed536ebf2e9675c9f655f39c31772193c53df4273cc8d60f209e85723f1c
data/lib/trophonius.rb CHANGED
@@ -4,6 +4,7 @@ require 'trophonius_config'
4
4
 
5
5
  module Trophonius # :nodoc:
6
6
  def self.configuration
7
+ Ethon.logger = Logger.new(nil)
7
8
  @configuration ||= Configuration.new
8
9
  end
9
10
 
@@ -2,14 +2,21 @@ require "json"
2
2
  require "trophonius_config"
3
3
  require "trophonius_record"
4
4
  require "trophonius_recordset"
5
+ require "trophonius_query"
5
6
  require "trophonius_error"
6
7
 
7
8
  module Trophonius
8
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.
9
10
  class Trophonius::Model
11
+ attr_reader :configuration
12
+ attr_accessor :current_query
13
+
14
+ def initialize(config:)
15
+ @configuration = config
16
+ end
10
17
 
11
18
  ##
12
- # Sets up the configuration for the model.
19
+ # Sets up the configuration for the model.
13
20
  #
14
21
  # @param [Hash] configuration: the hash containing the config to setup the model correctly.
15
22
  # configuration = {layout_name: "theFileMakerLayoutForThisModel", non_modifiable_fields: ["an", "array", "containing", "calculation_fields", "etc."]}
@@ -23,29 +30,83 @@ module Trophonius
23
30
 
24
31
  ##
25
32
  # Returns the FileMaker layout this Model corresponds to
33
+ #
34
+ # @return [String] layout name of the model
26
35
  def self.layout_name
27
36
  @configuration.layout_name
28
37
  end
29
-
38
+
30
39
  ##
31
40
  # Returns the fields that FileMaker won't allow us to modify
41
+ #
42
+ # @return [[Array]] fields that FileMaker won't allow us to modify
32
43
  def self.non_modifiable_fields
33
44
  @configuration.non_modifiable_fields
34
45
  end
35
46
 
36
47
  ##
37
48
  # Returns the translations of the fields
49
+ #
50
+ # @return [Hash] translations of the fields Rails -> FileMaker
38
51
  def self.translations
39
52
  @configuration.translations
40
53
  end
41
-
54
+
55
+ ##
56
+ # creates Rails -> FileMaker field translations by requesting the first record
57
+ #
58
+ # @return [Hash] translations of the fields Rails -> FileMaker
42
59
  def self.create_translations
43
60
  self.first
61
+ @configuration.translations
62
+ end
63
+
64
+ def self.method_missing(method, *args, &block)
65
+ new_instance = Trophonius::Model.new(config: @configuration)
66
+ new_instance.current_query = Trophonius::Query.new(trophonius_model: self)
67
+ # new_instance.current_query.build_query[0].merge!(fieldData)
68
+ args << new_instance
69
+ if new_instance.current_query.respond_to?(method)
70
+ new_instance.current_query.send(method, args)
71
+ end
72
+ end
73
+
74
+ def method_missing(method, *args, &block)
75
+ if @current_query.respond_to?(method)
76
+ args << self
77
+ @current_query.send(method, args)
78
+ elsif @current_query.response.respond_to?(method)
79
+ @current_query.run_query(method, *args, &block)
80
+ end
81
+ end
82
+
83
+ ##
84
+ # Finds all records in FileMaker corresponding to the requested query
85
+ # @param [Hash] fieldData: the data to find
86
+ #
87
+ # @return [Trophonius::Model] new instance of the model
88
+ def self.where(fieldData)
89
+ new_instance = Trophonius::Model.new(config: @configuration)
90
+ new_instance.current_query = Trophonius::Query.new(trophonius_model: self)
91
+ new_instance.current_query.build_query[0].merge!(fieldData)
92
+ new_instance
93
+ end
94
+
95
+ ##
96
+ # Finds all records in FileMaker corresponding to the requested query
97
+ # This method is created to enable where chaining
98
+ #
99
+ # @param [Hash] fieldData: the data to find
100
+ #
101
+ # @return [Trophonius::Model] new instance of the model
102
+ def where(fieldData)
103
+ @current_query.build_query[0].merge!(fieldData)
104
+ self
44
105
  end
45
106
 
46
107
  ##
47
108
  # Creates and saves a record in FileMaker
48
- #
109
+ #
49
110
  # @param [Hash] fieldData: the fields to fill with the data
50
111
  #
51
112
  # @return [Record] the created record
@@ -76,16 +137,16 @@ module Trophonius
76
137
  return ret_val
77
138
  end
78
139
  end
79
-
140
+
80
141
  ##
81
- # Finds and returns a RecordSet containing the records fitting the find request
82
- #
142
+ # Finds and returns the first Record containing fitting the find request
143
+ #
83
144
  # @param [Hash] fieldData: the data to find
84
145
  #
85
- # @return [RecordSet] a RecordSet containing all the Record objects that correspond to FileMaker records fitting the find request
86
- # Model.where(fieldOne: "Data")
87
- def self.where(fieldData)
88
- url = URI("http#{Trophonius.config.ssl == true ? "s" : ""}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{self.layout_name}/_find")
146
+ # @return [Record] a Record object that correspond to FileMaker record fitting the find request
147
+ # Model.find_by(fieldOne: "Data")
148
+ def self.find_by(fieldData)
149
+ url = URI("http#{Trophonius.config.ssl == true ? "s" : ""}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{self.layout_name}/_find?_limit=1")
89
150
  new_field_data = {}
90
151
  if @configuration.translations.keys.empty?
91
152
  create_translations
@@ -109,15 +170,15 @@ module Trophonius
109
170
  hash = build_result(r)
110
171
  ret_val << hash
111
172
  end
112
- return ret_val
173
+ return ret_val.first
113
174
  end
114
175
  end
115
176
 
116
177
  ##
117
178
  # Finds and returns a Record corresponding to the record_id
118
- #
179
+ #
119
180
  # @param [Integer] record_id: the record id to retrieve from FileMaker
120
- #
181
+ #
121
182
  # @return [Record] the record
122
183
  def self.find(record_id)
123
184
  url = URI("http#{Trophonius.config.ssl == true ? "s" : ""}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}")
@@ -135,10 +196,10 @@ module Trophonius
135
196
 
136
197
  ##
137
198
  # Deletes a record from FileMaker
138
- #
199
+ #
139
200
  # @param [Integer] record_id: the record id to retrieve from FileMaker
140
201
  #
141
- # @return [Boolean] True if the delete was successful
202
+ # @return [Boolean] True if the delete was successful
142
203
  def self.delete(record_id)
143
204
  url = URI("http#{Trophonius.config.ssl == true ? "s" : ""}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}")
144
205
  response = Request.make_request(url, "Bearer #{Request.get_token}", "delete", "{}")
@@ -151,7 +212,7 @@ module Trophonius
151
212
 
152
213
  ##
153
214
  # Edits a record in FileMaker
154
- #
215
+ #
155
216
  # @param [Integer] record_id: the record id to edit in FileMaker
156
217
  #
157
218
  # @param [Hash] fieldData: A hash containing the fields to edit and the new data to fill them with
@@ -181,7 +242,7 @@ module Trophonius
181
242
 
182
243
  ##
183
244
  # Builds the resulting Record
184
- #
245
+ #
185
246
  # @param [JSON] result: the HTTP result from FileMaker
186
247
  #
187
248
  # @return [Record] A Record with singleton_methods for the fields where possible
@@ -250,7 +311,7 @@ module Trophonius
250
311
  ##
251
312
  # Runs a FileMaker script from the context of the Model.
252
313
  #
253
- # @param [String] script: the FileMaker script to run
314
+ # @param [String] script: the FileMaker script to run
254
315
  #
255
316
  # @param [String] scriptparameter: the parameter required by the FileMaker script
256
317
  #
@@ -270,7 +331,7 @@ module Trophonius
270
331
  ##
271
332
  # Retrieve the first 10000000 records from FileMaker from the context of the Model.
272
333
  #
273
- # @param [Hash] sort: a hash containing the fields to sort by and the direction to sort in (optional)
334
+ # @param [Hash] sort: a hash containing the fields to sort by and the direction to sort in (optional)
274
335
  #
275
336
  # @return [RecordSet]: a RecordSet containing all the Record objects that correspond to the FileMaker records.
276
337
  def self.all(sort: {})
@@ -0,0 +1,99 @@
1
+ require "json"
2
+ require "trophonius_config"
3
+ require "trophonius_record"
4
+ require "trophonius_recordset"
5
+ require "trophonius_error"
6
+
7
+ module Trophonius
8
+ class Trophonius::Query
9
+ attr_reader :response
10
+
11
+ ##
12
+ # Creates a new instance of the Trophonius::Query class
13
+ #
14
+ # @param [Trophonius::Model] base model for the new query
15
+ # @return [Trophonius::Query] new instance of Trophonius::Query
16
+ def initialize(trophonius_model:)
17
+ @response = RecordSet.new(trophonius_model.layout_name, trophonius_model.non_modifiable_fields)
18
+ @trophonius_model = trophonius_model
19
+ end
20
+
21
+ ##
22
+ # Returns the current query, creates an empty query if no current query exists
23
+ #
24
+ # @return [Array[Hash]] array representing the FileMaker find request
25
+ def build_query
26
+ @current_query ||= [{}]
27
+ end
28
+
29
+ def inspect
30
+ @current_query
31
+ end
32
+
33
+ ##
34
+ # Adds a find request to the original query, resulting in an "Or" find-request for FileMaker
35
+ #
36
+ # @param [args] arguments containing a Hash containing the FileMaker find request, and the base model object for the query
37
+ # @return [Trophonius::Model] updated base model
38
+ def or(args)
39
+ args[1].current_query.build_query << args[0]
40
+ args[1]
41
+ end
42
+
43
+ ##
44
+ # Adds an omit request to the original query, resulting in an "omit" find for FileMaker
45
+ #
46
+ # @param [args] arguments containing a Hash containing the FileMaker omit request, and the base model object for the query
47
+ # @return [Trophonius::Model] updated base model
48
+ def not(args)
49
+ args[1].current_query.build_query << args[0].merge!({omit: true})
50
+ args[1]
51
+ end
52
+
53
+ ##
54
+ # Performs the query in FileMaker
55
+ #
56
+ # @param [method] original called method, will be called on the response
57
+ # @param [*args] original arguments, will be passed to the method call
58
+ # @param [&block] original block, will be passed to the method call
59
+ #
60
+ # @return Response of the called method
61
+ def run_query(method, *args, &block)
62
+ url = URI("http#{Trophonius.config.ssl == true ? "s" : ""}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{@trophonius_model.layout_name}/_find")
63
+ new_field_data = @current_query.map { |q| {} }
64
+ if @trophonius_model.translations.keys.empty?
65
+ @trophonius_model.create_translations
66
+ end
67
+ @current_query.each_with_index do |query, index|
68
+ query.keys.each do |k|
69
+ if @trophonius_model.translations.keys.include?(k.to_s)
70
+ new_field_data[index].merge!({"#{@trophonius_model.translations[k.to_s]}" => query[k].to_s})
71
+ else
72
+ new_field_data[index].merge!({"#{k}" => query[k].to_s})
73
+ end
74
+ end
75
+ end
76
+ body = {query: new_field_data, limit:"100000"}.to_json
77
+ response = Request.make_request(url, "Bearer #{Request.get_token}", "post", body)
78
+ if response["messages"][0]["code"] != "0"
79
+ if response["messages"][0]["code"] == "101" || response["messages"][0]["code"] == "401"
80
+ RecordSet.new(@trophonius_model.layout_name, @trophonius_model.non_modifiable_fields).send(method, *args, &block)
81
+ return
82
+ else
83
+ Error.throw_error(response["messages"][0]["code"])
84
+ end
85
+ else
86
+ r_results = response["response"]["data"]
87
+ ret_val = RecordSet.new(@trophonius_model.layout_name, @trophonius_model.non_modifiable_fields)
88
+ r_results.each do |r|
89
+ hash = @trophonius_model.build_result(r)
90
+ ret_val << hash
91
+ end
92
+ @response = ret_val
93
+ return @response.send(method, *args, &block)
94
+ end
95
+ end
96
+
97
+ alias_method :to_s, :inspect
98
+ end
99
+ 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.10
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kempen Automatisering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-03 00:00:00.000000000 Z
11
+ date: 2019-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -64,6 +64,7 @@ files:
64
64
  - lib/trophonius_connection.rb
65
65
  - lib/trophonius_error.rb
66
66
  - lib/trophonius_model.rb
67
+ - lib/trophonius_query.rb
67
68
  - lib/trophonius_record.rb
68
69
  - lib/trophonius_recordset.rb
69
70
  - lib/trophonius_request.rb