Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
186 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'instapaper/error' | ||
|
||
module Instapaper | ||
module HTTP | ||
class Response | ||
attr_reader :response, :raw_format, :path | ||
def initialize(response, path, raw_format = false) | ||
@response = response | ||
@path = path | ||
@raw_format = raw_format | ||
end | ||
|
||
def body | ||
raw_format ? response.to_s : parsed | ||
end | ||
|
||
def valid? | ||
!error? | ||
end | ||
|
||
def error? | ||
fail_if_body_unparseable unless raw_format | ||
fail_if_body_contains_error | ||
fail_if_http_error | ||
end | ||
|
||
private | ||
|
||
def parsed | ||
@parsed_response ||= begin | ||
response.parse(:json) | ||
rescue | ||
response.body | ||
end | ||
end | ||
|
||
def fail_if_http_error | ||
return if response.status.ok? | ||
|
||
if Instapaper::Error::CODES.include?(response.status.code) | ||
fail Instapaper::Error.from_response(response.status.code, path) | ||
else | ||
fail Instapaper::Error, 'Unknown Error' | ||
end | ||
end | ||
|
||
def fail_if_body_unparseable | ||
response.parse(:json) | ||
rescue JSON::ParserError | ||
raise Instapaper::Error::ServiceUnavailableError | ||
end | ||
|
||
def fail_if_body_contains_error | ||
return unless parsed.is_a?(Array) | ||
return unless parsed.size > 0 | ||
return unless parsed.first['type'] == 'error' | ||
|
||
fail Instapaper::Error.from_response(parsed.first['error_code'], @path) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'spec_helper' | ||
|
||
class FakeResponse | ||
def initialize(body) | ||
@body = body | ||
end | ||
|
||
def parse(_) | ||
::JSON.parse(@body) | ||
end | ||
end | ||
|
||
describe Instapaper::HTTP::Response do | ||
describe '#body' do | ||
context 'raw response' do | ||
it 'returns the response in raw text' do | ||
resp = Instapaper::HTTP::Response.new('foo', '', true) | ||
expect(resp.body).to eq('foo') | ||
end | ||
end | ||
|
||
context 'regular response' do | ||
let(:fake_response) { FakeResponse.new('{"foo":"bar"}') } | ||
it 'returns the parsed response' do | ||
resp = Instapaper::HTTP::Response.new(fake_response, '') | ||
expect(resp.body).to be_a(Hash) | ||
end | ||
end | ||
end | ||
|
||
describe '#valid?' do | ||
context 'when http error' do | ||
end | ||
|
||
context 'when body unparseable' do | ||
end | ||
|
||
context 'when error in body' do | ||
end | ||
end | ||
end |