Class: OpenStax::Content::Archive
- Inherits:
-
Object
- Object
- OpenStax::Content::Archive
- Defined in:
- lib/openstax/content/archive.rb
Instance Method Summary collapse
- #add_latest_book_version_if_missing(object) ⇒ Object
- #base_url ⇒ Object
- #fetch(object) ⇒ Object
-
#initialize(version: nil) ⇒ Archive
constructor
A new instance of Archive.
- #json(object) ⇒ Object
- #previous_version ⇒ Object
- #s3 ⇒ Object
- #slug(object) ⇒ Object
- #url_for(object) ⇒ Object
- #version ⇒ Object
- #versions ⇒ Object
- #webview_uri_for(page) ⇒ Object
Constructor Details
#initialize(version: nil) ⇒ Archive
Returns a new instance of Archive.
5 6 7 8 |
# File 'lib/openstax/content/archive.rb', line 5 def initialize(version: nil) @version = version @slugs = {} end |
Instance Method Details
#add_latest_book_version_if_missing(object) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/openstax/content/archive.rb', line 90 def add_latest_book_version_if_missing(object) book_id, page_id = object.split(':', 2) book_uuid, book_version = book_id.split('@', 2) return object unless book_version.nil? && s3.bucket_configured? s3.ls(version).each do |book| s3_uuid, s3_version = book.split('@') next unless s3_uuid == book_uuid book_version = s3_version break end book_id = "#{book_uuid}@#{book_version}".chomp('@') "#{book_id}:#{page_id}".chomp(':') end |
#base_url ⇒ Object
26 27 28 |
# File 'lib/openstax/content/archive.rb', line 26 def base_url @base_url ||= "https://#{OpenStax::Content.domain}/#{OpenStax::Content.archive_path}/#{version}" end |
#fetch(object) ⇒ Object
74 75 76 77 78 |
# File 'lib/openstax/content/archive.rb', line 74 def fetch(object) url = url_for object OpenStax::Content.logger.debug { "Fetching #{url}" } Faraday.get(url).body end |
#json(object) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/openstax/content/archive.rb', line 80 def json(object) begin JSON.parse(fetch(object)).tap do |hash| @slugs[object] = hash['slug'] end rescue JSON::ParserError => err raise "OpenStax Content Archive returned invalid JSON for #{url_for object}: #{err.}" end end |
#previous_version ⇒ Object
22 23 24 |
# File 'lib/openstax/content/archive.rb', line 22 def previous_version versions[versions.index(version) + 1] end |
#s3 ⇒ Object
10 11 12 |
# File 'lib/openstax/content/archive.rb', line 10 def s3 @s3 ||= OpenStax::Content::S3.new end |
#slug(object) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/openstax/content/archive.rb', line 107 def slug(object) @slugs[object] ||= begin object_with_version = add_latest_book_version_if_missing object slug = json(object_with_version)['slug'] @slugs[object_with_version] = slug if object_with_version != object slug end end |
#url_for(object) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/openstax/content/archive.rb', line 30 def url_for(object) return if object.nil? begin uri = Addressable::URI.parse object rescue Addressable::URI::InvalidURIError begin uri = Addressable::URI.parse "/#{object}" rescue Addressable::URI::InvalidURIError OpenStax::Content.logger.warn { "Invalid url: \"#{object}\" in archive link" } return object end end if uri.absolute? # Force absolute URLs to be https uri.scheme = 'https' return uri.to_s end if uri.path.empty? OpenStax::Content.logger.warn do "#{self.class.name} received an unexpected fragment-only URL in url_for: \"#{object}\"" end return object end if uri.path.start_with?('../') uri.path = uri.path.sub('..', '') "#{base_url}#{uri.to_s}" elsif uri.path.start_with?(OpenStax::Content.archive_path) || uri.path.start_with?("/#{OpenStax::Content.archive_path}") uri.path.start_with?('/') ? "https://#{OpenStax::Content.domain}#{uri.to_s}" : "https://#{OpenStax::Content.domain}/#{uri.to_s}" else uri.path = "#{uri.path.chomp('.json').chomp('.xhtml')}.json" uri.path.start_with?('/') ? "#{base_url}/contents#{uri.to_s}" : "#{base_url}/contents/#{uri.to_s}" end end |
#version ⇒ Object
18 19 20 |
# File 'lib/openstax/content/archive.rb', line 18 def version @version || versions.first end |
#versions ⇒ Object
14 15 16 |
# File 'lib/openstax/content/archive.rb', line 14 def versions @versions ||= s3.ls.reverse end |
#webview_uri_for(page) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/openstax/content/archive.rb', line 116 def webview_uri_for(page) uri = if page.is_a?(Addressable::URI) page else begin Addressable::URI.parse page rescue Addressable::URI::InvalidURIError begin Addressable::URI.parse "/#{page}" rescue Addressable::URI::InvalidURIError OpenStax::Content.logger.warn { "Invalid page url: \"#{page}\"" } return page end end end object = uri.path.split('/').last book_id, page_id = object.split(':', 2) page_uuid = page_id.split('@', 2).first book_slug = slug book_id page_slug = slug object uri.path = "books/#{book_slug}/pages/#{page_slug}" Addressable::URI.join "https://#{OpenStax::Content.domain}", uri end |