Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #20 from alphagov/zendesk_api_wrapper_cleanup

Refactoring the Zendesk API wrapping code
  • Loading branch information...
commit f64360514bbf71b3715812f107de50a2cc7598cd 2 parents 7473302 + 28f0d86
@mnowster mnowster authored
View
4 app/controllers/requests_controller.rb
@@ -1,4 +1,4 @@
-require "zendesk_request"
+require "zendesk_tickets"
require "zendesk_client"
class RequestsController < ApplicationController
@@ -19,7 +19,7 @@ def create
def raise_ticket(ticket)
load_client
- ticket = ZendeskRequest.raise_ticket(@client, ticket)
+ ticket = ZendeskTickets.new(@client).raise_ticket(ticket)
if ticket
redirect_to acknowledge_path
View
17 lib/zendesk_client.rb
@@ -14,20 +14,13 @@ def self.get_client(logger)
@client.insert_callback do |env|
logger.info env
- if env[:body]["user"]
- if env[:body]["id"].nil?
- raise ZendeskError.new("Authentication Error", env)
- end
- end
+
status_401 = env[:status].to_s.start_with? "401"
too_many_login_attempts = env[:body].to_s.start_with? "Too many failed login attempts"
- if status_401 || too_many_login_attempts
- raise ZendeskError.new("Authentication Error", env)
- end
-
- if env[:body]["error"]
- raise ZendeskError.new("Error creating ticket", env)
- end
+
+ raise ZendeskError, "Authentication Error: #{env.inspect}" if status_401 || too_many_login_attempts
+
+ raise ZendeskError, "Error creating ticket: #{env.inspect}" if env[:body]["error"]
end
@client
View
11 lib/zendesk_error.rb
@@ -1,10 +1 @@
-class ZendeskError < StandardError
-
- attr_reader :message, :details_from_zendesk
-
- def initialize(message, details_from_zendesk)
- @message = message.to_s
- @details_from_zendesk = details_from_zendesk
- end
-
-end
+class ZendeskError < StandardError; end
View
18 lib/zendesk_request.rb → lib/zendesk_tickets.rb
@@ -1,6 +1,10 @@
require_relative "zendesk_ticket"
-class ZendeskRequest
+class ZendeskTickets
+ def initialize(client)
+ @client = client
+ end
+
def self.field_ids
{ job: "21487987",
phone: "21471291",
@@ -8,16 +12,16 @@ def self.field_ids
not_before_date: "21502036" }
end
- def self.raise_ticket(client, ticket_to_raise)
- client.ticket.create(
+ def raise_ticket(ticket_to_raise)
+ @client.ticket.create(
:subject => ticket_to_raise.subject,
:description => "Created via Govt API",
:priority => "normal",
:requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
- :fields => [{"id" => ZendeskRequest.field_ids[:job], "value" => ticket_to_raise.job},
- {"id" => ZendeskRequest.field_ids[:phone], "value" => ticket_to_raise.phone},
- {"id" => ZendeskRequest.field_ids[:needed_by_date], "value" => ticket_to_raise.needed_by_date},
- {"id" => ZendeskRequest.field_ids[:not_before_date], "value" => ticket_to_raise.not_before_date}],
+ :fields => [{"id" => ZendeskTickets.field_ids[:job], "value" => ticket_to_raise.job},
+ {"id" => ZendeskTickets.field_ids[:phone], "value" => ticket_to_raise.phone},
+ {"id" => ZendeskTickets.field_ids[:needed_by_date], "value" => ticket_to_raise.needed_by_date},
+ {"id" => ZendeskTickets.field_ids[:not_before_date], "value" => ticket_to_raise.not_before_date}],
:tags => ticket_to_raise.tags,
:comment => {:value => ticket_to_raise.comment})
end
View
4 test/zendesk_api_stubs.rb
@@ -1,4 +1,4 @@
-require 'zendesk_request'
+require 'zendesk_tickets'
module ZendeskApiStubsHelper
def stub_zendesk_ticket_submission
@@ -20,7 +20,7 @@ def create(options)
end
end
- ZendeskRequest.field_ids.each do |field_name, field_id|
+ ZendeskTickets.field_ids.each do |field_name, field_id|
define_method(field_name) do
value_of_field_with_id(field_id)
end
Please sign in to comment.
Something went wrong with that request. Please try again.