Module: Carnivore::Http::Utils::Params

Included in:
Source::HttpSource
Defined in:
lib/carnivore-http/utils.rb

Overview

URL parameter helpers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Load cgi library on demand

Parameters:

  • klass (Class)


15
16
17
# File 'lib/carnivore-http/utils.rb', line 15

def included(klass)
  require 'cgi'
end

Instance Method Details

#dump_query_string(hash) ⇒ String

Generate query string

Parameters:

  • hash (Hash)

    request parameters

Returns:

  • (String)


38
39
40
# File 'lib/carnivore-http/utils.rb', line 38

def dump_query_string(hash)
  Rack::Utils.build_nested_query(hash)
end

#format_query_args(args) ⇒ Hash

Cast hash values when possible

Parameters:

  • args (Hash)

Returns:

  • (Hash)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/carnivore-http/utils.rb', line 46

def format_query_args(args)
  new_args = {}
  args.each do |k, v|
    k = k.to_sym
    case v
    when Hash
      new_args[k] = format_query_args(v)
    when Array
      v = v.map{|obj| format_query_type(obj)}
      new_args[k] = v.size == 1 ? v.first : v
    else
      new_args[k] = format_query_type(v)
    end
  end
  new_args.to_smash
end

#format_query_type(obj) ⇒ Object

Best attempt to convert to true type

Parameters:

  • obj (Object)

    generally string value

Returns:

  • (Object)

    result of best attempt



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/carnivore-http/utils.rb', line 67

def format_query_type(obj)
  case obj
  when 'true'
    true
  when 'false'
    false
  else
    if(obj.to_i.to_s == obj)
      obj.to_i
    elsif(obj.to_f.to_s == obj)
      obj.to_f
    else
      obj
    end
  end
end

#parse_query_string(string) ⇒ Hash

Generate hash of parsed query String

Parameters:

  • string (String)

    HTTP query string

Returns:

  • (Hash)


25
26
27
28
29
30
31
32
# File 'lib/carnivore-http/utils.rb', line 25

def parse_query_string(string)
  unless(string.to_s.empty?)
    args = Rack::Utils.parse_nested_query(string)
    format_query_args(args)
  else
    Smash.new
  end
end