Class: OpenStax::Content::S3

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

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name: nil) ⇒ S3

Returns a new instance of S3.



4
5
6
7
# File 'lib/openstax/content/s3.rb', line 4

def initialize(bucket_name: nil)
  @bucket_name = bucket_name
  @ls = Hash.new { |hash, key| hash[key] = Hash.new { |hash, key| hash[key] = {} } }
end

Instance Method Details

#bucket_configured?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/openstax/content/s3.rb', line 13

def bucket_configured?
  !bucket_name.nil? && !bucket_name.empty?
end

#bucket_nameObject



9
10
11
# File 'lib/openstax/content/s3.rb', line 9

def bucket_name
  @bucket_name ||= OpenStax::Content.bucket_name
end

#clientObject



17
18
19
20
21
22
23
# File 'lib/openstax/content/s3.rb', line 17

def client
  @client ||= Aws::S3::Client.new(
    region: OpenStax::Content.s3_region,
    access_key_id: OpenStax::Content.s3_access_key_id,
    secret_access_key: OpenStax::Content.s3_secret_access_key
  )
end

#find_page(page_uuid, archive_version: nil, extension: 'json') ⇒ Object

Checks all books for the given page uuid and returns the path to the first one found



76
77
78
79
80
81
82
83
84
85
# File 'lib/openstax/content/s3.rb', line 76

def find_page(page_uuid, archive_version: nil, extension: 'json')
  archive_version ||= ls.last

  ls(archive_version).each do |book_id|
    return path_for(archive_version, book_id, page_uuid, extension) \
      if ls(archive_version, book_id, page_uuid).include?(extension)
  end

  nil
end

#ls(archive_version = nil, book_id = nil, page_uuid = nil) ⇒ Object

Without an archive version, returns a list of archive versions With an archive version, returns a list of book ids (uuid@version) With an archive version and a book, returns a list of page uuids With an archive version, book id and page uuid, returns the available extensions, if any



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
73
# File 'lib/openstax/content/s3.rb', line 47

def ls(archive_version = nil, book_id = nil, page_uuid = nil)
  return @ls[archive_version][book_id][page_uuid] \
    unless @ls[archive_version][book_id][page_uuid].nil?
  return unless bucket_configured?

  prefix = path_for archive_version, book_id, page_uuid

  delimiter = if archive_version.nil?
    '/'
  elsif book_id.nil?
    ':'
  elsif page_uuid.nil?
    '.'
  else
    nil
  end

  responses = client.list_objects_v2 bucket: bucket_name, prefix: prefix, delimiter: delimiter

  @ls[archive_version][book_id][page_uuid] = if page_uuid.nil?
    responses.flat_map(&:common_prefixes).map do |common_prefix|
      common_prefix.prefix.sub(prefix, '').chomp(delimiter)
    end
  else
    responses.flat_map(&:contents).map { |content| content.key.sub(prefix, '') }
  end
end

#path_for(archive_version = nil, book_id = nil, page_uuid = nil, extension = nil) ⇒ Object

Returns the archive path for the given archive_version, book_id, page_uuid and extension If not all arguments are given, returns the prefix instead



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/openstax/content/s3.rb', line 27

def path_for(archive_version = nil, book_id = nil, page_uuid = nil, extension = nil)
  archive_path = OpenStax::Content.archive_path.chomp('/')

  if archive_version.nil?
    "#{archive_path}/"
  elsif book_id.nil?
    "#{archive_path}/#{archive_version}/contents/"
  elsif page_uuid.nil?
    "#{archive_path}/#{archive_version}/contents/#{book_id}:"
  elsif extension.nil?
    "#{archive_path}/#{archive_version}/contents/#{book_id}:#{page_uuid}."
  else
    "#{archive_path}/#{archive_version}/contents/#{book_id}:#{page_uuid}.#{extension}"
  end
end