Skip to content

Commit 8e08a3c

Browse files
committed
Prefer class << self over def self.method
1 parent 5bb275b commit 8e08a3c

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

app/models/message.rb

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,40 @@ class Message < ApplicationRecord
1010
# 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}")
15-
16-
m = self.from_string(obj.body.read)
17-
m.list_id = List.find_by_name(list_name).id
18-
m.list_seq = list_seq
19-
m
20-
end
21-
22-
def self.from_string(str)
23-
# There are a few hacks to import messages from blade.ruby-lang.org's
24-
# S3 bucket.
25-
26-
# Need to call String#b. There are messages that have headers in non-UTF8,
27-
# but the body is in UTF-8, such as ruby-list:2882.
28-
headers_str, body = str.b.split(/\n\n/, 2)
29-
30-
# ruby-list:2840 doesn't have a proper From header.
31-
headers_str = Kconv.toutf8(headers_str).gsub(/\r\n/, '')
32-
33-
headers = headers_str.split(/\n/).map { |line|
34-
line.split(/:\s+/, 2)
35-
}.to_h
36-
37-
published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z')
38-
39-
self.new(
40-
body: Kconv.toutf8(body),
41-
subject: headers['Subject'],
42-
from: headers['From'],
43-
published_at: published_at,
44-
)
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}")
16+
17+
m = self.from_string(obj.body.read)
18+
m.list_id = List.find_by_name(list_name).id
19+
m.list_seq = list_seq
20+
m
21+
end
22+
23+
def from_string(str)
24+
# There are a few hacks to import messages from blade.ruby-lang.org's
25+
# S3 bucket.
26+
27+
# Need to call String#b. There are messages that have headers in non-UTF8,
28+
# but the body is in UTF-8, such as ruby-list:2882.
29+
headers_str, body = str.b.split(/\n\n/, 2)
30+
31+
# ruby-list:2840 doesn't have a proper From header.
32+
headers_str = Kconv.toutf8(headers_str).gsub(/\r\n/, '')
33+
34+
headers = headers_str.split(/\n/).map { |line|
35+
line.split(/:\s+/, 2)
36+
}.to_h
37+
38+
published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z')
39+
40+
self.new(
41+
body: Kconv.toutf8(body),
42+
subject: headers['Subject'],
43+
from: headers['From'],
44+
published_at: published_at,
45+
)
46+
end
4547
end
4648

4749
def reload_from_s3(s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION))

0 commit comments

Comments
 (0)