This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
forked from Vonage/vonage-ruby-sdk
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This makes it possible to configure the underlying net/http connections for things like timeouts, proxies, and SSL certificate settings.
- Loading branch information
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
require 'net/http' | ||
|
||
module Nexmo | ||
module HTTP # :nodoc: | ||
class Options | ||
def initialize(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 | ||
|
||
def set(http) | ||
@hash.each do |name, value| | ||
http.public_send(defined_options.fetch(name), value) | ||
end | ||
end | ||
|
||
private | ||
|
||
def defined_options | ||
@defined_options ||= Net::HTTP.instance_methods.grep(/\w=\z/).each_with_object({}) do |name, hash| | ||
hash[name.to_s.chomp('=').to_sym] = name | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'minitest/autorun' | ||
require 'nexmo/http' | ||
|
||
class NexmoHTTPOptionsTest < Minitest::Test | ||
def read_timeout | ||
5 | ||
end | ||
|
||
def proxy_address | ||
'localhost' | ||
end | ||
|
||
def ca_file | ||
'cert.pem' | ||
end | ||
|
||
def test_invalid_option_raises_argument_error | ||
exception = assert_raises(ArgumentError) { Nexmo::HTTP::Options.new(foo_timeout: 1) } | ||
|
||
assert_includes exception.message, ':foo_timeout is not a valid option' | ||
end | ||
|
||
def test_set_method | ||
http = Net::HTTP.new('example.com', Net::HTTP.https_default_port, p_addr = nil) | ||
|
||
hash = {read_timeout: read_timeout, proxy_address: proxy_address, ca_file: ca_file} | ||
|
||
options = Nexmo::HTTP::Options.new(hash) | ||
options.set(http) | ||
|
||
assert_equal read_timeout, http.read_timeout | ||
assert_equal proxy_address, http.proxy_address | ||
assert_equal ca_file, http.ca_file | ||
end | ||
end |