york-bin-collection 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +24 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/lib/york-bin-collection.rb +107 -0
- data/york-bin-collection.gemspec +30 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 05451ceebf8d458827b64fbd6269bfebff27e91716a8c78684d73549df9ffdae
|
4
|
+
data.tar.gz: e968fe6fe813c4528beced7068c56836497e784e9e5eb30a3d7f7fd4ec387348
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d852ef9738a06acf0731f943378a40a15b3673e2f775c7ef0fd22fd907c02d2392c8508d2053aca1fad826113231ab5eebbea6a5c159332f1212aa3ed7dfb6c3
|
7
|
+
data.tar.gz: '079c2d4d8b5ae5abc995da7010919a28e8c48c533db2290b08d02a92eac93c772c0072688a47220c3e5eadb001752feea079d4116e394caf4fa39ce94a596bb2'
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
york-bin-collection (1.0.0)
|
5
|
+
nokogiri (~> 1.10)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
mini_portile2 (2.4.0)
|
11
|
+
nokogiri (1.10.4)
|
12
|
+
mini_portile2 (~> 2.4.0)
|
13
|
+
rake (10.5.0)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
bundler (~> 2.0)
|
20
|
+
rake (~> 10.0)
|
21
|
+
york-bin-collection!
|
22
|
+
|
23
|
+
BUNDLED WITH
|
24
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Aaron Christiansen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# York::Bin::Collection
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/york/bin/collection`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'york-bin-collection'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install york-bin-collection
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/york-bin-collection.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'cgi'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module YorkBinCollection
|
7
|
+
VERSION = '1.0.0'
|
8
|
+
|
9
|
+
CALENDARS_ENDPOINT = 'https://doitonline.york.gov.uk/BinsApi/Calendars/Index'
|
10
|
+
EXOR_ENDPOINT = 'https://doitonline.york.gov.uk/BinsApi/EXOR'
|
11
|
+
|
12
|
+
CollectionDates = Struct.new('CollectionDates', :recycling, :household, :garden)
|
13
|
+
|
14
|
+
##
|
15
|
+
# Checks if a UPRN is syntatically valid; that is, if is a string is
|
16
|
+
# alphanumeric.
|
17
|
+
# According to the Ordinance Survey, UPRNs can be alphanumeric, although I
|
18
|
+
# have yet to find an example with alphabetic characters in.
|
19
|
+
# @param [String] uprn The string to validate as a UPRN number.
|
20
|
+
# @return [Boolean] True if the string is a syntatically valid UPRN, false
|
21
|
+
# otherwise.
|
22
|
+
def self.valid_uprn?(uprn)
|
23
|
+
/[A-Za-z0-9]+/ === uprn || up
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# A hash of collection types, as keys in a CollectionDates object, to their
|
28
|
+
# 'friendly' names.
|
29
|
+
def self.collection_types
|
30
|
+
{
|
31
|
+
recycling: 'Recycling',
|
32
|
+
household: 'Household waste',
|
33
|
+
garden: 'Garden waste'
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Given a valid UPRN in the City of York, returns an CollectionDates struct of
|
39
|
+
# dates each bin will be collected. The keys are :recycling, :household and
|
40
|
+
# :garden.
|
41
|
+
# @param [String] uprn A valid UPRN.
|
42
|
+
# @return [CollectionDates] The known waste collection dates.
|
43
|
+
def self.get_collection_dates(uprn)
|
44
|
+
raise ArgumentError, 'invalid uprn' unless valid_uprn?(uprn)
|
45
|
+
url = CALENDARS_ENDPOINT + "?uprn=#{uprn}"
|
46
|
+
document = Nokogiri::HTML(open(url))
|
47
|
+
|
48
|
+
# The format of the document is:
|
49
|
+
# body
|
50
|
+
# div.container-fluid
|
51
|
+
# ~header~
|
52
|
+
# div
|
53
|
+
# div.col-xs-12 col-sm-3
|
54
|
+
# h2: December 2018
|
55
|
+
# div.row
|
56
|
+
# div: Tue 04
|
57
|
+
# div: Household waste
|
58
|
+
# ...
|
59
|
+
# h2: January 2019
|
60
|
+
# ...
|
61
|
+
# div.col-xs-12 col-sm-3
|
62
|
+
# h2: March 2019
|
63
|
+
# ...
|
64
|
+
# ...
|
65
|
+
|
66
|
+
# Let's start by getting the div which contains the columns, and flatten out
|
67
|
+
# the structure to get rid of them
|
68
|
+
collection_date_elements = []
|
69
|
+
document.css('body > div.container-fluid > div > div.col-xs-12.col-sm-3').each do |col|
|
70
|
+
collection_date_elements.push(*col.children)
|
71
|
+
end
|
72
|
+
|
73
|
+
collection_dates = CollectionDates.new([], [], [])
|
74
|
+
current_heading = nil
|
75
|
+
|
76
|
+
collection_date_elements.each do |row_or_heading|
|
77
|
+
if row_or_heading.name == "h2"
|
78
|
+
current_heading = row_or_heading.text
|
79
|
+
else
|
80
|
+
raise 'format error: didn\'t encounter heading before date' unless current_heading
|
81
|
+
|
82
|
+
day, kind = row_or_heading.children.map(&:text)
|
83
|
+
date = Date.parse("#{day} #{current_heading}")
|
84
|
+
key = collection_types.rassoc(kind)&.first
|
85
|
+
raise "unknown collection kind #{kind}" unless kind
|
86
|
+
|
87
|
+
collection_dates.send(key) << date
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
collection_dates
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# Given a postcode, returns a hash of UPRNs in that postcode to their house
|
96
|
+
# addresses.
|
97
|
+
# @param [String] postcode A valid postcode, which is permitted to include
|
98
|
+
# a space.
|
99
|
+
# @param [Hash] A hash of UPRNs to short addresses for the property. If the
|
100
|
+
# postcode is invalid, this may be blank.
|
101
|
+
def self.get_uprns_for_postcode(postcode)
|
102
|
+
url = "#{EXOR_ENDPOINT}/getPropertiesForPostCode?postcode=#{CGI.escape(postcode)}"
|
103
|
+
JSON.parse(open(url).read).map do |property|
|
104
|
+
[property['Uprn'].to_s, property['ShortAddress']]
|
105
|
+
end.to_h
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "york-bin-collection"
|
6
|
+
spec.version = "1.0.0"
|
7
|
+
spec.authors = ["Aaron Christiansen"]
|
8
|
+
spec.email = ["aaronc20000@gmail.com"]
|
9
|
+
|
10
|
+
spec.summary = "Scraper for the City of York Council Bin Collection page"
|
11
|
+
spec.homepage = "https://github.com/Aaronc81/york-bin-collection"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/Aaronc81/york-bin-collection"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency "nokogiri", "~> 1.10"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: york-bin-collection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Christiansen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-09-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :runtime
|
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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- aaronc20000@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/york-bin-collection.rb
|
69
|
+
- york-bin-collection.gemspec
|
70
|
+
homepage: https://github.com/Aaronc81/york-bin-collection
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata:
|
74
|
+
homepage_uri: https://github.com/Aaronc81/york-bin-collection
|
75
|
+
source_code_uri: https://github.com/Aaronc81/york-bin-collection
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubygems_version: 3.0.3
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Scraper for the City of York Council Bin Collection page
|
95
|
+
test_files: []
|