Class: ServerlessRedirector::LocalDestination

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) ⇒ LocalDestination

Returns a new instance of LocalDestination.



28
29
30
31
# File 'lib/serverless_redirector/destination.rb', line 28

def initialize(uri)
  @uri = uri
  @path = File.join uri.host, uri.path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/serverless_redirector/destination.rb', line 26

def path
  @path
end

#uriObject (readonly)

Returns the value of attribute uri.



26
27
28
# File 'lib/serverless_redirector/destination.rb', line 26

def uri
  @uri
end

Instance Method Details

#existingObject



33
34
35
36
37
38
# File 'lib/serverless_redirector/destination.rb', line 33

def existing
  Dir[File.join(@path, "**/*.json")].map do |json_path|
    contents = JSON.parse(File.read(json_path))
    ServerlessRedirector::Manifest::Redirect.new(contents)
  end.compact
end

#remove_key(key) ⇒ Object



40
41
42
43
44
45
# File 'lib/serverless_redirector/destination.rb', line 40

def remove_key(key)
  html_path = File.join(@path, "#{key}.html")
  File.delete(html_path) if File.exist?(html_path)
  json_path = File.join(@path, "#{key}.json")
  File.delete(json_path) if File.exist?(json_path)
end

#write_key(key, location, contents) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/serverless_redirector/destination.rb', line 47

def write_key(key, location, contents)
  html_path = File.join(@path, "#{key}.html")
  FileUtils.mkdir_p File.dirname(html_path)
  File.write html_path, contents
  json_path = File.join(@path, "#{key}.json")
  File.write json_path, JSON.dump(url: location, path: key)
end