Module: Hyperb::Utils
- Included in:
- Compose, Container, Containers, Func, Funcs, HostConfig, Images, Network, Services, Snapshots, Volume, Volumes
- Defined in:
- lib/hyperb/utils.rb
Overview
utils functions
Instance Method Summary collapse
-
#camelize(value) ⇒ Object
converts from Symbol or String to CamelCase.
-
#check_arguments(params, *args) ⇒ Boolean
checks if all args are keys into the hash.
-
#downcase_symbolize(obj) ⇒ Object
hyper.sh responses are capital camel cased json, ie:.
-
#prepare_json(params = {}) ⇒ Object
prepares all json payloads before sending to hyper.
- #underscore(word) ⇒ Object
Instance Method Details
#camelize(value) ⇒ Object
converts from Symbol or String to CamelCase
6 7 8 |
# File 'lib/hyperb/utils.rb', line 6 def camelize(value) value.to_s.split('_').collect(&:capitalize).join end |
#check_arguments(params, *args) ⇒ Boolean
checks if all args are keys into the hash
16 17 18 19 20 21 22 |
# File 'lib/hyperb/utils.rb', line 16 def check_arguments(params, *args) contains = true args.each do |arg| contains = false unless params.key? arg.to_sym end contains end |
#downcase_symbolize(obj) ⇒ Object
hyper.sh responses are capital camel cased json, ie:
“value”
this fn is used to format all hyper.sh api reponses
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/hyperb/utils.rb', line 29 def downcase_symbolize(obj) if obj.is_a? Hash return obj.each_with_object({}) do |(k, v), memo| memo.tap { |m| m[underscore(k)] = downcase_symbolize(v) } end end if obj.is_a? Array return obj.each_with_object([]) do |v, memo| memo << downcase_symbolize(v) memo end end obj end |
#prepare_json(params = {}) ⇒ Object
prepares all json payloads before sending to hyper
input: { foo_bar: ‘test’ } output: {‘FooBar’: ‘test’ }
49 50 51 52 53 54 55 56 |
# File 'lib/hyperb/utils.rb', line 49 def prepare_json(params = {}) json = {} params.each do |key, value| value = prepare_json(value) if value.is_a?(Hash) json[camelize(key)] = value end json end |
#underscore(word) ⇒ Object
based on api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore
underscores and symbolize a string
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hyperb/utils.rb', line 63 def underscore(word) word .to_s .gsub(/([A-Z]+)([A-Z]+)([A-Z][a-z])/, '\12_\3') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase .to_sym end |