Class: ZendeskSupportAPI::Tickets
- Inherits:
-
Object
- Object
- ZendeskSupportAPI::Tickets
- Defined in:
- lib/zendesk_support_api/tickets.rb
Overview
Tickets class - developer.zendesk.com/rest_api/docs/support/tickets
Class Method Summary collapse
-
.all(client) ⇒ Array
Lists out all tickets (paginates over every page).
-
.create(client, ticket) ⇒ Hash|String
Create a ticket.
-
.create_many(client, tickets) ⇒ ZendeskSupportAPI::Client.handle_job
Creates many tickets.
-
.includes ⇒ String
Prints out the include string.
-
.list(client, sort = 'id', order = 'asc') ⇒ Array
List out tickets (first 100).
-
.list_followers(client, tid) ⇒ Array
List followers of a ticket.
-
.mark_as_spam(client, tid) ⇒ String
Mark a ticket as spam and suspend the user.
-
.mark_many_as_spam(client, tids) ⇒ ZendeskSupportAPI::Client.handle_job
Mark many tickets as spam and suspend their users.
-
.merge(client, tid, hash) ⇒ ZendeskSupportAPI::Client.handle_job
Merge Tickets into Target Ticket.
-
.next_page(res) ⇒ nil|String
Returns the string of the next_page for pagination.
-
.org_id ⇒ String
Prints out organization_id.
-
.select_obj(array, id) ⇒ Hash
Selects an object from an array based on a user ID.
-
.show(client, tid) ⇒ Hash
Shows details about a specific ticket.
-
.show_many(client, tids) ⇒ Array
Show details about many tickets.
-
.sort_order(sort, order) ⇒ String
Prints out the sort_by and order_by string for the url.
-
.sort_valid?(sort) ⇒ Boolean
Determines if given string is a valid sort function.
-
.ticket_map(tickets, res) ⇒ Hash
Maps users into user_objects.
-
.ticket_object(ticket, res) ⇒ Hash
Creates a ticket object.
-
.update(client, tid, ticket) ⇒ Hash|String
Updates a ticket.
-
.update_many(client, tickets) ⇒ ZendeskSupportAPI::Client.handle_job
Updates many tickets.
Class Method Details
.all(client) ⇒ Array
Lists out all tickets (paginates over every page)
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/zendesk_support_api/tickets.rb', line 144 def self.all(client) tickets = [] page = "tickets.json#{includes}&page=1" until page.nil? res = client.request(:get, page) client.spinner("tickets (total: #{res['count']})", page.split('=').last) tickets += ticket_map(res['tickets'], res) page = next_page(res) end puts ' ...done' tickets end |
.create(client, ticket) ⇒ Hash|String
Create a ticket
214 215 216 217 218 219 |
# File 'lib/zendesk_support_api/tickets.rb', line 214 def self.create(client, ticket) res = client.request(:post, 'tickets.json', ticket: ticket) return "Creation failed: #{res['details']}" if res['error'] res end |
.create_many(client, tickets) ⇒ ZendeskSupportAPI::Client.handle_job
Creates many tickets
227 228 229 230 |
# File 'lib/zendesk_support_api/tickets.rb', line 227 def self.create_many(client, tickets) res = client.request(:post, 'tickets/create_many.json', tickets: tickets) client.handle_job(res) end |
.includes ⇒ String
Prints out the include string
29 30 31 |
# File 'lib/zendesk_support_api/tickets.rb', line 29 def self.includes '?include=users,organizations,ticket_forms,comment_count' end |
.list(client, sort = 'id', order = 'asc') ⇒ Array
List out tickets (first 100)
125 126 127 128 129 130 131 132 |
# File 'lib/zendesk_support_api/tickets.rb', line 125 def self.list(client, sort = 'id', order = 'asc') return "Invalid sort '#{sort}'" unless sort_valid?(sort) return "Invalid order '#{order}'" unless %w[asc desc].include?(order) url = "tickets.json#{includes}#{sort_order(sort, order)}" res = client.request(:get, url) res['tickets'].map { |t| ticket_object(t, res) } end |
.list_followers(client, tid) ⇒ Array
List followers of a ticket
327 328 329 330 331 332 333 |
# File 'lib/zendesk_support_api/tickets.rb', line 327 def self.list_followers(client, tid) res = client.request(:get, "tickets/#{tid}/followers")['users'] res['users'].each do |user| user['organization'] = select_obj(res['organizations'], user[org_id]) end res['users'] end |
.mark_as_spam(client, tid) ⇒ String
Mark a ticket as spam and suspend the user
282 283 284 285 286 287 |
# File 'lib/zendesk_support_api/tickets.rb', line 282 def self.mark_as_spam(client, tid) res = client.request(:put, "tickets/#{tid}/mark_as_spam.json") return "Unable to mark ticket as spam: #{res['error']}" if res['error'] "Ticket #{tid} marked as spam" end |
.mark_many_as_spam(client, tids) ⇒ ZendeskSupportAPI::Client.handle_job
Mark many tickets as spam and suspend their users
295 296 297 298 299 |
# File 'lib/zendesk_support_api/tickets.rb', line 295 def self.mark_many_as_spam(client, tids) url = "tickets/mark_many_as_spam.json?ids=#{tids.join(',')}" res = client.request(:put, url) client.handle_job(res) end |
.merge(client, tid, hash) ⇒ ZendeskSupportAPI::Client.handle_job
Merge Tickets into Target Ticket
316 317 318 319 |
# File 'lib/zendesk_support_api/tickets.rb', line 316 def self.merge(client, tid, hash) res = client.request(:post, "tickets/#{tid}/merge.json", hash) client.handle_job(res) end |
.next_page(res) ⇒ nil|String
Returns the string of the next_page for pagination
62 63 64 |
# File 'lib/zendesk_support_api/tickets.rb', line 62 def self.next_page(res) (res['next_page'].nil? ? nil : res['next_page'].split('/').last) end |
.org_id ⇒ String
Prints out organization_id
73 74 75 |
# File 'lib/zendesk_support_api/tickets.rb', line 73 def self.org_id 'organization_id' end |
.select_obj(array, id) ⇒ Hash
Selects an object from an array based on a user ID
98 99 100 |
# File 'lib/zendesk_support_api/tickets.rb', line 98 def self.select_obj(array, id) array.select { |a| a['id'] == id }.first end |
.show(client, tid) ⇒ Hash
Shows details about a specific ticket
171 172 173 174 175 |
# File 'lib/zendesk_support_api/tickets.rb', line 171 def self.show(client, tid) url = "tickets/#{tid}.json#{includes}" res = client.request(:get, url) res['ticket'].map { |t| ticket_object(t, res) } end |
.show_many(client, tids) ⇒ Array
Show details about many tickets
183 184 185 186 187 |
# File 'lib/zendesk_support_api/tickets.rb', line 183 def self.show_many(client, tids) url = "tickets/show_many.json#{includes}&ids=#{tids.join(',')}" res = client.request(:get, url) res['tickets'].map { |t| ticket_object(t, res) } end |
.sort_order(sort, order) ⇒ String
Prints out the sort_by and order_by string for the url
43 44 45 |
# File 'lib/zendesk_support_api/tickets.rb', line 43 def self.sort_order(sort, order) "&sort_by=#{sort}&order_by=#{order}" end |
.sort_valid?(sort) ⇒ Boolean
Determines if given string is a valid sort function
15 16 17 18 19 |
# File 'lib/zendesk_support_api/tickets.rb', line 15 def self.sort_valid?(sort) valid = %w[assignee assignee_name created_at group id locale requester requester.name status subject updated_at] valid.include?(sort) end |
.ticket_map(tickets, res) ⇒ Hash
Maps users into user_objects
53 54 55 |
# File 'lib/zendesk_support_api/tickets.rb', line 53 def self.ticket_map(tickets, res) tickets.map { |t| ticket_object(t, res) } end |
.ticket_object(ticket, res) ⇒ Hash
Creates a ticket object
83 84 85 86 87 88 89 90 |
# File 'lib/zendesk_support_api/tickets.rb', line 83 def self.ticket_object(ticket, res) ticket.merge( organization: select_obj(res['organizations'], ticket[org_id]), form: select_obj(res['ticket_forms'], ticket['form_id']), requester: select_obj(res['users'], ticket['requester_id']), assignee: select_obj(res['users'], ticket['assignee_id']) ) end |
.update(client, tid, ticket) ⇒ Hash|String
Updates a ticket
239 240 241 242 243 244 |
# File 'lib/zendesk_support_api/tickets.rb', line 239 def self.update(client, tid, ticket) res = client.request(:put, "tickets/#{tid}.json", ticket: ticket) return "Update of #{tid} failed: #{res['error']}" if res['error'] res end |
.update_many(client, tickets) ⇒ ZendeskSupportAPI::Client.handle_job
Updates many tickets
271 272 273 274 |
# File 'lib/zendesk_support_api/tickets.rb', line 271 def self.update_many(client, tickets) res = client.request(:put, 'tickets/update_many.json', tickets: tickets) client.handle_job(res) end |