Module: WebScrapingAI::QueryEncoder

Defined in:
lib/webscraping_ai/query_encoder.rb

Overview

Faraday-compatible params encoder that follows the conventions used by the WebScraping.AI OpenAPI spec:

* Hash values are encoded as deepObject/explode: `key[subkey]=value`
(used for `headers` and `fields`).
* Array values are encoded as form/explode: `key=v1&key=v2`
(used for `selectors`).
* Booleans serialize as the strings `"true"`/`"false"`.
* `nil` values are dropped entirely (both top level and inside hashes).

Class Method Summary collapse

Class Method Details

.decode(query) ⇒ Object



30
31
32
33
34
# File 'lib/webscraping_ai/query_encoder.rb', line 30

def decode(query)
  return nil if query.nil?

  URI.decode_www_form(query)
end

.encode(params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/webscraping_ai/query_encoder.rb', line 17

def encode(params)
  return nil if params.nil?
  return "" if params.empty?

  pairs = []
  params.each do |key, value|
    next if value.nil?

    encode_pair(key.to_s, value, pairs)
  end
  pairs.join("&")
end