Class: ServerlessRedirector::S3Destination
- Inherits:
-
Destination
- Object
- Destination
- ServerlessRedirector::S3Destination
- Defined in:
- lib/serverless_redirector/destination.rb
Constant Summary
Constants inherited from Destination
Destination::REDIRECT_HEADER_KEY
Instance Attribute Summary collapse
-
#bucket_name ⇒ Object
readonly
Returns the value of attribute bucket_name.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #existing ⇒ Object
-
#initialize(uri) ⇒ S3Destination
constructor
A new instance of S3Destination.
- #remove_key(key) ⇒ Object
- #write_key(key, location, contents) ⇒ Object
Constructor Details
#initialize(uri) ⇒ S3Destination
Returns a new instance of S3Destination.
61 62 63 64 65 66 67 68 |
# File 'lib/serverless_redirector/destination.rb', line 61 def initialize(uri) @uri = uri @bucket_name = uri.host @prefix = uri.path.to_s.empty? ? nil : ::File.join(uri.path[1..-1], "") @options = CGI.parse(uri.query || "").each_with_object({}) do |(key, values), out| out[key] = values.last end end |
Instance Attribute Details
#bucket_name ⇒ Object (readonly)
Returns the value of attribute bucket_name.
59 60 61 |
# File 'lib/serverless_redirector/destination.rb', line 59 def bucket_name @bucket_name end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
59 60 61 |
# File 'lib/serverless_redirector/destination.rb', line 59 def prefix @prefix end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
59 60 61 |
# File 'lib/serverless_redirector/destination.rb', line 59 def uri @uri end |
Instance Method Details
#existing ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/serverless_redirector/destination.rb', line 70 def existing bucket.objects(prefix: prefix).map(&:object).select do |o| !o.[REDIRECT_HEADER_KEY].to_s.empty? end.map do |o| path = o.key.dup path.gsub! /^#{Regexp.escape(prefix)}\/?/, '' if prefix ServerlessRedirector::Manifest::Redirect.new 'path' => path, 'url' => o.[REDIRECT_HEADER_KEY].to_s end end |
#remove_key(key) ⇒ Object
80 81 82 83 |
# File 'lib/serverless_redirector/destination.rb', line 80 def remove_key(key) path = build_path key bucket.object(path).delete end |
#write_key(key, location, contents) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/serverless_redirector/destination.rb', line 85 def write_key(key, location, contents) # Remove the prefix to the path... path = build_path(key).gsub(/\A\/+/, '') bucket.put_object({ acl: "public-read", content_type: "text/html", body: contents, key: path, website_redirect_location: location, metadata: { REDIRECT_HEADER_KEY => location } }) end |