Module: Dagger::Utils

Defined in:
lib/dagger.rb

Class Method Summary collapse

Class Method Details

.append_key(root_key, key) ⇒ Object



37
38
39
# File 'lib/dagger.rb', line 37

def self.append_key(root_key, key)
  root_key.nil? ? key : "#{root_key}[#{key.to_s}]"
end

.encode(value, key = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/dagger.rb', line 27

def self.encode(value, key = nil)
  case value
  when Hash  then value.map { |k,v| encode(v, append_key(key,k)) }.join('&')
  when Array then value.map { |v| encode(v, "#{key}[]") }.join('&')
  when nil   then ''
  else
    "#{key}=#{URI.escape(value.to_s)}"
  end
end

.parse_uri(uri) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
# File 'lib/dagger.rb', line 19

def self.parse_uri(uri)
  uri = 'http://' + uri unless uri.to_s['http']
  uri = URI.parse(uri)
  raise ArgumentError.new("Invalid URI: #{uri}") unless uri.is_a?(URI::HTTP)
  uri.path = '/' if uri.path == ''
  uri
end