crafty_clicks 0.0.7

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: 9221c4075b97109bf68b53e150ab561e442ad826
4
+ data.tar.gz: cfe49bd4a8bc5638f9b27a9f0fbaa400ae5a6a00
5
+ SHA512:
6
+ metadata.gz: 8016e8b8bd7bf11f1fd2165a8448429c78e7e1e8a423152466afca24435139f3b420555a9fefab5b8a1bb379622aa2d068f10fc53657d4355629af6e651e5f0e
7
+ data.tar.gz: a507fd34f1bcea2cbcb934c14438b76d5d924f034224dd4ff175bb1660819a13fa03ba08ab50350d0544c1e45d13e4aa65302526c35326f559f5891c65dc111c
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crafty_clicks.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Michael Malet
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # CraftyClicks
2
+
3
+ This gem allows you to use the Crafty Clicks Address lookup API in your Rails apps (and also in plain Ruby).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'crafty_clicks'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install crafty_clicks
18
+
19
+ ## Usage
20
+
21
+ ### Basic Example
22
+
23
+ ```ruby
24
+ [1] pry(main)> cc = CraftyClicks.new(key: 'your-api-key', env: :production)
25
+ # => #<CraftyClicks:0x000001061ea648 @env=:production, @key="your-api-key">
26
+
27
+ [2] pry(main)> cc.lookup_postcode('aa11aa')
28
+ # => {:addresses=>
29
+ # ["LITTLE COTTAGE 17 HIGH STREET",
30
+ # "BIG HOUSE HIGH STREET",
31
+ # "1 HIGH STREET",
32
+ # "3 HIGH STREET",
33
+ # "7 HIGH STREET"],
34
+ # :town=>"BIG CITY",
35
+ # :postcode=>"AA1 1AA"}
36
+ ```
37
+
38
+ ### Advanced Example
39
+
40
+ ```ruby
41
+ class Address
42
+ def self.lookup_provider
43
+ @@provider ||= CraftyClicks.new(
44
+ key: Rails.application.secrets.crafty_clicks_key,
45
+ env: Rails.env.to_sym
46
+ )
47
+ end
48
+
49
+ def self.find_by(postcode:)
50
+ raise 'Must provide a postcode' if postcode.blank?
51
+ lookup_provider.lookup_postcode postcode
52
+ end
53
+ end
54
+
55
+ [1] pry(main)> Address.find_by(postcode: 'aa11ab')
56
+ # => {:addresses=>
57
+ # ["LITTLE HOUSE CRAFTYIER ROAD",
58
+ # "CUTE HOUSE CRAFTYIER ROAD",
59
+ # "SMALL COTTAGE CRAFTYIER ROAD",
60
+ # "1C, CRAFTY HOUSE A BUSINESS CENTRE, CRAFTY ROAD",
61
+ # "1B, CRAFTY HOUSE A BUSINESS CENTRE, CRAFTY ROAD",
62
+ # "1A, CRAFTY HOUSE A BUSINESS CENTRE, CRAFTY ROAD"],
63
+ # :town=>"BIG CITY",
64
+ # :postcode=>"AA1 1AB"}
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/[my-github-username]/crafty_clicks/fork )
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "crafty_clicks"
7
+ spec.version = '0.0.7'
8
+ spec.authors = ["Michael Malet"]
9
+ spec.email = ["michael@nervd.com"]
10
+ spec.summary = %q{Crafty Clicks - Addresses by Postcode}
11
+ spec.description = %q{This gem interfaces with Crafty Clicks' JSONP API}
12
+ spec.homepage = "https://github.com/malet/crafty_clicks"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake", '~> 0'
22
+ end
@@ -0,0 +1,66 @@
1
+ require 'json'
2
+ require 'uri'
3
+ require 'net/http'
4
+
5
+ class CraftyClicks
6
+ RAPID_URL = 'http://pcls1.craftyclicks.co.uk/json/rapidaddress'
7
+
8
+ def initialize(key:, env: :test)
9
+ # Only set the auth key if in production (to avoid unwanted charges)
10
+ @env = env
11
+ @key = @env == :production ? key : nil
12
+ end
13
+
14
+ def lookup_postcode(postcode)
15
+ addresses(lookup_postcode_cached(postcode))
16
+ end
17
+
18
+ private
19
+
20
+ def addresses(json)
21
+ # Format the list of delivery points into a readable list
22
+ address_list = json['delivery_points'].map do |delivery_point|
23
+ address = ""
24
+ address += delivery_point['line_1'] unless delivery_point['line_1']. == ""
25
+ address += " #{delivery_point['line_2']}" unless delivery_point['line_2']. == ""
26
+
27
+ address
28
+ end.sort do |a1, a2|
29
+ # Sort by house number where possible
30
+ a1.match(/^[0-9]+/).try(:[], 0).to_i <=>
31
+ a2.match(/^[0-9]+/).try(:[], 0).to_i
32
+ end
33
+
34
+ {
35
+ addresses: address_list,
36
+ town: json['town'],
37
+ postcode: json['postcode']
38
+ }
39
+ end
40
+
41
+ def lookup_postcode_uncached(postcode)
42
+ form_hash = { postcode: postcode, response: 'data_formatted'}
43
+ form_hash.merge!({ key: @key }) if @key
44
+ uri = URI(RAPID_URL)
45
+ uri.query = URI.encode_www_form(form_hash)
46
+
47
+ response = Net::HTTP.get_response(uri)
48
+ JSON.parse(response.body)
49
+ end
50
+
51
+ def lookup_postcode_cached(postcode)
52
+ # Automatically use the rails cache if it's available
53
+ if defined? Rails
54
+ key = ['craftyclicks', @env, postcode]
55
+ if cached = Rails.cache.fetch(key)
56
+ return cached
57
+ else
58
+ json = lookup_postcode_uncached(postcode)
59
+ Rails.cache.write key, json
60
+ return json
61
+ end
62
+ else
63
+ return lookup_postcode_uncached(postcode)
64
+ end
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crafty_clicks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Michael Malet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-21 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This gem interfaces with Crafty Clicks' JSONP API
42
+ email:
43
+ - michael@nervd.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - crafty_clicks.gemspec
54
+ - lib/crafty_clicks.rb
55
+ homepage: https://github.com/malet/crafty_clicks
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.2.2
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Crafty Clicks - Addresses by Postcode
79
+ test_files: []
80
+ has_rdoc: