Class: ZendeskSupportAPI::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/zendesk_support_api/search.rb

Overview

Class Method Summary collapse

Class Method Details

.bad_search(string) ⇒ String

Prints out an error message

Examples:

ZendeskSupportAPI::Search.bad_search('invalid sort - name')
#=> "Search cannot be completed: invalid sort - name"

Parameters:

  • string (String)

    The error that occurred

Returns:

  • (String)


91
92
93
# File 'lib/zendesk_support_api/search.rb', line 91

def self.bad_search(string)
  "Search cannot be completed: #{string}"
end

.list(client, query, sort = '', order = 'desc') ⇒ Array

Perform a search and list results (first 100)

Examples:

ZendeskSupportAPI::Search.list(client, 'bob')
#=> [
#=>   {
#=>     "name":        "Bob McBob",
#=>     "created_at":  "2009-05-13T00:07:08Z",
#=>     "updated_at":  "2011-07-22T00:11:12Z",
#=>     "id":          211,
#=>     "result_type": "user"
#=>     "url":         "https://zendesk.com/api/v2/users/211.json"
#=>   },
#=>   {
#=>     "name":        "Bob's Company",
#=>     "created_at":  "2009-08-26T00:07:08Z",
#=>     "updated_at":  "2010-05-13T00:07:08Z",
#=>     "id":          122,
#=>     "result_type": "group"
#=>     "url":         "https://zendesk.com/api/v2/groups/122.json"
#=>   },
#=>   ...
#=> ]

Parameters:

  • client (ZendeskSupportAPI::Client)

    The client instance to use

  • query (String)

    The query to use

  • sort (String) (defaults to: '')

    The sorting method to use (defaults to revelance)

  • order (String) (defaults to: 'desc')

    The order to use (defaults to desc)

Returns:

  • (Array)


36
37
38
39
40
41
42
# File 'lib/zendesk_support_api/search.rb', line 36

def self.list(client, query, sort = '', order = 'desc')
  return bad_search("invalid sort - #{sort}") unless sort_valid?(sort)
  return bad_search("invalid order - #{order}") unless order_valid?(order)

  sort_order = sort_order_string(sort, order)
  client.request(:get, "search.json?query=#{query}#{sort_order}")['results']
end

.order_valid?(order) ⇒ Boolean

Determines if a given order string is valid

Examples:

ZendeskSupportAPI::Search.order_valid? 'asc'
#=> true
ZendeskSupportAPI::Search.order_valid? 'desc'
#=> true
ZendeskSupportAPI::Search.order_valid? 'random'
#=> false

Parameters:

  • order (String)

    The order string to check

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/zendesk_support_api/search.rb', line 77

def self.order_valid?(order)
  valid = %w[asc desc]
  valid.include? order
end

.sort_order_string(sort, order) ⇒ String

Determines the sort and order to use in the query

Examples:

ZendeskSupportAPI::Search.sort_order_string('', 'asc')
#=> "&order_by=asc"
ZendeskSupportAPI::Search.sort_order_string('priority', 'desc')
#=> "&sort_by=priority"
ZendeskSupportAPI::Search.sort_order_string('updated_at', 'asc')
#=> "&sort_by=updated_at&order_by=asc"

Parameters:

  • sort (String)

    The sort string to use

  • order (String)

    The order string to use

Returns:

  • (String)


109
110
111
112
113
# File 'lib/zendesk_support_api/search.rb', line 109

def self.sort_order_string(sort, order)
  sort = (sort.empty? ? '' : "&sort_by=#{sort}")
  order = (order == 'desc' ? '' : '&order_by=asc')
  "#{sort}#{order}"
end

.sort_valid?(sort) ⇒ Boolean

Determines if a given sort string is valid

Examples:

ZendeskSupportAPI::Search.sort_valid? 'updated_at'
#=> true
ZendeskSupportAPI::Search.sort_valid? 'ticket_type'
#=> true
ZendeskSupportAPI::Search.sort_valid? 'random'
#=> false

Parameters:

  • sort (String)

    The sort string to check

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/zendesk_support_api/search.rb', line 57

def self.sort_valid?(sort)
  return true if sort.empty?

  valid = %w[updated_at created_at priority status ticket_type]
  valid.include? sort
end