Class: Strut::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/strut/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Config

Returns a new instance of Config.



8
9
10
11
# File 'lib/strut/config.rb', line 8

def initialize(config_file)
  path = File.dirname(config_file)
  read_and_store(config_file, path)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/strut/config.rb', line 6

def host
  @host
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



6
7
8
# File 'lib/strut/config.rb', line 6

def max_attempts
  @max_attempts
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/strut/config.rb', line 6

def namespace
  @namespace
end

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/strut/config.rb', line 6

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/strut/config.rb', line 6

def port
  @port
end

#runnerObject (readonly)

Returns the value of attribute runner.



6
7
8
# File 'lib/strut/config.rb', line 6

def runner
  @runner
end

#swaggerObject (readonly)

Returns the value of attribute swagger.



6
7
8
# File 'lib/strut/config.rb', line 6

def swagger
  @swagger
end

Instance Method Details

#extract_enum_value(yaml, name, enums) ⇒ Object



54
55
56
57
58
# File 'lib/strut/config.rb', line 54

def extract_enum_value(yaml, name, enums)
  value = extract_value(yaml, name).to_sym
  throw "'#{name}' must be one of #{enums}." unless enums.include?(value)
  value
end

#extract_int_value(yaml, name) ⇒ Object



48
49
50
51
52
# File 'lib/strut/config.rb', line 48

def extract_int_value(yaml, name)
  value = extract_value(yaml, name).to_i
  throw "'#{name}' must be a number > 0." unless value > 0
  value
end

#extract_optional_value(yaml, name) ⇒ Object



37
38
39
# File 'lib/strut/config.rb', line 37

def extract_optional_value(yaml, name)
  extract_value(yaml, name, true)
end

#extract_value(yaml, name, optional = false) ⇒ Object



41
42
43
44
45
46
# File 'lib/strut/config.rb', line 41

def extract_value(yaml, name, optional = false)
  value = yaml[name]
  value = value["value"] unless value.nil?
  throw "No '#{name}' specified." if value.nil? and !optional
  value
end

#read_and_store(config_file, path) ⇒ Object



13
14
15
16
17
# File 'lib/strut/config.rb', line 13

def read_and_store(config_file, path)
  yaml = File.read(config_file)
  parsed_yaml = Psych::parse_yaml(yaml)
  store_values(parsed_yaml, path)
end

#store_values(yaml, config_path) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/strut/config.rb', line 19

def store_values(yaml, config_path)
  @swagger = swagger_path(yaml, config_path)
  @runner = extract_optional_value(yaml, "runner")
  @host = extract_value(yaml, "host")
  @port = extract_int_value(yaml, "port")
  @max_attempts = extract_int_value(yaml, "max_attempts")
  @namespace = extract_value(yaml, "namespace")
  @output = extract_enum_value(yaml, "output", [:junit, :pretty])
end

#swagger_path(yaml, config_path) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/strut/config.rb', line 29

def swagger_path(yaml, config_path)
  swagger_path = Pathname.new(extract_value(yaml, "swagger"))
  unless swagger_path.absolute?
    swagger_path = Pathname.new(config_path) + swagger_path
  end
  swagger_path.to_s
end