Module: RemoteResource::Util

Defined in:
lib/remote_resource/util.rb

Constant Summary collapse

FILTERED =
'[FILTERED]'.freeze

Class Method Summary collapse

Class Method Details

.encode_params_to_query(params) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/remote_resource/util.rb', line 14

def self.encode_params_to_query(params)
  if params.is_a?(String)
    pairs = [params]
  else
    pairs = recursively_generate_query(params, nil)
  end

  URI.encode_www_form(pairs)
end

.filter_params(query_string_or_json_body, filtered_params:) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/remote_resource/util.rb', line 6

def self.filter_params(query_string_or_json_body, filtered_params:)
  filtered = query_string_or_json_body
  filtered_params.each do |filtered_param|
    filtered = filtered.to_s.gsub(/(?<="#{filtered_param}":|#{filtered_param}=)(.*?)(?=,|}|&|$)/, FILTERED)
  end
  filtered
end

.recursively_generate_query(component, prefix, pairs = []) ⇒ Object

This method is based on the monkey patched method: Ethon::Easy::Queryable#recursively_generate_pairs

The monkey patch was needed to pass Array params without an index.

The problem is described in typhoeus/typhoeus issue #320: github.com/typhoeus/typhoeus/issues/320

The fix is described in dylanfareed/ethon commit 548033a: github.com/dylanfareed/ethon/commit/548033a8557a48203b7d49f3f98812bd79bc05e4



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/remote_resource/util.rb', line 36

def self.recursively_generate_query(component, prefix, pairs = [])
  case component
  when Hash
    component.each do |key, value|
      key = prefix.nil? ? key : "#{prefix}[#{key}]"

      if value.respond_to?(:each)
        recursively_generate_query(value, key, pairs)
      else
        pairs.push([key, value.to_s])
      end
    end
  when Array
    component.each do |value|
      key = "#{prefix}[]"

      if value.respond_to?(:each)
        recursively_generate_query(value, key, pairs)
      else
        pairs.push([key, value.to_s])
      end
    end
  end

  pairs
end