Module: Smash::CloudPowers::Helper
- Includes:
- Auth
- Included in:
- Smash::CloudPowers, AwsResources, Delegator, SelfAwareness, Synapse, Synapse::Queue, Synapse::Queue::Board, Zenv
- Defined in:
- lib/cloud_powers/helper.rb
Instance Method Summary collapse
- #attr_map!(keys) ⇒ Object
- #called_from ⇒ Object
- #create_logger ⇒ Object
- #errors ⇒ Object
- #format_error_message(error) ⇒ Object
-
#log_file ⇒ Object
Gets the path from the environment and sets @log_file using the path.
-
#logger ⇒ Object
@returns: An instance of Logger, cached as @logger.
-
#smart_retry(test, allowed_attempts = Float::INFINITY) ⇒ Object
Lets you retry a piece of logic with 1 second sleep in between attempts until another bit of logic does what it's supposed to, kind of like continuing to poll something and doing something when a package is ready to be taken and processed.
- #task_path(file = '') ⇒ Object
- #task_require_path(file_name = '') ⇒ Object
- #to_camel(var) ⇒ Object
- #to_i_var(var) ⇒ Object
- #to_pascal(var) ⇒ Object
- #to_ruby_file_name(name) ⇒ Object
- #to_snake(var) ⇒ Object
- #update_message_body(opts = {}) ⇒ Object
- #valid_json?(json) ⇒ Boolean
Methods included from Auth
Instance Method Details
#attr_map!(keys) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/cloud_powers/helper.rb', line 13 def attr_map!(keys) keys.map do |attr| key = to_i_var(attr) value = yield key if block_given? instance_variable_set(key, value) unless instance_variable_get(to_i_var(key)) end end |
#called_from ⇒ Object
21 22 23 |
# File 'lib/cloud_powers/helper.rb', line 21 def called_from File.(File.dirname($0)) end |
#create_logger ⇒ Object
25 26 27 28 29 |
# File 'lib/cloud_powers/helper.rb', line 25 def create_logger logger = Logger.new(STDOUT) logger.datetime_format = '%Y-%m-%d %H:%M:%S' logger end |
#errors ⇒ Object
31 32 33 34 |
# File 'lib/cloud_powers/helper.rb', line 31 def errors # TODO: needs work $errors ||= SmashError.instance end |
#format_error_message(error) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/cloud_powers/helper.rb', line 36 def (error) begin [error., error.backtrace.join("\n")].join("\n") rescue Exception => e # if the formatting won't work, return the original exception error end end |
#log_file ⇒ Object
Gets the path from the environment and sets @log_file using the path
47 48 49 |
# File 'lib/cloud_powers/helper.rb', line 47 def log_file @log_file ||= zfind('LOG_FILE') end |
#logger ⇒ Object
@returns: An instance of Logger, cached as @logger
52 53 54 |
# File 'lib/cloud_powers/helper.rb', line 52 def logger @logger ||= create_logger end |
#smart_retry(test, allowed_attempts = Float::INFINITY) ⇒ Object
Lets you retry a piece of logic with 1 second sleep in between attempts until another bit of logic does what it's supposed to, kind of like continuing to poll something and doing something when a package is ready to be taken and processed. @params:
* [allowed_attempts] or Infinity(default) <Number>: The number of times
the loop should be allowed to...well, loop, before a failed retry occurs.
* &test <Block>: A predicate method or block of code that is callable
is used to test if the block being retried is successful yet.
* []
Sample usage: check_stuff = lambda { |params| return true } smart_retry(3, check_stuff(params)) { do_stuff_that_needs_to_be_checked }
69 70 71 72 73 74 75 76 77 |
# File 'lib/cloud_powers/helper.rb', line 69 def smart_retry(test, allowed_attempts = Float::INFINITY) result = yield if block_given? tries = 1 until test.call(result) || tries >= allowed_attempts result = yield if block_given? tries += 1 sleep 1 end end |
#task_path(file = '') ⇒ Object
Gives the path from the project root to lib/tasks @params:
* [file] <String>: name of a file
@returns:
* path[/file] <String>
* If a `file` is given, it will have a '.rb' file extension
* If no `file` is given, it will return the `#task_require_path`
86 87 88 89 |
# File 'lib/cloud_powers/helper.rb', line 86 def task_path(file = '') return task_require_path if file.empty? Pathname(__FILE__).parent.dirname + 'tasks' + to_ruby_file_name(file) end |
#task_require_path(file_name = '') ⇒ Object
Gives the path from the project root to lib/tasks @params:
* [file] <String>: name of a file
@returns:
* path[/file] <String>
* Neither path nor file will have a file extension
97 98 99 100 |
# File 'lib/cloud_powers/helper.rb', line 97 def task_require_path(file_name = '') file = File.basename(file_name, File.extname(file_name)) Pathname(__FILE__).parent.dirname + 'tasks' + file end |
#to_camel(var) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/cloud_powers/helper.rb', line 102 def to_camel(var) var = var.to_s unless var.kind_of? String step_one = to_snake(var) step_two = to_pascal(step_one) step_two[0, 1].downcase + step_two[1..-1] end |
#to_i_var(var) ⇒ Object
109 110 111 112 |
# File 'lib/cloud_powers/helper.rb', line 109 def to_i_var(var) var = var.to_s unless var.kind_of? String /^\W*@\w+/ =~ var ? to_snake(var) : "@#{to_snake(var)}" end |
#to_pascal(var) ⇒ Object
114 115 116 117 |
# File 'lib/cloud_powers/helper.rb', line 114 def to_pascal(var) var = var.to_s unless var.kind_of? String var.gsub(/^(.{1})|\W.{1}|\_.{1}/) { |s| s.gsub(/[^a-z0-9]+/i, '').capitalize } end |
#to_ruby_file_name(name) ⇒ Object
119 120 121 |
# File 'lib/cloud_powers/helper.rb', line 119 def to_ruby_file_name(name) name[/\.rb$/].nil? ? "#{to_snake(name)}.rb" : "#{to_snake(name)}" end |
#to_snake(var) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cloud_powers/helper.rb', line 123 def to_snake(var) var = var.to_s unless var.kind_of? String var.gsub(/:{2}|\//, '_'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). gsub(/\s+/, '_'). tr("-", "_"). downcase end |
#update_message_body(opts = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/cloud_powers/helper.rb', line 134 def (opts = {}) # TODO: find better implementation of merging nested hashes # this should be fixed with Job #sitrep_message # TODO: find a way to trim this method down and get rid # of a lof of the repitition with these messages # IDEA: throw events and have a separate thread listening. the separate # thread could be a communication or status update thread unless opts.kind_of? Hash update = opts.to_s opts[:extraInfo] = { message: update } end updated_extra_info = opts.delete(:extraInfo) || {} { instanceId: @instance_id || 'none-aquired', type: 'status-update', content: 'running', extraInfo: updated_extra_info }.merge(opts) end |
#valid_json?(json) ⇒ Boolean
155 156 157 158 159 160 161 162 |
# File 'lib/cloud_powers/helper.rb', line 155 def valid_json?(json) begin JSON.parse(json) true rescue Exception => e false end end |