Class: Close::Filter
- Inherits:
-
Object
- Object
- Close::Filter
- Extended by:
- APIOperations
- Defined in:
- lib/close/filter.rb
Constant Summary collapse
- @@queries =
{}
Class Method Summary collapse
-
.add_query(name, query_body) ⇒ Void
This method is used to defined a query at run time.
-
.apply_params(query_string, params) ⇒ String
Applies the params to the query string.
-
.execute(query = {}) ⇒ Array
Executes a raw query against the Close API.
-
.find_params(str) ⇒ Array
Scans a string a returns the parameters it will expect when executed.
-
.load_query(name) ⇒ String
Loads a query from a file or from memory.
-
.load_query_from_file(name) ⇒ String
Loads a predefined query from a file.
-
.preflight_params(params, expected_params) ⇒ Void
Check that all of the params are present in expected_params.
-
.run(name, params = {}) ⇒ Array
Executes a query by name.
Methods included from APIOperations
Class Method Details
.add_query(name, query_body) ⇒ Void
This method is used to defined a query at run time. If a name collision occurs, the query will be overwritten.
49 50 51 |
# File 'lib/close/filter.rb', line 49 def self.add_query(name, query_body) @@queries[name.to_s] = query_body.to_json end |
.apply_params(query_string, params) ⇒ String
Applies the params to the query string.
57 58 59 60 61 62 63 |
# File 'lib/close/filter.rb', line 57 def self.apply_params(query_string, params) qs = query_string.dup params.each do |key, value| qs.gsub!(/%#{key.upcase}%/, value) end qs end |
.execute(query = {}) ⇒ Array
Executes a raw query against the Close API.
16 17 18 19 |
# File 'lib/close/filter.rb', line 16 def self.execute(query = {}) response = request(:post, 'api/v1/data/search/', query) response['data'] end |
.find_params(str) ⇒ Array
Scans a string a returns the parameters it will expect when executed.
95 96 97 |
# File 'lib/close/filter.rb', line 95 def self.find_params(str) str.scan(/%[A-Z]+(?:_[A-Z]+)*%/).map{ |x| x[1..-2].downcase }.uniq end |
.load_query(name) ⇒ String
Loads a query from a file or from memory.
36 37 38 39 40 41 42 |
# File 'lib/close/filter.rb', line 36 def self.load_query(name) if @@queries[name.to_s] @@queries[name.to_s] else load_query_from_file(name) end end |
.load_query_from_file(name) ⇒ String
Loads a predefined query from a file.
82 83 84 85 86 87 88 89 |
# File 'lib/close/filter.rb', line 82 def self.load_query_from_file(name) begin file = File.read(File.join(File.dirname(__FILE__), "data/filters/#{name}.json")) #file = File.read(File.expand_path("lib/close/data/filters/#{name}.json", __dir__)) rescue Errno::ENOENT raise Close::QueryNotFoundError.new("Query #{name} not found.") end end |
.preflight_params(params, expected_params) ⇒ Void
Check that all of the params are present in expected_params.
70 71 72 73 74 75 76 |
# File 'lib/close/filter.rb', line 70 def self.preflight_params(params, expected_params) expected_params.each do |param| if !params.transform_keys(&:to_s).has_key?(param.to_s) raise Close::MissingParameterError, "Missing parameter: #{param}" end end end |
.run(name, params = {}) ⇒ Array
Executes a query by name.
25 26 27 28 29 30 31 |
# File 'lib/close/filter.rb', line 25 def self.run(name, params = {}) query_string = load_query_from_file(name) expected_params = find_params(query_string) preflight_params(params, expected_params) parameterized_query = apply_params(query_string, params) execute(parameterized_query) end |