Class: NeverBounce::CLI::UserConfig::FileContent

Inherits:
Object
  • Object
show all
Defined in:
lib/never_bounce/cli/user_config/file_content.rb

Overview

User's configuration file content.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyString

YAML configuration, source text.

Returns:

  • (String)

21
22
23
24
25
26
27
# File 'lib/never_bounce/cli/user_config/file_content.rb', line 21

def body
  @body ||= begin
    File.read(filename)
  rescue Errno::ENOENT    # Missing file is okay, let other exceptions manifest.
    ""
  end
end

#body_hashHash

YAML configuration, parsed.

Returns:

  • (Hash)

32
33
34
# File 'lib/never_bounce/cli/user_config/file_content.rb', line 32

def body_hash
  @body_hash ||= body.to_s.empty?? {} : YAML.load(body)
end

#envHash

A copy of environment for read purposes. Default is ENV.to_h.

Returns:

  • (Hash)

38
39
40
# File 'lib/never_bounce/cli/user_config/file_content.rb', line 38

def env
  @env ||= ENV.to_h
end

#filenameString

Configuration filename. Default is $HOME/.neverbounce.yml.

Returns:

  • (String)

44
45
46
# File 'lib/never_bounce/cli/user_config/file_content.rb', line 44

def filename
  @filename ||= File.join(env["HOME"], ".neverbounce.yml")
end

Instance Method Details

#[](k) ⇒ Object

Fetch a value.

config_file["api_key"]
config_file[:api_key]   # Same as above.

54
55
56
# File 'lib/never_bounce/cli/user_config/file_content.rb', line 54

def [](k)
  body_hash[k.to_s]
end

#has_key?(k) ⇒ Boolean

true if key is set.

has_key?("api_key")
has_key?(:api_key)      # Identical to the previous one.

Returns:

  • (Boolean)

62
63
64
# File 'lib/never_bounce/cli/user_config/file_content.rb', line 62

def has_key?(k)
  body_hash.has_key? k.to_s
end