Skip to content

Commit

Permalink
Eliminate Mailgunner::Response class in favour of raising exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
timcraft committed Jul 29, 2014
1 parent 415356f commit 9330421
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 275 deletions.
23 changes: 21 additions & 2 deletions lib/mailgunner.rb
@@ -1,10 +1,11 @@
require 'net/http'
require 'json'
require 'cgi'
require 'mailgunner/response'
require 'mailgunner/delivery_method' if defined?(ActionMailer)

module Mailgunner
class Error < StandardError; end

class Client
attr_accessor :domain, :api_key, :http

Expand Down Expand Up @@ -241,7 +242,25 @@ def transmit(message)

yield message if block_given?

Response.new(@http.request(message))
parse(@http.request(message))
end

def parse(response)
if Net::HTTPSuccess === response
json?(response) ? JSON.parse(response.body) : response.body
else
if json?(response)
raise Error, "HTTP #{response.code}: #{JSON.parse(response.body).fetch('message')}"
else
raise Error, "HTTP #{response.code}"

This comment has been minimized.

Copy link
@bergerjac

bergerjac Jan 22, 2015

ref

when @domain is nil, this error is too generic. e.g. HTTP 401

end
end
end

def json?(response)
content_type = response['Content-Type']

content_type && content_type.split(';').first == 'application/json'
end

def request_uri(path, params_hash)
Expand Down
13 changes: 1 addition & 12 deletions lib/mailgunner/delivery_method.rb
@@ -1,9 +1,6 @@
require 'mail/check_delivery_params'

module Mailgunner
class DeliveryFailed < StandardError
end

class DeliveryMethod
include Mail::CheckDeliveryParams

Expand All @@ -22,15 +19,7 @@ def initialize(values)
def deliver!(mail)
check_delivery_params(mail)

response = @client.send_mime(mail)

if response.ok?
return response
elsif response.json? && response.object.has_key?('message')
raise DeliveryFailed, response.object['message']
else
raise DeliveryFailed, "#{response.code} #{response.message}"
end
@client.send_mime(mail)
end
end

Expand Down
35 changes: 0 additions & 35 deletions lib/mailgunner/response.rb

This file was deleted.

4 changes: 3 additions & 1 deletion spec/mailgunner_delivery_method_spec.rb
Expand Up @@ -42,7 +42,9 @@ def registration_confirmation(user)
body: '{"message": "Invalid API key"}'
})

proc { ExampleMailer.registration_confirmation(email: @address).deliver }.must_raise(Mailgunner::DeliveryFailed)
exception = proc { ExampleMailer.registration_confirmation(email: @address).deliver }.must_raise(Mailgunner::Error)

exception.message.must_include('Invalid API key')
end

it 'allows the domain to be specified explicitly via the delivery method settings' do
Expand Down
81 changes: 0 additions & 81 deletions spec/mailgunner_response_spec.rb

This file was deleted.

0 comments on commit 9330421

Please sign in to comment.