Class: Zendesk2::SearchOrganization

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/zendesk2/search_organization.rb

Instance Attribute Summary collapse

Attributes included from Request

#params

Instance Method Summary collapse

Methods included from Request

cistern_included, #data, #delete!, #error!, #find!, #html_url_for, included, #mock_response, #page, #page_params!, #page_params?, #pluralize, #real, #real_request, #request_body, #request_params, #request_path, #resources, #response, #timestamp, #url_for

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
end

Instance Method Details

#call(query, params = {}) ⇒ Object



13
14
15
16
# File 'lib/zendesk2/search_organization.rb', line 13

def call(query, params = {})
  @query = query
  super(params)
end

#mockObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/zendesk2/search_organization.rb', line 18

def mock
  terms = Hash[query.split(' ').map { |t| t.split(':') }]
  terms.delete('type') # context already provided

  collection = data[:organizations].values

  # organization name is fuzzy matched
  organization_name = terms.delete('name')
  organization_name && terms['name'] = "*#{organization_name}*"

  compiled_terms = terms.inject({}) do |r, (term, raw_condition)|
    condition = if raw_condition.include?('*')
                  Regexp.compile(raw_condition.gsub('*', '.*'), Regexp::IGNORECASE)
                else
                  raw_condition
                end
    r.merge(term => condition)
  end

  results = collection.select do |v|
    compiled_terms.all? do |term, condition|
      condition.is_a?(Regexp) ? condition.match(v[term.to_s]) : v[term.to_s].to_s == condition.to_s
    end
  end

  page(results, params: { 'query' => query }, root: 'results')
end