Module: WebFunction::Utils Private

Defined in:
lib/web_function/utils.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Internal utility methods.

Class Method Summary collapse

Class Method Details

.add_query_params(url, params = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/web_function/utils.rb', line 54

def add_query_params(url, params = {})
  uri = ::URI.parse(url)
  existing_params = ::URI.decode_www_form(uri.query || "").to_h
  new_params = params.reject { |_, value| value.nil? }.transform_keys(&:to_s)
  merged_params = existing_params.merge(new_params)

  unless merged_params.empty?
    uri.query = ::URI.encode_www_form(merged_params)
  end

  uri.to_s
end

.get_body_from_url(url, extra_query_params: {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
47
48
49
50
51
52
# File 'lib/web_function/utils.rb', line 44

def get_body_from_url(url, extra_query_params: {})
  url = add_query_params(url, extra_query_params)
  response = ::Excon.get(url, headers: {
    "User-Agent": "webfunction/#{::WebFunction::VERSION}",
    "Accept-Encoding": "gzip",
  })

  response.body
end

.normalize_array(collection) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Normalizes a collection. Yields each item to the block if a block is given, otherwise returns the item. Any nil items are removed.

Parameters:

  • collection (Array)

    The collection to normalize

Returns:

  • (Array)

    The normalized array



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

def normalize_array(collection)
  unless collection.is_a?(Array)
    return []
  end

  items = collection.map do |item|
    if block_given?
      yield item
    else
      item
    end
  end

  items.compact
end

.normalize_array_of_strings(collection) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Normalizes an array of strings. Uses #normalize_array under the hood.

Parameters:

  • collection (Array)

    The collection to normalize

Returns:

  • (Array)

    The normalized array



40
41
42
# File 'lib/web_function/utils.rb', line 40

def normalize_array_of_strings(collection)
  normalize_array(collection) { |item| item.to_s }
end