Class: Embargoed::MaintenanceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/embargoed/maintenance_file.rb

Constant Summary collapse

SETTINGS =
[:reason, :allowed_paths, :allowed_ips, :response_code, :retry_after]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MaintenanceFile



11
12
13
14
15
16
17
18
19
20
# File 'lib/embargoed/maintenance_file.rb', line 11

def initialize(path)
  @path = path
  @reason = Embargoed.config.default_reason
  @allowed_paths = Embargoed.config.default_allowed_paths
  @allowed_ips = Embargoed.config.default_allowed_ips
  @response_code = Embargoed.config.default_response_code
  @retry_after = Embargoed.config.default_retry_after

  import_yaml if exists?
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/embargoed/maintenance_file.rb', line 6

def path
  @path
end

Class Method Details

.defaultObject



70
71
72
# File 'lib/embargoed/maintenance_file.rb', line 70

def self.default
  self.new(named_paths.values.first)
end

.findObject

Find the first MaintenanceFile that exists



60
61
62
63
# File 'lib/embargoed/maintenance_file.rb', line 60

def self.find
  path = named_paths.values.find { |p| File.exist? p }
  self.new(path) if path
end

.named(name) ⇒ Object



65
66
67
68
# File 'lib/embargoed/maintenance_file.rb', line 65

def self.named(name)
  path = named_paths[name.to_sym]
  self.new(path) unless path.nil?
end

Instance Method Details

#deleteObject



46
47
48
# File 'lib/embargoed/maintenance_file.rb', line 46

def delete
  File.delete(path) if exists?
end

#exists?Boolean



22
23
24
# File 'lib/embargoed/maintenance_file.rb', line 22

def exists?
  File.exist? path
end

#import(hash) ⇒ Object Also known as: import_env_vars



50
51
52
53
54
55
56
# File 'lib/embargoed/maintenance_file.rb', line 50

def import(hash)
  SETTINGS.map(&:to_s).each do |att|
    self.send(:"#{att}=", hash[att]) unless hash[att].nil?
  end

  true
end

#to_hObject



26
27
28
29
30
# File 'lib/embargoed/maintenance_file.rb', line 26

def to_h
  SETTINGS.each_with_object({}) do |att, hash|
    hash[att] = send(att)
  end
end

#to_yaml(key_mapper = :to_s) ⇒ Object



32
33
34
35
36
# File 'lib/embargoed/maintenance_file.rb', line 32

def to_yaml(key_mapper = :to_s)
  to_h.each_with_object({}) { |(key, val), hash|
    hash[key.send(key_mapper)] = val
  }.to_yaml
end

#writeObject



38
39
40
41
42
43
44
# File 'lib/embargoed/maintenance_file.rb', line 38

def write
  FileUtils.mkdir_p(dir_path) unless Dir.exist? dir_path

  File.open(path, 'w') do |file|
    file.write to_yaml
  end
end