openstax_content 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 578cf5b445c79d9e7dd1a904da90cb3daf527d2d8b7de128d07640e15d8bd066
4
- data.tar.gz: b2bfe3524398a047b6722a047aa245de8ed52d02e4c9c37ca28f0b36840ba081
3
+ metadata.gz: ed22f20143aaa95ef57af3f123e218992d12320a2f3fc11181312aae26fc7372
4
+ data.tar.gz: 717b1f2bdce0faa6282fb3cbfef02845a590f2dab6be6cca515e4cb3e113c0dc
5
5
  SHA512:
6
- metadata.gz: c2f7708cce0fb71727d985dcef91f6b0844f1b3ba2b134a55cc6f4eb3bddf0132c633cce04ed8221d16f8aac785a08f1ef16fe17e6f184c7f481c479054e496b
7
- data.tar.gz: 07bd028f00e935ef240585da56b2fd88e82959da61213787f03421e1f07c0afecb8f3257b9b269ff1affea5483a227f70d299e1782a528e7ebb1c4d96dd2dd6b
6
+ metadata.gz: 31c7272f68a28f24aeb69b1a6f04bb8323976e484392e835d828b68c58838fe9a164ce54b2be073400c15485525d40359d7ea279fe3b8c64caeeb9d59476437b
7
+ data.tar.gz: 1eae72c6d0e78d02ae0152a741e90be6363ddac96b6670fe44080fe207b082e9fe54615ec9b316d1de8ce3394f7c0ed6aea79767c26b951486f5d793cb995c4f
@@ -48,6 +48,10 @@ class OpenStax::Content::Archive
48
48
  if uri.path.start_with?('../')
49
49
  uri.path = uri.path.sub('..', '')
50
50
  "#{base_url}#{uri.to_s}"
51
+ elsif uri.path.start_with?(OpenStax::Content.archive_path) ||
52
+ uri.path.start_with?("/#{OpenStax::Content.archive_path}")
53
+ uri.path.start_with?('/') ? "https://#{OpenStax::Content.domain}#{uri.to_s}" :
54
+ "https://#{OpenStax::Content.domain}/#{uri.to_s}"
51
55
  else
52
56
  uri.path = "#{uri.path.chomp('.json').chomp('.xhtml')}.json"
53
57
 
@@ -2,7 +2,7 @@ require 'aws-sdk-s3'
2
2
 
3
3
  class OpenStax::Content::S3
4
4
  def initialize
5
- @ls = {}
5
+ @ls = Hash.new { |hash, key| hash[key] = Hash.new { |hash, key| hash[key] = {} } }
6
6
  end
7
7
 
8
8
  def bucket_name
@@ -21,24 +21,65 @@ class OpenStax::Content::S3
21
21
  )
22
22
  end
23
23
 
24
- def ls(archive_version = nil)
25
- return @ls[archive_version] unless @ls[archive_version].nil?
26
- return unless bucket_configured?
27
-
24
+ # Returns the archive path for the given archive_version, book_id, page_uuid and extension
25
+ # If not all arguments are given, returns the prefix instead
26
+ def path_for(archive_version = nil, book_id = nil, page_uuid = nil, extension = nil)
28
27
  archive_path = OpenStax::Content.archive_path.chomp('/')
29
28
 
30
29
  if archive_version.nil?
31
- prefix = "#{archive_path}/"
32
- delimiter = '/'
30
+ "#{archive_path}/"
31
+ elsif book_id.nil?
32
+ "#{archive_path}/#{archive_version}/contents/"
33
+ elsif page_uuid.nil?
34
+ "#{archive_path}/#{archive_version}/contents/#{book_id}:"
35
+ elsif extension.nil?
36
+ "#{archive_path}/#{archive_version}/contents/#{book_id}:#{page_uuid}."
37
+ else
38
+ "#{archive_path}/#{archive_version}/contents/#{book_id}:#{page_uuid}.#{extension}"
39
+ end
40
+ end
41
+
42
+ # Without an archive version, returns a list of archive versions
43
+ # With an archive version, returns a list of book ids (uuid@version)
44
+ # With an archive version and a book, returns a list of page uuids
45
+ # With an archive version, book id and page uuid, returns the available extensions, if any
46
+ def ls(archive_version = nil, book_id = nil, page_uuid = nil)
47
+ return @ls[archive_version][book_id][page_uuid] \
48
+ unless @ls[archive_version][book_id][page_uuid].nil?
49
+ return unless bucket_configured?
50
+
51
+ prefix = path_for archive_version, book_id, page_uuid
52
+
53
+ delimiter = if archive_version.nil?
54
+ '/'
55
+ elsif book_id.nil?
56
+ ':'
57
+ elsif page_uuid.nil?
58
+ '.'
59
+ else
60
+ nil
61
+ end
62
+
63
+ responses = client.list_objects_v2 bucket: bucket_name, prefix: prefix, delimiter: delimiter
64
+
65
+ @ls[archive_version][book_id][page_uuid] = if page_uuid.nil?
66
+ responses.flat_map(&:common_prefixes).map do |common_prefix|
67
+ common_prefix.prefix.sub(prefix, '').chomp(delimiter)
68
+ end
33
69
  else
34
- prefix = "#{archive_path}/#{archive_version}/contents/"
35
- delimiter = ':'
70
+ responses.flat_map(&:contents).map { |content| content.key.sub(prefix, '') }
36
71
  end
72
+ end
37
73
 
38
- @ls[archive_version] = client.list_objects_v2(
39
- bucket: bucket_name, prefix: prefix, delimiter: delimiter
40
- ).flat_map(&:common_prefixes).map do |common_prefix|
41
- common_prefix.prefix.sub(prefix, '').chomp(delimiter)
74
+ # Checks all books for the given page uuid and returns the path to the first one found
75
+ def find_page(page_uuid, archive_version: nil, extension: 'json')
76
+ archive_version ||= ls.last
77
+
78
+ ls(archive_version).each do |book_id|
79
+ return path_for(archive_version, book_id, page_uuid, extension) \
80
+ if ls(archive_version, book_id, page_uuid).include?(extension)
42
81
  end
82
+
83
+ nil
43
84
  end
44
85
  end
@@ -1,5 +1,5 @@
1
1
  module OpenStax
2
2
  module Content
3
- VERSION = '0.0.2'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openstax_content
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dante Soares
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-11 00:00:00.000000000 Z
11
+ date: 2021-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-s3