Class: Archivist::Model::Document
- Inherits:
-
Object
- Object
- Archivist::Model::Document
- 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
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
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.
24 25 26 |
# File 'lib/archivist/models/document.rb', line 24 def initialize @conn = DEFAULT_CONNECTION end |
Instance Attribute Details
#conn ⇒ Object (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_index ⇒ Object
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_path ⇒ Object
47 48 49 |
# File 'lib/archivist/models/document.rb', line 47 def index_xml_path download_path("#{identifier}_files.xml") end |