companies-house-gateway 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 730c887e04b54fc8bb3e60f7540e68eae00d59a9
4
- data.tar.gz: b5f7058d26397da50d1afb2ae9be1eda41e916b8
3
+ metadata.gz: 1849937f401ec3fe9197d91cc25f8ace31043df1
4
+ data.tar.gz: bbcfd60440e011fa247b5ba9aa324e3de76569a7
5
5
  SHA512:
6
- metadata.gz: 6b0d5806eb08164da30a9fc469429ee2807ad4fb9e7f98eee4da224a2d0d2acd8c57deeb2270bf84588b15bdd42d3ef047a188e83abedc4ad172737f2fdea9a9
7
- data.tar.gz: 11dc3b0517c6d66b87654fd3136ee3a1c6bacb5391eb6e5bfc9c670ab00e9ddb8599232ddcd0317d244effdd10db905ad7d1d329df5b11b51d0b20dc1fe6cd8a
6
+ metadata.gz: 9d0b5788b304f5a6e380678910bb3416d6c917f80e783a7a76fc1fd63808c3d93b7f8878114938032e1afb012c8675fc3cdc5eea1e6839d40f5a533208503951
7
+ data.tar.gz: b6e03f18aea13bd71d6e082d09e7c13c4e54b4854e448e416b0c1e1b06ce415653628fd6c6866827d53a0c08cad95bba41d46a50b6823ec64543c86008da6262
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.3.6 - March 3, 2014
2
+
3
+ - Add error_code attribute to APIError
4
+
1
5
  ## 0.3.5 - February 26, 2014
2
6
 
3
7
  - Allow requests to be cached
@@ -1,4 +1,16 @@
1
1
  module CompaniesHouseGateway
2
2
  class APIError < CompaniesHouseGatewayError
3
+ attr_accessor :error_code
4
+
5
+ def initialize(message, error_code=nil, status=nil, response_body=nil)
6
+ super(message, status, response_body)
7
+ @error_code = error_code
8
+ end
9
+
10
+ def to_s
11
+ str = super
12
+ str += " [Error code: #{error_code}]" if error_code
13
+ str
14
+ end
3
15
  end
4
16
  end
@@ -2,7 +2,7 @@ module CompaniesHouseGateway
2
2
  class InvalidRequestError < CompaniesHouseGatewayError
3
3
  attr_accessor :param
4
4
 
5
- def initialize(message, param, status=nil, response_body=nil)
5
+ def initialize(message=nil, param=nil, status=nil, response_body=nil)
6
6
  super(message, status, response_body)
7
7
  @param = param
8
8
  end
@@ -3,18 +3,7 @@ module CompaniesHouseGateway
3
3
  class CheckResponse < Faraday::Middleware
4
4
  def call(env)
5
5
  @app.call(env).on_complete do |env|
6
- response_body = env[:body]["GovTalkMessage"]
7
-
8
- unless response_body && response_body["GovTalkDetails"]
9
- raise InvalidResponseError.new(nil, env[:status], env)
10
- end
11
-
12
- if errors = response_body["GovTalkDetails"]["GovTalkErrors"]
13
- errors = errors.values.flatten
14
- messages = errors.map { |e| "(#{e['Number']}) #{e['Text']}" }
15
- raise APIError.new(messages.join(" | "), env[:status], env)
16
- end
17
-
6
+ check_for_errors(env)
18
7
  response_values(env)
19
8
  end
20
9
  end
@@ -22,6 +11,18 @@ module CompaniesHouseGateway
22
11
  def response_values(env)
23
12
  { status: env[:status], headers: env[:headers], body: env[:body] }
24
13
  end
14
+
15
+ def check_for_errors(env)
16
+ body = env[:body]["GovTalkMessage"]
17
+ unless body && body["GovTalkDetails"]
18
+ raise InvalidResponseError.new(env[:body], env[:status], env)
19
+ end
20
+
21
+ if body["GovTalkDetails"]["GovTalkErrors"]
22
+ error = body["GovTalkDetails"]["GovTalkErrors"].values.flatten.first
23
+ raise APIError.new(error['Text'], error['Number'], env[:status], env)
24
+ end
25
+ end
25
26
  end
26
27
 
27
28
  Faraday.register_middleware :response, check_ch_response: CheckResponse
@@ -1,3 +1,3 @@
1
1
  module CompaniesHouseGateway
2
- VERSION = '0.3.5'.freeze
2
+ VERSION = '0.3.6'.freeze
3
3
  end
data/spec/request_spec.rb CHANGED
@@ -79,8 +79,10 @@ describe CompaniesHouseGateway::Request do
79
79
 
80
80
  it "wraps the error" do
81
81
  expect { perform_check }.
82
- to raise_error(
83
- CompaniesHouseGateway::APIError, "(604) Error text")
82
+ to raise_error CompaniesHouseGateway::APIError do |error|
83
+ error.error_code.should == "604"
84
+ error.message.should == "Error text"
85
+ end
84
86
  end
85
87
  end
86
88
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: companies-house-gateway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grey Baker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-27 00:00:00.000000000 Z
11
+ date: 2014-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday