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:) ⇒ Trophonius::Query

Creates a new instance of the Trophonius::Query class

Parameters:



16
17
18
19
# File 'lib/trophonius_query.rb', line 16

def initialize(trophonius_model:)
  @response = RecordSet.new(trophonius_model.layout_name, trophonius_model.non_modifiable_fields)
  @trophonius_model = trophonius_model
end

Instance Attribute Details

#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_queryArray[Hash]

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

Returns:

  • (Array[Hash])

    array representing the FileMaker find request



25
26
27
# File 'lib/trophonius_query.rb', line 25

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

#inspectObject Also known as: to_s



29
30
31
# File 'lib/trophonius_query.rb', line 29

def inspect
  @current_query
end

#not(args) ⇒ Trophonius::Model

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

Parameters:

  • arguments (args)

    containing a Hash containing the FileMaker omit request, and the base model object for the query

Returns:



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

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

Parameters:

  • arguments (args)

    containing a Hash containing the FileMaker find request, and the base model object for the query

Returns:



38
39
40
41
# File 'lib/trophonius_query.rb', line 38

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

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

Performs the query in FileMaker

Parameters:

  • original (method)

    called method, will be called on the response

  • original (*args)

    arguments, will be passed to the method call

  • original (&block)

    block, will be passed to the method call

Returns:

  • Response of the called method



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/trophonius_query.rb', line 61

def run_query(method, *args, &block)
  url = URI("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| {} }
  if @trophonius_model.translations.keys.empty?
    @trophonius_model.create_translations
  end
  @current_query.each_with_index do |query, index|
    query.keys.each do |k|
      if @trophonius_model.translations.keys.include?(k.to_s)
        new_field_data[index].merge!({"#{@trophonius_model.translations[k.to_s]}" => query[k].to_s})
      else
        new_field_data[index].merge!({"#{k}" => query[k].to_s})
      end
    end
  end
  body = {query: new_field_data, limit:"100000"}.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"
      RecordSet.new(@trophonius_model.layout_name, @trophonius_model.non_modifiable_fields).send(method, *args, &block)
      return
    else
      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|
      hash = @trophonius_model.build_result(r)
      ret_val << hash
    end
    @response = ret_val
    return @response.send(method, *args, &block)
  end
end