open-charities 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ .bundle
4
+ .rbenv-version
5
+ vendor
6
+
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,61 @@
1
+ # Open Charities
2
+
3
+ A simple library for querying http://opencharities.org/ database.
4
+
5
+ ## Installation
6
+
7
+ To install as a standalone
8
+ ```sh
9
+ $ gem install open-charities
10
+ ```
11
+ or as a dependency in a Gemfile
12
+ ```ruby
13
+ gem "open-charities", "~> 0.1.0", require: 'open_charities'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ruby
19
+ charity = OpenCharities.lookup("1149855")
20
+ => #<OpenCharities::Response:0x00000001738d30 @attributes={"address"...>
21
+
22
+ charity['title']
23
+ => "VETERAN'S HEADQUARTERS"
24
+
25
+ charity.title
26
+ => "VETERAN'S HEADQUARTERS"
27
+
28
+ charity.charity_number
29
+ => "1149855"
30
+
31
+ charity.address
32
+ => {"region"=>nil, "raw_address"=>nil, "created_at"=>"2012-11-22T05:00:54+00:00"...}
33
+ ```
34
+ As can be seen from this example, some of the attributes are exposed as instance
35
+ methods. More specifically, the methods that are exposed correspond to the
36
+ top-level keys in the Hash containing information about the charity.
37
+ As in the above example, the `address` attribute returns another Hash.
38
+
39
+ ## Caching
40
+
41
+ Requests can be cached by configuring the gem to use an external cache (for example,
42
+ the Rails cache).
43
+
44
+ ```ruby
45
+ # file config/initializers/open_charities.rb
46
+
47
+ # swap in your own cache here
48
+ OpenCharities.cache = Rails.cache
49
+
50
+ # Optional
51
+ OpenCharities.cache_args = { expires_in: 10.minutes }
52
+ ```
53
+
54
+ ## Testing
55
+
56
+ To run the tests, issue `rspec spec` on the command-line.
57
+
58
+ ### Available testing data
59
+
60
+ There is testing data available `spec/data/veteransheadquarters.json`. The same
61
+ data is used by the tests in `spec/`.
data/Rakefile ADDED
File without changes
@@ -0,0 +1,37 @@
1
+ module OpenCharities
2
+
3
+ class Request
4
+
5
+ BASE_URI = "http://opencharities.org/charities/%s.json"
6
+
7
+ def initialize(reg_number)
8
+ @reg_number = validate(reg_number)
9
+ end
10
+
11
+ def perform
12
+ url = BASE_URI % @reg_number
13
+ response = with_caching { Faraday.get(url) }
14
+ Response.new(response)
15
+ end
16
+
17
+ private
18
+
19
+ def with_caching(&block)
20
+ args = OpenCharities.cache_args || {}
21
+ cache = OpenCharities.cache
22
+ cache ? cache.fetch(@reg_number, args, &block) : yield
23
+ end
24
+
25
+ def validate(reg_number)
26
+ number = reg_number.to_s.strip
27
+
28
+ # match 7-digit numbers *only*
29
+ charities_regex = Regexp.new("\\A\\d{7}\\z")
30
+
31
+ msg = "#{number} is not a valid UK charity registration number"
32
+ raise InvalidRegistration.new(msg) unless number =~ charities_regex
33
+
34
+ number
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,49 @@
1
+ module OpenCharities
2
+
3
+ class Response
4
+
5
+ attr_reader :attributes
6
+
7
+ def initialize(response)
8
+ check_for_errors(response)
9
+ parse_response(response)
10
+ end
11
+
12
+ def [](key)
13
+ @attributes[key]
14
+ end
15
+
16
+ private
17
+
18
+ def check_for_errors(response)
19
+ if response.status == 404
20
+ msg = "Charity (reg. number %s) not found" % @reg_number
21
+ raise CharityNotFound.new(msg)
22
+ end
23
+
24
+ unless response.status == 200
25
+ msg = "OpenCharities responded with status #{response.status}"
26
+ raise ServerError.new(msg)
27
+ end
28
+ end
29
+
30
+ def parse_response(response)
31
+ data = JSON.parse(response.body)
32
+ @attributes = data["charity"]
33
+
34
+ generate_convenience_methods
35
+ end
36
+
37
+ # convert the top-level keys in the returned JSON to singleton methods on
38
+ # the instance
39
+ # NOTE: this *only* converts keys that are in the top-level.
40
+ # for example, instance.address returns a hash, but title returns a string
41
+ def generate_convenience_methods
42
+ @attributes.each_pair do |k, v|
43
+ define_singleton_method(k) { v }
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,3 @@
1
+ module OpenCharities
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ require 'open_charities/request'
4
+ require 'open_charities/response'
5
+
6
+ module OpenCharities
7
+
8
+ class CharityNotFound < StandardError
9
+ end
10
+
11
+ class InvalidRegistration < StandardError
12
+ end
13
+
14
+ class ServerError < StandardError
15
+ end
16
+
17
+ class << self
18
+
19
+ def lookup(reg_number)
20
+ Request.new(reg_number).perform
21
+ end
22
+
23
+ attr_accessor :cache, :cache_args
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../lib/open_charities/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_runtime_dependency "faraday", "~> 0.8.4"
9
+ gem.add_runtime_dependency "json"
10
+
11
+ gem.name = 'open-charities'
12
+ gem.summary = "A wrapper around Open Charities API"
13
+ gem.version = OpenCharities::VERSION.dup
14
+ gem.authors = ['Karl Sutt']
15
+ gem.email = ['karl@gocardless.com']
16
+ gem.homepage = 'https://github.com/gocardless/open-charities'
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 @@
1
+ {"charity":{"address":{"region":null,"raw_address":null,"created_at":"2012-11-22T05:00:54+00:00","country":null,"updated_at":"2012-11-22T05:00:58+00:00","postal_code":"NE24 2EY","lng":-1.5115205,"id":373286,"street_address":"14 WINCHESTER AVENUE, BLYTH","former":false,"addressee_id":329375,"locality":"NORTHUMBERLAND","lat":55.1234131,"addressee_type":"Charity"},"facebook_account_name":null,"financial_breakdown":null,"housing_association_number":null,"vat_number":null,"grants":[],"contact_name":"MR ROGER SMITH","corrected_company_number":null,"created_at":"2012-11-22T05:00:53+00:00","date_registered":"2012-11-21","signed_up_for_1010":false,"classifications":[],"annual_reports":[],"twitter_account_name":null,"company_number":null,"governing_document":"MEMORANDUM AND ARTICLES INCORPORATED 12/06/2012 AS AMENDED BY SPECIAL RESOLUTION REGISTERED AT COMPANIES HOUSE ON 15/11/2012","title":"VETERAN'S HEADQUARTERS","volunteers":null,"area_of_benefit":null,"feed_url":null,"updated_at":"2012-11-22T05:00:54Z","id":329375,"other_names":"VHQ (Working Name)","subsidiary_number":null,"trustees":[{"uid":"11334837","full_name":"FRANK SPRATT"},{"uid":"11334838","full_name":"MR ROGER SMITH"}],"youtube_account_name":null,"accounts":null,"activities":null,"date_removed":null,"spending":null,"charity_number":"1149855","employees":null,"fax":null,"income":null,"website":"http://www.veteransheadquarters.org.uk","last_checked":"2012-11-22T05:00:54+00:00","manually_updated":null,"telephone":"01670 734037","accounts_date":null,"normalised_title":"veterans headquarters"}}
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpenCharities do
4
+
5
+ let(:subject) { OpenCharities }
6
+
7
+ describe ".lookup" do
8
+ it "initializes a request" do
9
+ OpenCharities::Request.expects(:new).with("1234567").
10
+ returns(stub(perform: true))
11
+ subject.lookup("1234567")
12
+ end
13
+
14
+ it "calls perform on the request and returns the response" do
15
+ OpenCharities::Request.stubs(:new).with("1234567").
16
+ returns(mock(perform: true))
17
+ subject.lookup("1234567")
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpenCharities::Request do
4
+
5
+ let(:subject) { OpenCharities::Request }
6
+
7
+ describe "#new" do
8
+ context "throws an exception" do
9
+ it "with a number that's too short" do
10
+ expect {
11
+ subject.new "123456"
12
+ }.to raise_exception OpenCharities::InvalidRegistration
13
+ end
14
+
15
+ it "that's too long" do
16
+ expect {
17
+ subject.new "12345678"
18
+ }.to raise_exception OpenCharities::InvalidRegistration
19
+ end
20
+
21
+ it "that's nil" do
22
+ expect {
23
+ subject.new nil
24
+ }.to raise_exception OpenCharities::InvalidRegistration
25
+ end
26
+
27
+ it "that contains spaces" do
28
+ expect {
29
+ subject.new "1234 ABC"
30
+ }.to raise_exception OpenCharities::InvalidRegistration
31
+ end
32
+
33
+ it "that contains non alphanumeric chars" do
34
+ expect {
35
+ subject.new "1234?456"
36
+ }.to raise_exception OpenCharities::InvalidRegistration
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+
43
+ describe "perform" do
44
+
45
+ before { @request = subject.new("1149855") }
46
+
47
+ it "makes an http request" do
48
+ OpenCharities::Response.stubs(:new)
49
+ Faraday.expects(:get).with("http://opencharities.org/charities/1149855.json")
50
+ @request.perform
51
+ end
52
+
53
+ it "passes the response to a Response object" do
54
+ stub_response = stub
55
+ OpenCharities::Response.expects(:new).with(stub_response)
56
+ Faraday.stubs(:get).returns(stub_response)
57
+ @request.perform
58
+ end
59
+
60
+ it "caches the request (if enabled)" do
61
+ cached_response = stub
62
+ cache_stub = stub(fetch: cached_response)
63
+ OpenCharities.cache = cache_stub # configure
64
+ OpenCharities::Response.expects(:new).with(cached_response)
65
+ @request.perform
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpenCharities::Response do
4
+
5
+ let(:subject) { OpenCharities::Response }
6
+
7
+ context "with a successful http response" do
8
+ before do
9
+ url = "http://opencharities.org/charities/1149855.json"
10
+ stub_request(:get, url).to_return({
11
+ body: load_fixture("veteransheadquarters.json"),
12
+ status: 200
13
+ })
14
+ @response = subject.new(Faraday.get(url))
15
+ end
16
+
17
+ it "loads attributes into the object body" do
18
+ @response["title"].should == "VETERAN'S HEADQUARTERS"
19
+ end
20
+
21
+ it "makes raw attributes available as well" do
22
+ @response.attributes["title"].should == "VETERAN'S HEADQUARTERS"
23
+ end
24
+
25
+ it "converts top-level hash keys into singleton methods" do
26
+ @response.title.should == "VETERAN'S HEADQUARTERS"
27
+ @response.charity_number.should == "1149855"
28
+ end
29
+
30
+ it "leaves nested hashes unconverted" do
31
+ @response.address.class.should == Hash
32
+ end
33
+ end
34
+
35
+ context "with a company that doesn't exist" do
36
+ before do
37
+ @url = "http://opencharities.org/charities/0000000.json"
38
+ stub_request(:get, @url).to_return({
39
+ body: "Not found",
40
+ status: 404
41
+ })
42
+ end
43
+
44
+ it "raises an exception" do
45
+ expect {
46
+ subject.new(Faraday.get(@url))
47
+ }.to raise_exception OpenCharities::CharityNotFound
48
+ end
49
+ end
50
+
51
+ context "when the server is down" do
52
+ before do
53
+ @url = "http://data.OpenCharities.gov.uk/doc/company/12345678.json"
54
+ stub_request(:get, @url).to_return({
55
+ body: "Oh noes",
56
+ status: 500
57
+ })
58
+ end
59
+
60
+ it "raises an exception" do
61
+ expect {
62
+ subject.new(Faraday.get(@url))
63
+ }.to raise_exception OpenCharities::ServerError
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,18 @@
1
+ require 'mocha'
2
+ require 'json'
3
+ require 'webmock/rspec'
4
+
5
+ require 'open_charities'
6
+
7
+ RSpec.configure do |config|
8
+
9
+ config.before(:suite) do
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,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open-charities
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Karl Sutt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-27 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
+ - karl@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/open_charities.rb
107
+ - lib/open_charities/request.rb
108
+ - lib/open_charities/response.rb
109
+ - lib/open_charities/version.rb
110
+ - open_charities.gemspec
111
+ - spec/data/veteransheadquarters.json
112
+ - spec/open_charities/open_charities_spec.rb
113
+ - spec/open_charities/request_spec.rb
114
+ - spec/open_charities/response_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/gocardless/open-charities
117
+ licenses: []
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.23
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: A wrapper around Open Charities API
140
+ test_files:
141
+ - spec/data/veteransheadquarters.json
142
+ - spec/open_charities/open_charities_spec.rb
143
+ - spec/open_charities/request_spec.rb
144
+ - spec/open_charities/response_spec.rb
145
+ - spec/spec_helper.rb