class Zendesk2::SearchOrganization

Attributes

query[R]

Public Instance Methods

call(query, params = {}) click to toggle source
Calls superclass method Zendesk2::Request#call
# File lib/zendesk2/search_organization.rb, line 13
def call(query, params = {})
  @query = query
  super(params)
end
mock() click to toggle source
# 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