Module: Dagger::Utils

Defined in:
lib/dagger.rb

Class Method Summary collapse

Class Method Details

.append_key(root_key, key) ⇒ Object



63
64
65
# File 'lib/dagger.rb', line 63

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

.encode(obj, key = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dagger.rb', line 49

def self.encode(obj, key = nil)
  if key.nil? && obj.is_a?(String) # && obj['=']
    return obj
  end

  case obj
  when Hash  then obj.map { |k, v| encode(v, append_key(key,k)) }.join('&')
  when Array then obj.map { |v| encode(v, "#{key}[]") }.join('&')
  when nil   then ''
  else
    "#{key}=#{ERB::Util.url_encode(obj.to_s)}"
  end
end

.parse_uri(uri) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
# File 'lib/dagger.rb', line 33

def self.parse_uri(uri)
  raise ArgumentError.new("Empty URI") if uri.to_s.strip == ''
  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

.resolve_uri(uri, host = nil, query = nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/dagger.rb', line 42

def self.resolve_uri(uri, host = nil, query = nil)
  uri = host + uri if uri[0] == '/' && host
  uri = parse_uri(uri.to_s)
  uri.path.sub!(/\?.*|$/, '?' + Utils.encode(query)) if query and query.any?
  uri
end