postnummer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2012 Fredrik Wallgren
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
21
+
data/README.textile ADDED
@@ -0,0 +1,11 @@
1
+ h1. Library for looking up swedish zip codes using posten.se
2
+
3
+ Simple library for parsing streets from posten.se using a zip code.
4
+
5
+ h2. Usage
6
+
7
+ bc. parser = Postnummer::Parser.new('12345')
8
+ streets = parser.parse
9
+ streets.each do |street|
10
+ p street.name
11
+ end
data/lib/postnummer.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'mechanize'
2
+
3
+ require 'postnummer/version'
4
+ require 'postnummer/parser'
5
+ require 'postnummer/street'
@@ -0,0 +1,43 @@
1
+ module Postnummer
2
+
3
+ # Class that do the parsing from posten.se
4
+ class Parser
5
+
6
+ # @param [String] zip_code a swedish zip_code
7
+ def initialize(zip_code)
8
+ @zip_code = zip_code
9
+ end
10
+
11
+ # Makes the web call and parses out a list of Streets
12
+ # @return [Array<Street>] a list of Street objects. Returns empty array if no streets found
13
+ def parse
14
+ url = "http://www.posten.se/soktjanst/postnummersok/resultat.jspv?pnr=#{@zip_code}"
15
+ agent = Mechanize.new
16
+
17
+ streets = []
18
+
19
+ agent.get url do |page|
20
+ rows = page.search 'table.result tr'
21
+ rows.each do |tr|
22
+ name = tr.search('td[1]').text
23
+ street_numbers_string = tr.search('td[2]').text
24
+ zip_code = tr.search('td[3]').text
25
+ city = tr.search('td[4]').text
26
+
27
+ street_numbers = parse_street_numbers(street_numbers_string)
28
+ streets << Street.new(name, street_numbers[:first], street_numbers[:last], zip_code, city)
29
+ end
30
+ end
31
+
32
+ streets
33
+ end
34
+
35
+ # Parses the street numbers from a street_numbers string
36
+ # @params street_numbers [String] a string with the format '1 - 4'
37
+ # @return [Hash] hash that has the start and end values. Use key :first and :last to access
38
+ def parse_street_numbers(street_numbers)
39
+ numbers = street_numbers.split ' - '
40
+ { first: numbers[0], last: numbers[1] }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ module Postnummer
2
+
3
+ # Contains street data
4
+ class Street
5
+ attr_reader :name, :first_number, :last_number, :zip_code, :city
6
+
7
+ # @param [String] name the name of the street
8
+ # @param [String] first_number the first number on the street
9
+ # @param [String] last_number the last number on the street
10
+ # @param [String] zip_code the zip_code
11
+ # @param [String] city the city
12
+ def initialize(name, first_number, last_number, zip_code, city)
13
+ @name = name
14
+ @first_number = first_number
15
+ @last_number = last_number
16
+ @zip_code = zip_code
17
+ @city = city
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Postnummer
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postnummer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fredrik Wallgren
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.5.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.5.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
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: '0'
62
+ description: Library for looking up swedish zip_codes using posten.se. Capable of
63
+ finding all streets that have a certain zip_code.
64
+ email: fredrik.wallgren@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files:
68
+ - README.textile
69
+ - LICENSE
70
+ files:
71
+ - lib/postnummer.rb
72
+ - lib/postnummer/version.rb
73
+ - lib/postnummer/street.rb
74
+ - lib/postnummer/parser.rb
75
+ - LICENSE
76
+ - README.textile
77
+ homepage: https://github.com/walle/postnummer
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ segments:
91
+ - 0
92
+ hash: 4348620662459896116
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ segments:
100
+ - 0
101
+ hash: 4348620662459896116
102
+ requirements: []
103
+ rubyforge_project: postnummer
104
+ rubygems_version: 1.8.24
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Library for looking up swedish zip_codes using posten.se
108
+ test_files: []
109
+ has_rdoc: