class Wp2hatena::Wordpress::Image

Public Class Methods

new() click to toggle source
# File lib/wp2hatena/wordpress/image.rb, line 7
def initialize
  @tmp_dir = Dir.mktmpdir
end

Public Instance Methods

download(xml_path) { |data| ... } click to toggle source
# File lib/wp2hatena/wordpress/image.rb, line 11
def download(xml_path)
  open(xml_path, 'r') do |fp|
    fp.each_line do |line|
      line.match(/<a.*?href="([^"]+?)"><img.*?src="(.+?)".*?><\/a>/) do |md|
        image_url = nil
        image_url = md[2] if md[2].match(/.+\.(jpg|jpeg|gif|png|bmp)\Z/)
        image_url = md[1] if md[1].match(/.+\.(jpg|jpeg|gif|png|bmp)\Z/)
        data = {
            file_path: fetch_image(image_url),
            width: match_width(line),
            height: match_height(line),
            html_tag: line,
        }
        yield data
      end
    end
  end
end

Private Instance Methods

fetch_image(url) click to toggle source
# File lib/wp2hatena/wordpress/image.rb, line 32
def fetch_image(url)
  file_path = File.join(@tmp_dir, File.basename(url))
  open(file_path, 'wb') do |fp|
    open(url) do |data|
      fp.write(data.read)
    end
  end
  file_path
end
match_height(str) click to toggle source
# File lib/wp2hatena/wordpress/image.rb, line 50
def match_height(str)
  height = nil
  str.match(/height="([^"]+)"/) do |md|
    height = md[1]
  end
  height
end
match_width(str) click to toggle source
# File lib/wp2hatena/wordpress/image.rb, line 42
def match_width(str)
  width = nil
  str.match(/width="([^"]+)"/) do |md|
    width = md[1]
  end
  width
end