Module: Smash::CloudPowers::Delegator

Extended by:
Auth
Includes:
AwsResources, Helper, Storage
Included in:
Smash::CloudPowers
Defined in:
lib/cloud_powers/delegator.rb

Overview

The Delegator module is a way to dynamically source and use Ruby source code. You pass a message or something that can respond to #body() and give back a String. The String should be the name of the class you're trying to instantiate and use

Instance Method Summary collapse

Methods included from Auth

creds, region

Methods included from Zenv

#env_vars, #file_tree_search, #i_vars, #project_root, #project_root=, #system_vars, #zfind

Methods included from Helper

#attr_map!, #available_resources, #called_from, #create_logger, #deep_modify_keys_with, #format_error_message, #log_file, #logger, #modify_keys_with, #smart_retry, #task_path, #task_require_path, #to_camel, #to_hyph, #to_i_var, #to_pascal, #to_ruby_file_name, #to_snake, #update_message_body, #valid_json?, #valid_url?

Methods included from Storage

#search, #send_logs_to_s3, #source_task

Methods included from AwsResources

#ec2, #image, #kinesis, #region, #s3, #sns, #sqs

Instance Method Details

#approved_task?(name = nil) ⇒ Boolean

Predicate method to return true for valid job titles and false for invalid ones

Parameters

  • name String (optional) - name of the task in snake_case

Returns Boolean

Notes

  • TODO: needs improvement

Returns:

  • (Boolean)


68
69
70
# File 'lib/cloud_powers/delegator.rb', line 68

def approved_task?(name = nil)
  ['demo', 'testinz'].include? to_snake(name)
end

#build(id, msg) ⇒ Object

responsible for sourcing, loading into the global namespace and use of Ruby source code, through the #new() method on that class

Parameters

  • id String - the instance id of the calling node. This gets used in communication tools etc.
  • msg - Anything that responds to #body() and returns a String that can be used in the search for the file in your local and S3 bucket locations

Returns Object - a newly instantiated object of the class that matches the name retrieved from the msg parameter

Example class Job; include Smash::CloudPowers::Delegator; end job = Job.new

the message responds to #body() with "ExampleTask"

job.build('abc-1234', Aws::SQS::Message)

=> ExampleTask:Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cloud_powers/delegator.rb', line 40

def build(id, msg)
  body = JSON.parse(msg.body)
  begin
    task = body.delete('task')
    if approved_task? task
      source_task(task)
      require_relative task_require_path(task)
      Smash.const_get(to_pascal(task)).new(id, msg)
    else
      Smash::Task.new(id, msg) # returns a default Task
    end
  rescue JSON::ParserError => e
    message = [msg.body, format_error_message(e)].join("\n")
    logger.info "Message in backlog is ill-formatted: #{message}"
    pipe_to(:status_stream) { sitrep(extraInfo: { message: message }) }
  end
end