japan_etc 0.1.0

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,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'japan_etc/entrance_or_exit'
4
+ require 'japan_etc/error'
5
+ require 'japan_etc/road'
6
+ require 'japan_etc/direction'
7
+ require 'japan_etc/util'
8
+
9
+ module JapanETC
10
+ class Tollbooth
11
+ include Util
12
+
13
+ attr_accessor :identifier, :road, :name, :entrance_or_exit, :direction, :notes
14
+
15
+ def self.create(
16
+ road_number:,
17
+ tollbooth_number:,
18
+ road_name:,
19
+ route_name: nil,
20
+ name:,
21
+ direction: nil,
22
+ entrance_or_exit: nil,
23
+ note: nil
24
+ )
25
+ identifier = Identifier.new(road_number, tollbooth_number)
26
+ road = Road.new(road_name, route_name)
27
+ new(identifier, road, name, direction, entrance_or_exit, note)
28
+ end
29
+
30
+ def initialize(identifier, road, name, direction = nil, entrance_or_exit = nil, note = nil) # rubocop:disable Metrics/LineLength
31
+ raise ValidationError if identifier.nil? || road.nil? || name.nil?
32
+
33
+ @identifier = identifier
34
+ @road = road
35
+ @name = normalize(name)
36
+ @direction = direction
37
+ @entrance_or_exit = entrance_or_exit
38
+ @notes = []
39
+ notes << normalize(note) if note
40
+
41
+ extract_notes_from_name!
42
+ extract_direction_from_notes!
43
+ extract_entrance_or_exit_from_notes!
44
+ extract_direction_from_name!
45
+ extract_entrance_or_exit_from_name!
46
+ end
47
+
48
+ def initialize_copy(original)
49
+ @road = original.road.dup
50
+ @name = original.name.dup
51
+ end
52
+
53
+ def ==(other)
54
+ other.is_a?(self.class) && identifier == other.identifier
55
+ end
56
+
57
+ alias eql? ==
58
+
59
+ def hash
60
+ identifier.hash
61
+ end
62
+
63
+ def to_a
64
+ [
65
+ identifier.to_a,
66
+ road.to_a,
67
+ name,
68
+ direction,
69
+ entrance_or_exit,
70
+ notes.empty? ? nil : notes.join(' ')
71
+ ].flatten
72
+ end
73
+
74
+ def extract_notes_from_name!
75
+ name.sub!(/(?<head>.+?)?\s*[(\(](?<note>.+?)[)\)]\s*(?<tail>.+)?/) do
76
+ match = Regexp.last_match
77
+
78
+ if match[:head]
79
+ prepend_to_notes(match[:tail]) if match[:tail]
80
+ prepend_to_notes(match[:note])
81
+ match[:head]
82
+ elsif match[:tail]
83
+ prepend_to_notes(match[:note])
84
+ match[:tail]
85
+ else
86
+ match[:note]
87
+ end
88
+ end
89
+
90
+ name.sub!(/第[一二三]\z/) do |match|
91
+ prepend_to_notes(match)
92
+ ''
93
+ end
94
+ end
95
+
96
+ def extract_direction_from_notes!
97
+ notes.reject! do |note|
98
+ found_direction = Direction.from(note)
99
+ next false unless found_direction
100
+
101
+ if direction
102
+ raise ValidationError unless found_direction == direction
103
+ else
104
+ @direction = found_direction
105
+ end
106
+
107
+ true
108
+ end
109
+ end
110
+
111
+ def extract_entrance_or_exit_from_notes!
112
+ notes.reject! do |note|
113
+ found_entrance_or_exit = EntranceOrExit.from(note)
114
+ next false unless found_entrance_or_exit
115
+
116
+ if entrance_or_exit
117
+ raise ValidationError unless found_entrance_or_exit == entrance_or_exit
118
+ else
119
+ @entrance_or_exit = found_entrance_or_exit
120
+ end
121
+
122
+ true
123
+ end
124
+ end
125
+
126
+ def extract_direction_from_name!
127
+ name.sub!(/(?:上り|下り|[東西南北]行き?)/) do |match|
128
+ found_direction = Direction.from(match)
129
+
130
+ if direction
131
+ found_direction == direction ? '' : match
132
+ else
133
+ @direction = found_direction
134
+ ''
135
+ end
136
+ end
137
+ end
138
+
139
+ def extract_entrance_or_exit_from_name!
140
+ name.sub!(/(?:入口|出口|料金所)/) do |match|
141
+ found_entrance_or_exit = EntranceOrExit.from(match)
142
+ found_entrance_or_exit ||= EntranceOrExit::EXIT
143
+
144
+ if entrance_or_exit
145
+ found_entrance_or_exit == entrance_or_exit ? '' : match
146
+ else
147
+ @entrance_or_exit = found_entrance_or_exit
148
+ ''
149
+ end
150
+ end
151
+ end
152
+
153
+ def prepend_to_notes(note)
154
+ note = normalize(note)
155
+ notes.prepend(note)
156
+ end
157
+
158
+ Identifier = Struct.new(:road_number, :tollbooth_number) do
159
+ include Util
160
+
161
+ def initialize(road_number, tollbooth_number)
162
+ road_number = convert_to_integer(road_number)
163
+ raise ValidationError, '#road_number cannot be nil' if road_number.nil?
164
+
165
+ tollbooth_number = convert_to_integer(tollbooth_number)
166
+ raise ValidationError, '#tollbooth_number cannot be nil' if tollbooth_number.nil?
167
+
168
+ super(road_number, tollbooth_number)
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JapanETC
4
+ module Util
5
+ module_function
6
+
7
+ def normalize(string)
8
+ return nil unless string
9
+
10
+ convert_fullwidth_characters_to_halfwidth(string).strip
11
+ end
12
+
13
+ def convert_fullwidth_characters_to_halfwidth(string)
14
+ return nil unless string
15
+
16
+ string.tr(' A-Za-z0-9', ' A-Za-z0-9')
17
+ end
18
+
19
+ def convert_to_integer(object)
20
+ case object
21
+ when Numeric
22
+ Integer(object)
23
+ when String
24
+ Integer(object.sub(/\A0+/, ''))
25
+ else
26
+ raise ArgumentError
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JapanETC
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: japan_etc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuji Nakayama
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pdf-reader
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: spreadsheet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ description: Japan ETC (Electronic Toll Collection System) database
70
+ email:
71
+ - nkymyj@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".rubocop_todo.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/generate_database
86
+ - bin/setup
87
+ - database/japan_etc_tollbooths.csv
88
+ - japan_etc.gemspec
89
+ - lib/japan_etc.rb
90
+ - lib/japan_etc/database.rb
91
+ - lib/japan_etc/database_provider.rb
92
+ - lib/japan_etc/database_provider/base.rb
93
+ - lib/japan_etc/database_provider/central_nexco.rb
94
+ - lib/japan_etc/database_provider/hanshin_expressway.rb
95
+ - lib/japan_etc/database_provider/metropolitan_expressway.rb
96
+ - lib/japan_etc/direction.rb
97
+ - lib/japan_etc/entrance_or_exit.rb
98
+ - lib/japan_etc/error.rb
99
+ - lib/japan_etc/road.rb
100
+ - lib/japan_etc/tollbooth.rb
101
+ - lib/japan_etc/util.rb
102
+ - lib/japan_etc/version.rb
103
+ homepage: https://github.com/yujinakayama/japan_etc
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubygems_version: 3.0.3
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Japan ETC (Electronic Toll Collection System) database
126
+ test_files: []