Class: UrlHunter
- Inherits:
-
Object
- Object
- UrlHunter
- Defined in:
- lib/url_hunter.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#original_url ⇒ Object
readonly
Returns the value of attribute original_url.
-
#tries ⇒ Object
readonly
Returns the value of attribute tries.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.resolve(url, limit = 5) ⇒ Object
Class level wrapper.
Instance Method Summary collapse
-
#initialize(url, limit = 5) ⇒ UrlHunter
constructor
Init.
-
#resolve ⇒ Object
Instance resolve function.
Constructor Details
#initialize(url, limit = 5) ⇒ UrlHunter
Init
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/url_hunter.rb', line 16 def initialize(url, limit = 5) if not url =~ /^(http|https):\/\//i raise UrlHunter::Error, 'URL must begin with http:// or https://' end @limit = limit > 0 ? limit : 5 @original_url = url @url = url @tries = 0 end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
8 9 10 |
# File 'lib/url_hunter.rb', line 8 def limit @limit end |
#original_url ⇒ Object (readonly)
Returns the value of attribute original_url.
8 9 10 |
# File 'lib/url_hunter.rb', line 8 def original_url @original_url end |
#tries ⇒ Object (readonly)
Returns the value of attribute tries.
8 9 10 |
# File 'lib/url_hunter.rb', line 8 def tries @tries end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
8 9 10 |
# File 'lib/url_hunter.rb', line 8 def url @url end |
Class Method Details
.resolve(url, limit = 5) ⇒ Object
Class level wrapper
11 12 13 |
# File 'lib/url_hunter.rb', line 11 def self.resolve(url, limit = 5) self.new(url, limit = 5).resolve end |
Instance Method Details
#resolve ⇒ Object
Instance resolve function
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/url_hunter.rb', line 28 def resolve # Return what we have so far if we're above the limit return @url if @tries >= @limit u = URI.parse(@url) # Try to get a NET response # Or just return URL begin res = Net::HTTP.get_response(u) rescue Exception return @url end if res.kind_of?(Net::HTTPRedirection) redirect = res['Location'] # Append Location re-direct to previous URL if it's relative if not redirect =~ /^(http|https):\/\//i redirect = u.scheme + '://' + u.hostname + redirect end @url = redirect @tries += 1 resolve else @url end end |