Class: Archivist::Model::Document

Inherits:
Object
  • Object
show all
Includes:
Client::Constants
Defined in:
lib/archivist/models/document.rb

Constant Summary collapse

UnsupportedFormat =
Class.new(StandardError)

Constants included from Client::Constants

Client::Constants::DEFAULT_CONNECTION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



23
24
25
# File 'lib/archivist/models/document.rb', line 23

def initialize
  @conn = DEFAULT_CONNECTION
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



15
16
17
# File 'lib/archivist/models/document.rb', line 15

def conn
  @conn
end

Instance Method Details

#download(format = :text) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/archivist/models/document.rb', line 36

def download(format=:text)
  # e.g. format_index.text_format
  file_format = format_index.send(:"#{format}_format")
  if file_format.nil?
    raise UnsupportedFormat, "#{identifier} not available in format #{format}"
  else
    # e.g. /download/firstbooknapole00gruagoog/firstbooknapole00gruagoog_djvu.txt
    @conn.get(download_path(file_format.name)).
      body.force_encoding('UTF-8')
  end
end

#download_path(file) ⇒ Object



48
49
50
# File 'lib/archivist/models/document.rb', line 48

def download_path(file)
  "/download/#{identifier}/#{file}"
end

#format_indexObject



27
28
29
30
31
32
33
34
# File 'lib/archivist/models/document.rb', line 27

def format_index
  response = @conn.get(index_xml_path)
  Model::FormatIndex.new.tap do |idx|
    rep = Representation::FormatIndex.new(idx)
    # binding.pry
    rep.from_xml(response.body)
  end
end

#index_xml_pathObject



52
53
54
# File 'lib/archivist/models/document.rb', line 52

def index_xml_path
  download_path("#{identifier}_files.xml")
end