Module: SimpleAWS::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/simple_aws/core/util.rb

Overview

Collection of helper methods used in the library

Instance Method Summary collapse

Instance Method Details

#build_xml_from(hash, namespace = nil) ⇒ Object

Take a hash and build a simple XML string from that Hash. This does not support properties on elements, and is meant for request bodies like what CloudFront expects.

The hash body can contain symbols, strings, arrays and hashes



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simple_aws/core/util.rb', line 42

def build_xml_from(hash, namespace = nil)
  doc = Nokogiri::XML::Document.new
  doc.encoding = "UTF-8"

  root_key = hash.keys.first
  root = Nokogiri::XML::Element.new root_key.to_s, doc
  root["xmlns"] = namespace if namespace

  doc << root

  build_body root, hash[root_key]

  doc.to_s
end

#camelcase(string, lower_first_char = false) ⇒ Object

Simpler version of ActiveSupport's camelize



12
13
14
15
16
17
18
19
20
# File 'lib/simple_aws/core/util.rb', line 12

def camelcase(string, lower_first_char = false)
  return string if string =~ /[A-Z]/

  if lower_first_char
    string[0,1].downcase + camelcase(string)[1..-1]
  else
    string.split(/_/).map{ |word| word.capitalize }.join('')
  end
end

#uri_escape(string) ⇒ Object

AWS URI escaping, as implemented by Fog



24
25
26
27
28
29
30
31
32
33
# File 'lib/simple_aws/core/util.rb', line 24

def uri_escape(string)
  # Quick hack for already escaped string, don't escape again
  # I don't think any requests require a % in a parameter, but if
  # there is one I'll need to rethink this
  return string if string =~ /%/

  string.gsub(/([^a-zA-Z0-9_.\-~]+)/) {
    "%" + $1.unpack("H2" * $1.bytesize).join("%").upcase
  }
end