Skip to content

Commit ceacc98

Browse files
authored
Merge pull request #62 from amatsuda/code_cleanups
Code cleanups
2 parents 9c5d007 + ce76e09 commit ceacc98

File tree

5 files changed

+54
-56
lines changed

5 files changed

+54
-56
lines changed

.rubocop.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,5 @@ Style:
1111
Layout:
1212
Enabled: false
1313

14-
Metrics/BlockLength:
15-
Max: 50
16-
17-
Metrics/MethodLength:
18-
Max: 40
19-
20-
Metrics/AbcSize:
21-
Max: 50
14+
Metrics:
15+
Enabled: false

app/helpers/messages_helper.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ def without_list_prefix(subject)
77
def search_snippet(body, keyword)
88
snippet = ''
99

10-
offset = 0
11-
while (i = body.index(keyword, offset))
12-
start = [i - MARGIN, offset].max
13-
len = keyword.length + MARGIN
14-
snippet += body[start, len]
15-
offset = start + len
16-
end
10+
offset = 0
11+
while (i = body.index(keyword, offset))
12+
start = [i - MARGIN, offset].max
13+
len = keyword.length + MARGIN
14+
snippet += body[start, len]
15+
offset = start + len
16+
end
1717

18-
if snippet.empty?
19-
return body[0, MARGIN * 2]
20-
else
21-
snippet
22-
end
18+
if snippet.empty?
19+
return body[0, MARGIN * 2]
20+
else
21+
snippet
22+
end
2323
end
2424
end

app/models/list.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class List
22
def initialize(name, id)
33
@name = name
4-
@id = id
4+
@id = id
55
end
66
attr_reader :name, :id
77

8-
# Ordered by the established dates. ruby-list was started in 1995.
8+
# Ordered by the established dates. ruby-list was started in 1995.
99
LISTS = [
10-
List.new('ruby-list', 1),
11-
List.new('ruby-dev', 2),
12-
List.new('ruby-core', 3),
13-
List.new('ruby-talk', 4),
10+
List.new('ruby-list', 1),
11+
List.new('ruby-dev', 2),
12+
List.new('ruby-core', 3),
13+
List.new('ruby-talk', 4),
1414
]
1515

1616
def self.find_by_name(name)

app/models/message.rb

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@
44
require 'kconv'
55

66
class Message < ApplicationRecord
7-
# Not really sure we will utlize this configuration,
8-
# but I don't want to make this column.
9-
# https://blade.ruby-lang.org/ruby-talk/1 is JST.
10-
# https://blade.ruby-lang.org/ruby-talk/410000 is not.
7+
# Not really sure we will utlize this configuration,
8+
# but I don't want to make this column.
9+
# https://blade.ruby-lang.org/ruby-talk/1 is JST.
10+
# https://blade.ruby-lang.org/ruby-talk/410000 is not.
1111
self.skip_time_zone_conversion_for_attributes = [:published_at]
1212

13-
def self.from_s3(list_name, list_seq, s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION))
14-
obj = s3_client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}")
13+
class << self
14+
def from_s3(list_name, list_seq, s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION))
15+
obj = s3_client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}")
1516

1617
m = self.from_string(obj.body.read)
1718
m.list_id = List.find_by_name(list_name).id
1819
m.list_seq = list_seq
1920
m
20-
end
21+
end
2122

22-
def self.from_string(str)
23+
def from_string(str)
2324
# There are a few hacks to import messages from blade.ruby-lang.org's
2425
# S3 bucket.
2526

2627
# Need to call String#b. There are messages that have headers in non-UTF8,
2728
# but the body is in UTF-8, such as ruby-list:2882.
28-
headers_str, body = str.b.split(/\n\n/, 2)
29+
headers_str, body = str.b.split(/\n\n/, 2)
2930

3031
# ruby-list:2840 doesn't have a proper From header.
3132
headers_str = Kconv.toutf8(headers_str).gsub(/\r\n/, '')
@@ -37,21 +38,22 @@ def self.from_string(str)
3738
published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z')
3839

3940
self.new(
40-
body: Kconv.toutf8(body),
41-
subject: headers['Subject'],
42-
from: headers['From'],
43-
published_at: published_at,
41+
body: Kconv.toutf8(body),
42+
subject: headers['Subject'],
43+
from: headers['From'],
44+
published_at: published_at,
4445
)
46+
end
4547
end
4648

4749
def reload_from_s3(s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION))
4850
m = Message.from_s3(List.find_by_id(self.list_id).name, self.list_seq, s3_client)
4951

50-
self.body = m.body
51-
self.subject = m.subject
52-
self.from = from
53-
self.published_at = m.published_at
52+
self.body = m.body
53+
self.subject = m.subject
54+
self.from = from
55+
self.published_at = m.published_at
5456

55-
m
57+
m
5658
end
5759
end

import.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
params = {}
44
OptionParser.new do |opts|
55
opts.on('--list LIST')
6-
opts.on('--from FROM', Integer)
7-
opts.on('--to TO', Integer)
6+
opts.on('--from FROM', Integer)
7+
opts.on('--to TO', Integer)
88
end.parse!(into: params)
99

1010
list = params[:list]
1111

12-
(params[:from]..params[:to]).each do |seq|
13-
begin
14-
message = Message.from_s3(list, seq)
15-
message.save
16-
rescue ActiveRecord::RecordNotUnique
17-
STDERR.puts("#{list}:#{seq} already exists in Postgres")
18-
rescue Aws::S3::Errors::NoSuchKey
19-
STDERR.puts("#{list}:#{seq} doesn't exist in S3")
20-
rescue StandardError => e
21-
STDERR.puts("failed to import #{list}:#{seq}: #{e}")
12+
Message.transaction do
13+
(params[:from]..params[:to]).each do |seq|
14+
begin
15+
message = Message.from_s3(list, seq)
16+
message.save!
17+
rescue ActiveRecord::RecordNotUnique
18+
STDERR.puts("#{list}:#{seq} already exists in Postgres")
19+
rescue Aws::S3::Errors::NoSuchKey
20+
STDERR.puts("#{list}:#{seq} doesn't exist in S3")
21+
rescue StandardError => e
22+
STDERR.puts("failed to import #{list}:#{seq}: #{e}")
23+
end
2224
end
2325
end

0 commit comments

Comments
 (0)