Class: Trophonius::Query
- Inherits:
-
Object
- Object
- Trophonius::Query
- Defined in:
- lib/trophonius_query.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#build_portal_limits ⇒ Hash
Returns the current portal limits.
-
#build_query ⇒ Array[Hash]
Returns the current query, creates an empty query if no current query exists.
-
#build_sort ⇒ Array[Hash]
Returns the current sort order, creates an empty sort order if no current sort order exists.
-
#initialize(trophonius_model:, limit:, offset:) ⇒ Trophonius::Query
constructor
Creates a new instance of the Trophonius::Query class.
- #inspect ⇒ Object (also: #to_s)
-
#not(args) ⇒ Trophonius::Model
Adds an omit request to the original query, resulting in an “omit” find for FileMaker.
-
#or(args) ⇒ Trophonius::Model
Adds a find request to the original query, resulting in an “Or” find-request for FileMaker.
-
#paginate(args) ⇒ Trophonius::Model
Sets or updates the limit and offset for a query.
-
#run_query(method, *args, &block) ⇒ Object
Performs the query in FileMaker.
-
#set_portal_limits(args) ⇒ Trophonius::Model
Adds a portal limit to the request.
-
#sort(args) ⇒ Trophonius::Model
Adds a sort request to the original query, resulting in an “sorted” query.
Constructor Details
#initialize(trophonius_model:, limit:, offset:) ⇒ Trophonius::Query
Creates a new instance of the Trophonius::Query class
18 19 20 21 22 23 |
# File 'lib/trophonius_query.rb', line 18 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 end |
Instance Attribute Details
#response ⇒ Object (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_limits ⇒ Hash
Returns the current portal limits
49 50 51 |
# File 'lib/trophonius_query.rb', line 49 def build_portal_limits() @portal_limits ||= {} end |
#build_query ⇒ Array[Hash]
Returns the current query, creates an empty query if no current query exists
29 30 31 |
# File 'lib/trophonius_query.rb', line 29 def build_query @current_query ||= [{}] end |
#build_sort ⇒ Array[Hash]
Returns the current sort order, creates an empty sort order if no current sort order exists
37 38 39 |
# File 'lib/trophonius_query.rb', line 37 def build_sort @current_sort ||= [] end |
#inspect ⇒ Object Also known as: to_s
41 42 43 |
# File 'lib/trophonius_query.rb', line 41 def inspect @current_query end |
#not(args) ⇒ Trophonius::Model
Adds an omit request to the original query, resulting in an “omit” find for FileMaker
78 79 80 81 |
# File 'lib/trophonius_query.rb', line 78 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
68 69 70 71 |
# File 'lib/trophonius_query.rb', line 68 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
88 89 90 91 92 |
# File 'lib/trophonius_query.rb', line 88 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/trophonius_query.rb', line 119 def run_query(method, *args, &block) 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 @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| 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
58 59 60 61 |
# File 'lib/trophonius_query.rb', line 58 def set_portal_limits(args) args[1].current_query.build_portal_limits.merge!(args[0]) args[1] end |
#sort(args) ⇒ Trophonius::Model
Adds a sort request to the original query, resulting in an “sorted” query
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/trophonius_query.rb', line 99 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 |