Skip to content

Commit

Permalink
Factorize HTTP client builder
Browse files Browse the repository at this point in the history
  • Loading branch information
davidstosik committed Nov 16, 2023
1 parent 57578f8 commit 91e4d32
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/smart_todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module SmartTodo
autoload :CLI, "smart_todo/cli"
autoload :Todo, "smart_todo/todo"
autoload :CommentParser, "smart_todo/comment_parser"
autoload :HttpClientBuilder, "smart_todo/http_client_builder"

module Dispatchers
autoload :Base, "smart_todo/dispatchers/base"
Expand Down
14 changes: 2 additions & 12 deletions lib/smart_todo/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,11 @@ def spec_set
end

def rubygems_client
@rubygems_client ||= Net::HTTP.new("rubygems.org", Net::HTTP.https_default_port).tap do |client|
client.use_ssl = true
client.read_timeout = 30
client.ssl_timeout = 15
client.max_retries = 2
end
@rubygems_client ||= HttpClientBuilder.build("rubygems.org")
end

def github_client
@github_client ||= Net::HTTP.new("api.github.com", Net::HTTP.https_default_port).tap do |client|
client.use_ssl = true
client.read_timeout = 30
client.ssl_timeout = 15
client.max_retries = 2
end
@github_client ||= HttpClientBuilder.build("api.github.com")
end

def github_headers(organization, repo)
Expand Down
19 changes: 19 additions & 0 deletions lib/smart_todo/http_client_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "net/http"

module SmartTodo
# @api private
class HttpClientBuilder
class << self
def build(endpoint)
Net::HTTP.new(endpoint, Net::HTTP.https_default_port).tap do |client|
client.use_ssl = true
client.read_timeout = 30
client.ssl_timeout = 15
client.max_retries = 2
end
end
end
end
end
7 changes: 1 addition & 6 deletions lib/smart_todo/slack_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ def initialize(response_body)
# @param slack_token [String]
def initialize(slack_token)
@slack_token = slack_token
@client = Net::HTTP.new("slack.com", Net::HTTP.https_default_port).tap do |client|
client.use_ssl = true
client.read_timeout = 30
client.ssl_timeout = 15
client.max_retries = 2
end
@client = HttpClientBuilder.build("slack.com")
end

# Retrieve the Slack ID of a user from his email
Expand Down
17 changes: 17 additions & 0 deletions test/smart_todo/http_client_builder_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "test_helper"
require "tempfile"

module SmartTodo
class HttpBuilderTest < Minitest::Test
def test_builds_http_client_with_proper_config
client = HttpClientBuilder.build("example.com")

assert_equal("example.com", client.address)
assert_equal(15, client.ssl_timeout)
assert_equal(30, client.read_timeout)
assert_equal(2, client.max_retries)
end
end
end

0 comments on commit 91e4d32

Please sign in to comment.