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
-
#approved_task?(name = nil) ⇒ Boolean
Predicate method to return true for valid job titles and false for invalid ones.
-
#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. -
#decipher_message(msg) ⇒ Object
Get the body of the message out from a few different types of objects.
Methods included from Auth
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_exist?, #task_home, #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
29 30 31 |
# File 'lib/cloud_powers/delegator.rb', line 29 def approved_task?(name = nil) ['demo', 'testinz', 'true_roas'].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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cloud_powers/delegator.rb', line 54 def build(id, msg) body = (msg) begin task = body['task'] if approved_task? task source_task(task) require_relative task_require_path(task) # Smash.const_get(to_pascal(task)).new(id, body) Smash.const_get(to_pascal(task)).create(id, body) else Smash::Task.new(id, body) # returns a default Task end rescue JSON::ParserError = [msg.body, (e)].join("\n") logger.info "Message in backlog is ill-formatted: #{}" pipe_to(:status_stream) { sitrep(extraInfo: { message: }) } end end |
#decipher_message(msg) ⇒ Object
Get the body of the message out from a few different types of objects. The idea is to allow JSON, a String or some object that responds to :body through while continually attempting to decipher other types of objects finally giving up. But wait, after it gives up, it just turns it into a Hash and assumes that the value is a Task name.
Parameters
- msg
String
Returns
Hash
Example
given hash_message = { task: 'example' }
givem json_message = ""task":"example""
given message_with_body = <Object @body="stuff stuff stuff">
()
# => { task: 'example' }
()
# => { task: 'example' }
()
# => { task: 'example' }
('some ridiculous string')
# => { task: 'some_ridiculous_string'}
Notes
See #to_snake()
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/cloud_powers/delegator.rb', line 101 def (msg) begin if msg.respond_to? :body (msg.body) else msg.kind_of?(Hash) ? msg : JSON.parse(msg) end rescue Exception { task: to_snake(msg.to_s) } end end |