Skip to content

Commit

Permalink
Replace EmbiggenedURI#reason with #error
Browse files Browse the repository at this point in the history
Rather than preserving only a string message describing the cause of an
expansion failure, expose the actual error object (which can then be
interrogated for its message).
  • Loading branch information
mudge committed Apr 20, 2015
1 parent e5bc9cd commit 81a4493
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 23 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ uri.success?
# If there weren't successful, you can ask them why
uri.success?
#=> false
uri.reason
#=> 'following https://youtu.be/dQw4w9WgXcQ did not redirect'
uri.error
#=> #<Embiggen::TooManyRedirects ...>
# or
#=> 'https://youtu.be/dQw4w9WgXcQ redirected too many times'
#=> #<Embiggen::BadShortenedURI ...>
# or
#=> #<Timeout::Error ...>

# Before expansion, you can check whether a URI is shortened or not
Embiggen::URI('https://youtu.be/dQw4w9WgXcQ').shortened?
Expand Down
9 changes: 9 additions & 0 deletions lib/embiggen/bad_shortened_uri.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'embiggen/error'

module Embiggen
class BadShortenedURI < Error
def self.for(uri)
new("following #{uri} did not redirect")
end
end
end
10 changes: 5 additions & 5 deletions lib/embiggen/embiggened_uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Embiggen
class EmbiggenedURI
extend Forwardable
attr_reader :uri, :success, :reason
attr_reader :uri, :success, :error
alias_method :success?, :success
def_delegators :uri, :to_s, :fragment, :host, :path, :port, :query,
:scheme, :request_uri
Expand All @@ -12,14 +12,14 @@ def self.success(uri)
new(uri, :success => true)
end

def self.failure(uri, reason = nil)
new(uri, :success => false, :reason => reason)
def self.failure(uri, error = nil)
new(uri, :success => false, :error => error)
end

def initialize(uri, options = {})
@uri = uri
@success = options.fetch(:success)
@reason = options[:reason]
@success = options.fetch(:success) { !options.key?(:error) }
@error = options[:error]
end

def inspect
Expand Down
4 changes: 4 additions & 0 deletions lib/embiggen/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Embiggen
class Error < ::RuntimeError
end
end
9 changes: 9 additions & 0 deletions lib/embiggen/too_many_redirects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'embiggen/error'

module Embiggen
class TooManyRedirects < Error
def self.for(uri)
new("#{uri} redirected too many times")
end
end
end
27 changes: 15 additions & 12 deletions lib/embiggen/uri.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'embiggen/embiggened_uri'
require 'embiggen/configuration'
require 'embiggen/too_many_redirects'
require 'embiggen/bad_shortened_uri'
require 'net/http'

module Embiggen
Expand All @@ -15,7 +17,7 @@ def expand(request_options = {})

follow_redirects(request_options)
rescue ::Timeout::Error, ::Errno::ECONNRESET => e
EmbiggenedURI.failure(uri, e.message)
EmbiggenedURI.failure(uri, e)
end

def expand!(request_options = {})
Expand All @@ -41,23 +43,20 @@ def inspect

def follow_redirects(request_options = {})
redirects = request_options.fetch(:redirects) { Configuration.redirects }
return EmbiggenedURI.failure(uri) if redirects.zero?
return too_many_redirects if redirects.zero?

location = head_location(request_options)
return EmbiggenedURI.failure(uri, "following #{uri} did not " \
'redirect') unless location
return bad_shortened_uri unless location

location.expand(request_options.merge(:redirects => redirects - 1))
end

def follow_redirects!(request_options = {})
redirects = request_options.fetch(:redirects) { Configuration.redirects }
fail TooManyRedirects, "#{uri} redirected too many " \
'times' if redirects.zero?
fail TooManyRedirects.for(uri) if redirects.zero?

location = head_location(request_options)
fail BadShortenedURI, "following #{uri} did not " \
'redirect' unless location
fail BadShortenedURI.for(uri) unless location

location.expand!(request_options.merge(:redirects => redirects - 1))
end
Expand All @@ -80,9 +79,13 @@ def http

http
end
end

class Error < ::StandardError; end
class BadShortenedURI < Error; end
class TooManyRedirects < Error; end
def too_many_redirects
EmbiggenedURI.failure(uri, TooManyRedirects.for(uri))
end

def bad_shortened_uri
EmbiggenedURI.failure(uri, BadShortenedURI.for(uri))
end
end
end
13 changes: 10 additions & 3 deletions spec/embiggen/embiggened_uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ module Embiggen
expect(failed_uri).to_not be_success
end

it 'takes an optional reason for failure' do
it 'takes an optional cause for failure' do
uri = URI.new('http://bit.ly/bad')
failed_uri = described_class.failure(uri, 'something went wrong')
error = Error.new('whoops')
failed_uri = described_class.failure(uri, error)

expect(failed_uri.reason).to eq('something went wrong')
expect(failed_uri.error).to eq(error)
end
end

it 'defaults to being successful if there is no error' do
uri = described_class.new(URI('http://www.altmetric.com'))

expect(uri).to be_success
end

describe '#uri' do
it 'returns the URI within' do
uri = described_class.success(URI('http://www.altmetric.com'))
Expand Down

0 comments on commit 81a4493

Please sign in to comment.