Skip to content

Commit

Permalink
Merge commit '74e943a861f6e3ff71eeee07f5f7bcf7acf797c6' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins committed Sep 14, 2012
2 parents 1fccf05 + 74e943a commit 91c55c5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem "zendesk_api"
gem "rake"
gem "unicorn"
gem 'slimmer', '1.2.4'
gem "mail"

group :test do
gem "rack-test"
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ GEM
null_logger
plek
hashie (1.2.0)
i18n (0.6.1)
inflection (1.0.0)
json (1.7.5)
kgio (2.7.4)
lrucache (0.1.3)
PriorityQueue (~> 0.1.2)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
metaclass (0.0.1)
mime-types (1.19)
mocha (0.12.3)
Expand All @@ -26,6 +31,7 @@ GEM
null_logger (0.0.1)
plek (0.3.0)
builder
polyglot (0.3.3)
rack (1.4.1)
rack-protection (1.2.0)
rack
Expand All @@ -45,6 +51,9 @@ GEM
plek (>= 0.1.8)
rack (>= 1.3.5)
tilt (1.3.3)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
unicorn (4.3.1)
kgio (~> 2.6)
rack
Expand All @@ -62,6 +71,7 @@ PLATFORMS
ruby

DEPENDENCIES
mail
mocha
rack-test
rake
Expand Down
1 change: 1 addition & 0 deletions config/mailer.yml
32 changes: 28 additions & 4 deletions lib/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_relative "zendesk_request"
require_relative "zendesk_error"
require_relative "validations"
require_relative "exception_mailer"

class App < Sinatra::Base

Expand All @@ -16,6 +17,10 @@ class App < Sinatra::Base

before do
@client = ZendeskClient.get_client(logger)

#if @client.current_user["id"].nil?
# raise ZendeskError.new("Authentication error", "Check logs for details")
#end
end

get '/' do
Expand All @@ -30,7 +35,7 @@ class App < Sinatra::Base
erb :fail
end

# Content routing
#Content routing
get '/amend-content' do
on_get("Content Change", "content/content_amend_message", "content/amend")
end
Expand All @@ -44,7 +49,7 @@ class App < Sinatra::Base
on_post(params, "amend-content")
end

# User access routing
#User access routing
get '/create-user' do
on_get("Create New User", "useraccess/user_create_message", "useraccess/user")
end
Expand Down Expand Up @@ -145,6 +150,7 @@ def on_post(params, route)
if ticket
redirect '/acknowledge'
else
ExceptionMailer.deliver_exception_notification(env['sinatra.error'])
redirect '/failed-submission'
end
else
Expand All @@ -153,12 +159,30 @@ def on_post(params, route)
end

error do
@error_msg = "And error IS: #{request.env['sinatra.error']}"
ExceptionMailer.deliver_exception_notification(env['sinatra.error'])
@error_msg = request.env['sinatra.error']
erb :error_page
end

error ZendeskError do
redirect '/failed-submission'
exception_message = format_exception_message(env['sinatra.error'].message, env['sinatra.error'].details_from_zendesk)
ExceptionMailer.deliver_exception_notification(exception_message)

redirect "/failed-submission"
end

def format_exception_message(message, details)
time = Time.now
message = <<-EOF
At #{time}
Error
#{message}
Zendesk responded with:
#{details}
EOF

message
end

end
21 changes: 21 additions & 0 deletions lib/exception_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'mail'
require 'yaml'

class ExceptionMailer
def self.deliver_exception_notification(message)
mailer_file = YAML.load_file("config/mailer.yml")
environment = ENV['GOVUK_ENV'] || "development"

Mail.deliver do
delivery_method :smtp, {
:address => "smtp.gmail.com", :port => 587, :domain => 'alphagov.co.uk',
:user_name => mailer_file[environment]["username"].to_s, :password => mailer_file[environment]["password"].to_s, :authentication => 'plain',
:enable_starttls_auto => true
}
from 'Winston Smith-Churchill <winston@alphagov.co.uk>'
to 'govuk-exceptions@digital.cabinet-office.gov.uk'
subject "Zendesk MI5-minesweeper has blown"
body message
end
end
end
4 changes: 2 additions & 2 deletions lib/zendesk_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def self.get_client(logger)
logger.info env
if env[:body]["user"]
if env[:body]["id"].nil?
raise ZendeskError.new("Authentication Error")
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")
raise ZendeskError.new("Authentication Error", env)
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/zendesk_error.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class ZendeskError < StandardError

attr_reader :message
attr_reader :message, :details_from_zendesk

def initialize(message)
def initialize(message, details_from_zendesk)
@message = message.to_s
@details_from_zendesk = details_from_zendesk
end

end

0 comments on commit 91c55c5

Please sign in to comment.