clli 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.
@@ -0,0 +1,79 @@
1
+ class CLLI
2
+ ##
3
+ # This module provides methods to validate the parsed +CLLI+ attributes.
4
+ module Validations
5
+ ##
6
+ # When this modeule is included then extend the including class with the
7
+ # methods defined in +ClassMethods+.
8
+ def self.included(o)
9
+ o.extend(ClassMethods)
10
+ end
11
+
12
+ ##
13
+ # These methods will be available in the CLLI class.
14
+ module ClassMethods
15
+ def validate(str, attributes, **options)
16
+ validate_place(str, attributes[:place], options)
17
+ validate_region(str, attributes[:region], options)
18
+ %i(network_site entity_code location_code location_id customer_code customer_id).each do |attribute|
19
+ send("validate_#{attribute}", str, attributes[attribute], options) if attributes.key?(attribute)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def validate_place(str, place, **options)
26
+ error_prefix = "Invalid CLLI #{str.inspect} place (characters 0..3)"
27
+ validate_attribute(error_prefix, place, 4, /\A[A-Z]+\s*\z/, options)
28
+ end
29
+
30
+ def validate_region(str, region, **options)
31
+ error_prefix = "Invalid CLLI #{str.inspect} region (characters 4..5)"
32
+ validate_attribute(error_prefix, region, 2, /\A[A-Z]{2}\z/, options)
33
+ end
34
+
35
+ def validate_network_site(str, site, **options)
36
+ error_prefix = "Invalid CLLI #{str.inspect} network site (characters 6..7)"
37
+ validate_attribute(error_prefix, site, 2, /\A([A-Z]{2}|[0-9]{2})\z/, options)
38
+ end
39
+
40
+ def validate_entity_code(str, entity_code, **options)
41
+ error_prefix = "Invalid CLLI #{str.inspect} entity code (characters 8..11)"
42
+ validate_attribute(error_prefix, entity_code, 3, /\A[A-Z0-9]{3}\z/, options)
43
+ end
44
+
45
+ def validate_location_code(str, location, **options)
46
+ error_prefix = "Invalid CLLI #{str.inspect} non-building location code (character 8)"
47
+ validate_attribute(error_prefix, location, 1, /\A[A-Z]\z/, options)
48
+ end
49
+
50
+ def validate_location_id(str, location, **options)
51
+ error_prefix = "Invalid CLLI #{str.inspect} non-building location ID (characters 9..11)"
52
+ validate_attribute(error_prefix, location, 4, /\A[0-9]{4}\z/, options)
53
+ end
54
+
55
+ def validate_customer_code(str, location, **options)
56
+ error_prefix = "Invalid CLLI #{str.inspect} customer location code (characters 8)"
57
+ validate_attribute(error_prefix, location, 1, /\A[0-9]\z/, options)
58
+ end
59
+
60
+ def validate_customer_id(str, location, **options)
61
+ error_prefix = "Invalid CLLI #{str.inspect} customer location ID (characters 9..11)"
62
+ validate_attribute(error_prefix, location, 4, /\A[A-Z][0-9]{3}\z/, options)
63
+ end
64
+
65
+ def validate_attribute(error_prefix, value, length, regexp, **options)
66
+ fail "#{error_prefix} cannot be nil." if value.nil?
67
+ fail "#{error_prefix} cannot be empty." if value.to_s.empty?
68
+ fail "#{error_prefix} must be #{length} #{pluralize(length, 'character')} long." unless value.to_s.size == length
69
+ return unless options[:strict]
70
+ fail "#{error_prefix} is not properly formatted." unless regexp =~ value
71
+ end
72
+
73
+ def pluralize(count, singular, plural = nil)
74
+ plural ||= singular + 's'
75
+ count == 1 ? singular : plural
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,4 @@
1
+ # This file defines the gem version.
2
+ class CLLI
3
+ VERSION = '0.0.1'
4
+ end
@@ -0,0 +1,56 @@
1
+ class CLLI
2
+ ##
3
+ # This module provides methods to load external data from YAML files.
4
+ class YAMLData
5
+ class << self
6
+ @cache = nil
7
+
8
+ ##
9
+ # Expand the relative path into a real path.
10
+ #
11
+ # Params:
12
+ # +paths+:: a splat containing the path names.
13
+ def real_path(*paths)
14
+ File.join([File.dirname(__FILE__), '..'] + paths)
15
+ end
16
+ end
17
+
18
+ ##
19
+ # Create a new +YAMLData+ object loading the data from the specified YAML
20
+ # file.
21
+ #
22
+ # Params:
23
+ # +paths+:: a splat containing the path names.
24
+ def initialize(*paths)
25
+ @cache = YAML.load_file(self.class.real_path(paths)).freeze
26
+ end
27
+
28
+ ##
29
+ # Get the +Hash+ keys from the cached data.
30
+ def keys
31
+ @cache.keys
32
+ end
33
+
34
+ ##
35
+ # Get the cached data +Hash+. Note the +hash+ is frozen, any modification
36
+ # will cause a +RuntimeError+ to be thrown.
37
+ def data
38
+ @cache
39
+ end
40
+
41
+ ##
42
+ # Get the data at the corresponding keys. Note the keys are interpreted as a
43
+ # tree. So a second key indicates that the first key returns a +Hash+ which
44
+ # contains the second key. If no matching records are found then +nil+ will
45
+ # be returned.
46
+ #
47
+ # Porams:
48
+ # +keys+:: the keys to lookup.
49
+ def get(*keys)
50
+ keys.inject(@cache) do |data, key|
51
+ return nil unless data.respond_to?(:[])
52
+ data[key]
53
+ end
54
+ end
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Steven Wheeler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-13 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: countries
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - - ">"
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ - - ">"
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ description: A simple gem to parse CLLI strings.
76
+ email:
77
+ - steven@steventwheeler.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".gitignore"
83
+ - ".rubocop.yml"
84
+ - ".travis.yml"
85
+ - Gemfile
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - clli.gemspec
92
+ - doc/795-100-100_I5.pdf
93
+ - lib/clli.rb
94
+ - lib/clli/cities.rb
95
+ - lib/clli/data/cities.yml
96
+ - lib/clli/data/entity_types.yml
97
+ - lib/clli/data/location_types.yml
98
+ - lib/clli/data/region_conversions.yml
99
+ - lib/clli/entity_type.rb
100
+ - lib/clli/iso3166.rb
101
+ - lib/clli/location_type.rb
102
+ - lib/clli/pattern.rb
103
+ - lib/clli/validations.rb
104
+ - lib/clli/version.rb
105
+ - lib/clli/yaml_data.rb
106
+ homepage: https://github.com/steventwheeler/clli
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.5.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: A gem to parse CLLI strings
130
+ test_files: []