Module: MdstyleLinker

Defined in:
lib/mdstyle_linker.rb,
lib/mdstyle_linker/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.mdstyle(url) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mdstyle_linker.rb', line 8

def self.mdstyle(url)
  begin
    res = open(url, read_timeout: 2)
    # break if res.status != ["200", "OK"]
    raise InternetConnectionError, 'ネット環境がアレみたいです' if res.status[0] != '200'

    # res.readはプレーンなstringで返ってくる
    # EOFがめんどくさいので最初に弾いちゃう
    title = res.read.gsub(/[\r\n\t]/, '').match(/<head.*>.*<title>(.+?)<\/title>.*<\/head>/)[1]

    # titleのブラケットをエスケープする
    # ちなみにrubyではputsとかでバックスラッシュを出力するとそのまま表示されてしまうらしい
    table = {"[" => "\\[", "]" => "\\]"}
    title.gsub!(/[\[\]]/, table)
    "[#{title}](#{url})"
  rescue StandardError => e
    puts e
    exit
  end
end