Class: Archivist::Model::Document
- Inherits:
-
Object
- Object
- Archivist::Model::Document
- Defined in:
- lib/archivist/models/document.rb
Instance Method Summary collapse
- #download(format = :text) ⇒ Object
- #download_path(file) ⇒ Object
- #format_index ⇒ Object
- #index_xml_path ⇒ Object
-
#initialize ⇒ Document
constructor
A new instance of Document.
Constructor Details
#initialize ⇒ Document
Returns a new instance of Document.
15 16 17 18 19 20 21 22 |
# File 'lib/archivist/models/document.rb', line 15 def initialize @conn = Faraday.new(url: "http://archive.org") do |faraday| faraday.use FaradayMiddleware::FollowRedirects faraday.request :url_encoded # form-encode POST params # faraday.response :logger # log requests to STDOUT faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end |
Instance Method Details
#download(format = :text) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/archivist/models/document.rb', line 31 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
39 40 41 |
# File 'lib/archivist/models/document.rb', line 39 def download_path(file) "/download/#{identifier}/#{file}" end |
#format_index ⇒ Object
24 25 26 27 28 29 |
# File 'lib/archivist/models/document.rb', line 24 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_path ⇒ Object
43 44 45 |
# File 'lib/archivist/models/document.rb', line 43 def index_xml_path download_path("#{identifier}_files.xml") end |