Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Add http options
Browse files Browse the repository at this point in the history
This makes it possible to configure the underlying net/http connections
for things like timeouts, proxies, and SSL certificate settings.
  • Loading branch information
timcraft committed Nov 22, 2018
1 parent 0e18b29 commit 192c251
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/nexmo.rb
@@ -1,6 +1,7 @@
require 'nexmo/version'
require 'nexmo/params'
require 'nexmo/form_data'
require 'nexmo/http'
require 'nexmo/json'
require 'nexmo/jwt'
require 'nexmo/signature'
Expand Down
8 changes: 8 additions & 0 deletions lib/nexmo/client.rb
Expand Up @@ -25,9 +25,17 @@ def initialize(options = {})

@user_agent = UserAgent.string(options[:app_name], options[:app_version])

self.http_options = options[:http]

self.logger = options[:logger] || (defined?(Rails.logger) && Rails.logger)
end

attr_reader :http_options

def http_options=(hash)
@http_options = HTTP::Options.new(hash)
end

def logger
@logger
end
Expand Down
32 changes: 32 additions & 0 deletions lib/nexmo/http.rb
@@ -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
4 changes: 3 additions & 1 deletion lib/nexmo/namespace.rb
Expand Up @@ -11,8 +11,10 @@ def initialize(client)

@host = self.class.host

@http = Net::HTTP.new(@host, Net::HTTP.https_default_port)
@http = Net::HTTP.new(@host, Net::HTTP.https_default_port, p_addr = nil)
@http.use_ssl = true

@client.http_options.set(@http)
end

def self.host
Expand Down
35 changes: 35 additions & 0 deletions test/nexmo/http_options_test.rb
@@ -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

0 comments on commit 192c251

Please sign in to comment.