Class: Archivist::Model::Document

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

Constant Summary collapse

DEFAULT_CONNECTION =
Faraday.new(url: 'http://archive.org') do |faraday|
  faraday.use FaradayMiddleware::FollowRedirects
  # faraday.response :logger                  # log requests to STDOUT
  faraday.request  :url_encoded             # form-encode POST params
  faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



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

def initialize
  @conn = DEFAULT_CONNECTION
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



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

def conn
  @conn
end

Instance Method Details

#download(format = :text) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/archivist/models/document.rb', line 35

def download(format=:text)
  # e.g. format_index.text_format
  file_format = format_index.send(:"#{format}_format")
  # e.g. /download/firstbooknapole00gruagoog/firstbooknapole00gruagoog_djvu.txt
  @conn.get(download_path(file_format.name)).
    body.force_encoding('UTF-8')
end

#download_path(file) ⇒ Object



43
44
45
# File 'lib/archivist/models/document.rb', line 43

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

#format_indexObject



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

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

#index_xml_pathObject



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

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