Class: Ogo::Utils::RedirectFollower
- Inherits:
-
Object
- Object
- Ogo::Utils::RedirectFollower
- Defined in:
- lib/ogo/utils/redirect_follower.rb
Defined Under Namespace
Classes: EmptyURLError, TooManyRedirects
Constant Summary collapse
- REDIRECT_DEFAULT_LIMIT =
5
- HTTP_DEFAULT_TIMEOUT =
seconds
3
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#charset ⇒ Object
Returns the value of attribute charset.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http_timeout ⇒ Object
Returns the value of attribute http_timeout.
-
#redirect_limit ⇒ Object
Returns the value of attribute redirect_limit.
-
#response ⇒ Object
Returns the value of attribute response.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url, options = {}) ⇒ RedirectFollower
constructor
A new instance of RedirectFollower.
- #redirect_url ⇒ Object
- #resolve ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ RedirectFollower
Returns a new instance of RedirectFollower.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ogo/utils/redirect_follower.rb', line 15 def initialize(url, = {}) raise EmptyURLError if url.to_s.empty? @redirect_limit = [:redirect_limit] || Ogo.config[:redirect_limit] || REDIRECT_DEFAULT_LIMIT @http_timeout = [:http_timeout] || Ogo.config[:http_timeout] || HTTP_DEFAULT_TIMEOUT @headers = [:headers] || {} @url = url.start_with?('http') ? url : "http://#{url}" end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
12 13 14 |
# File 'lib/ogo/utils/redirect_follower.rb', line 12 def body @body end |
#charset ⇒ Object
Returns the value of attribute charset.
12 13 14 |
# File 'lib/ogo/utils/redirect_follower.rb', line 12 def charset @charset end |
#headers ⇒ Object
Returns the value of attribute headers.
12 13 14 |
# File 'lib/ogo/utils/redirect_follower.rb', line 12 def headers @headers end |
#http_timeout ⇒ Object
Returns the value of attribute http_timeout.
12 13 14 |
# File 'lib/ogo/utils/redirect_follower.rb', line 12 def http_timeout @http_timeout end |
#redirect_limit ⇒ Object
Returns the value of attribute redirect_limit.
12 13 14 |
# File 'lib/ogo/utils/redirect_follower.rb', line 12 def redirect_limit @redirect_limit end |
#response ⇒ Object
Returns the value of attribute response.
12 13 14 |
# File 'lib/ogo/utils/redirect_follower.rb', line 12 def response @response end |
#url ⇒ Object
Returns the value of attribute url.
12 13 14 |
# File 'lib/ogo/utils/redirect_follower.rb', line 12 def url @url end |
Instance Method Details
#redirect_url ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/ogo/utils/redirect_follower.rb', line 59 def redirect_url if response['location'].nil? response.body.match(/<a href=\"([^>]+)\">/i)[1] else response['location'] end end |
#resolve ⇒ Object
27 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 54 55 56 57 |
# File 'lib/ogo/utils/redirect_follower.rb', line 27 def resolve raise TooManyRedirects if redirect_limit < 0 uri = Addressable::URI.parse(url).normalize http = Net::HTTP.new(uri.host, uri.inferred_port) http.read_timeout = http_timeout if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end self.response = http.request_get(uri.request_uri, headers) if response.kind_of?(Net::HTTPRedirection) self.url = redirect_url self.redirect_limit -= 1 resolve end charset = nil if content_type = response['content-type'] if content_type =~ /charset=(.+)/i charset = $1 end end self.charset = charset self.body = response.body self end |