Skip to content

Commit

Permalink
Merge pull request #19 from ukparliament/204-response
Browse files Browse the repository at this point in the history
added nocontenterror for 204 where no content is returned from the api
  • Loading branch information
katylouise committed Feb 14, 2017
2 parents 46b3202 + 47c5c48 commit 6d36c44
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/parliament.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'parliament/version'
require 'parliament/request'
require 'parliament/response'
require 'parliament/no_content_error'

# require all the decorators
Dir[File.join(File.dirname(__FILE__), 'parliament/decorators', '*.rb')].each { |decorator| require decorator }
Expand Down
9 changes: 9 additions & 0 deletions lib/parliament/no_content_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Parliament
class NoContentError < StandardError
attr_reader :message

def initialize
@message = 'No content'
end
end
end
27 changes: 24 additions & 3 deletions lib/parliament/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,38 @@ def respond_to_missing?(method, include_private = false)
end

def get
response = Net::HTTP.get_response(URI(api_endpoint))
net_response = Net::HTTP.get_response(URI(api_endpoint))

raise StandardError, 'This is a HTTPClientError' if response.is_a?(Net::HTTPClientError)
raise StandardError, 'This is a HTTPServerError' if response.is_a?(Net::HTTPServerError)
handle_errors(net_response)

build_parliament_response(net_response)
end

def build_parliament_response(response)
objects = Grom::Reader.new(response.body).objects
objects.map { |object| assign_decorator(object) }

Parliament::Response.new(objects)
end

def handle_errors(response)
handle_not_found_error(response)
handle_server_error(response)
handle_no_content_error(response)
end

def handle_server_error(response)
raise StandardError, 'This is a HTTPServerError' if response.is_a?(Net::HTTPServerError)
end

def handle_not_found_error(response)
raise StandardError, 'This is a HTTPClientError' if response.is_a?(Net::HTTPClientError)
end

def handle_no_content_error(response)
raise Parliament::NoContentError if response.code == '204'
end

def assign_decorator(object)
object_type = Grom::Helper.get_id(object.type)
return object unless Parliament::Decorators.constants.include?(object_type.to_sym)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions spec/parliament/no_content_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require_relative '../spec_helper'

describe Parliament::NoContentError, vcr: true do
subject { Parliament::NoContentError.new }

it 'has a message attribute' do
expect(subject.message).to eq 'No content'
end
end
11 changes: 10 additions & 1 deletion spec/parliament/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@
end
end

context 'it returns any other status code than a 200' do
context 'it returns a status code of 204 and...' do
it 'raises a Parliament::NoContentError' do
past_person_id = '321f496b-5c8b-4455-ab49-a96e42b34739'
expect do
Parliament::Request.new(base_url: 'http://localhost:3030').people(past_person_id).parties.current.get
end.to raise_error(Parliament::NoContentError, 'No content')
end
end

context 'it returns a status code in either the 400 or 500 range' do
it 'and raises client error when status is within the 400 range' do
stub_request(:get, 'http://localhost:3030/dogs/cats.nt').to_return(status: 400)

Expand Down

0 comments on commit 6d36c44

Please sign in to comment.