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) ⇒ Archive
constructor
A new instance of Archive.
- #json(object) ⇒ Object
- #s3 ⇒ Object
- #slug(object) ⇒ Object
- #url_for(object) ⇒ Object
- #webview_uri_for(page) ⇒ Object
Constructor Details
#initialize(version) ⇒ Archive
Returns a new instance of Archive.
5 6 7 8 |
# File 'lib/openstax/content/archive.rb', line 5 def initialize(version) @version = version @slugs = {} end |
Instance Method Details
#add_latest_book_version_if_missing(object) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/openstax/content/archive.rb', line 79 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| uuid, version = book.split('@') next unless uuid == book_uuid book_version = version break end book_id = "#{book_uuid}@#{book_version}".chomp('@') "#{book_id}:#{page_id}".chomp(':') end |
#base_url ⇒ Object
10 11 12 13 |
# File 'lib/openstax/content/archive.rb', line 10 def base_url @base_url ||= "https://#{OpenStax::Content.domain}/#{ OpenStax::Content.archive_path}/#{@version}" end |
#fetch(object) ⇒ Object
59 60 61 62 63 |
# File 'lib/openstax/content/archive.rb', line 59 def fetch(object) url = url_for object OpenStax::Content.logger.debug { "Fetching #{url}" } Faraday.get(url).body end |
#json(object) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/openstax/content/archive.rb', line 65 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 |
#s3 ⇒ Object
75 76 77 |
# File 'lib/openstax/content/archive.rb', line 75 def s3 @s3 ||= OpenStax::Content::S3.new end |
#slug(object) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/openstax/content/archive.rb', line 96 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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 |
# File 'lib/openstax/content/archive.rb', line 15 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 |
#webview_uri_for(page) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/openstax/content/archive.rb', line 105 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 |