Class: OpenStax::Content::Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/content/archive.rb

Instance Method Summary collapse

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/openstax/content/archive.rb', line 83

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_urlObject



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



63
64
65
66
67
# File 'lib/openstax/content/archive.rb', line 63

def fetch(object)
  url = url_for object
  OpenStax::Content.logger.debug { "Fetching #{url}" }
  Faraday.get(url).body
end

#json(object) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/openstax/content/archive.rb', line 69

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.message}"
  end
end

#s3Object



79
80
81
# File 'lib/openstax/content/archive.rb', line 79

def s3
  @s3 ||= OpenStax::Content::S3.new
end

#slug(object) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/openstax/content/archive.rb', line 100

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
58
59
60
61
# 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?
    OpenStax::Content.logger.warn do
      "#{self.class.name} received an unexpected absolute URL in url_for: \"#{object}\""
    end

    # 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