Class: Pal::Main

Inherits:
Object
  • Object
show all
Includes:
Configuration, Log, Plugin
Defined in:
lib/pal/main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

#config, #register_config

Methods included from Log

#log_debug, #log_error, #log_info, #log_warn

Methods included from Plugin

#register_plugins

Constructor Details

#initialize(config) ⇒ Main

Returns a new instance of Main.

Parameters:

  • config (Pal::Config)


21
22
23
# File 'lib/pal/main.rb', line 21

def initialize(config)
  register_config(config)
end

Instance Attribute Details

#managerPal::Handler::Manager



18
19
20
# File 'lib/pal/main.rb', line 18

def manager
  @manager
end

#runbookPal::Request::Runbook



15
16
17
# File 'lib/pal/main.rb', line 15

def runbook
  @runbook
end

Instance Method Details

#create_runbook(file_location) ⇒ Pal::Request::Runbook

Parameters:

  • file_location (String)

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pal/main.rb', line 40

def create_runbook(file_location)
  file_relative = file_location.start_with?("/") ? file_location : File.join(File.dirname(__FILE__), file_location)

  log_debug "Attempting to read file from [#{file_relative}]"
  log_debug "Script executed from [#{__dir__}]"

  request_content = File.read(file_relative)
  Pal::Request::Runbook.new.from_json(request_content)
rescue JSON::ParserError => e
  log_error("Malformed JSON request for file [#{file_location}]")
  raise e, "Malformed JSON request for file [#{file_location}]"
end

#create_service_managerPal::Handler::Manager



54
55
56
57
58
59
60
61
# File 'lib/pal/main.rb', line 54

def create_service_manager
  clazz_name = "Pal::Handler::#{@runbook..handler}HandlerImpl"
  impl = Kernel.const_get(clazz_name).new(@runbook)
  Pal::Handler::Manager.new(impl)
rescue NameError => e
  log_error("Cannot find a valid handler impl for #{@runbook..handler}")
  raise e
end

#processArray, Hash

Returns:

  • (Array, Hash)


34
35
36
# File 'lib/pal/main.rb', line 34

def process
  @manager.process_runbook(@runbook)
end

#setupObject

set config for process



26
27
28
29
30
31
# File 'lib/pal/main.rb', line 26

def setup
  register_plugins

  @runbook = create_runbook(config.template_file_loc)
  @manager = create_service_manager
end