Class: ServerlessRedirector::S3Destination

Inherits:
Destination
  • Object
show all
Defined in:
lib/serverless_redirector/destination.rb

Constant Summary

Constants inherited from Destination

Destination::REDIRECT_HEADER_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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

#prefixObject (readonly)

Returns the value of attribute prefix.



59
60
61
# File 'lib/serverless_redirector/destination.rb', line 59

def prefix
  @prefix
end

#uriObject (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

#existingObject



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