chjsonapi 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e93a21d70d4ccbde889354002123685c6b38afb6
4
+ data.tar.gz: 3ee907f4e75fb4c60aac551ef8ed4fff2802f51e
5
+ SHA512:
6
+ metadata.gz: 2b79297eea78c3e5046b38888c408c12793de6b21f97e76a7759b95c738be1a44a35b1d635f0df6a5c32a114ea1397aeda840b5a448aeb814569091de137e267
7
+ data.tar.gz: 86e86b6e9aa8cb4f457f396d9daedc4846d031a970db778556dcf667911d9e254ab0f58dcd9a4913b3111278ba3868b11b6872cde149305a959f4ea8cb6d07e4
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ api_key.txt
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chjsonapi.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chjsonapi (0.3.2)
5
+ curb (~> 0.8)
6
+ json (~> 1.8)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ curb (0.8.8)
12
+ diff-lcs (1.2.5)
13
+ json (1.8.3)
14
+ rake (10.4.2)
15
+ rspec (3.3.0)
16
+ rspec-core (~> 3.3.0)
17
+ rspec-expectations (~> 3.3.0)
18
+ rspec-mocks (~> 3.3.0)
19
+ rspec-core (3.3.1)
20
+ rspec-support (~> 3.3.0)
21
+ rspec-expectations (3.3.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.3.0)
24
+ rspec-mocks (3.3.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.3.0)
27
+ rspec-support (3.3.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.10)
34
+ chjsonapi!
35
+ rake (~> 10.0)
36
+ rspec (~> 3.3)
37
+
38
+ BUNDLED WITH
39
+ 1.10.6
data/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # ChJsonApi <br> Companies House JSON API Gem
2
+
3
+ Ruby Gem to help make requests to <a href="https://developer.companieshouse.gov.uk/api/docs/" title="Companies House">Companies House</a> public API.
4
+
5
+ Version 0.3.2
6
+ - Adding support for multiple keys.
7
+
8
+ Version 0.3.1
9
+ - Changing exception thrown on Too Many Requests
10
+
11
+ Version 0.3.0
12
+ - Handling "Too Many Requests" message (CH says that happens when 600 requests are sent in 5 minutes. Then the next requests have the HTTP status header of 429 Too Many Requests)
13
+
14
+ Version 0.2.2:
15
+ - Refactoring
16
+ - Changing gem name
17
+
18
+ Version 0.2.1:
19
+ - Removing CH Api key
20
+ - Changing version on Gemspec file
21
+
22
+ Version 0.2.0:
23
+ - Changed basic API. No longer accepts a single String as parameter. A named hash must always be provided to the functions.
24
+ - Added optional parameters to the officers request.
25
+ - Created Company Filing
26
+
27
+
28
+ Version 0.1.0:
29
+ - Created Company profile API call
30
+ - Created Company registered office API call
31
+ - Created Company officers API call
32
+
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ ```ruby
39
+ gem 'chjsonapi'
40
+ ```
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install chjsonapi
49
+
50
+ ## Usage
51
+
52
+ ###Require
53
+
54
+ Add this line to files where the Gem will be used.
55
+
56
+ ```ruby
57
+ require 'chjsonapi'
58
+ ```
59
+
60
+ ###Companies House API Key
61
+
62
+ To use Companies House API, you need to configure an API Key, which you can obtain from <a target="_blank" href="https://developer.companieshouse.gov.uk/api/docs/index/gettingStarted/apikey_authorisation.html" title="Companies House Authentication API key"> their website</a>.
63
+
64
+ Before making any requests, you need to initialise the Gem by calling
65
+
66
+ ```ruby
67
+ ChJsonApi.init "YOUR_API_KEY"
68
+ ```
69
+
70
+ Or, if you have more than one key available, you can initialise the Gem with an array of keys by calling
71
+
72
+ ```ruby
73
+ ChJsonApi.init ["FIRST_KEY", "SECOND_KEY", ... , "LAST_KEY"]
74
+ ```
75
+
76
+ ###Querying for <a target="_blank" href="https://developer.companieshouse.gov.uk/api/docs/company/company_number/readCompanyProfile.html" title="Company Profile">Company profile</a>
77
+
78
+ ```ruby
79
+ json = ChJsonApi::Company.profile company_number:'COMPANY_NUMBER'
80
+ puts json['company_number']
81
+ ```
82
+
83
+ ###Querying for <a target="_blank" href="https://developer.companieshouse.gov.uk/api/docs/company/company_number/registered-office-address/readRegisteredOfficeAddress.html" title="Company Registered Office Address">Company Registered Office Address</a>
84
+
85
+ ```ruby
86
+ json = ChJsonApi::Company.registered_office company_number:'COMPANY_NUMBER'
87
+ puts json['locality']
88
+ ```
89
+
90
+ ###Querying for <a target="_blank" href="https://developer.companieshouse.gov.uk/api/docs/company/company_number/officers/officerList.html" title="Company Officers List">Company Officers List</a>
91
+
92
+ ```ruby
93
+ json = ChJsonApi::Company.officers company_number:'COMPANY_NUMBER'
94
+ puts json['items'][0]['address']['premises']
95
+ ```
96
+
97
+ Or any of the optional parameters
98
+
99
+ ```ruby
100
+ json = ChJsonApi::Company.officers company_number:'COMPANY_NUMBER',items_per_page:X,start_index:X,order_by:'ORDER_BY'
101
+ puts json['items'][0]['address']['premises']
102
+ ```
103
+
104
+
105
+ ###Querying for a specific <a target="_blank" href="https://developer.companieshouse.gov.uk/api/docs/company/company_number/filing-history/transaction_id/getFilingHistoryItem.html" title="Filing History Item">Filing History Item</a>
106
+
107
+ ```ruby
108
+ json = ChJsonApi::Company.filing_item company_number:'COMPANY_NUMBER',items_per_page:X,start_index:X,order_by:'ORDER_BY'
109
+ puts json['transaction_id']
110
+ ```
111
+
112
+
113
+ ###Querying for <a target="_blank" href="https://developer.companieshouse.gov.uk/api/docs/company/company_number/filing-history/getFilingHistoryList.html" title="Filing History List">Filing History</a>
114
+
115
+ ```ruby
116
+ json = ChJsonApi::Company.filing_list company_number:'COMPANY_NUMBER',items_per_page:X,start_index:X,order_by:'ORDER_BY'
117
+ puts json['transaction_id']
118
+ ```
119
+
120
+ Or any of the optional parameters
121
+
122
+ ```ruby
123
+ json = ChJsonApi::Company.officers company_number:'COMPANY_NUMBER',items_per_page:X,start_index:X,category:'ORDER_BY'
124
+ puts json['transaction_id']
125
+ ```
126
+
127
+ ## Development
128
+
129
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
130
+
131
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
132
+
133
+
134
+ ## License
135
+
136
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "chjsonapi"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/chjsonapi.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chjsonapi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chjsonapi"
8
+ spec.version = ChJsonApi::VERSION
9
+ spec.authors = ["Matheus Serpellone"]
10
+ spec.email = ["m.serpellone@gmail.com"]
11
+
12
+
13
+ spec.summary = %q{Gem to assist on calls to Companies House JSON API}
14
+ spec.description = %q{Companies House JSON API gem}
15
+ spec.homepage = "https://github.com/inniaccounts/companies-house-json-api"
16
+ spec.license = "MIT"
17
+
18
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
19
+ # delete this section to allow pushing this gem to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.3"
34
+
35
+ spec.add_dependency "curb", "~> 0.8"
36
+ spec.add_dependency "json", "~> 1.8"
37
+
38
+ end
data/lib/chjsonapi.rb ADDED
@@ -0,0 +1,143 @@
1
+ require "chjsonapi/version"
2
+ require 'curb'
3
+ require 'json'
4
+ require_relative 'chjsonapi/company'
5
+
6
+ # Base class for the CH Json Api
7
+ # Can be used to make direct API calls to Companies House domain
8
+ # The extended classes should implement specific methods to interface with api_call by providing the appropriate
9
+ # URL handles, querystrings and request types
10
+ class ChJsonApi
11
+
12
+
13
+
14
+ #Call this before using any other method
15
+ def self.init(key)
16
+ if key.is_a? Array
17
+ @key = key
18
+ elsif key.is_a? String
19
+ @key = [key]
20
+ else
21
+ raise 'Invalid Key Type. String or Array of Strings accepted only.'
22
+ end
23
+
24
+ @index = 0
25
+ @tries = 0
26
+ true
27
+ end
28
+
29
+
30
+ #Calls the Companies House API at the service specified by handler and with the optional parameters provided in query
31
+ def self.api_call(handler, query)
32
+
33
+ raise 'Uninitialised API. Call ChJsonApi.init(key) with your Companies House API key before running any requests' if !@key || @key.empty?
34
+
35
+ query = normalise_query(query)
36
+
37
+ result = execute_call(handler, query)
38
+
39
+ return extract_response(result.body_str)
40
+
41
+ end
42
+
43
+ def self.execute_call(handler, query)
44
+ @result ||= Curl::Easy.new
45
+
46
+ @result.url = "https://api.companieshouse.gov.uk/#{handler}#{query}"
47
+
48
+ @result.username = "#{choose_key}:"
49
+ @result.http_auth_types = :basic
50
+
51
+ @result.perform
52
+
53
+ code = @result.response_code
54
+
55
+ #Detect any errors.
56
+ #If found, treat them and retry the function
57
+ #This will
58
+ if code == 401
59
+ handle_invalid_key(@result)
60
+ @result = execute_call(handler, query)
61
+ end
62
+
63
+ if code == 429
64
+ handle_too_many_requests(@result)
65
+ @result = execute_call(handler, query)
66
+ end
67
+
68
+ ##All ok, reset the "tries" counter and return the result
69
+ @tries = 0
70
+ @result
71
+ end
72
+
73
+ def self.extract_response(response)
74
+ return {} if response.empty?
75
+ json = JSON.parse(response)
76
+
77
+ return json unless json['errors']
78
+
79
+ raise json['errors'][0].map { |key, value| "#{key}=>#{value}" }.join(' ')
80
+
81
+ end
82
+
83
+ #TODO accept a key => value hash
84
+ def self.normalise_query(query)
85
+ if !query
86
+ query = ''
87
+ elsif query.kind_of? Array
88
+ query = "?#{query.join '&'}"
89
+ elsif !query.kind_of? String
90
+ raise 'Query must be a string or an array with each element being a "key=value" string'
91
+ end
92
+ query
93
+ end
94
+
95
+
96
+ private
97
+ def self.choose_key
98
+ if @key.length > 0
99
+ @key[@index % @key.count]
100
+ else
101
+ raise 'No valid keys!'
102
+ end
103
+ end
104
+
105
+ def self.try_another_key
106
+ #try next key and increment the number of tries so far
107
+ #Increment to next key
108
+ @index ||= 0
109
+ @index += 1
110
+
111
+ #Increase number of tries
112
+ @tries ||= 0
113
+ @tries += 1
114
+
115
+ #If we have tried all keys, return false
116
+ continue?
117
+ end
118
+
119
+ def self.handle_invalid_key(result)
120
+ #Removes key from the array
121
+ @key.delete @key[@index]
122
+ if continue?
123
+ @tries += 1
124
+ return true
125
+ else
126
+ raise RuntimeError.new({message: 'Unauthorized. Please check your API keys.', request: result})
127
+ end
128
+ end
129
+
130
+ def self.continue?
131
+ #Check if we tried all keys.
132
+ @tries <= @key.count
133
+ end
134
+
135
+ def self.handle_too_many_requests(result)
136
+ #Too Many Requests, just switch to next key
137
+ unless try_another_key
138
+ raise RuntimeError.new({message: 'Too Many Requests', request: result})
139
+ end
140
+ true
141
+ end
142
+
143
+ end
@@ -0,0 +1,59 @@
1
+
2
+ class ChJsonApi
3
+ #Subclass of CHJsonApi that handles requests about the Company
4
+ class Company
5
+
6
+ def self.profile(param_hash)
7
+ req, _ = validate_param_hash param_hash, [:company_number]
8
+ ChJsonApi.api_call("company/#{req[:company_number]}", nil)
9
+ end
10
+
11
+ def self.registered_office(param_hash)
12
+ req, _ = validate_param_hash param_hash, [:company_number]
13
+ ChJsonApi.api_call("company/#{req[:company_number]}/registered-office-address", nil)
14
+ end
15
+
16
+ def self.officers(param_hash)
17
+ req, opt = validate_param_hash param_hash, [:company_number], [:items_per_page,:start_index,:order_by]
18
+ ChJsonApi.api_call("company/#{req[:company_number]}/officers", opt)
19
+ end
20
+
21
+ def self.filing_item(param_hash)
22
+ req, _ = validate_param_hash param_hash, [:company_number, :transaction_id]
23
+ ChJsonApi.api_call("company/#{req[:company_number]}/filing-history/#{req[:transaction_id]}", nil)
24
+ end
25
+
26
+ def self.filing_list(param_hash)
27
+ req, opt = validate_param_hash param_hash, [:company_number], [:items_per_page,:start_index,:category]
28
+ ChJsonApi.api_call("company/#{req[:company_number]}/filing-history", opt)
29
+ end
30
+
31
+
32
+
33
+ def self.validate_param_hash(param_hash, required, optional=[])
34
+ if param_hash.kind_of? Hash
35
+
36
+ required_fields = {}
37
+ optional_fields = []
38
+ param_hash.each do |key, value|
39
+
40
+ valid_value = value.to_s.scan(/[^0-9a-zA-Z_\-=,]/).empty?
41
+ if (required.include? key) && valid_value
42
+ required_fields[key] = value
43
+ required.delete key
44
+ elsif (optional.include? key) && valid_value
45
+ optional_fields << "#{key}=#{value}"
46
+ optional.delete key
47
+ end
48
+
49
+ end
50
+
51
+ raise "Required fields not provided: #{required.to_s}" unless required.empty?
52
+
53
+ [required_fields, optional_fields]
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ class ChJsonApi
2
+ VERSION = '0.3.2'
3
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,4 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new(:spec) do |task|
3
+ task.rspec_opts = ['--color']
4
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chjsonapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: ruby
6
+ authors:
7
+ - Matheus Serpellone
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: curb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.8'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.8'
83
+ description: Companies House JSON API gem
84
+ email:
85
+ - m.serpellone@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - chjsonapi.gemspec
98
+ - lib/chjsonapi.rb
99
+ - lib/chjsonapi/company.rb
100
+ - lib/chjsonapi/version.rb
101
+ - tasks/rspec.rake
102
+ homepage: https://github.com/inniaccounts/companies-house-json-api
103
+ licenses:
104
+ - MIT
105
+ metadata:
106
+ allowed_push_host: https://rubygems.org
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Gem to assist on calls to Companies House JSON API
127
+ test_files: []
128
+ has_rdoc: