Class: Trophonius::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/trophonius_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trophonius_model:, limit:, offset:) ⇒ Trophonius::Query

Creates a new instance of the Trophonius::Query class



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/trophonius_query.rb', line 19

def initialize(trophonius_model:, limit:, offset:)
  @response = RecordSet.new(trophonius_model.layout_name, trophonius_model.non_modifiable_fields)
  @trophonius_model = trophonius_model
  @limit = limit
  @offset = offset
  @presort_script = ''
  @presort_scriptparam = ''
  @prerequest_script = ''
  @prerequest_scriptparam = ''
  @post_request_script = ''
  @post_request_scriptparam = ''
end

Instance Attribute Details

#post_request_scriptObject

Returns the value of attribute post_request_script.



10
11
12
# File 'lib/trophonius_query.rb', line 10

def post_request_script
  @post_request_script
end

#post_request_scriptparamObject

Returns the value of attribute post_request_scriptparam.



10
11
12
# File 'lib/trophonius_query.rb', line 10

def post_request_scriptparam
  @post_request_scriptparam
end

#prerequest_scriptObject

Returns the value of attribute prerequest_script.



10
11
12
# File 'lib/trophonius_query.rb', line 10

def prerequest_script
  @prerequest_script
end

#prerequest_scriptparamObject

Returns the value of attribute prerequest_scriptparam.



10
11
12
# File 'lib/trophonius_query.rb', line 10

def prerequest_scriptparam
  @prerequest_scriptparam
end

#presort_scriptObject

Returns the value of attribute presort_script.



10
11
12
# File 'lib/trophonius_query.rb', line 10

def presort_script
  @presort_script
end

#presort_scriptparamObject

Returns the value of attribute presort_scriptparam.



10
11
12
# File 'lib/trophonius_query.rb', line 10

def presort_scriptparam
  @presort_scriptparam
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/trophonius_query.rb', line 9

def response
  @response
end

Instance Method Details

#build_portal_limitsHash

Returns the current portal limits



56
57
58
# File 'lib/trophonius_query.rb', line 56

def build_portal_limits()
  @portal_limits ||= {}
end

#build_queryArray[Hash]

Returns the current query, creates an empty query if no current query exists



36
37
38
# File 'lib/trophonius_query.rb', line 36

def build_query
  @current_query ||= [{}]
end

#build_sortArray[Hash]

Returns the current sort order, creates an empty sort order if no current sort order exists



44
45
46
# File 'lib/trophonius_query.rb', line 44

def build_sort
  @current_sort ||= []
end

#inspectObject Also known as: to_s



48
49
50
# File 'lib/trophonius_query.rb', line 48

def inspect
  @current_query
end

#not(args) ⇒ Trophonius::Model

Adds an omit request to the original query, resulting in an “omit” find for FileMaker



145
146
147
148
# File 'lib/trophonius_query.rb', line 145

def not(args)
  args[1].current_query.build_query << args[0].merge!(omit: true)
  args[1]
end

#or(args) ⇒ Trophonius::Model

Adds a find request to the original query, resulting in an “Or” find-request for FileMaker



135
136
137
138
# File 'lib/trophonius_query.rb', line 135

def or(args)
  args[1].current_query.build_query << args[0]
  args[1]
end

#paginate(args) ⇒ Trophonius::Model

Sets or updates the limit and offset for a query



155
156
157
158
159
# File 'lib/trophonius_query.rb', line 155

def paginate(args)
  @offset = ((args[0] * args[1] - args[1]) + 1)
  @limit = args[1]
  args[2]
end

#run_query(method, *args, &block) ⇒ Object

Performs the query in FileMaker



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/trophonius_query.rb', line 186

def run_query(method, *args, &block)
  uri = URI::RFC2396_Parser.new
  url =
    URI(
      uri.escape(
        "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{
          Trophonius.config.database
        }/layouts/#{@trophonius_model.layout_name}/_find"
      )
    )
  new_field_data = @current_query.map { |_q| {} }

  @trophonius_model.create_translations if @trophonius_model.translations.keys.empty?
  @current_query.each_with_index do |query, index|
    query.keys.each do |k|
      if @trophonius_model.translations.key?(k.to_s)
        new_field_data[index].merge!(@trophonius_model.translations[k.to_s].to_s => query[k].to_s)
      else
        new_field_data[index].merge!(k.to_s => query[k].to_s)
      end
    end
  end
  if @offset.nil? || @limit.nil? || @offset == '' || @limit == '' || @offset == 0 || @limit == 0
    body = @current_sort.nil? ? { query: new_field_data, limit: '100000' } : { query: new_field_data, sort: @current_sort, limit: '100000' }
  else
    body =
      if @current_sort.nil?
        { query: new_field_data, limit: @limit.to_s, offset: @offset.to_s }
      else
        { query: new_field_data, sort: @current_sort, limit: @limit.to_s, offset: @offset.to_s }
      end
  end

  if @post_request_script.present?
    body.merge!(script: @post_request_script)
    body.merge!('script.param' => @post_request_scriptparam) if @post_request_scriptparam.present?
  end

  if @prerequest_script.present?
    body.merge!("script.prerequest" => @prerequest_script)
    body.merge!('script.prerequest.param' => @prerequest_scriptparam) if @prerequest_scriptparam.present?
  end

  if @presort_script.present?
    body.merge!("script.presort" => @presort_script)
    body.merge!('script.presort.param' => @presort_scriptparam) if @presort_scriptparam.present?
  end

  if @portal_limits
    portal_hash = { portal: @portal_limits.map { |portal_name, limit| "#{portal_name}" } }
    body.merge!(portal_hash)
    @portal_limits.each { |portal_name, limit| body.merge!({ "limit.#{portal_name}" => limit.to_s }) }
  end

  body = body.to_json
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body)

  if response['messages'][0]['code'] != '0'
    if response['messages'][0]['code'] == '101' || response['messages'][0]['code'] == '401'
      resp = RecordSet.new(@trophonius_model.layout_name, @trophonius_model.non_modifiable_fields).send(method, *args, &block)
      return resp
    else
      if response['messages'][0]['code'] == '102'
        results = Request.retrieve_first(@trophonius_model.layout_name)
        if results['messages'][0]['code'] != '0'
          Error.throw_error('102')
        else
          r_results = results['response']['data']
          ret_val = r_results.empty? ? Error.throw_error('102') : r_results[0]['fieldData']
          query_keys = new_field_data.map { |q| q.keys.map(&:downcase) }.uniq
          Error.throw_error('102', (query_keys - ret_val.keys.map(&:downcase)).flatten.join(', '), @trophonius_model.layout_name)
        end
      end
      Error.throw_error(response['messages'][0]['code'])
    end
  else
    r_results = response['response']['data']
    ret_val = RecordSet.new(@trophonius_model.layout_name, @trophonius_model.non_modifiable_fields)

    r_results.each do |r|
      r['fieldData'].merge!('post_request_script_result' => response['response']['scriptResult']) if response['response']['scriptResult']

      if response['response']['scriptResult.presort']
        r['fieldData'].merge!('presort_script_result' => response['response']['scriptResult.presort'])
      end

      if response['response']['scriptResult.prerequest']
        r['fieldData'].merge!('prerequest_script_result' => response['response']['scriptResult.prerequest'])
      end

      r['fieldData'].merge!('post_request_script_error' => response['response']['scriptError']) if response['response']['scriptError']

      r['fieldData'].merge!('presort_script_error' => response['response']['scriptError.presort']) if response['response']['scriptError.presort']

      if response['response']['scriptError.prerequest']
        r['fieldData'].merge!('prerequest_script_error' => response['response']['scriptError.prerequest'])
      end

      hash = @trophonius_model.build_result(r)
      ret_val << hash
    end
    @response = ret_val
    return @response.send(method, *args, &block)
  end
end

#set_portal_limits(args) ⇒ Trophonius::Model

Adds a portal limit to the request



65
66
67
68
# File 'lib/trophonius_query.rb', line 65

def set_portal_limits(args)
  args[1].current_query.build_portal_limits.merge!(args[0])
  args[1]
end

#set_post_request_script(args) ⇒ Trophonius::Model

Adds a post-request script to the request



75
76
77
78
# File 'lib/trophonius_query.rb', line 75

def set_post_request_script(args)
  args[1].current_query.post_request_script = args[0]
  args[1]
end

#set_post_request_script_param(args) ⇒ Trophonius::Model

Adds a post-request scriptparameter to the request



85
86
87
88
# File 'lib/trophonius_query.rb', line 85

def set_post_request_script_param(args)
  args[1].current_query.post_request_scriptparam = args[0]
  args[1]
end

#set_prerequest_script(args) ⇒ Trophonius::Model

Adds a pre-request script to the request



95
96
97
98
# File 'lib/trophonius_query.rb', line 95

def set_prerequest_script(args)
  args[1].current_query.prerequest_script = args[0]
  args[1]
end

#set_prerequest_script_param(args) ⇒ Trophonius::Model

Adds a pre-request scriptparameter to the request



105
106
107
108
# File 'lib/trophonius_query.rb', line 105

def set_prerequest_script_param(args)
  args[1].current_query.prerequest_scriptparam = args[0]
  args[1]
end

#set_presort_script(args) ⇒ Trophonius::Model

Adds a pre-sort script to the request



115
116
117
118
# File 'lib/trophonius_query.rb', line 115

def set_presort_script(args)
  args[1].current_query.presort_script = args[0]
  args[1]
end

#set_presort_script_param(args) ⇒ Trophonius::Model

Adds a pre-request scriptparameter to the request



125
126
127
128
# File 'lib/trophonius_query.rb', line 125

def set_presort_script_param(args)
  args[1].current_query.presort_scriptparam = args[0]
  args[1]
end

#sort(args) ⇒ Trophonius::Model

Adds a sort request to the original query, resulting in an “sorted” query



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/trophonius_query.rb', line 166

def sort(args)
  @trophonius_model.create_translations if @trophonius_model.translations.keys.empty?
  args[0].each do |key, value|
    if @trophonius_model.translations.key?(key.to_s)
      args[1].current_query.build_sort << { fieldName: "#{@trophonius_model.translations[key.to_s]}", sortOrder: "#{value}" }
    else
      args[1].current_query.build_sort << { fieldName: "#{key}", sortOrder: "#{value}" }
    end
  end
  args[1]
end