X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli.rb/blobdiff_plain/b8498f5c67d736e9724e124e85aa36a58648afef..143d7aad53cdb6e15f17ef7d1a98981dac7140da:/lib/lyricli/util.rb?ds=sidebyside diff --git a/lib/lyricli/util.rb b/lib/lyricli/util.rb index 9ae9f6c..e7320d0 100644 --- a/lib/lyricli/util.rb +++ b/lib/lyricli/util.rb @@ -1,14 +1,36 @@ module Lyricli + # This module contains several utility functions. module Util + + # Transforms a string from snake_case to UpperCamelCase + # + # @param [String] str the string that will be Camelized + # @return [String] the Camelized string. def camelize(str) str.split('_').map {|w| w.capitalize}.join end + # Takes a class name in snake_case and attempts to find the corresponding + # class from the sources. + # + # @param [String] class_name the snake_case name of the class to search for. + # @return [Class,nil] the found class or nil def parse_class(class_name) - klass = Module.const_get(class_name) - return klass if klass.is_a?(Class) + begin + path = "Sources::#{class_name}" + return eval(path) rescue NameError - return nil + return nil + end + end + + # Simply escapes a param and substitutes spaces and escaped plus signs for + # plus signs. + # + # @param [String] p the parameter to be sanitized + # @return [String] the sanitized parameter + def sanitize_param(p) + URI.encode_www_form_component(p.gsub(/ /, "+")).gsub("%2B", "+") end end end