44require 'kconv'
55
66class 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
5759end
0 commit comments