Class: Illustration
- Inherits:
-
Object
- Object
- Illustration
- Defined in:
- lib/illustration.rb
Overview
挿絵管理
Defined Under Namespace
Classes: UnknownMIMEType
Constant Summary collapse
- ILLUST_DIR =
"挿絵/"
- ILLUST_URL =
"http://%s.mitemin.net/userpageimage/viewimage/icode/%s/"
- MIME =
{ "image/jpeg" => "jpg", "image/png" => "png", "image/gif" => "gif", "image/bmp" => "bmp" }
Instance Method Summary collapse
-
#get(illust_tag) ⇒ Object
小説家になろうのイラストタグから挿絵データを取得する.
-
#initialize(setting, inspector) ⇒ Illustration
constructor
A new instance of Illustration.
- #make_illust_chuki(illust_path) ⇒ Object
- #make_illust_path(id1, id2, ext = "*", check = true) ⇒ Object
- #make_url(id1, id2) ⇒ Object
-
#to_rel(base, target) ⇒ Object
絶対パスから相対パスを作成 blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/36985.
Constructor Details
#initialize(setting, inspector) ⇒ Illustration
Returns a new instance of Illustration.
19 20 21 22 |
# File 'lib/illustration.rb', line 19 def initialize(setting, inspector) @setting = setting @inspector = inspector end |
Instance Method Details
#get(illust_tag) ⇒ 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 58 59 60 |
# File 'lib/illustration.rb', line 27 def get(illust_tag) if illust_tag =~ /<(.+)\|(.+)>/ id1, id2 = $1, $2 illust_path = make_illust_path(id1, id2) unless illust_path # 挿絵画像をサイトからダウンロードして保存する open(make_url(id1, id2)) do |read_fp| read_fp.autoclose = true content_type = read_fp.["content-type"] ext = MIME[content_type] or raise UnknownMIMEType, content_type illust_path = make_illust_path(id1, id2, ext, false) illust_dir = File.dirname(illust_path) Dir.mkdir(illust_dir) unless File.exists?(illust_dir) open(illust_path, "wb") do |write_fp| write_fp.write(read_fp.read) end @inspector.info("挿絵「#{File.basename(illust_path)}」を保存しました。") end end chuki = make_illust_chuki(illust_path) chuki else # 有効なイラストタグではなかった @inspector.error("Illustration#get: #{illust_tag} は有効なイラストタグではありません。") nil end rescue UnknownMIMEType => e @inspector.error("Illustration#get: イラストタグ #{illust_tag} は対応した画像フォーマットではありません" + \ "(content-type: #{e.})") nil rescue StandardError => e @inspector.error("Illustration#get: イラストタグ #{illust_tag} を処理中に例外が発生しました(#{e})") nil end |
#make_illust_chuki(illust_path) ⇒ Object
62 63 64 65 |
# File 'lib/illustration.rb', line 62 def make_illust_chuki(illust_path) rel_illust_path = to_rel(@setting.archive_path, illust_path) "[#挿絵(#{rel_illust_path})入る]" end |
#make_illust_path(id1, id2, ext = "*", check = true) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/illustration.rb', line 67 def make_illust_path(id1, id2, ext = "*", check = true) path = File.join(@setting.archive_path, ILLUST_DIR, "#{id1},#{id2}.#{ext}") if check Dir.glob(path)[0] else path end end |
#make_url(id1, id2) ⇒ Object
76 77 78 |
# File 'lib/illustration.rb', line 76 def make_url(id1, id2) ILLUST_URL % [id2, id1] end |
#to_rel(base, target) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/illustration.rb', line 84 def to_rel(base, target) sep = /#{File::SEPARATOR}+/o base = base.split(sep) base.pop target = target.split(sep) while base.shift == target.shift end File.join([".."]*base.size+target) end |