module Paperclip::Storage::WebMerge

Constants

VERSION

Public Class Methods

extended(base) click to toggle source
# File lib/paperclip_web_merge.rb, line 8
def self.extended base
  begin
    require 'web_merge'
  rescue LoadError => e
    e.message << " (You may need to install the web_merge gem)"
    raise e
  end

  base.instance_eval do
    @web_merge_credentials = parse_credentials(@options[:web_merge_credentials])
    @client = ::WebMerge::API.new(secret: @web_merge_credentials[:api_secret], key: @web_merge_credentials[:api_key])
  end
end

Public Instance Methods

copy_to_local_file(style, local_dest_path) click to toggle source
# File lib/paperclip_web_merge.rb, line 39
def copy_to_local_file(style, local_dest_path)
  log("copying #{path(style)} to local file #{local_dest_path}")
  ::File.open(local_dest_path, 'wb') do |local_file|
    file_contents = @client.get_document_file(instance.web_merge_id)['contents']
    web_merge_file = Base64.decode64(file_contents)
    local_file.write(web_merge_file)
  end
rescue
  warn("cannot copy #{path(style)} to local file #{local_dest_path}")
  false
end
exists?(style = default_style) click to toggle source
# File lib/paperclip_web_merge.rb, line 27
def exists?(style = default_style)
  if original_filename
    begin
      ::WebMerge::Document.find(instance.web_merge_id, client: @client)
    rescue
      false
    end
  else
    false
  end
end
file_type(file) click to toggle source
# File lib/paperclip_web_merge.rb, line 51
def file_type(file)
  correct_file_type = MIME::Types.type_for(file.path).first.to_s
  file = OpenStruct.new(content_type: correct_file_type) if ['application/zip', 'application/octect-stream'].include?(file.content_type)

  {
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document'   => 'docx',
    'application/pdf'                                                           => 'pdf',
    'application/vnd.ms-excel'                                                  => 'xlsx',
    'application/vnd.ms-excel.sheet.macroenabled.12'                            => 'xlsx',
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'         => 'xlsx',
    'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
    'application/vnd.ms-powerpoint'                                             => 'pptx',
  }[file.content_type]
end
flush_deletes() click to toggle source
# File lib/paperclip_web_merge.rb, line 76
def flush_deletes
  @queued_for_delete.each do |path|
    begin
      log("deleting #{path}")
      @client.delete_document(instance.web_merge_id)
      instance.update_column(:web_merge_id, nil)
    rescue
      log("error deleting #{path}")
    end
  end
  @queued_for_delete = []
end
flush_writes() click to toggle source
# File lib/paperclip_web_merge.rb, line 66
def flush_writes
  @queued_for_write.each do |style, file|
    log("Saving to WebMerge #{path(style)}")
    document = ::WebMerge::Document.new(client: @client, name: path, type: file_type(file), output: file_type(file), file_path: file.path)
    document.save!
    instance.update_column(:web_merge_id, document.id)
  end
  @queued_for_write = {}
end
parse_credentials(creds) click to toggle source
# File lib/paperclip_web_merge.rb, line 22
def parse_credentials creds
  creds = find_credentials(creds).stringify_keys
  creds.symbolize_keys
end

Private Instance Methods

find_credentials(creds) click to toggle source
# File lib/paperclip_web_merge.rb, line 89
def find_credentials creds
  case creds
  when File
    YAML::load(ERB.new(File.read(creds.path)).result)
  when String
    YAML::load(ERB.new(File.read(creds)).result)
  when Hash
    creds
  else
    raise ArgumentError, "Credentials are not a path, file, or hash."
  end
end