class NewspaperWorks::Ingest::BaseIngest

base class for ingesting works, implements, as-needed, temp files

Attributes

filename[RW]
io[RW]
path[RW]
work[RW]

Public Class Methods

new(work) click to toggle source
# File lib/newspaper_works/ingest/base_ingest.rb, line 11
def initialize(work)
  # adapted context:
  @work = work
end

Public Instance Methods

import() click to toggle source

default handler attaches file to work's file set, subclasses

may overwride or wrap this.
# File lib/newspaper_works/ingest/base_ingest.rb, line 53
def import
  files = NewspaperWorks::Data::WorkFiles.new(work)
  files.assign(path)
  files.commit!
end
ingest(source, filename: nil) click to toggle source
# File lib/newspaper_works/ingest/base_ingest.rb, line 63
def ingest(source, filename: nil)
  load(source, filename: filename)
  import
end
load(source, filename: nil) click to toggle source
# File lib/newspaper_works/ingest/base_ingest.rb, line 36
def load(source, filename: nil)
  # source is a string path, Pathname object, or quacks like an IO
  unless source.class == String ||
         source.class == Pathname ||
         source.respond_to?('read')
    raise ArgumentError, 'Source is neither path nor IO object'
  end
  # permit the possibility of a filename identifier metadata distinct
  #   from the actual path on disk:
  @filename = filename
  ispath = source.class == String || source.class == Pathname
  loader = ispath ? method(:loadpath) : method(:loadio)
  loader.call(source)
end
loadio(source) click to toggle source
# File lib/newspaper_works/ingest/base_ingest.rb, line 26
def loadio(source)
  # either an IO with a path, or an IO with filename passed in
  #   args; presume we need a filename to describe/identify.
  raise ArgumentError, 'Explicit or inferred file name required' unless
    source.respond_to?('path') || @filename
  @io = source
  @path = source.respond_to?('path') ? source.path : nil
  @filename ||= File.split(@path)[-1]
end
loadpath(source) click to toggle source
# File lib/newspaper_works/ingest/base_ingest.rb, line 16
def loadpath(source)
  # quick check the file exists and is readable on filesystem:
  raise ArgumentError, 'File not found or readable' unless
    File.readable?(source)
  # path may be relative to Dir.pwd, but no matter for our use
  @path = source.to_s
  @io = File.open(@path)
  @filename ||= File.split(@path)[-1]
end
user() click to toggle source
# File lib/newspaper_works/ingest/base_ingest.rb, line 59
def user
  defined?(current_user) ? current_user : User.batch_user
end