Class: HatenablogPublisher::Options

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/hatenablog_publisher/options.rb

Constant Summary collapse

REQUIRE_KEYS =
%i[
  consumer_key
  consumer_secret
  access_token
  access_token_secret
  user
  site
  filename
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



6
7
8
# File 'lib/hatenablog_publisher/options.rb', line 6

def args
  @args
end

Class Method Details

.create(args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/hatenablog_publisher/options.rb', line 29

def create(args)
  config_file = args[:config] || './hatenablog_publisher_config.yml'
  from_file = if File.exist?(config_file)
                YAML.safe_load(ERB.new(File.read(config_file)).result)
              else
                {}
              end
  config = new(from_file.symbolize_keys.merge(args) { |_k, o, n| n.nil? ? o : n })
  config.valid_or_raise
end

Instance Method Details

#valid_or_raiseObject



18
19
20
21
22
23
24
25
26
# File 'lib/hatenablog_publisher/options.rb', line 18

def valid_or_raise
  config_keys = to_h.keys
  key_is_set = ->(key) { config_keys.include?(key) && !to_h[key].nil? }
  unless (lacking_keys = REQUIRE_KEYS.reject { |key| key_is_set.call(key) }).empty?
    raise "Following keys are not setup. #{lacking_keys.map(&:to_s)}"
  end

  self
end