-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathhttp.rb
43 lines (34 loc) · 1.19 KB
/
http.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# typed: strict
# frozen_string_literal: true
require 'net/http/persistent'
module Vonage
module HTTP
class Options
extend T::Sig
sig { params(hash: T::Hash[Symbol, T.untyped]).void }
def initialize(hash)
raise ArgumentError, 'hash parameter cannot be empty or nil' if hash == {} || hash.nil?
@hash = T.let(@hash, T::Hash[Symbol, T.untyped]) if defined? @hash
@hash = hash
@hash.each_key do |name|
next if defined_options.key?(name)
raise ArgumentError, "#{name.inspect} is not a valid option"
end
end
sig { params(http: Net::HTTP::Persistent).returns(T::Hash[Symbol, T.untyped]) }
def set(http)
@hash.each do |name, value|
http.public_send(defined_options.fetch(name), value)
end
end
private
sig { returns(T::Hash[Symbol, T.untyped]) }
def defined_options
@defined_options = T.let(@defined_options, T.nilable(T::Hash[Symbol, T.untyped]))
@defined_options ||= Net::HTTP::Persistent.instance_methods.grep(/\w=\z/).each_with_object({}) do |name, hash|
hash[name.to_s.chomp('=').to_sym] = name
end
end
end
end
end