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.



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

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#existingObject



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

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



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

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



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

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