Class: KisHttp::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/kis_http/options.rb

Overview

A url options builder class for outgoing requests.

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Options

Returns a new instance of Options.



7
8
9
# File 'lib/kis_http/options.rb', line 7

def initialize(args = {})
  build(args)
end

Instance Method Details

#+(other) ⇒ Options

Add the values of one ‘Options` into another

Parameters:

  • other (Options)

    instance of ‘Options`

Returns:



98
99
100
101
102
103
104
105
106
# 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 = {}) ⇒ Options

Add or update url options

Returns:



25
26
27
28
29
30
31
32
33
34
# 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) ⇒ String

Fetch the ‘key=value`

Parameters:

  • key (Symbol, String)

    of the key/value pair to fetch

Returns:

  • (String)

Raises:

  • (KeyError)


40
41
42
43
44
45
46
47
48
# 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) ⇒ String

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

Parameters:

  • key (Symbol, String)

    of the key/value pair to fetch

Returns:

  • (String)


54
55
56
57
58
# File 'lib/kis_http/options.rb', line 54

def fetch!(key, &block)
  result = fetch(key, &block)
  remove(key)
  result
end

#immutable {|Options| ... } ⇒ Object

Execute a block of code and restore original ‘Options` state afterwards

Yields:



62
63
64
65
66
67
# File 'lib/kis_http/options.rb', line 62

def immutable
  old = @opts
  result = yield self
  @opts = old
  result
end

#optsString

url safe rendering of options for the url

Returns:

  • (String)

    url options



14
15
16
17
18
19
20
# File 'lib/kis_http/options.rb', line 14

def opts
  if @opts.empty?
    ''
  else
    "?#{@opts.join('&')}"
  end
end

#remove(key) ⇒ String?

Remove key/value from options via key

Parameters:

  • key (Symbol, String)

    key to look up

Returns:

  • (String, nil)

    returns a ‘String` if key found, `nil` otherwise.



73
74
75
76
77
78
79
80
81
82
83
# 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!Options

this purges all options

Returns:



88
89
90
91
92
# File 'lib/kis_http/options.rb', line 88

def reset!
  @opts = []

  self
end

#to_hHash

Returns hash of the ‘Options`.

Returns:

  • (Hash)

    hash of the ‘Options`



114
115
116
# File 'lib/kis_http/options.rb', line 114

def to_h
  @opts.map(&split).to_h
end

#to_sString

url safe rendering of options for the url

Returns:

  • (String)

    url options



109
110
111
# File 'lib/kis_http/options.rb', line 109

def to_s
  opts
end