Class: Metric::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/metric/util.rb

Class Method Summary collapse

Class Method Details

.build_query_string(object, prefix = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/metric/util.rb', line 7

def self.build_query_string(object, prefix = nil)
  string = []
  object.each_pair do |key, value|
    k = prefix ? "#{prefix}[#{key}]" : key
    next if value.nil?
    if value.is_a?(Hash)
      string << build_query_string(value, k)
    else
      string << "#{escape(k)}=#{escape(value.to_s)}"
    end
  end
  string.join("&")
end

.escape(string) ⇒ Object



3
4
5
# File 'lib/metric/util.rb', line 3

def self.escape(string)
  URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end