module Textmagic::REST::Utils

Public Instance Methods

key_map(s, method, capitalize = true) click to toggle source
   # File lib/textmagic-ruby/rest/utils.rb
40 def key_map(s, method, capitalize = true)
41   s = s.to_a.flat_map do |pair|
42     [send(method, pair[0], capitalize).to_sym, pair[1]]
43   end
44   Hash[*s]
45 end
resource(client, *resources) click to toggle source
   # File lib/textmagic-ruby/rest/utils.rb
25 def resource(client, *resources)
26   resources.each do |r|
27     resource = to_camel_case r
28     path = "#{@path}/#{resource}".downcase
29     enclosing_module = if @submodule == nil
30                          Textmagic::REST
31                        else
32                          Textmagic::REST.const_get(@submodule)
33                        end
34     resource_class = enclosing_module.const_get resource
35     instance_variable_set("@#{r}", resource_class.new(path, client))
36   end
37   self.class.instance_eval { attr_reader *resources }
38 end
to_camel_case(o, capitalize = true) click to toggle source
   # File lib/textmagic-ruby/rest/utils.rb
 5 def to_camel_case(o, capitalize = true)
 6   return key_map(o, :to_camel_case, capitalize) if o.is_a? Hash
 7   string = o.to_s
 8   string = string.split('_').map do |s_part|
 9     s_part[0,1].capitalize + s_part[1..-1]
10   end.join
11   unless capitalize
12     return string[0,1].downcase + string[1..-1]
13   else
14     return string
15   end
16 end
to_underscore_case(o, capitalize = false) click to toggle source
   # File lib/textmagic-ruby/rest/utils.rb
18 def to_underscore_case(o, capitalize = false)
19   return key_map(o, :to_underscore_case) if o.is_a? Hash
20   string = o.to_s
21   string = string[0,1].downcase + string[1..-1]
22   string.gsub(/[A-Z][a-z]*/) {|s| "_#{s.downcase}"}
23 end