Skip to content

Commit

Permalink
UrlFetchTitle: サーバー接続不可表示を実装した
Browse files Browse the repository at this point in the history
refs #5
  • Loading branch information
ochaochaocha3 committed Mar 15, 2015
1 parent 7e4935b commit 59a7a74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/rgrb/plugin/url_fetch_title/generator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# vim: fileencoding=utf-8

require 'uri'
require 'socket'
require 'mechanize'
require 'active_support'
require 'active_support/core_ext/numeric/conversions'
Expand Down Expand Up @@ -114,6 +115,8 @@ def fetch_title(url)
]
"(#{contents.join('; ')})"
end
rescue SocketError
body = '(サーバーに接続できませんでした)'
rescue Mechanize::ResponseCodeError => response_code_error
response_code = response_code_error.response_code.to_i
reason_phrase =
Expand Down
17 changes: 16 additions & 1 deletion spec/rgrb/plugin/url_fetch_title/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@
end
end

context 'サーバーに接続できない場合' do
let(:url) { 'http://invaliddomain.net' }

before do
stub_request(:get, url).to_raise(SocketError)
end

subject { generator.fetch_title(url) }
it 'サーバーに接続できないことを示すエラーメッセージを返す' do
expect(subject).to eq(
"#{default_prefix}(サーバーに接続できませんでした)#{default_suffix}"
)
end
end

context '401 Unauthorized' do
let(:url) { 'http://example.net/need_login' }

Expand Down Expand Up @@ -141,7 +156,7 @@
end
end

context '不明なエラー' do
context '不明な HTTP エラー' do
let(:url) { 'http://example.net/strange_error' }

before do
Expand Down

0 comments on commit 59a7a74

Please sign in to comment.