Module: ChalkRuby::Helpers
- Defined in:
- lib/chalk_ruby/helpers.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#get_option(hash:, key:) ⇒ Object
Retrieve the given value associated with a key, in string or symbol format.
-
#json_to_hash(json:, symbolize_keys:) ⇒ Object
Convert a json object to an hash.
-
#path_encode(path, *args) ⇒ Object
Build a path with the given arguments.
-
#symbolize_hash(hash:) ⇒ Object
Converts each key of a hash to symbols.
-
#to_json(body:) ⇒ Object
Convert an Hash to json.
Class Method Details
.included(base) ⇒ Object
40 41 42 |
# File 'lib/chalk_ruby/helpers.rb', line 40 def self.included(base) base.extend(Helpers) end |
Instance Method Details
#get_option(hash:, key:) ⇒ Object
Retrieve the given value associated with a key, in string or symbol format
25 26 27 |
# File 'lib/chalk_ruby/helpers.rb', line 25 def get_option(hash:, key:) hash[key.to_sym] || hash[key] || nil end |
#json_to_hash(json:, symbolize_keys:) ⇒ Object
Convert a json object to an hash
19 20 21 |
# File 'lib/chalk_ruby/helpers.rb', line 19 def json_to_hash(json:, symbolize_keys:) MultiJson.load(json, symbolize_keys: symbolize_keys) end |
#path_encode(path, *args) ⇒ Object
Build a path with the given arguments
31 32 33 34 35 36 37 38 |
# File 'lib/chalk_ruby/helpers.rb', line 31 def path_encode(path, *args) arguments = [] args.each do |arg| arguments.push(CGI.escape(arg.to_s)) end format(path, *arguments) end |
#symbolize_hash(hash:) ⇒ Object
Converts each key of a hash to symbols
13 14 15 |
# File 'lib/chalk_ruby/helpers.rb', line 13 def symbolize_hash(hash:) hash.each_with_object({}) { |(k, v), h| h[k.to_sym] = v } end |
#to_json(body:) ⇒ Object
Convert an Hash to json
7 8 9 |
# File 'lib/chalk_ruby/helpers.rb', line 7 def to_json(body:) body.is_a?(String) ? body : MultiJson.dump(body) end |