municipitaly 0.0.1.pre

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.
@@ -0,0 +1,204 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Municipitaly
4
+ # +Municipitaly::Search+ class implement different search methods.
5
+ #
6
+ # You <b>must use</b> shortcut <b>class methods</b> that invoke private
7
+ # instance methods having same method name, for example:
8
+ #
9
+ # Search.municipalities_from_postal_code('36040')
10
+ #
11
+ # invoke to:
12
+ #
13
+ # Search.new('36040').municipalities_from_postal_code
14
+ #
15
+ class Search
16
+ include DataCaller
17
+
18
+ CLASS_METHODS = %i[zone_from_code region_from_istat
19
+ regions_from_zone_code province_from_istat
20
+ province_from_acronym provinces_from_region_istat
21
+ provinces_from_zone_code municipalities_from_name
22
+ municipality_from_cadastrial municipality_from_istat
23
+ municipalities_from_postal_code
24
+ municipalities_from_province_istat
25
+ municipalities_from_region_istat
26
+ municipalities_from_zone_code].freeze # :nodoc:
27
+
28
+ CLASS_METHODS.each do |method|
29
+ define_singleton_method method do |message|
30
+ Search.new(message).send(method)
31
+ end
32
+ end # :nodoc:
33
+
34
+ attr_accessor :term # :nodoc:
35
+
36
+ def initialize(term) # :nodoc:
37
+ @term = term.to_s.strip
38
+ end
39
+
40
+ protected
41
+
42
+ # returns a +Municipitaly::Zone+ object from a <b>zone code</b> term
43
+ #
44
+ # example usage:
45
+ # zone = Search.zone_from_code('4')
46
+ def zone_from_code # :doc:
47
+ data.zones.find do |z|
48
+ z.code == term
49
+ end
50
+ end
51
+
52
+ # returns a +Municipitaly::Region+ object from a <b>region istat</b> term
53
+ #
54
+ # example usage:
55
+ # region = Search.region_from_istat('15')
56
+ def region_from_istat # :doc:
57
+ data.regions.find do |r|
58
+ r.istat == term
59
+ end
60
+ end
61
+
62
+ # returns an array of +Municipitaly::Region+ objects from a <b>zone
63
+ # code</b> term
64
+ #
65
+ # example usage:
66
+ # regions = Search.regions_from_zone_code('3')
67
+ def regions_from_zone_code # :doc:
68
+ data.regions.select do |r|
69
+ r.zone_code == term
70
+ end
71
+ end
72
+
73
+ # returns a +Municipitaly::Province+ object from a <b>province istat</b>
74
+ # term
75
+ #
76
+ # example usage:
77
+ # province = Search.province_from_istat('061')
78
+ def province_from_istat # :doc:
79
+ data.provinces.find do |p|
80
+ p.istat == term
81
+ end
82
+ end
83
+
84
+ # returns a +Municipitaly::Province+ object from a <b>province acronym</b>
85
+ # term
86
+ #
87
+ # example usage:
88
+ # province = Search.province_from_acronym('MI')
89
+ def province_from_acronym # :doc:
90
+ data.provinces.find do |p|
91
+ p.acronym == term
92
+ end
93
+ end
94
+
95
+ # returns an array of +Municipitaly::Province+ objects from a <b>region
96
+ # istat</b> term
97
+ #
98
+ # example usage:
99
+ # provinces = Search.provinces_from_region_istat('05')
100
+ def provinces_from_region_istat # :doc:
101
+ data.provinces.select do |p|
102
+ p.region_istat == term
103
+ end
104
+ end
105
+
106
+ # returns an array of +Municipitaly::Province+ objects from a <b>zone
107
+ # code</b> term
108
+ #
109
+ # example usage:
110
+ # provinces = Search.provinces_from_zone_code('5')
111
+ def provinces_from_zone_code # :doc:
112
+ region_istats = regions_from_zone_code.map(&:istat)
113
+ data.provinces.select do |p|
114
+ region_istats.include? p.region_istat
115
+ end
116
+ end
117
+
118
+ # returns an array of +Municipitaly::Municipality+ objects from a
119
+ # <b>municipality name</b> term.
120
+ # Term can be a partial string and is case insensitive.
121
+ #
122
+ # example usage:
123
+ # municipalities = Search.municipalities_from_name('monte')
124
+ def municipalities_from_name # :doc:
125
+ data.municipalities.select do |m|
126
+ m.name =~ Regexp.new(term, true)
127
+ end
128
+ end
129
+
130
+ # returns a +Municipitaly::Municipality+ object from a <b>cadastrial
131
+ # code</b> term
132
+ #
133
+ # example usage:
134
+ # municipality = Search.municipality_from_cadastrial('D791')
135
+ def municipality_from_cadastrial # :doc:
136
+ data.municipalities.find do |m|
137
+ m.cadastrial_code == term.upcase
138
+ end
139
+ end
140
+
141
+ # returns a +Municipitaly::Municipality+ object from a <b>municipality
142
+ # istat</b> term
143
+ #
144
+ # example usage:
145
+ # municipality = Search.municipality_from_istat('066032')
146
+ def municipality_from_istat # :doc:
147
+ province_istat = term.slice!(0...3)
148
+ partial_istat = term
149
+ data.municipalities.find do |m|
150
+ m.province_istat == province_istat && m.partial_istat == partial_istat
151
+ end
152
+ end
153
+
154
+ # returns an array of +Municipitaly::Municipality+ objects from a
155
+ # <b>postal code</b> term.
156
+ #
157
+ # example usage:
158
+ # municipalities = Search.municipalities_from_postal_code('00163')
159
+ def municipalities_from_postal_code # :doc:
160
+ data.municipalities.select do |m|
161
+ m.postal_codes.include? term
162
+ end
163
+ end
164
+
165
+ # returns an array of +Municipitaly::Municipality+ objects from a
166
+ # <b>province istat</b> term.
167
+ #
168
+ # example usage:
169
+ # municipalities = Search.municipalities_from_province_istat('090')
170
+ def municipalities_from_province_istat # :doc:
171
+ data.municipalities.select do |m|
172
+ m.province_istat == term
173
+ end
174
+ end
175
+
176
+ # returns an array of +Municipitaly::Municipality+ objects from a
177
+ # <b>region istat</b> term.
178
+ #
179
+ # example usage:
180
+ # municipalities = Search.municipalities_from_region_istat('13')
181
+ def municipalities_from_region_istat # :doc:
182
+ province_istats = provinces_from_region_istat.map(&:istat)
183
+ municipalities_from_province_istats(province_istats)
184
+ end
185
+
186
+ # returns an array of +Municipitaly::Municipality+ objects from a
187
+ # <b>zone code</b> term.
188
+ #
189
+ # example usage:
190
+ # municipalities = Search.municipalities_from_zone_code('3')
191
+ def municipalities_from_zone_code # :doc:
192
+ province_istats = provinces_from_zone_code.map(&:istat)
193
+ municipalities_from_province_istats(province_istats)
194
+ end
195
+
196
+ private
197
+
198
+ def municipalities_from_province_istats(istats)
199
+ data.municipalities.select do |m|
200
+ istats.include? m.province_istat
201
+ end
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Municipitaly
4
+ # :nodoc:
5
+ VERSION = '0.0.1.pre'
6
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Municipitaly
4
+ # Define data structure for a Zone
5
+ class Zone
6
+ include DataCaller
7
+
8
+ def initialize(name:, code:)
9
+ @name = name
10
+ @code = code
11
+ end
12
+
13
+ attr_reader :name, :code
14
+
15
+ # returns an array of all +Municipitaly::Zone+ objects.
16
+ def self.all
17
+ data.zones
18
+ end
19
+
20
+ # returns an array of all +Municipitaly::Region+ objects belongs to
21
+ # current zone.
22
+ def regions
23
+ Search.regions_from_zone_code(code)
24
+ end
25
+
26
+ # returns an array of all +Municipitaly::Province+ objects belongs
27
+ # to current zone.
28
+ def provinces
29
+ @provinces ||= Search.provinces_from_zone_code(code)
30
+ end
31
+
32
+ # returns an array of all +Municipitaly::Municipality+ objects belongs
33
+ # to current zone.
34
+ def municipalities
35
+ @municipalities ||= Search.municipalities_from_zone_code(code)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Municipitaly
4
+ # shared module to delegate Zone istance and its methods
5
+ module ZoneDelegator
6
+ extend Forwardable
7
+
8
+ def_delegator :zone, :name, :zone_name
9
+
10
+ def zone
11
+ @zone ||= Search.zone_from_code(zone_code)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+ require 'forwardable'
5
+
6
+ require_relative 'municipitaly/version'
7
+ require_relative 'municipitaly/data'
8
+ require_relative 'municipitaly/zone_delegator'
9
+ require_relative 'municipitaly/region_delegator'
10
+ require_relative 'municipitaly/data_caller'
11
+ require_relative 'municipitaly/zone'
12
+ require_relative 'municipitaly/region'
13
+ require_relative 'municipitaly/province'
14
+ require_relative 'municipitaly/municipality'
15
+ require_relative 'municipitaly/search'
16
+
17
+ # top level namespace
18
+ module Municipitaly
19
+ # :nodoc:
20
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'municipitaly/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'municipitaly'
9
+ spec.version = Municipitaly::VERSION
10
+ spec.authors = ['NatyDev']
11
+ spec.email = ['natydev@aol.com']
12
+
13
+ spec.summary = 'Municipitaly'
14
+ spec.description = 'Codes (postal, istat, cadastrian, ...) about Italian ' \
15
+ 'subdivisions and municipalities'
16
+ spec.homepage = 'https://github.com/natydev/municipitaly'
17
+ spec.license = 'MIT'
18
+ spec.metadata = {
19
+ 'documentation_uri' => 'https://www.rubydoc.info/github/natydev/municipitaly/master'
20
+ }
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0")
26
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 2.1.4'
33
+ spec.add_development_dependency 'rake', '~> 13.0.1'
34
+ spec.add_development_dependency 'rspec', '~> 3.9'
35
+ spec.add_development_dependency 'rubocop', '~> 0.77'
36
+ end