Class: Close::Filter

Inherits:
Object
  • Object
show all
Extended by:
APIOperations
Defined in:
lib/close/filter.rb

Constant Summary collapse

@@queries =
{}

Class Method Summary collapse

Methods included from APIOperations

request

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.

Parameters:

  • name (String)

    The name of the query.

  • query_body (Hash)

    A hash with placeholders in keys.

Returns:

  • (Void)


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.

Parameters:

  • query_string (String)

    The stringified JSON query.

  • params (Hash)

    The parameters to apply.

Returns:

  • (String)

    The stringified JSON query with the parameters applied.



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.

Parameters:

  • query (Hash) (defaults to: {})

    The query to execute.

Returns:

  • (Array)

    An array of results.



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.

Parameters:

  • str (String)

    The string to scan.

Returns:

  • (Array)

    An array of parameters.



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.

Parameters:

  • name (String)

    The name of the query.

Returns:

  • (String)

    A stringified JSON query.



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.

Parameters:

  • name (String)

    The name of the query.

Returns:

  • (String)

    A stringified JSON query.

Raises:



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.

Parameters:

  • params (Hash)

    The parameters to check.

  • expected_params (Array)

    The expected parameters.

Returns:

  • (Void)

Raises:



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.

Parameters:

  • name (String)

    The name of the query to execute.

  • params (Hash) (defaults to: {})

    The parameters to pass to the query.

Returns:

  • (Array)

    An array of results.



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