openstax_content 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openstax/content/archive.rb +4 -0
- data/lib/openstax/content/s3.rb +54 -13
- data/lib/openstax/content/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed22f20143aaa95ef57af3f123e218992d12320a2f3fc11181312aae26fc7372
|
4
|
+
data.tar.gz: 717b1f2bdce0faa6282fb3cbfef02845a590f2dab6be6cca515e4cb3e113c0dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
|
data/lib/openstax/content/s3.rb
CHANGED
@@ -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
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
32
|
-
|
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
|
35
|
-
delimiter = ':'
|
70
|
+
responses.flat_map(&:contents).map { |content| content.key.sub(prefix, '') }
|
36
71
|
end
|
72
|
+
end
|
37
73
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
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
|
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
|
+
date: 2021-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-s3
|