Sha256: 29847726d7cf65a1363f668082e31e3e650dda8addf83a1f481dc29a4f06bccf

Contents?: true

Size: 928 Bytes

Versions: 1

Compression:

Stored size: 928 Bytes

Contents

require 'base64'

module Evernote2org
  class Resource

    attr_accessor :doc, :id, :binary, :mime, :file_ext_name

    def initialize(doc)
      @doc = doc
      if recognition = @doc.css('recognition').first
        @id = Nokogiri::XML(recognition.content).css('recoIndex').first.attr('objID')
      end

      if @mime = @doc.css('mime').first&.content
        @file_ext_name = '.' + @mime.split('/').last
      end

      if data = @doc.css('data').first
        @binary = Base64.decode64(data.content)
      end
    end

    def export_to(out_dir)
      return unless @id && @mime
      File.open(File.join(out_dir, file_name), 'w') do |resource_file|
        resource_file.write(binary)
      end
    end

    def file_name
      @id + @file_ext_name
    end

    def to_img_tag(path, html_doc)
      img = Nokogiri::XML::Node.new('img', html_doc)
      img[:src] = File.join(path, file_name)
      img
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
evernote2org-0.1.3 lib/evernote2org/resource.rb