companies-house 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.0.6. now raise CompaniesHouse::Exception if response XML contains error
2
+
1
3
  v0.0.5. now sic_codes.sic_texts always returns array
2
4
 
3
5
  v0.0.4. cleaned up gem dependencies
data/Manifest CHANGED
@@ -15,5 +15,3 @@ README
15
15
  README.rdoc
16
16
  spec/lib/companies_house/request_spec.rb
17
17
  spec/lib/companies_house_spec.rb
18
- spec/spec.opts
19
- spec/spec_helper.rb
@@ -2,16 +2,16 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{companies-house}
5
- s.version = "0.0.5"
5
+ s.version = "0.0.6"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Rob McKinnon"]
9
- s.date = %q{2010-10-20}
9
+ s.date = %q{2010-10-22}
10
10
  s.description = %q{Ruby API to UK Companies House XML Gateway.
11
11
  }
12
12
  s.email = ["rob ~@nospam@~ rubyforge.org"]
13
13
  s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README"]
14
- s.files = ["CHANGELOG", "companies-house.yml.example", "init.rb", "lib/companies_house/company_details.haml", "lib/companies_house/exception.rb", "lib/companies_house/name_search.haml", "lib/companies_house/number_search.haml", "lib/companies_house/request.haml", "lib/companies_house/request.rb", "lib/companies_house.rb", "LICENSE", "Manifest", "Rakefile", "README", "README.rdoc", "spec/lib/companies_house/request_spec.rb", "spec/lib/companies_house_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "companies-house.gemspec"]
14
+ s.files = ["CHANGELOG", "companies-house.yml.example", "init.rb", "lib/companies_house/company_details.haml", "lib/companies_house/exception.rb", "lib/companies_house/name_search.haml", "lib/companies_house/number_search.haml", "lib/companies_house/request.haml", "lib/companies_house/request.rb", "lib/companies_house.rb", "LICENSE", "Manifest", "Rakefile", "README", "README.rdoc", "spec/lib/companies_house/request_spec.rb", "spec/lib/companies_house_spec.rb", "companies-house.gemspec"]
15
15
  s.homepage = %q{}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Companies-house", "--main", "README", "--inline-source"]
17
17
  s.require_paths = ["lib"]
@@ -14,7 +14,7 @@ require File.dirname(__FILE__) + '/companies_house/exception'
14
14
  $KCODE = 'UTF8' unless RUBY_VERSION >= "1.9"
15
15
 
16
16
  module CompaniesHouse
17
- VERSION = "0.0.5" unless defined? CompaniesHouse::VERSION
17
+ VERSION = "0.0.6" unless defined? CompaniesHouse::VERSION
18
18
 
19
19
  class << self
20
20
 
@@ -83,9 +83,40 @@ module CompaniesHouse
83
83
 
84
84
  def objectify response_xml
85
85
  doc = Hpricot.XML(response_xml)
86
- xml = doc.at('Body')
87
- if xml && xml.children.select(&:elem?).size > 0
88
- xml = xml.children.select(&:elem?).first.to_s
86
+ qualifier = doc.at('Qualifier')
87
+ if qualifier && qualifier.inner_text.to_s[/error/]
88
+ raise_error doc
89
+ else
90
+ body = doc.at('Body')
91
+ if body && body.children.select(&:elem?).size > 0
92
+ objectify_body body
93
+ else
94
+ nil
95
+ end
96
+ end
97
+ end
98
+
99
+ private
100
+
101
+ def add_to_message attribute, error, message, suffix=''
102
+ if value = error.at(attribute)
103
+ message << "#{value.inner_text}#{suffix}"
104
+ end
105
+ end
106
+
107
+ def raise_error doc
108
+ message = []
109
+ if error = doc.at('Error')
110
+ add_to_message 'RaisedBy', error, message
111
+ add_to_message 'Type', error, message
112
+ add_to_message 'Number', error, message, ':'
113
+ add_to_message 'Text', error, message
114
+ end
115
+ raise CompaniesHouse::Exception.new(message.join(' '))
116
+ end
117
+
118
+ def objectify_body body
119
+ xml = body.children.select(&:elem?).first.to_s
89
120
  hash = Hash.from_xml(xml)
90
121
  object = Morph.from_hash(hash, CompaniesHouse)
91
122
  if object && object.class.name == 'CompaniesHouse::CompanyDetails'
@@ -101,12 +132,7 @@ module CompaniesHouse
101
132
  end
102
133
  end
103
134
  object
104
- else
105
- nil
106
135
  end
107
- end
108
-
109
- private
110
136
 
111
137
  def get_response(data, root_element='NameSearch')
112
138
  begin
@@ -3,37 +3,89 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
3
3
  describe CompaniesHouse do
4
4
 
5
5
  describe 'when objectifying xml' do
6
- it 'should return an instance of CompaniesHouse::CompanyDetails' do
7
- xml = '<Body><company_details><company_name>£IBRA FINANCIAL SERVICES LIMITED</company_name></company_details></Body>'
8
- object = CompaniesHouse.objectify xml
9
- object.class.name.should == 'CompaniesHouse::CompanyDetails'
10
- end
11
- describe 'and xml contains expanded unicode' do
12
- it 'should convert to utf8' do
6
+ describe 'and xml contains response in Body' do
7
+ it 'should return an instance of CompaniesHouse::CompanyDetails' do
13
8
  xml = '<Body><company_details><company_name>£IBRA FINANCIAL SERVICES LIMITED</company_name></company_details></Body>'
14
9
  object = CompaniesHouse.objectify xml
15
- object.company_name.should == "£IBRA FINANCIAL SERVICES LIMITED"
10
+ object.class.name.should == 'CompaniesHouse::CompanyDetails'
16
11
  end
17
- end
18
- describe 'and xml contains single sic_text' do
19
- it 'should convert to sic_code sic_texts' do
20
- xml = '<Body><company_details><sic_codes><sic_text>9261 - Operate sports arenas &amp; stadiums</sic_text></sic_codes></company_details></Body>'
12
+ describe 'and xml contains expanded unicode' do
13
+ it 'should convert to utf8' do
14
+ xml = '<Body><company_details><company_name>£IBRA FINANCIAL SERVICES LIMITED</company_name></company_details></Body>'
15
+ object = CompaniesHouse.objectify xml
16
+ object.company_name.should == "£IBRA FINANCIAL SERVICES LIMITED"
17
+ end
18
+ end
19
+ describe 'and xml contains single sic_text' do
20
+ it 'should convert to sic_code sic_texts' do
21
+ xml = '<Body><company_details><sic_codes><sic_text>9261 - Operate sports arenas &amp; stadiums</sic_text></sic_codes></company_details></Body>'
21
22
 
22
- object = CompaniesHouse.objectify xml
23
- object.sic_codes.sic_text.should == '9261 - Operate sports arenas & stadiums'
24
- object.sic_codes.sic_texts.should == ['9261 - Operate sports arenas & stadiums']
23
+ object = CompaniesHouse.objectify xml
24
+ object.sic_codes.sic_text.should == '9261 - Operate sports arenas & stadiums'
25
+ object.sic_codes.sic_texts.should == ['9261 - Operate sports arenas & stadiums']
26
+ end
27
+ end
28
+ describe 'and xml contains no sic_text' do
29
+ it 'should return empty sic_code sic_texts' do
30
+ xml = '<Body><company_details><sic_codes></sic_codes></company_details></Body>'
31
+ object = CompaniesHouse.objectify xml
32
+ object.sic_codes.sic_texts.should == []
33
+ end
34
+ it 'should return empty sic_code sic_texts again' do
35
+ xml = '<Body><company_details><sic_codes><sic_text></sic_text></sic_codes></company_details></Body>'
36
+ object = CompaniesHouse.objectify xml
37
+ object.sic_codes.sic_texts.should == []
38
+ end
25
39
  end
26
40
  end
27
- describe 'and xml contains no sic_text' do
28
- it 'should return empty sic_code sic_texts' do
29
- xml = '<Body><company_details><sic_codes></sic_codes></company_details></Body>'
30
- object = CompaniesHouse.objectify xml
31
- object.sic_codes.sic_texts.should == []
41
+
42
+ describe 'and xml contains Error' do
43
+ it 'should raise CompaniesHouse::Exception' do
44
+ xml = xml_error
45
+ lambda { CompaniesHouse.objectify xml }.should raise_error(CompaniesHouse::Exception)
32
46
  end
33
- it 'should return empty sic_code sic_texts again' do
34
- xml = '<Body><company_details><sic_codes><sic_text></sic_text></sic_codes></company_details></Body>'
35
- object = CompaniesHouse.objectify xml
36
- object.sic_codes.sic_texts.should == []
47
+
48
+ it 'should include error message in exception' do
49
+ xml = xml_error
50
+ lambda { CompaniesHouse.objectify xml }.should raise_error(CompaniesHouse::Exception, 'NameSearch fatal 503: Repeat/non-incremental Transaction ID sent with request - Rejecting')
51
+ end
52
+
53
+ def xml_error
54
+ %Q|<?xml version="1.0" encoding="UTF-8" ?>
55
+ <GovTalkMessage xsi:schemaLocation="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch.xsd" xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
56
+ <EnvelopeVersion>1.0</EnvelopeVersion>
57
+ <Header>
58
+ <MessageDetails>
59
+ <Class>NameSearch</Class>
60
+ <Qualifier>error</Qualifier>
61
+ <TransactionID>1287070383</TransactionID>
62
+ <GatewayTimestamp>2010-10-14T16:33:04-00:00</GatewayTimestamp>
63
+ </MessageDetails>
64
+ <SenderDetails>
65
+ <IDAuthentication>
66
+ <SenderID>63884537357901904930357805221487</SenderID>
67
+ <Authentication>
68
+ <Method>CHMD5</Method>
69
+ <Value>3985a067aa3f4613adcf3589b420f4e2</Value>
70
+ </Authentication>
71
+ </IDAuthentication>
72
+ </SenderDetails>
73
+ </Header>
74
+ <GovTalkDetails>
75
+ <Keys/>
76
+ <GovTalkErrors>
77
+ <Error>
78
+ <RaisedBy>NameSearch</RaisedBy>
79
+ <Number>503</Number>
80
+ <Type>fatal</Type>
81
+ <Text>Repeat/non-incremental Transaction ID sent with request - Rejecting</Text>
82
+ <Location></Location>
83
+ </Error>
84
+ </GovTalkErrors>
85
+ </GovTalkDetails>
86
+ <Body>
87
+ </Body>
88
+ </GovTalkMessage>|
37
89
  end
38
90
  end
39
91
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: companies-house
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rob McKinnon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-20 00:00:00 +01:00
18
+ date: 2010-10-22 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -96,8 +96,6 @@ files:
96
96
  - README.rdoc
97
97
  - spec/lib/companies_house/request_spec.rb
98
98
  - spec/lib/companies_house_spec.rb
99
- - spec/spec.opts
100
- - spec/spec_helper.rb
101
99
  - companies-house.gemspec
102
100
  has_rdoc: true
103
101
  homepage: ""
data/spec/spec.opts DELETED
@@ -1,7 +0,0 @@
1
- --color
2
- --format
3
- progress
4
- --loadby
5
- mtime
6
- --reverse
7
- --diff
data/spec/spec_helper.rb DELETED
@@ -1,9 +0,0 @@
1
- require 'rubygems'
2
- gem 'rspec'
3
- require 'spec'
4
-
5
- require File.dirname(__FILE__) + '/../lib/companies_house'
6
-
7
- def fixture(filename)
8
- open("#{File.dirname(__FILE__)}/fixtures/#{filename}").read
9
- end