Sha256: d4da696b0788dfa8cc1d99a76953817d7f3c882de20b806a660064f69548936c

Contents?: true

Size: 868 Bytes

Versions: 2

Compression:

Stored size: 868 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
      @mime = @doc.css('mime').first.content
      @file_ext_name = '.' + @mime.split('/').last
      @binary = Base64.decode64(@doc.css('data').first.content)
    end

    def export_to(out_dir)
      return unless @id
      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

2 entries across 2 versions & 1 rubygems

Version Path
evernote2org-0.1.2 lib/evernote2org/resource.rb
evernote2org-0.1.1 lib/evernote2org/resource.rb