Class: Atig::IFilter::ExpandUrl
- Inherits:
-
Object
- Object
- Atig::IFilter::ExpandUrl
- Includes:
- Util
- Defined in:
- lib/atig/ifilter/expand_url.rb
Instance Method Summary collapse
- #call(status) ⇒ Object
-
#initialize(context) ⇒ ExpandUrl
constructor
A new instance of ExpandUrl.
- #resolve_http_redirect(uri, limit = 3) ⇒ Object
Methods included from ExceptionUtil
Constructor Details
Instance Method Details
#call(status) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/atig/ifilter/expand_url.rb', line 18 def call(status) target = if @opts.untiny_whole_urls then URI.regexp(%w[http https]) else %r{ http:// (?: (?: bit\.ly | (?: tin | rub) yurl\.com | j\.mp | t\.co | htn.to | is\.gd | cli\.gs | tr\.im | u\.nu | airme\.us | ff\.im | twurl.nl | bkite\.com | tumblr\.com | pic\.gd | sn\.im | digg\.com | goo\.gl) / [0-9a-z=-]+ | blip\.fm/~ (?> [0-9a-z]+) (?! /) | flic\.kr/[a-z0-9/]+ ) }ix end status.merge :text => status.text.gsub(target) {|url| x = @cache[url] if x then x else @cache[url] = resolve_http_redirect(URI(url)).to_s || url end } end |
#resolve_http_redirect(uri, limit = 3) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/atig/ifilter/expand_url.rb', line 46 def resolve_http_redirect(uri, limit = 3) return uri if limit.zero? or uri.nil? log :debug, uri.inspect req = @http.req :head, uri @http.http(uri, 3, 2).request(req) do |res| break if not res.is_a?(Net::HTTPRedirection) or not res.key?("Location") begin location = URI(res["Location"]) rescue URI::InvalidURIError end unless location.is_a? URI::HTTP begin location = URI.join(uri.to_s, res["Location"]) rescue URI::InvalidURIError, URI::BadURIError # FIXME end end uri = resolve_http_redirect(location, limit - 1) end uri rescue Errno::ETIMEDOUT, IOError, Timeout::Error, Errno::ECONNRESET => e log :error, e.inspect uri end |