http_api_client 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -159,3 +159,6 @@ This will install `/usr/local/opt/curl-ca-bundle/share/ca-bundle.crt`
159
159
  ### 0.1.4 - 2014-04-07
160
160
  * Don't url encode log values
161
161
 
162
+ ### 0.1.5 - 2014-04-08
163
+ * Logging and metrics are separate concerns and both are injectable
164
+
@@ -6,7 +6,7 @@ require 'http_api_client/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'http_api_client'
8
8
  spec.version = HttpApiClient::VERSION
9
- spec.authors = ['Rob Monie', 'Andrei Miulescu', 'Stuart Liston', 'Chris Rhode']
9
+ spec.authors = ['Rob Monie', 'Andrei Miulescu', 'Stuart Liston', 'Chris Rode']
10
10
  spec.email = ['robmonie@gmail.com']
11
11
  spec.description = %q{Http client wrapper for simplified api access}
12
12
  spec.summary = %q{}
@@ -88,7 +88,7 @@ module HttpApiClient
88
88
  end
89
89
 
90
90
  def handle_response(response, method, path)
91
- if ok?(response) || validation_failed?(response)
91
+ if ok?(response)
92
92
  if response.body
93
93
  #Don't use regular load method - any strings starting with ':' ( :-) from example) will be interpreted as a symbol
94
94
  Oj.strict_load(response.body)
@@ -1,4 +1,5 @@
1
- require 'http_api_client'
1
+ # encoding: utf-8
2
+ require 'oj'
2
3
 
3
4
  module HttpApiClient
4
5
 
@@ -19,7 +20,7 @@ module HttpApiClient
19
20
  messages = [@message]
20
21
  messages << nested_error.message if nested_error
21
22
  messages << response_body
22
- messages.join("\n\n")
23
+ messages.join(", ")
23
24
  end
24
25
 
25
26
  end
@@ -33,7 +34,11 @@ module HttpApiClient
33
34
  class NotAcceptable < BaseError ; end
34
35
  class RequestTimeout < BaseError ; end
35
36
  class UnknownStatus < BaseError ; end
36
- class UnprocessableEntity < BaseError ; end
37
+ class UnprocessableEntity < BaseError
38
+ def as_json
39
+ Oj.load(response_body)
40
+ end
41
+ end
37
42
  class TooManyRequests < BaseError ; end
38
43
 
39
44
  #500 Range
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module HttpApiClient
2
- VERSION = "0.1.5"
4
+ VERSION = "0.2.0"
3
5
  end
@@ -54,6 +54,50 @@ module HttpApiClient
54
54
 
55
55
  describe "#create" do
56
56
 
57
+ shared_examples "#create" do
58
+ let(:response_double) {double('post response', body: response_json, status: status)}
59
+ let(:response_json) {{'this' => 'that'}.to_json}
60
+ context "when the post returns a valid response" do
61
+ let(:status) {200}
62
+ it "calls http connection with post and correct url and post data" do
63
+ connection.should_receive(:post).with('/test-base-uri/path', JSON.fast_generate(payload), base_update_headers).and_return(response_double)
64
+ expect(client.create('/path', payload)).to eq(JSON.parse(response_json))
65
+ end
66
+ end
67
+ context "when the post returns a 422 response" do
68
+ let(:status) {422}
69
+ before do
70
+ connection.should_receive(:post).with('/test-base-uri/path', JSON.fast_generate(payload), base_update_headers).and_return(response_double)
71
+ end
72
+ it "returns a UnprocessableEntity exception" do
73
+ expect{client.create('/path', payload)}.to raise_error(Errors::UnprocessableEntity)
74
+ end
75
+ it "has the correct response data" do
76
+ begin
77
+ client.create('/path', payload)
78
+ rescue Errors::BaseError => e
79
+ expect(e.response_body).to eq(response_json)
80
+ end
81
+ end
82
+ end
83
+ context "when the post returns an error response" do
84
+ let(:status) {500}
85
+ before do
86
+ connection.should_receive(:post).with('/test-base-uri/path', JSON.fast_generate(payload), base_update_headers).and_return(response_double)
87
+ end
88
+ it "returns a InternalServerError exception" do
89
+ expect{client.create('/path', payload)}.to raise_error(Errors::InternalServerError)
90
+ end
91
+ it "has the correct response data" do
92
+ begin
93
+ client.create('/path', payload)
94
+ rescue Errors::BaseError => e
95
+ expect(e.response_body).to eq(response_json)
96
+ end
97
+ end
98
+ end
99
+ end
100
+
57
101
  context "with auth params" do
58
102
  let(:client) do
59
103
  klass = Class.new(Client) do
@@ -63,18 +107,15 @@ module HttpApiClient
63
107
  end
64
108
  klass.new(:my_client, 'spec/config/http_api_clients_with_basic_auth.yml')
65
109
  end
66
-
67
- it "calls http connection with post and correct url and post data" do
68
- payload = { text: "hello", :key => 'one' }
69
- connection.should_receive(:post).with('/test-base-uri/path', JSON.fast_generate(payload), base_update_headers)
70
- client.create('/path', payload)
110
+ it_behaves_like "#create" do
111
+ let(:payload) {{ text: "hello", :key => "one"}}
71
112
  end
72
113
  end
73
114
 
74
- it "calls http connection with post and correct url and post data" do
75
- payload = { text: "hello"}
76
- connection.should_receive(:post).with('/test-base-uri/path', JSON.fast_generate(payload), base_update_headers)
77
- client.create('/path', payload)
115
+ context "without auth params" do
116
+ it_behaves_like "#create" do
117
+ let(:payload) {{ text: "hello"}}
118
+ end
78
119
  end
79
120
 
80
121
  end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
 
2
4
  require 'http_api_client/errors'
3
5
 
@@ -31,5 +33,19 @@ module HttpApiClient
31
33
  end
32
34
 
33
35
  end
36
+ describe UnprocessableEntity do
37
+ let(:exception) {described_class.new(message,Oj.dump(response_body))}
38
+ let(:response_body) {{'some' => 'json thing'}}
39
+ let(:message) { 'this is a message that is really informative and stuff and junk'}
40
+
41
+ it "is a BaseError" do
42
+ expect(exception).to be_a_kind_of(BaseError)
43
+ end
44
+ describe "#as_json" do
45
+ it "returns the response_body parsed" do
46
+ expect(exception.as_json).to eq(response_body)
47
+ end
48
+ end
49
+ end
34
50
  end
35
51
  end
metadata CHANGED
@@ -1,21 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Rob Monie
8
9
  - Andrei Miulescu
9
10
  - Stuart Liston
10
- - Chris Rhode
11
+ - Chris Rode
11
12
  autorequire:
12
13
  bindir: bin
13
14
  cert_chain: []
14
- date: 2014-04-08 00:00:00.000000000 Z
15
+ date: 2014-04-14 00:00:00.000000000 Z
15
16
  dependencies:
16
17
  - !ruby/object:Gem::Dependency
17
18
  name: bundler
18
19
  requirement: !ruby/object:Gem::Requirement
20
+ none: false
19
21
  requirements:
20
22
  - - ~>
21
23
  - !ruby/object:Gem::Version
@@ -23,6 +25,7 @@ dependencies:
23
25
  type: :development
24
26
  prerelease: false
25
27
  version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
26
29
  requirements:
27
30
  - - ~>
28
31
  - !ruby/object:Gem::Version
@@ -30,6 +33,7 @@ dependencies:
30
33
  - !ruby/object:Gem::Dependency
31
34
  name: rspec
32
35
  requirement: !ruby/object:Gem::Requirement
36
+ none: false
33
37
  requirements:
34
38
  - - ~>
35
39
  - !ruby/object:Gem::Version
@@ -37,6 +41,7 @@ dependencies:
37
41
  type: :development
38
42
  prerelease: false
39
43
  version_requirements: !ruby/object:Gem::Requirement
44
+ none: false
40
45
  requirements:
41
46
  - - ~>
42
47
  - !ruby/object:Gem::Version
@@ -44,6 +49,7 @@ dependencies:
44
49
  - !ruby/object:Gem::Dependency
45
50
  name: pry
46
51
  requirement: !ruby/object:Gem::Requirement
52
+ none: false
47
53
  requirements:
48
54
  - - ~>
49
55
  - !ruby/object:Gem::Version
@@ -51,6 +57,7 @@ dependencies:
51
57
  type: :development
52
58
  prerelease: false
53
59
  version_requirements: !ruby/object:Gem::Requirement
60
+ none: false
54
61
  requirements:
55
62
  - - ~>
56
63
  - !ruby/object:Gem::Version
@@ -58,6 +65,7 @@ dependencies:
58
65
  - !ruby/object:Gem::Dependency
59
66
  name: pry-debugger
60
67
  requirement: !ruby/object:Gem::Requirement
68
+ none: false
61
69
  requirements:
62
70
  - - ~>
63
71
  - !ruby/object:Gem::Version
@@ -65,6 +73,7 @@ dependencies:
65
73
  type: :development
66
74
  prerelease: false
67
75
  version_requirements: !ruby/object:Gem::Requirement
76
+ none: false
68
77
  requirements:
69
78
  - - ~>
70
79
  - !ruby/object:Gem::Version
@@ -72,6 +81,7 @@ dependencies:
72
81
  - !ruby/object:Gem::Dependency
73
82
  name: activesupport
74
83
  requirement: !ruby/object:Gem::Requirement
84
+ none: false
75
85
  requirements:
76
86
  - - ! '>='
77
87
  - !ruby/object:Gem::Version
@@ -79,6 +89,7 @@ dependencies:
79
89
  type: :runtime
80
90
  prerelease: false
81
91
  version_requirements: !ruby/object:Gem::Requirement
92
+ none: false
82
93
  requirements:
83
94
  - - ! '>='
84
95
  - !ruby/object:Gem::Version
@@ -86,6 +97,7 @@ dependencies:
86
97
  - !ruby/object:Gem::Dependency
87
98
  name: faraday
88
99
  requirement: !ruby/object:Gem::Requirement
100
+ none: false
89
101
  requirements:
90
102
  - - ~>
91
103
  - !ruby/object:Gem::Version
@@ -93,6 +105,7 @@ dependencies:
93
105
  type: :runtime
94
106
  prerelease: false
95
107
  version_requirements: !ruby/object:Gem::Requirement
108
+ none: false
96
109
  requirements:
97
110
  - - ~>
98
111
  - !ruby/object:Gem::Version
@@ -100,6 +113,7 @@ dependencies:
100
113
  - !ruby/object:Gem::Dependency
101
114
  name: net-http-persistent
102
115
  requirement: !ruby/object:Gem::Requirement
116
+ none: false
103
117
  requirements:
104
118
  - - ~>
105
119
  - !ruby/object:Gem::Version
@@ -107,6 +121,7 @@ dependencies:
107
121
  type: :runtime
108
122
  prerelease: false
109
123
  version_requirements: !ruby/object:Gem::Requirement
124
+ none: false
110
125
  requirements:
111
126
  - - ~>
112
127
  - !ruby/object:Gem::Version
@@ -114,6 +129,7 @@ dependencies:
114
129
  - !ruby/object:Gem::Dependency
115
130
  name: oj
116
131
  requirement: !ruby/object:Gem::Requirement
132
+ none: false
117
133
  requirements:
118
134
  - - ~>
119
135
  - !ruby/object:Gem::Version
@@ -121,6 +137,7 @@ dependencies:
121
137
  type: :runtime
122
138
  prerelease: false
123
139
  version_requirements: !ruby/object:Gem::Requirement
140
+ none: false
124
141
  requirements:
125
142
  - - ~>
126
143
  - !ruby/object:Gem::Version
@@ -161,26 +178,27 @@ files:
161
178
  homepage: ''
162
179
  licenses:
163
180
  - MIT
164
- metadata: {}
165
181
  post_install_message:
166
182
  rdoc_options: []
167
183
  require_paths:
168
184
  - lib
169
185
  required_ruby_version: !ruby/object:Gem::Requirement
186
+ none: false
170
187
  requirements:
171
188
  - - ! '>='
172
189
  - !ruby/object:Gem::Version
173
190
  version: '0'
174
191
  required_rubygems_version: !ruby/object:Gem::Requirement
192
+ none: false
175
193
  requirements:
176
194
  - - ! '>='
177
195
  - !ruby/object:Gem::Version
178
196
  version: '0'
179
197
  requirements: []
180
198
  rubyforge_project:
181
- rubygems_version: 2.2.2
199
+ rubygems_version: 1.8.23
182
200
  signing_key:
183
- specification_version: 4
201
+ specification_version: 3
184
202
  summary: ''
185
203
  test_files:
186
204
  - spec/config/http_api_clients.yml
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MWRkMGU2ZjI2YjVjMTA1ODJhMjhiYjI0ZDQxMGRlZjRhMDYwY2FjYg==
5
- data.tar.gz: !binary |-
6
- NDFjZjk2NjlkYmY5MjUxMzk5MzFkNGZiNTQxNWI5Yzc4YzFlYjFlMQ==
7
- SHA512:
8
- metadata.gz: !binary |-
9
- NzNiZmQ5M2Y2OGVlZDQwYjkyYTcwNDE5ZWJjMTA1NzM3NzJiNThlZjBkZWJm
10
- NmFmNWUwNjIzMzUyNmY0ODU3NTY0Y2I2Y2VkZmI3MTU3MGM1YTllNmUxOGIx
11
- NWFlYjIwYTI0MmY5ODQ1OTNhOWNkMDVjZWZlYzcwZDhmYWVjNzc=
12
- data.tar.gz: !binary |-
13
- MmQwMzk0ZTFhN2JlNzA4ZTFhOGRjMjZkMGEzZjE4YTBlYjQ3NjYyNmNhZDBk
14
- ODhhNGIyNmJmZTViYzA0YmM4NTE5NjBmM2M5ZTVkMDJiZDhiMWEzM2RhNzEx
15
- YjQwMjRkMWNhNjVkN2Q3ZmE4NjRiZmMxMWFiZGY5ZWVkNzc4Yjc=