open-companies-house 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .rvmrc
2
+ .DS_Store
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 GoCardless
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ Open Companies House
2
+ ====================
3
+
4
+ Simple ruby wrapper around Companies House Open API
5
+
6
+ Usage:
7
+ =====
8
+
9
+ company = CompaniesHouse.lookup "07495895"
10
+ => #<CompaniesHouse:0x00000100932870 @registration_number="07495895"... >
11
+
12
+ company["CompanyName"]
13
+ => "GOCARDLESS LTD"
14
+
15
+ company.sic_code # convenience method
16
+ => "62090"
17
+
18
+ Specs
19
+ =====
20
+
21
+ rspec spec
22
+
23
+
24
+ Available Data
25
+ ==============
26
+
27
+ The following data is currently available on the `company` object returned by
28
+ `CompaniesHouse.lookup`
29
+
30
+
31
+ Call it like this:
32
+
33
+ company["RegAddress"]["AddressLine1"]
34
+ => "22-25 FINSBURY SQUARE"
35
+
36
+ All data:
37
+
38
+ {
39
+ "CompanyName" : "GOCARDLESS LTD",
40
+ "CompanyNumber" : "07495895",
41
+ "RegAddress" : {
42
+ "AddressLine1" : "22-25 FINSBURY SQUARE",
43
+ "PostTown" : "LONDON",
44
+ "Country" : "UNITED KINGDOM",
45
+ "Postcode" : "EC2A 1DX"
46
+ },
47
+ "CompanyCategory" : "Private Limited Company",
48
+ "CompanyStatus" : "Active",
49
+ "CountryOfOrigin" : "United Kingdom",
50
+ "IncorporationDate" : "17/01/2011",
51
+ "PreviousNames" : [
52
+ {
53
+ "CONDate" : "25/08/2011",
54
+ "CompanyName" : "GROUPAY LIMITED"
55
+ }
56
+ ],
57
+ "Accounts" : {
58
+ "AccountRefDay" : "31",
59
+ "AccountRefMonth" : "01",
60
+ "NextDueDate" : "31/10/2013",
61
+ "LastMadeUpDate" : "31/01/2012",
62
+ "AccountCategory" : "TOTAL EXEMPTION SMALL"
63
+ },
64
+ "Returns" : {
65
+ "NextDueDate" : "14/02/2013",
66
+ "LastMadeUpDate" : "17/01/2012"
67
+ },
68
+ "Mortgages" : {
69
+ "NumMortCharges" : "1",
70
+ "NumMortOutstanding" : "1",
71
+ "NumMortPartSatisfied" : "0",
72
+ "NumMortSatisfied" : "0"
73
+ },
74
+ "SICCodes" : {
75
+ "SicText" : [
76
+ "62090 - Other information technology service activities"
77
+ ]
78
+ }
79
+ }
80
+
81
+
82
+ TODO: Use some funky metaprogramming to make these available as human-readable
83
+ method calls, eg `company.name` or `company.registered_address.line_1`.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ desc "Run the test suite"
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.pattern = FileList['spec/**/*_spec.rb']
6
+ t.rspec_opts = %w(--color --format doc)
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,95 @@
1
+ class CompaniesHouse
2
+
3
+ class CompanyNotFound < StandardError
4
+ end
5
+
6
+ class InvalidRegistration < StandardError
7
+ end
8
+
9
+ class ServerError < StandardError
10
+ end
11
+
12
+ BASE_URI = 'http://data.companieshouse.gov.uk/doc/company/'
13
+
14
+ # Public API
15
+ #
16
+ # Usage:
17
+ # company = CompaniesHouse.lookup "07495895"
18
+ # => #<CompaniesHouse:0x00000100932870 @registration_number="07495895"... >
19
+ #
20
+ # company["CompanyName"]
21
+ # => "GOCARDLESS LTD"
22
+ #
23
+ def self.lookup(registration_number)
24
+ obj = self.new(registration_number)
25
+ obj.lookup
26
+ obj
27
+ end
28
+
29
+ # Return a SIC code from the ugly hash
30
+ #
31
+ # company.sic_code
32
+ # => "62090"
33
+ #
34
+ def sic_code
35
+ if self["SICCodes"] && self["SICCodes"]["SicText"]
36
+ self["SICCodes"]["SicText"].first.split[0]
37
+ end
38
+ end
39
+
40
+ # So that you can call attributes on the companies house object:
41
+ # company["CompanyName"]
42
+ def [](key)
43
+ @attributes[key]
44
+ end
45
+
46
+ attr_accessor :registration_number
47
+
48
+ # Don't call this directly. Instead, use CompaniesHouse.lookup "01234567"
49
+ def initialize(registration_number)
50
+ @registration_number = check_registration(registration_number)
51
+ @attributes = {}
52
+ end
53
+
54
+ # perform the HTTP request
55
+ def lookup
56
+ url = BASE_URI + @registration_number + ".json"
57
+ response = Faraday.get(url)
58
+ check_for_errors(response)
59
+ parse_response(response)
60
+ end
61
+
62
+ private
63
+
64
+ def check_registration(number)
65
+ number = number.to_s.strip # remove whitespace
66
+
67
+ number = "0" + number if number.length == 7 # 0-pad for luck
68
+
69
+ msg = "#{number} is not a valid UK company registration number"
70
+ raise InvalidRegistration.new(msg) unless number =~ /\A[0-9]{8}\z/
71
+
72
+ number
73
+ end
74
+
75
+ def check_for_errors(response)
76
+ if response.status == 404
77
+ msg = "Company not found with registration #{@registration_number}"
78
+ raise CompanyNotFound.new(msg)
79
+ end
80
+
81
+ unless response.status == 200
82
+ msg = "Companies House Responded with status #{response.status}"
83
+ raise ServerError.new(msg)
84
+ end
85
+ end
86
+
87
+ def parse_response(response)
88
+ # The response uses HTTP default Latin-1 encoding. Convert it to
89
+ # valid UTF8 as storing this in Mongo (for example) can cause
90
+ # problems.
91
+ body = response.body.encode("UTF-8", "ISO-8859-1")
92
+ data = JSON.parse(body)
93
+ @attributes = data["primaryTopic"]
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ class CompaniesHouse
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,4 @@
1
+ # require 'rubygems'
2
+ require 'faraday'
3
+ require 'json'
4
+ require File.join(File.dirname(__FILE__), 'companies_house/companies_house')
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../lib/companies_house/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.add_development_dependency 'rspec', '~> 2.9.0'
5
+ gem.add_development_dependency 'mocha', '~> 0.10.5'
6
+ gem.add_development_dependency 'webmock', '~> 1.8.8'
7
+
8
+ gem.add_dependency "faraday", "~> 0.8.4"
9
+ gem.add_dependency "json"
10
+
11
+ gem.name = 'open-companies-house'
12
+ gem.summary = "A wrapper around Companies House Open API"
13
+ gem.version = CompaniesHouse::VERSION.dup
14
+ gem.authors = ['Tom Blomfield']
15
+ gem.email = ['tom@gocardless.com']
16
+ gem.homepage = 'https://github.com/gocardless/open-companies-house'
17
+ gem.require_paths = ['lib']
18
+ gem.files = `git ls-files`.split("\n")
19
+ gem.test_files = `git ls-files -- spec/*`.split("\n")
20
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ describe CompaniesHouse do
4
+
5
+ let(:subject) { CompaniesHouse }
6
+
7
+ describe ".lookup" do
8
+
9
+ context "throws an exception" do
10
+ it "with a number that's too short" do
11
+ expect {
12
+ subject.lookup "123456"
13
+ }.to raise_exception CompaniesHouse::InvalidRegistration
14
+ end
15
+
16
+ it "that's too long" do
17
+ expect {
18
+ subject.lookup "123456789"
19
+ }.to raise_exception CompaniesHouse::InvalidRegistration
20
+ end
21
+
22
+ it "that's not numeric" do
23
+ expect {
24
+ subject.lookup "abcdefgh"
25
+ }.to raise_exception CompaniesHouse::InvalidRegistration
26
+ end
27
+
28
+ it "that's nil" do
29
+ expect {
30
+ subject.lookup nil
31
+ }.to raise_exception CompaniesHouse::InvalidRegistration
32
+ end
33
+ end
34
+
35
+ context "with a valid request" do
36
+ before do
37
+ url = "http://data.companieshouse.gov.uk/doc/company/07495895.json"
38
+ stub_request(:get, url).to_return(
39
+ :body => load_fixture("gocardless.json"),
40
+ :status => 200
41
+ )
42
+ end
43
+
44
+ it "0-pads 7 digit registration numbers" do
45
+ company = subject.lookup "7495895"
46
+ company.registration_number.should == "07495895"
47
+ end
48
+
49
+ it "loads attributes into the object body" do
50
+ company = subject.lookup "07495895"
51
+ company["CompanyName"].should == "GOCARDLESS LTD"
52
+ end
53
+
54
+ it "makes SIC code available as a convenience method" do
55
+ company = subject.lookup "07495895"
56
+ company.sic_code.should == "62090"
57
+ end
58
+ end
59
+
60
+ context "with a company that doesn't exist" do
61
+ before do
62
+ url = "http://data.companieshouse.gov.uk/doc/company/12345678.json"
63
+ stub_request(:get, url).to_return(
64
+ :body => "Some bullshit HTML response :-(",
65
+ :status => 404
66
+ )
67
+ end
68
+
69
+ it "raises an exception" do
70
+ expect {
71
+ company = subject.lookup "12345678"
72
+ }.to raise_exception CompaniesHouse::CompanyNotFound
73
+ end
74
+ end
75
+
76
+ context "when the server is down" do
77
+ before do
78
+ url = "http://data.companieshouse.gov.uk/doc/company/12345678.json"
79
+ stub_request(:get, url).to_return(
80
+ :body => "Oh noes",
81
+ :status => 500
82
+ )
83
+ end
84
+
85
+ it "raises an exception" do
86
+ expect {
87
+ company = subject.lookup "12345678"
88
+ }.to raise_exception CompaniesHouse::ServerError
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,44 @@
1
+ {
2
+ "primaryTopic" : {
3
+ "CompanyName" : "GOCARDLESS LTD",
4
+ "CompanyNumber" : "07495895",
5
+ "RegAddress" : {
6
+ "AddressLine1" : "22-25 FINSBURY SQUARE",
7
+ "PostTown" : "LONDON",
8
+ "Country" : "UNITED KINGDOM",
9
+ "Postcode" : "EC2A 1DX"
10
+ },
11
+ "CompanyCategory" : "Private Limited Company",
12
+ "CompanyStatus" : "Active",
13
+ "CountryOfOrigin" : "United Kingdom",
14
+ "IncorporationDate" : "17/01/2011",
15
+ "PreviousNames" : [
16
+ {
17
+ "CONDate" : "25/08/2011",
18
+ "CompanyName" : "GROUPAY LIMITED"
19
+ }
20
+ ],
21
+ "Accounts" : {
22
+ "AccountRefDay" : "31",
23
+ "AccountRefMonth" : "01",
24
+ "NextDueDate" : "31/10/2013",
25
+ "LastMadeUpDate" : "31/01/2012",
26
+ "AccountCategory" : "TOTAL EXEMPTION SMALL"
27
+ },
28
+ "Returns" : {
29
+ "NextDueDate" : "14/02/2013",
30
+ "LastMadeUpDate" : "17/01/2012"
31
+ },
32
+ "Mortgages" : {
33
+ "NumMortCharges" : "1",
34
+ "NumMortOutstanding" : "1",
35
+ "NumMortPartSatisfied" : "0",
36
+ "NumMortSatisfied" : "0"
37
+ },
38
+ "SICCodes" : {
39
+ "SicText" : [
40
+ "62090 - Other information technology service activities"
41
+ ]
42
+ }
43
+ }
44
+ }
@@ -0,0 +1,18 @@
1
+ require 'mocha'
2
+ require 'companies_house'
3
+ require 'json'
4
+ require 'webmock/rspec'
5
+
6
+ RSpec.configure do |config|
7
+
8
+ config.before(:suite) do
9
+ # Disable all live HTTP requests
10
+ WebMock.disable_net_connect!(allow_localhost: true)
11
+ end
12
+
13
+ config.mock_with :mocha
14
+ end
15
+
16
+ def load_fixture(*filename)
17
+ File.open(File.join('spec', 'data', *filename)).read
18
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open-companies-house
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Blomfield
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.9.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.9.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.10.5
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.10.5
46
+ - !ruby/object:Gem::Dependency
47
+ name: webmock
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.8
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.8
62
+ - !ruby/object:Gem::Dependency
63
+ name: faraday
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.8.4
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.8.4
78
+ - !ruby/object:Gem::Dependency
79
+ name: json
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description:
95
+ email:
96
+ - tom@gocardless.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE
104
+ - README.md
105
+ - Rakefile
106
+ - lib/companies_house.rb
107
+ - lib/companies_house/companies_house.rb
108
+ - lib/companies_house/version.rb
109
+ - open_companies_house.gemspec
110
+ - spec/companies_house/companies_house_spec.rb
111
+ - spec/data/gocardless.json
112
+ - spec/spec_helper.rb
113
+ homepage: https://github.com/gocardless/open-companies-house
114
+ licenses: []
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 1.8.24
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: A wrapper around Companies House Open API
137
+ test_files:
138
+ - spec/companies_house/companies_house_spec.rb
139
+ - spec/data/gocardless.json
140
+ - spec/spec_helper.rb