Skip to content

Commit a3a7c2a

Browse files
committed
Add status to headers for all responses
1 parent bb38b0e commit a3a7c2a

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

Gemfile.lock

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
PATH
2+
remote: .
3+
specs:
4+
cortex-client (0.2.5)
5+
faraday (~> 0.9)
6+
faraday_middleware (~> 0.9.0)
7+
oauth2 (~> 0.9)
8+
9+
GEM
10+
remote: https://rubygems.org/
11+
specs:
12+
diff-lcs (1.2.5)
13+
faraday (0.9.0)
14+
multipart-post (>= 1.2, < 3)
15+
faraday_middleware (0.9.1)
16+
faraday (>= 0.7.4, < 0.10)
17+
jwt (1.0.0)
18+
multi_json (1.10.1)
19+
multi_xml (0.5.5)
20+
multipart-post (2.0.0)
21+
oauth2 (0.9.4)
22+
faraday (>= 0.8, < 0.10)
23+
jwt (~> 1.0)
24+
multi_json (~> 1.3)
25+
multi_xml (~> 0.5)
26+
rack (~> 1.2)
27+
rack (1.5.2)
28+
rake (10.3.2)
29+
rspec (3.0.0)
30+
rspec-core (~> 3.0.0)
31+
rspec-expectations (~> 3.0.0)
32+
rspec-mocks (~> 3.0.0)
33+
rspec-core (3.0.3)
34+
rspec-support (~> 3.0.0)
35+
rspec-expectations (3.0.3)
36+
diff-lcs (>= 1.2.0, < 2.0)
37+
rspec-support (~> 3.0.0)
38+
rspec-mocks (3.0.3)
39+
rspec-support (~> 3.0.0)
40+
rspec-support (3.0.3)
41+
42+
PLATFORMS
43+
ruby
44+
45+
DEPENDENCIES
46+
cortex-client!
47+
rake (~> 10.3.2)
48+
rspec (~> 3.0)

lib/cortex/request.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def save(path, model)
5050

5151
def parse_response(response)
5252
if response.status < 300 || (response.body.kind_of?(Hash) && response.body['error'])
53-
OpenStruct.new({body: response.body, headers: response.headers.select { |k| ['content-range', 'x-total-items'].include? k } })
53+
OpenStruct.new({body: response.body, headers: { status: response.status }.merge(response.headers.select { |k| ['content-range', 'x-total-items'].include? k }) })
5454
else
55-
OpenStruct.new({:error => response.body, :status => response.status, :original => response})
55+
OpenStruct.new({body: {error: response.body, status: response.status, original: response}, headers: { status: response.status }})
5656
end
5757
end
5858
end

lib/cortex/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Cortex
2-
VERSION = '0.2.5'
2+
VERSION = '0.2.6'
33
end

spec/request_spec.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@
66
context 'with a cortex error response' do
77
it 'should return the error object' do
88
body = {'error' => 'Validation error or something'}
9-
response = OpenStruct.new(:status => 422, :body => body, :headers => { :whatever => "Whatever"})
10-
expect(client.parse_response(response).to_h).to eq({ :body => body, :headers => {}})
9+
response = OpenStruct.new(status: 422, body: body, headers: { :whatever => "Whatever"})
10+
expect(client.parse_response(response).to_h).to eq({ :body => body, :headers => {status: 422}})
1111
end
1212
end
1313

1414
context 'with a non-cortex error response' do
1515
it 'should return a wrapped response' do
1616
body = 'Catastrophic error'
17-
response = OpenStruct.new(:status => 500, :body => body, :headers => { :whatever => "Whatever" })
17+
response = OpenStruct.new(status: 500, body: body, headers: { whatever: "Whatever", status: 500 } )
1818
parsed = client.parse_response(response)
19-
expect(parsed.error).to eq(body)
20-
expect(parsed.status).to eq(500)
21-
expect(parsed.original).to eq(response)
19+
expect(parsed.body[:error]).to eq(body)
20+
expect(parsed.body[:status]).to eq(500)
21+
expect(parsed.body[:original]).to eq(response)
22+
expect(parsed.headers[:status]).to eq(500)
2223
end
2324
end
2425

2526
context 'with a successful response' do
2627
it 'should return the parsed body' do
2728
body = {:id => 1, title: 'A post'}
28-
response = OpenStruct.new(:status => 200, :body => body, :headers => { :whatever => "Whatever" })
29-
expect(client.parse_response(response).to_h).to eq({ :body => body, :headers => {}})
29+
response = OpenStruct.new(status: 200, body: body, headers: { :whatever => "Whatever" })
30+
expect(client.parse_response(response).to_h).to eq({ body: body, headers: {status: 200}})
3031
end
3132
end
3233
end

0 commit comments

Comments
 (0)