Module: URI

Defined in:
lib/twitter_image_parser/core_ext/uri.rb

Constant Summary collapse

URL_SHORTENINGS =
["bit.ly", "ow.ly", "fb.me", "goo.gl", "j.mp", "t.co", "youtu.be"]

Instance Method Summary collapse

Instance Method Details

#expandObject

短縮URLを展開



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/twitter_image_parser/core_ext/uri.rb', line 5

def expand
  uri = self
  host = uri.host
  while URL_SHORTENINGS.any? { |val| val == host }
    next_uri = Net::HTTP.new(uri.host, uri.port).get(uri.path).header['location']
    break if next_uri.blank?  # 最終URLが上記ドメインの場合もあるため

    uri = URI.parse(next_uri)
    host = uri.host
  end
  uri
end

#img_urlObject

画像投稿URLから画像パスを取得



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/twitter_image_parser/core_ext/uri.rb', line 19

def img_url
  img_url = nil
  if self.host == 'twitpic.com'
    img_url = self.to_s.gsub("twitpic.com/", "twitpic.com/show/full/")
  elsif self.host == 'p.twipple.jp'
    img_url = self.to_s.gsub("p.twipple.jp/", "p.twpl.jp/show/orig/")
  elsif self.host == 'ow.ly'
    img_url = self.to_s.gsub("ow.ly/i/", "static.ow.ly/photos/original/")+'.jpg'
  elsif self.host == 'photozou.jp'
    img_url = self.to_s.gsub(/photozou\.jp\/photo\/show\/(\d)+\//, "photozou.jp/p/img/")
  elsif self.host == 'instagram.com'
    img_url = self.to_s + 'media/?size=l'
  elsif self.host == 'f.hatena.ne.jp'
    img_url = self.to_s.gsub(/f\.hatena.ne.jp\/(.+)\/(\d{8})(\d+)/) {"f.st-hatena.com/images/fotolife/#{$1[0,1]}/#{$1}/#{$2}/#{$2}#{$3}.jpg"}
  end
  img_url
end