Skip to content

Commit

Permalink
Split out the Response class/spec
Browse files Browse the repository at this point in the history
  • Loading branch information
timcraft committed Nov 3, 2013
1 parent 03a8a7d commit aaaddc1
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 138 deletions.
42 changes: 2 additions & 40 deletions lib/mailgunner.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require 'net/http'
require 'json'
require 'cgi'

if defined?(ActionMailer)
require 'mailgunner/delivery_method'
end
require 'mailgunner/response'
require 'mailgunner/delivery_method' if defined?(ActionMailer)

module Mailgunner
class Client
Expand Down Expand Up @@ -274,40 +272,4 @@ def escape(component)
CGI.escape(component.to_s)
end
end

class Response
def initialize(http_response, options = {})
@http_response = http_response

@json = options.fetch(:json) { JSON }
end

def method_missing(name, *args, &block)
@http_response.send(name, *args, &block)
end

def respond_to_missing?(name, include_private = false)
@http_response.respond_to?(name)
end

def ok?
code.to_i == 200
end

def client_error?
(400 .. 499).include?(code.to_i)
end

def server_error?
(500 .. 599).include?(code.to_i)
end

def json?
self['Content-Type'].split(';').first == 'application/json'
end

def object
@object ||= @json.parse(body)
end
end
end
37 changes: 37 additions & 0 deletions lib/mailgunner/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Mailgunner
class Response
def initialize(http_response, options = {})
@http_response = http_response

@json = options.fetch(:json) { JSON }
end

def method_missing(name, *args, &block)
@http_response.send(name, *args, &block)
end

def respond_to_missing?(name, include_private = false)
@http_response.respond_to?(name)
end

def ok?
code.to_i == 200
end

def client_error?
(400 .. 499).include?(code.to_i)
end

def server_error?
(500 .. 599).include?(code.to_i)
end

def json?
self['Content-Type'].split(';').first == 'application/json'
end

def object
@object ||= @json.parse(body)
end
end
end
101 changes: 101 additions & 0 deletions spec/mailgunner_response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require 'minitest/autorun'
require 'mocha/setup'
require 'mailgunner'

describe 'Mailgunner::Response' do
before do
@http_response = mock()

@response = Mailgunner::Response.new(@http_response)
end

it 'delegates missing methods to the http response object' do
@http_response.stubs(:code).returns('200')

@response.code.must_equal('200')
end

describe 'ok query method' do
it 'returns true if the status code is 200' do
@http_response.expects(:code).returns('200')

@response.ok?.must_equal(true)
end

it 'returns false otherwise' do
@http_response.expects(:code).returns('400')

@response.ok?.must_equal(false)
end
end

describe 'client_error query method' do
it 'returns true if the status code is 4xx' do
@http_response.stubs(:code).returns(%w(400 401 402 404).sample)

@response.client_error?.must_equal(true)
end

it 'returns false otherwise' do
@http_response.stubs(:code).returns('200')

@response.client_error?.must_equal(false)
end
end

describe 'server_error query method' do
it 'returns true if the status code is 5xx' do
@http_response.stubs(:code).returns(%w(500 502 503 504).sample)

@response.server_error?.must_equal(true)
end

it 'returns false otherwise' do
@http_response.stubs(:code).returns('200')

@response.server_error?.must_equal(false)
end
end

describe 'json query method' do
it 'returns true if the response has a json content type' do
@http_response.expects(:[]).with('Content-Type').returns('application/json;charset=utf-8')

@response.json?.must_equal(true)
end

it 'returns false otherwise' do
@http_response.expects(:[]).with('Content-Type').returns('text/html')

@response.json?.must_equal(false)
end
end

describe 'object method' do
it 'decodes the response body as json and returns a hash' do
@http_response.expects(:body).returns('{"foo":"bar"}')

@response.object.must_equal({'foo' => 'bar'})
end
end
end

describe 'Mailgunner::Response initialized with an alternative json implementation' do
before do
@json = mock()

@http_response = stub

@response = Mailgunner::Response.new(@http_response, :json => @json)
end

describe 'object method' do
it 'uses the alternative json implementation to parse the response body' do
@http_response.stubs(:body).returns(response_body = '{"foo":"bar"}')

@json.expects(:parse).with(response_body)

@response.object
end
end
end
98 changes: 0 additions & 98 deletions spec/mailgunner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -637,101 +637,3 @@
end
end
end

describe 'Mailgunner::Response' do
before do
@http_response = mock()

@response = Mailgunner::Response.new(@http_response)
end

it 'delegates missing methods to the http response object' do
@http_response.stubs(:code).returns('200')

@response.code.must_equal('200')
end

describe 'ok query method' do
it 'returns true if the status code is 200' do
@http_response.expects(:code).returns('200')

@response.ok?.must_equal(true)
end

it 'returns false otherwise' do
@http_response.expects(:code).returns('400')

@response.ok?.must_equal(false)
end
end

describe 'client_error query method' do
it 'returns true if the status code is 4xx' do
@http_response.stubs(:code).returns(%w(400 401 402 404).sample)

@response.client_error?.must_equal(true)
end

it 'returns false otherwise' do
@http_response.stubs(:code).returns('200')

@response.client_error?.must_equal(false)
end
end

describe 'server_error query method' do
it 'returns true if the status code is 5xx' do
@http_response.stubs(:code).returns(%w(500 502 503 504).sample)

@response.server_error?.must_equal(true)
end

it 'returns false otherwise' do
@http_response.stubs(:code).returns('200')

@response.server_error?.must_equal(false)
end
end

describe 'json query method' do
it 'returns true if the response has a json content type' do
@http_response.expects(:[]).with('Content-Type').returns('application/json;charset=utf-8')

@response.json?.must_equal(true)
end

it 'returns false otherwise' do
@http_response.expects(:[]).with('Content-Type').returns('text/html')

@response.json?.must_equal(false)
end
end

describe 'object method' do
it 'decodes the response body as json and returns a hash' do
@http_response.expects(:body).returns('{"foo":"bar"}')

@response.object.must_equal({'foo' => 'bar'})
end
end
end

describe 'Mailgunner::Response initialized with an alternative json implementation' do
before do
@json = mock()

@http_response = stub

@response = Mailgunner::Response.new(@http_response, :json => @json)
end

describe 'object method' do
it 'uses the alternative json implementation to parse the response body' do
@http_response.stubs(:body).returns(response_body = '{"foo":"bar"}')

@json.expects(:parse).with(response_body)

@response.object
end
end
end

0 comments on commit aaaddc1

Please sign in to comment.