class KisHttp::Options

A url options builder class for outgoing requests.

Public Class Methods

new(args = {}) click to toggle source
# File lib/kis_http/options.rb, line 7
def initialize(args = {})
  build(args)
end

Public Instance Methods

+(other) click to toggle source

Add the values of one `Options` into another

@param other [Options] instance of `Options` @return [Options]

# File lib/kis_http/options.rb, line 98
def +(other)
  unless other.is_a? Options
    raise TypeError, "Options type expected, #{other.class} given"
  end

  update other.instance_variable_get(:@opts)

  self
end
build(args = {}) click to toggle source

Add or update url options

@return [Options] self

# File lib/kis_http/options.rb, line 25
def build(args = {})
  @opts ||= []

  args.to_h.each do |(key, value)|
    remove(key)
    @opts.push("#{key}=#{value}")
  end

  self
end
fetch(key) { || ... } click to toggle source

Fetch the `key=value`

@param [Symbol, String] key of the key/value pair to fetch @return [String]

# File lib/kis_http/options.rb, line 40
def fetch(key)
  @opts.each do |item|
    return item if key.to_s == split.call(item).first
  end

  raise KeyError, "key not found #{key}" unless block_given?

  yield
end
fetch!(key, &block) click to toggle source

Fetch and remove `key=value`. Modifies `Options`.

@param [Symbol, String] key of the key/value pair to fetch @return [String]

# File lib/kis_http/options.rb, line 54
def fetch!(key, &block)
  result = fetch(key, &block)
  remove(key)
  result
end
immutable() { |self| ... } click to toggle source

Execute a block of code and restore original `Options` state afterwards @yield [Options]

# File lib/kis_http/options.rb, line 62
def immutable
  old = @opts
  result = yield self
  @opts = old
  result
end
opts() click to toggle source

url safe rendering of options for the url

@return [String] url options

# File lib/kis_http/options.rb, line 14
def opts
  if @opts.empty?
    ''
  else
    "?#{@opts.join('&')}"
  end
end
remove(key) click to toggle source

Remove key/value from options via key

@param key [Symbol, String] key to look up @return [String, nil] returns a `String` if key found, `nil` otherwise.

# File lib/kis_http/options.rb, line 73
def remove(key)
  return_value = nil

  @opts = @opts.delete_if do |item|
    head, tail = split.call item

    return_value = tail if head == key.to_s
  end

  return_value
end
reset!() click to toggle source

this purges all options

@return [Options] self

# File lib/kis_http/options.rb, line 88
def reset!
  @opts = []

  self
end
to_h() click to toggle source

@return [Hash] hash of the `Options`

# File lib/kis_http/options.rb, line 114
def to_h
  @opts.map(&split).to_h
end
to_s() click to toggle source

(see opts)

# File lib/kis_http/options.rb, line 109
def to_s
  opts
end