japan_etc 0.5.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'japan_etc/database_provider/base'
4
+ require 'japan_etc/tollbooth'
5
+ require 'faraday'
6
+ require 'nokogiri'
7
+
8
+ module JapanETC
9
+ module DatabaseProvider
10
+ # http://www.nagoya-expressway.or.jp/etc/etc-lane.html
11
+ class PastDatabase < Base
12
+ def source_id
13
+ 'PastDatabase'
14
+ end
15
+
16
+ def fetch_tollbooths
17
+ rows.map do |row|
18
+ create_tollbooth_from_row(row)
19
+ end
20
+ end
21
+
22
+ def create_tollbooth_from_row(row)
23
+ identifier = Tollbooth::Identifier.from(row[0])
24
+
25
+ road = Road.new(row[1], row[2])
26
+
27
+ Tollbooth.new(
28
+ identifier: identifier,
29
+ road: road,
30
+ name: row[3],
31
+ direction: row[4],
32
+ entrance_or_exit: row[5],
33
+ note: row[6],
34
+ source: source_id,
35
+ priority: -1
36
+ )
37
+ end
38
+
39
+ def rows
40
+ CSV.parse(csv, headers: :first_row)
41
+ end
42
+
43
+ def csv
44
+ path = File.join(__dir__, 'past_database.csv')
45
+ File.read(path)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -5,6 +5,7 @@ require 'japan_etc/util'
5
5
 
6
6
  module JapanETC
7
7
  Road = Struct.new(:name, :route_name) do
8
+ include Comparable
8
9
  include Util
9
10
 
10
11
  IRREGULAR_ABBREVIATIONS = {
@@ -22,7 +23,7 @@ module JapanETC
22
23
  def initialize(name, route_name = nil)
23
24
  raise ValidationError, '#name cannot be nil' if name.nil?
24
25
 
25
- super(normalize(name), normalize(route_name))
26
+ super(remove_whitespaces(normalize(name)), remove_whitespaces(normalize(route_name)))
26
27
  end
27
28
 
28
29
  def abbreviation
@@ -49,5 +50,14 @@ module JapanETC
49
50
 
50
51
  abbreviation
51
52
  end
53
+
54
+ def <=>(other)
55
+ [:name, :route_name].each do |attribute|
56
+ result = send(attribute) <=> other.send(attribute)
57
+ return result unless result.zero?
58
+ end
59
+
60
+ 0
61
+ end
52
62
  end
53
63
  end
@@ -8,9 +8,11 @@ require 'japan_etc/util'
8
8
 
9
9
  module JapanETC
10
10
  class Tollbooth
11
+ include Comparable
11
12
  include Util
12
13
 
13
- attr_accessor :identifier, :road, :name, :entrance_or_exit, :direction, :notes
14
+ attr_accessor :identifier, :road, :name, :entrance_or_exit, :direction, :notes,
15
+ :source, :priority
14
16
 
15
17
  def self.create(
16
18
  road_number:,
@@ -20,29 +22,48 @@ module JapanETC
20
22
  name:,
21
23
  direction: nil,
22
24
  entrance_or_exit: nil,
23
- note: nil
25
+ note: nil,
26
+ source: nil,
27
+ priority: 0
24
28
  )
25
29
  identifier = Identifier.new(road_number, tollbooth_number)
26
30
  road = Road.new(road_name, route_name)
27
- new(identifier, road, name, direction, entrance_or_exit, note)
31
+
32
+ new(
33
+ identifier: identifier,
34
+ road: road,
35
+ name: name,
36
+ direction: direction,
37
+ entrance_or_exit: entrance_or_exit,
38
+ note: note,
39
+ source: source,
40
+ priority: priority
41
+ )
28
42
  end
29
43
 
30
- def initialize(identifier, road, name, direction = nil, entrance_or_exit = nil, note = nil) # rubocop:disable Metrics/LineLength
44
+ def initialize(
45
+ identifier:,
46
+ road:,
47
+ name:,
48
+ direction:,
49
+ entrance_or_exit:,
50
+ note:,
51
+ source:,
52
+ priority:
53
+ )
31
54
  raise ValidationError if identifier.nil? || road.nil? || name.nil?
32
55
 
33
56
  @identifier = identifier
34
57
  @road = road
35
- @name = normalize(name)
58
+ @name = remove_whitespaces(normalize(name))
36
59
  @direction = direction
37
60
  @entrance_or_exit = entrance_or_exit
38
61
  @notes = []
39
62
  notes << normalize(note) if note
63
+ @source = source
64
+ @priority = priority
40
65
 
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!
66
+ normalize!
46
67
  end
47
68
 
48
69
  def initialize_copy(original)
@@ -60,6 +81,24 @@ module JapanETC
60
81
  identifier.hash
61
82
  end
62
83
 
84
+ def <=>(other)
85
+ result = identifier <=> other.identifier
86
+ return result unless result.zero?
87
+
88
+ result = priority <=> other.priority
89
+ return -result unless result.zero? # Tollbooth with higher priority comes first
90
+
91
+ return -1 if !obsolete? && other.obsolete?
92
+ return 1 if obsolete? && !other.obsolete?
93
+
94
+ %i[road name source].each do |attribute|
95
+ result = send(attribute) <=> other.send(attribute)
96
+ return result unless result.zero?
97
+ end
98
+
99
+ 0
100
+ end
101
+
63
102
  def to_a
64
103
  [
65
104
  identifier.to_s,
@@ -67,10 +106,27 @@ module JapanETC
67
106
  name,
68
107
  direction,
69
108
  entrance_or_exit,
70
- notes.empty? ? nil : notes.join(' ')
109
+ notes.empty? ? nil : notes.join(' '),
110
+ source
71
111
  ].flatten
72
112
  end
73
113
 
114
+ def obsolete?
115
+ notes.any? { |note| note.include?('迄') }
116
+ end
117
+
118
+ private
119
+
120
+ def normalize!
121
+ extract_notes_from_name!
122
+ extract_direction_from_notes!
123
+ extract_entrance_or_exit_from_notes!
124
+ extract_direction_from_name!
125
+ extract_entrance_or_exit_from_name!
126
+ name_was_changed = extract_name_from_notes!
127
+ normalize! if name_was_changed
128
+ end
129
+
74
130
  def extract_notes_from_name!
75
131
  name.sub!(/(?<head>.+?)?\s*[(\(](?<note>.+?)[)\)]\s*(?<tail>.+)?/) do
76
132
  match = Regexp.last_match
@@ -155,14 +211,32 @@ module JapanETC
155
211
  end
156
212
  end
157
213
 
214
+ def extract_name_from_notes!
215
+ name_was_changed = notes.reject! do |note|
216
+ match = note.match(/「(?<new_name>.+?)」へ名称変更/)
217
+ next false unless match
218
+
219
+ @name = normalize(match[:new_name])
220
+
221
+ true
222
+ end
223
+
224
+ name_was_changed
225
+ end
226
+
158
227
  def prepend_to_notes(note)
159
228
  note = normalize(note)
160
229
  notes.prepend(note)
161
230
  end
162
231
 
163
232
  Identifier = Struct.new(:road_number, :tollbooth_number) do
233
+ include Comparable
164
234
  include Util
165
235
 
236
+ def self.from(string)
237
+ new(*string.split('-'))
238
+ end
239
+
166
240
  def initialize(road_number, tollbooth_number)
167
241
  road_number = convert_to_integer(road_number)
168
242
  raise ValidationError, '#road_number cannot be nil' if road_number.nil?
@@ -176,7 +250,11 @@ module JapanETC
176
250
  end
177
251
 
178
252
  def to_s
179
- format('%02d-%03d', road_number, tollbooth_number)
253
+ @string ||= format('%02d-%03d', road_number, tollbooth_number)
254
+ end
255
+
256
+ def <=>(other)
257
+ to_s <=> other.to_s
180
258
  end
181
259
  end
182
260
  end
@@ -7,7 +7,7 @@ module JapanETC
7
7
  def normalize(string)
8
8
  return nil unless string
9
9
 
10
- convert_fullwidth_characters_to_halfwidth(string).strip
10
+ convert_fullwidth_characters_to_halfwidth(string)
11
11
  end
12
12
 
13
13
  def convert_fullwidth_characters_to_halfwidth(string)
@@ -16,6 +16,12 @@ module JapanETC
16
16
  string.tr(' A-Za-z0-9', ' A-Za-z0-9')
17
17
  end
18
18
 
19
+ def remove_whitespaces(string)
20
+ return nil unless string
21
+
22
+ string.tr(' ', '')
23
+ end
24
+
19
25
  def convert_to_integer(object)
20
26
  case object
21
27
  when Numeric
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JapanETC
4
- VERSION = '0.5.0'
4
+ VERSION = '0.8.0'
5
5
  end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: japan_etc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Nakayama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-13 00:00:00.000000000 Z
11
+ date: 2020-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: faraday
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: '0.15'
33
+ version: '1.0'
20
34
  type: :runtime
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: '0.15'
40
+ version: '1.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: nokogiri
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -102,12 +116,16 @@ files:
102
116
  - japan_etc.gemspec
103
117
  - lib/japan_etc.rb
104
118
  - lib/japan_etc/database.rb
105
- - lib/japan_etc/database_provider.rb
106
119
  - lib/japan_etc/database_provider/base.rb
120
+ - lib/japan_etc/database_provider/base_nexco.rb
107
121
  - lib/japan_etc/database_provider/hanshin_expressway.rb
108
122
  - lib/japan_etc/database_provider/metropolitan_expressway.rb
109
123
  - lib/japan_etc/database_provider/nagoya_expressway.rb
110
- - lib/japan_etc/database_provider/nexco.rb
124
+ - lib/japan_etc/database_provider/nexco_central.rb
125
+ - lib/japan_etc/database_provider/nexco_east.rb
126
+ - lib/japan_etc/database_provider/nexco_west.rb
127
+ - lib/japan_etc/database_provider/past_database.csv
128
+ - lib/japan_etc/database_provider/past_database.rb
111
129
  - lib/japan_etc/direction.rb
112
130
  - lib/japan_etc/entrance_or_exit.rb
113
131
  - lib/japan_etc/error.rb
@@ -134,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
152
  - !ruby/object:Gem::Version
135
153
  version: '0'
136
154
  requirements: []
137
- rubygems_version: 3.0.3
155
+ rubygems_version: 3.1.2
138
156
  signing_key:
139
157
  specification_version: 4
140
158
  summary: Japan ETC (Electronic Toll Collection System) database
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'japan_etc/database_provider/metropolitan_expressway'
4
- require 'japan_etc/database_provider/hanshin_expressway'
5
- require 'japan_etc/database_provider/nagoya_expressway'
6
- require 'japan_etc/database_provider/nexco'