lib/bbiff/bbs_reader.rb in bbiff-0.2.0 vs lib/bbiff/bbs_reader.rb in bbiff-0.2.1

- old
+ new

@@ -14,47 +14,44 @@ def dat_url(スレッド番号) return URI.parse("http://jbbs.shitaraba.net/bbs/rawmode.cgi/#{@カテゴリ}/#{@掲示板番号}/#{スレッド番号}/") end def 設定 - 応答 = Net::HTTP.start(@設定URL.host, @設定URL.port) { |http| - http.get(@設定URL.path) - } - r = 応答.body + r = ダウンロード(@設定URL) return 設定をパーズする(r.force_encoding("EUC-JP").encode("UTF-8")) end def スレ一覧 - 応答 = Net::HTTP.start(@スレ一覧URL.host, @スレ一覧URL.port) { |http| - http.get(@スレ一覧URL.path) - } - r = 応答.body + r = ダウンロード(@スレ一覧URL) return r.force_encoding("EUC-JP").encode("UTF-8") end def dat(スレッド番号) url = dat_url(スレッド番号) - 応答 = Net::HTTP.start(url.host, url.port) { |http| - p url - http.get(url.path) - } - r = 応答.body + r = ダウンロード(url) return r.force_encoding("EUC-JP").encode("UTF-8") end def thread(スレッド番号) threads.find { |t| t.id == スレッド番号 } end def threads スレ一覧.each_line.map do |line| - fail FormatError unless line =~ /^(\d+)\.cgi,([^(]+)\((\d+)\)$/ + fail 'スレ一覧のフォーマットが変です' unless line =~ /^(\d+)\.cgi,(.+?)\((\d+)\)$/ id, title, last = $1.to_i, $2, $3.to_i Thread.new(self, id, title, last) end end + def ダウンロード(url) + 応答 = Net::HTTP.start(url.host, url.port) { |http| + http.get(url.path) + } + return 応答.body + end + private def 設定をパーズする(文字列) 文字列.each_line.map { |line| line.chomp.split(/=/, 2) @@ -64,11 +61,11 @@ class Post attr_reader :no, :name, :mail, :body def self.from_line(line) - no, name, mail, date, body, = line.split('<>') + no, name, mail, date, body, = line.split('<>', 6) Post.new(no, name, mail, date, body) end def initialize(no, name, mail, date, body) @no = no.to_i @@ -81,11 +78,11 @@ def date str2time(@date) end def to_s - [no, name, mail, @date, body].join('<>') + [no, name, mail, @date, body, '', ''].join('<>') end private def str2time(str) @@ -111,10 +108,11 @@ def dat_url @board.dat_url(@id) end def posts(range) + fail ArgumentError unless range.is_a? Range dat_for_range(range).each_line.map do |line| Post.from_line(line.chomp).tap do |post| # ついでに last を更新 @last = [post.no, last].max end @@ -126,11 +124,10 @@ query = "#{range.first}-" else query = "#{range.first}-#{range.last}" end url = URI(dat_url + query) - res = Net::HTTP.start(url.host, url.port) { |http| http.get(url.path) } - res.body.force_encoding("EUC-JP").encode("UTF-8") + @board.ダウンロード(url).force_encoding("EUC-JP").encode("UTF-8") end end end # Module # include Bbs