Class: Bbiff::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/bbiff/executable.rb

Defined Under Namespace

Classes: LineIndicator, UsageError

Constant Summary collapse

RETRY_INTERVAL_SECONDS =
3

Instance Method Summary collapse

Constructor Details

#initializeExecutable

Returns a new instance of Executable.



76
77
78
# File 'lib/bbiff/executable.rb', line 76

def initialize
  @settings  = Settings.new
end

Instance Method Details

#get_board_settings(board) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/bbiff/executable.rb', line 94

def get_board_settings(board)
  return board.settings
rescue Bbs::Downloader::DownloadFailure => e
  STDERR.puts "Warning: 以下の場所から掲示板の設定が取得できませんでした。(#{e.response.message})"
  STDERR.puts board.settings_url
  return {'BBS_TITLE'=>'<不明>'}
end

#mainObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/bbiff/executable.rb', line 163

def main
  if ARGV.include?('-h') || ARGV.include?('--help')
    raise UsageError
  end

  if ARGV.size < 1 && !@settings.current['thread_url']
    raise UsageError
  elsif ARGV.size < 1
    url = @settings.current['thread_url']
  else
    url = ARGV[0]
  end

  begin
    thread = Bbs::create_thread(url)
    @settings.current['thread_url'] = url
  rescue Bbs::Downloader::DownloadFailure => e
    STDERR.puts "#{e.response.code} #{e.response.msg}: #{e.response.uri}"
    exit 1
  rescue => e
    STDERR.puts e.message
    exit 1
  end
  if thread.nil?
    STDERR.puts "スレッドのURLとして解釈できませんでした。(#{url})"
    exit 1
  end

  start_no = ARGV[1] ? ARGV[1].to_i : thread.last + 1
  start_polling(thread, start_no)
rescue UsageError
  usage
  exit 1
ensure
  @settings.save
end

#parse_range(str) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bbiff/executable.rb', line 80

def parse_range(str)
  if str == "all"
    1..Float::INFINITY
  elsif str =~ /^\d+$/
    str.to_i..str.to_i
  elsif str =~ /^\d+-$/
    str.to_i..Float::INFINITY
  elsif str =~ /^(\d+)-(\d+)$/
    $1.to_i..$2.to_i
  else
    fail ArgumentError
  end
end

#start_polling(thread, start_no) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/bbiff/executable.rb', line 104

def start_polling(thread, start_no)
  out = LineIndicator.new
  begin
    delay = @settings.current['delay_seconds']
    board_settings = get_board_settings(thread.board)
    thread_stop = (board_settings['BBS_THREAD_STOP'] || '1000').to_i

    puts "#{board_settings['BBS_TITLE']}#{thread.title}(#{thread.last})"
    puts "    #{@settings.current['thread_url']}"

    loop do
      out.set_line "#{thread.title}(#{thread.last}) 新着レス確認中"

      thread.posts(parse_range("#{start_no}-")).each do |post|
        out.puts "-----"
        puts render_post(post)

        system(@settings.current['bbiff_show'],
               thread.title, post.to_s)

        sleep 1
      end

      start_no = thread.last + 1
      if start_no > thread_stop
        out.puts "スレッドストップ"
        break 
      end

      delay.times do |i|
        j = i + 1
        out.set_line "#{thread.title}(#{thread.last}) 待機中 [#{'.'*j}#{' '*(delay - j)}]"
        sleep 1
      end
    end
  ensure
    out.close
  end
rescue Interrupt
  STDERR.puts "ユーザー割り込みにより停止"
rescue => e
  STDERR.print "Error: "
  STDERR.puts e.message
  STDERR.puts e.backtrace if $DEBUG
  STDERR.puts "#{RETRY_INTERVAL_SECONDS}秒後にリトライ"
  sleep RETRY_INTERVAL_SECONDS
  start_polling(thread, start_no)
end

#usageObject



153
154
155
156
157
158
159
160
161
# File 'lib/bbiff/executable.rb', line 153

def usage
  STDERR.puts "Usage: bbiff [http://jbbs.shitaraba.net/bbs/read.cgi/CATEGORY/BOARD_ID/THREAD_ID/] [START_NUMBER]"
  
  STDERR.puts <<"EOD"

Bbiff version #{Bbiff::VERSION}
Copyright © 2016-2019 Yoteichi
EOD
end