Class: ClientApiBuilder::QueryParams

Inherits:
Object
  • Object
show all
Defined in:
lib/client_api_builder/query_params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_value_separator: '=', param_separator: '&', custom_escape_proc: nil) ⇒ QueryParams

Returns a new instance of QueryParams.



12
13
14
15
16
# File 'lib/client_api_builder/query_params.rb', line 12

def initialize(name_value_separator: '=', param_separator: '&', custom_escape_proc: nil)
  @name_value_separator = name_value_separator
  @param_separator = param_separator
  @custom_escape_proc = custom_escape_proc
end

Instance Attribute Details

#custom_escape_procObject

Returns the value of attribute custom_escape_proc.



10
11
12
# File 'lib/client_api_builder/query_params.rb', line 10

def custom_escape_proc
  @custom_escape_proc
end

#name_value_separatorObject (readonly)

Returns the value of attribute name_value_separator.



7
8
9
# File 'lib/client_api_builder/query_params.rb', line 7

def name_value_separator
  @name_value_separator
end

#param_separatorObject (readonly)

Returns the value of attribute param_separator.



7
8
9
# File 'lib/client_api_builder/query_params.rb', line 7

def param_separator
  @param_separator
end

Instance Method Details

#escape(str) ⇒ Object

Raises:

  • (TypeError)


70
71
72
73
74
75
76
77
# File 'lib/client_api_builder/query_params.rb', line 70

def escape(str)
  return CGI.escape(str) unless custom_escape_proc

  result = custom_escape_proc.call(str)
  raise TypeError, "custom_escape_proc must return a String, got #{result.class}" unless result.is_a?(String)

  result
end

#to_query(data, namespace = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/client_api_builder/query_params.rb', line 18

def to_query(data, namespace = nil)
  case data
  when Hash
    to_query_from_hash(data, (namespace ? escape(namespace) : nil)).join(param_separator)
  when Array
    to_query_from_array(data, (namespace ? "#{escape(namespace)}[]" : '[]')).join(param_separator)
  else
    if namespace
      "#{escape(namespace)}#{name_value_separator}#{escape(data.to_s)}"
    else
      escape(data.to_s)
    end
  end
end

#to_query_from_array(array, namespace) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/client_api_builder/query_params.rb', line 53

def to_query_from_array(array, namespace)
  query_params = []

  array.each do |value|
    case value
    when Hash
      query_params += to_query_from_hash(value, namespace)
    when Array
      query_params += to_query_from_array(value, "#{namespace}[]")
    else
      query_params << "#{namespace}#{name_value_separator}#{escape(value.to_s)}"
    end
  end

  query_params
end

#to_query_from_hash(hsh, namespace) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/client_api_builder/query_params.rb', line 33

def to_query_from_hash(hsh, namespace)
  query_params = []

  hsh.each do |key, value|
    case value
    when Array
      array_namespace = namespace ? "#{namespace}[#{escape(key.to_s)}][]" : "#{escape(key.to_s)}[]"
      query_params += to_query_from_array(value, array_namespace)
    when Hash
      hash_namespace = namespace ? "#{namespace}[#{escape(key.to_s)}]" : escape(key.to_s).to_s
      query_params += to_query_from_hash(value, hash_namespace)
    else
      query_name = namespace ? "#{namespace}[#{escape(key.to_s)}]" : escape(key.to_s).to_s
      query_params << "#{query_name}#{name_value_separator}#{escape(value.to_s)}"
    end
  end

  query_params
end