Class: IesAuth::Rest::Options
- Inherits:
-
Object
- Object
- IesAuth::Rest::Options
- Defined in:
- lib/ies_auth/rest/options.rb
Overview
A url options builder class for outgoing requests.
Instance Method Summary collapse
-
#+(other) ⇒ Options
Add the values of one ‘Options` into another.
-
#build(args = {}) ⇒ Options
Add or update url options.
-
#fetch(key) ⇒ String
Fetch the ‘key=value`.
-
#fetch!(key, &block) ⇒ String
Fetch and remove ‘key=value`.
-
#immutable {|Options| ... } ⇒ Object
Execute a block of code and restore original ‘Options` state afterwards.
-
#initialize(args = {}) ⇒ Options
constructor
A new instance of Options.
-
#opts ⇒ String
url safe rendering of options for the url.
-
#remove(key) ⇒ String?
Remove key/value from options via key.
-
#reset! ⇒ Options
this purges all options.
-
#to_h ⇒ Hash
Hash of the ‘Options`.
-
#to_s ⇒ String
url safe rendering of options for the url.
Constructor Details
#initialize(args = {}) ⇒ Options
Returns a new instance of Options.
8 9 10 |
# File 'lib/ies_auth/rest/options.rb', line 8 def initialize(args = {}) build(args) end |
Instance Method Details
#+(other) ⇒ Options
Add the values of one ‘Options` into another
99 100 101 102 103 104 105 |
# File 'lib/ies_auth/rest/options.rb', line 99 def +(other) raise TypeError, "Options type expected, #{other.class} given" unless other.is_a? Options # rubocop:disable Metrics/LineLength update other.instance_variable_get(:@opts) self end |
#build(args = {}) ⇒ Options
Add or update url options
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ies_auth/rest/options.rb', line 26 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`
41 42 43 44 45 46 47 48 49 |
# File 'lib/ies_auth/rest/options.rb', line 41 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`.
55 56 57 58 59 |
# File 'lib/ies_auth/rest/options.rb', line 55 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
63 64 65 66 67 68 |
# File 'lib/ies_auth/rest/options.rb', line 63 def immutable old = @opts result = yield self @opts = old result end |
#opts ⇒ String
url safe rendering of options for the url
15 16 17 18 19 20 21 |
# File 'lib/ies_auth/rest/options.rb', line 15 def opts if @opts.empty? '' else "?#{@opts.join('&')}" end end |
#remove(key) ⇒ String?
Remove key/value from options via key
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ies_auth/rest/options.rb', line 74 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
89 90 91 92 93 |
# File 'lib/ies_auth/rest/options.rb', line 89 def reset! @opts = [] self end |
#to_h ⇒ Hash
Returns hash of the ‘Options`.
113 114 115 |
# File 'lib/ies_auth/rest/options.rb', line 113 def to_h @opts.map(&split).to_h end |
#to_s ⇒ String
url safe rendering of options for the url
108 109 110 |
# File 'lib/ies_auth/rest/options.rb', line 108 def to_s opts end |