Skip to content

Instantly share code, notes, and snippets.

@radar
Created Jun 15, 2018
Embed
What would you like to do?
Problematic code
<table>
<tr>
<th>Subject</th>
<th>Created At</th>
<th>Status</th>
</tr>
<% @tickets.each do |ticket| %>
<tr>
<td><a href="/ticket/<%= ticket.id %>"><%= ticket.subject %></a></td>
<td><%= parse_time(ticket.created_at) %></td>
<td><%= ticket.status %></td>
</tr>
<% end %>
</table>
require 'zendesk_api'
require 'json'
class Ticket
attr_accessor :id,
:subject,
:description,
:requester_id,
:assignee_id,
:created_at,
:updated_at,
:status
PAGE_LIMIT = 25
ZENDESK_RESPONSE = 100
def initialize(id, subject, description, requester_id, assignee_id, created_at, updated_at, status)
@id = id
@subject = subject
@description = description
@requester_id = requester_id
@assignee_id = assignee_id
@created_at = created_at
@updated_at = updated_at
@status = status
end
def self.ticket_list
api = ZendeskApi.new
tickets = JSON.parse(api.get(ZendeskApi::BASE_URL + 'tickets.json?sort_by=id&sort_order=desc').body)
tickets['tickets'].each do |ticket|
[ticket['id'], ticket['subject'], ticket['description'], ticket['requester_id'],
ticket['assignee_id'], ticket['created_at'], ticket['updated_at'], ticket['status']]
end
tickets
end
def self.single_ticket(id)
api = ZendeskApi.new
ticket = JSON.parse(api.get(ZendeskApi::BASE_URL + "tickets/#{id}.json").body)
[ticket['id']]
end
end
require 'ticket'
class TicketsController < ApplicationController
rescue_from HTTParty::Error, with: :error
def index
@tickets = Ticket.ticket_list
end
def show
@ticket = Ticket.single_ticket(params[:id])
end
def error
render :error
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment