Class: Emailvision::Tools
- Inherits:
-
Object
- Object
- Emailvision::Tools
- Defined in:
- lib/emailvision/tools.rb
Overview
Toolbox for the API This class is mainly used to convert data
Class Method Summary collapse
- .date_format(date) ⇒ Object
- .date_time_format(datetime) ⇒ Object
-
.r_camelize(obj) ⇒ Object
Convert hash keys to camel case.
-
.r_each(hash) { ... } ⇒ Object
Iterate throught a Hash recursively.
-
.sanitize_parameters(parameters) ⇒ Hash
Sanitize values from a Hash.
-
.to_xml_as_is(obj) ⇒ String
Convert data structure to XML.
Class Method Details
.date_format(date) ⇒ Object
29 30 31 |
# File 'lib/emailvision/tools.rb', line 29 def self.date_format(date) date.strftime('%d/%m/%Y') end |
.date_time_format(datetime) ⇒ Object
25 26 27 |
# File 'lib/emailvision/tools.rb', line 25 def self.date_time_format(datetime) datetime.strftime("%Y-%m-%dT%H:%M:%S") end |
.r_camelize(obj) ⇒ Object
Convert hash keys to camel case
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/emailvision/tools.rb', line 38 def self.r_camelize(obj) if obj.is_a?(Hash) new_obj = {} obj.each do |key, value| new_obj[key.to_s.camelize(:lower).to_sym] = r_camelize value end new_obj elsif obj.is_a?(Array) new_obj = [] obj.each_with_index do |item, index| new_obj[index] = r_camelize item end new_obj else obj end end |
.r_each(hash) { ... } ⇒ Object
Iterate throught a Hash recursively
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/emailvision/tools.rb', line 78 def self.r_each(hash, &block) return enum_for(:dfs, hash) unless block result = {} if hash.is_a?(Hash) hash.map do |k,v| result[k] = if v.is_a? Array v.map do |elm| r_each(elm, &block) end elsif v.is_a? Hash r_each(v, &block) else yield(v) end end else result = yield(hash) end result end |
.sanitize_parameters(parameters) ⇒ Hash
Sanitize values from a Hash
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/emailvision/tools.rb', line 13 def self.sanitize_parameters(parameters) r_each(parameters) do |value| if value.kind_of?(DateTime) or value.kind_of?(Time) date_time_format(value.to_datetime) elsif value.kind_of?(Date) date_format(value.to_date) else value end end end |
.to_xml_as_is(obj) ⇒ String
Convert data structure to XML
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/emailvision/tools.rb', line 61 def self.to_xml_as_is(obj) obj_xml = "" unless obj.nil? or obj.empty? xml = ::Builder::XmlMarkup.new(:target=> obj_xml) xml.instruct! :xml, :version=> "1.0" tag_obj xml, obj end obj_xml end |