File: tco.rb

package info (click to toggle)
mikutter 3.0.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,396 kB
  • ctags: 1,916
  • sloc: ruby: 16,619; sh: 117; makefile: 27
file content (39 lines) | stat: -rw-r--r-- 716 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
# エンティティで展開しきれなかった t.co で短縮されたURLを展開する。
# http://www.gistlog.org/gist/1008272

require 'uri'
require 'net/http'

class TCo < MessageConverters

  def plugin_name()
    :tco
  end

  def shrink_url(url)
    # 未対応
    return url
  end

  def shrinked_url?(url)
    !!(url =~ /http:\/\/t\.co\//)
  end

  def expand_url(url)
    type_strict url => :to_s
    hash = Hash.new
    begin
      res = timeout(5){ Net::HTTP.get_response(URI.parse(url.to_s)) }
      if res.is_a?(Net::HTTPRedirection)
        res["location"]
      else
        url.to_s end
    rescue Exception => e
      warn e
      url.to_s
    end
  end

  regist
end