From f1943f50cef03578766e0d2eba490ad1bf9f1d76 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 16 Oct 2025 12:27:49 +0900 Subject: [PATCH 1/5] << --- app/models/message.rb | 58 +++++++++++++++++++++---------------------- import.rb | 4 +-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/app/models/message.rb b/app/models/message.rb index 45390d1..2e9e7e5 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -4,54 +4,54 @@ require 'kconv' class Message < ApplicationRecord - # Not really sure we will utlize this configuration, - # but I don't want to make this column. - # https://blade.ruby-lang.org/ruby-talk/1 is JST. - # https://blade.ruby-lang.org/ruby-talk/410000 is not. + # Not really sure we will utlize this configuration, + # but I don't want to make this column. + # https://blade.ruby-lang.org/ruby-talk/1 is JST. + # https://blade.ruby-lang.org/ruby-talk/410000 is not. self.skip_time_zone_conversion_for_attributes = [:published_at] def self.from_s3(list_name, list_seq, s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION)) obj = s3_client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}") - m = self.from_string(obj.body.read) - m.list_id = List.find_by_name(list_name).id - m.list_seq = list_seq - m + m = self.from_string(obj.body.read) + m.list_id = List.find_by_name(list_name).id + m.list_seq = list_seq + m end def self.from_string(str) - # There are a few hacks to import messages from blade.ruby-lang.org's - # S3 bucket. + # There are a few hacks to import messages from blade.ruby-lang.org's + # S3 bucket. - # Need to call String#b. There are messages that have headers in non-UTF8, - # but the body is in UTF-8, such as ruby-list:2882. + # Need to call String#b. There are messages that have headers in non-UTF8, + # but the body is in UTF-8, such as ruby-list:2882. headers_str, body = str.b.split(/\n\n/, 2) - # ruby-list:2840 doesn't have a proper From header. - headers_str = Kconv.toutf8(headers_str).gsub(/\r\n/, '') + # ruby-list:2840 doesn't have a proper From header. + headers_str = Kconv.toutf8(headers_str).gsub(/\r\n/, '') - headers = headers_str.split(/\n/).map { |line| - line.split(/:\s+/, 2) - }.to_h + headers = headers_str.split(/\n/).map { |line| + line.split(/:\s+/, 2) + }.to_h - published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z') + published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z') - self.new( - body: Kconv.toutf8(body), - subject: headers['Subject'], - from: headers['From'], - published_at: published_at, - ) + self.new( + body: Kconv.toutf8(body), + subject: headers['Subject'], + from: headers['From'], + published_at: published_at, + ) end def reload_from_s3(s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION)) m = Message.from_s3(List.find_by_id(self.list_id).name, self.list_seq, s3_client) - self.body = m.body - self.subject = m.subject - self.from = from - self.published_at = m.published_at + self.body = m.body + self.subject = m.subject + self.from = from + self.published_at = m.published_at - m + m end end diff --git a/import.rb b/import.rb index 7b151a5..c35bb1e 100644 --- a/import.rb +++ b/import.rb @@ -3,8 +3,8 @@ params = {} OptionParser.new do |opts| opts.on('--list LIST') - opts.on('--from FROM', Integer) - opts.on('--to TO', Integer) + opts.on('--from FROM', Integer) + opts.on('--to TO', Integer) end.parse!(into: params) list = params[:list] From 5bb275bee420e5cddde2c939f6a6b262041355a1 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 16 Oct 2025 12:28:14 +0900 Subject: [PATCH 2/5] Use transaction --- import.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/import.rb b/import.rb index c35bb1e..71e4d92 100644 --- a/import.rb +++ b/import.rb @@ -9,15 +9,17 @@ list = params[:list] -(params[:from]..params[:to]).each do |seq| - begin - message = Message.from_s3(list, seq) - message.save -rescue ActiveRecord::RecordNotUnique - STDERR.puts("#{list}:#{seq} already exists in Postgres") -rescue Aws::S3::Errors::NoSuchKey - STDERR.puts("#{list}:#{seq} doesn't exist in S3") -rescue StandardError => e - STDERR.puts("failed to import #{list}:#{seq}: #{e}") +Message.transaction do + (params[:from]..params[:to]).each do |seq| + begin + message = Message.from_s3(list, seq) + message.save! + rescue ActiveRecord::RecordNotUnique + STDERR.puts("#{list}:#{seq} already exists in Postgres") + rescue Aws::S3::Errors::NoSuchKey + STDERR.puts("#{list}:#{seq} doesn't exist in S3") + rescue StandardError => e + STDERR.puts("failed to import #{list}:#{seq}: #{e}") + end end end From 8e08a3c8fdfd0e2b7045e7a4cedc10ef8dfd851e Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 16 Oct 2025 17:31:46 +0900 Subject: [PATCH 3/5] Prefer class << self over def self.method --- app/models/message.rb | 66 ++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/app/models/message.rb b/app/models/message.rb index 2e9e7e5..1f5151b 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -10,38 +10,40 @@ class Message < ApplicationRecord # https://blade.ruby-lang.org/ruby-talk/410000 is not. self.skip_time_zone_conversion_for_attributes = [:published_at] - def self.from_s3(list_name, list_seq, s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION)) - obj = s3_client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}") - - m = self.from_string(obj.body.read) - m.list_id = List.find_by_name(list_name).id - m.list_seq = list_seq - m - end - - def self.from_string(str) - # There are a few hacks to import messages from blade.ruby-lang.org's - # S3 bucket. - - # Need to call String#b. There are messages that have headers in non-UTF8, - # but the body is in UTF-8, such as ruby-list:2882. - headers_str, body = str.b.split(/\n\n/, 2) - - # ruby-list:2840 doesn't have a proper From header. - headers_str = Kconv.toutf8(headers_str).gsub(/\r\n/, '') - - headers = headers_str.split(/\n/).map { |line| - line.split(/:\s+/, 2) - }.to_h - - published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z') - - self.new( - body: Kconv.toutf8(body), - subject: headers['Subject'], - from: headers['From'], - published_at: published_at, - ) + class << self + def from_s3(list_name, list_seq, s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION)) + obj = s3_client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}") + + m = self.from_string(obj.body.read) + m.list_id = List.find_by_name(list_name).id + m.list_seq = list_seq + m + end + + def from_string(str) + # There are a few hacks to import messages from blade.ruby-lang.org's + # S3 bucket. + + # Need to call String#b. There are messages that have headers in non-UTF8, + # but the body is in UTF-8, such as ruby-list:2882. + headers_str, body = str.b.split(/\n\n/, 2) + + # ruby-list:2840 doesn't have a proper From header. + headers_str = Kconv.toutf8(headers_str).gsub(/\r\n/, '') + + headers = headers_str.split(/\n/).map { |line| + line.split(/:\s+/, 2) + }.to_h + + published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z') + + self.new( + body: Kconv.toutf8(body), + subject: headers['Subject'], + from: headers['From'], + published_at: published_at, + ) + end end def reload_from_s3(s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION)) From 44b3e69e5ba80c6fe9ce39742e621878869ecdd1 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 20 Oct 2025 19:05:00 +0900 Subject: [PATCH 4/5] rubocop --only Layout/CommentIndentation,Layout/FirstArrayElementIndentation,Layout/IndentationConsistency,Layout/ArrayAlignment -a --- app/helpers/messages_helper.rb | 24 ++++++++++++------------ app/models/list.rb | 12 ++++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb index 5981d9b..94ef84a 100644 --- a/app/helpers/messages_helper.rb +++ b/app/helpers/messages_helper.rb @@ -7,18 +7,18 @@ def without_list_prefix(subject) def search_snippet(body, keyword) snippet = '' - offset = 0 - while (i = body.index(keyword, offset)) - start = [i - MARGIN, offset].max - len = keyword.length + MARGIN - snippet += body[start, len] - offset = start + len - end + offset = 0 + while (i = body.index(keyword, offset)) + start = [i - MARGIN, offset].max + len = keyword.length + MARGIN + snippet += body[start, len] + offset = start + len + end - if snippet.empty? - return body[0, MARGIN * 2] - else - snippet - end + if snippet.empty? + return body[0, MARGIN * 2] + else + snippet + end end end diff --git a/app/models/list.rb b/app/models/list.rb index 641da36..d87c3cb 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -1,16 +1,16 @@ class List def initialize(name, id) @name = name - @id = id + @id = id end attr_reader :name, :id - # Ordered by the established dates. ruby-list was started in 1995. + # Ordered by the established dates. ruby-list was started in 1995. LISTS = [ - List.new('ruby-list', 1), - List.new('ruby-dev', 2), - List.new('ruby-core', 3), - List.new('ruby-talk', 4), + List.new('ruby-list', 1), + List.new('ruby-dev', 2), + List.new('ruby-core', 3), + List.new('ruby-talk', 4), ] def self.find_by_name(name) From ce76e0911ee4282ab6f2971d7401b7fe0fd943d4 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 20 Oct 2025 19:34:36 +0900 Subject: [PATCH 5/5] Who cares? --- .rubocop.yml | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index b1c8b19..144320f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -11,11 +11,5 @@ Style: Layout: Enabled: false -Metrics/BlockLength: - Max: 50 - -Metrics/MethodLength: - Max: 40 - -Metrics/AbcSize: - Max: 50 +Metrics: + Enabled: false