Skip to content

Commit

Permalink
Drop Faraday dependency and use plain Net::HTTP instead (#22)
Browse files Browse the repository at this point in the history
One less runtime dependency. 🎉
  • Loading branch information
mattbrictson authored Aug 1, 2024
1 parent 983b32e commit 359c040
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
1 change: 0 additions & 1 deletion bundle_update_interactive.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Gem::Specification.new do |spec|
# Runtime dependencies
spec.add_dependency "bundler", "~> 2.0"
spec.add_dependency "bundler-audit", ">= 0.9.1"
spec.add_dependency "faraday", ">= 2.8.0"
spec.add_dependency "pastel", ">= 0.8.0"
spec.add_dependency "tty-prompt", ">= 0.23.1"
spec.add_dependency "tty-screen", ">= 0.8.2"
Expand Down
1 change: 1 addition & 0 deletions lib/bundle_update_interactive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module BundleUpdateInteractive
autoload :CLI, "bundle_update_interactive/cli"
autoload :Error, "bundle_update_interactive/error"
autoload :Gemfile, "bundle_update_interactive/gemfile"
autoload :HTTP, "bundle_update_interactive/http"
autoload :Lockfile, "bundle_update_interactive/lockfile"
autoload :LockfileEntry, "bundle_update_interactive/lockfile_entry"
autoload :OutdatedGem, "bundle_update_interactive/outdated_gem"
Expand Down
11 changes: 5 additions & 6 deletions lib/bundle_update_interactive/changelog_locator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require "faraday"
require "json"

module BundleUpdateInteractive
Expand Down Expand Up @@ -32,16 +31,16 @@ def discover_changelog_uri(version)
return "https://github.com/#{changelog_path}" if changelog_path

releases_url = "https://github.com/#{path}/releases"
releases_url if Faraday.head("#{releases_url}/tag/v#{version}").success?
releases_url if HTTP.head("#{releases_url}/tag/v#{version}").success?
end

private

def fetch_repo_html(follow_redirect:)
response = Faraday.get("https://github.com/#{path}")
response = HTTP.get("https://github.com/#{path}")

if response.status == 301 && follow_redirect
@path = response.headers["Location"][GITHUB_PATTERN, 1]
if response.code == "301" && follow_redirect
@path = response["Location"][GITHUB_PATTERN, 1]
return fetch_repo_html(follow_redirect: false)
end

Expand Down Expand Up @@ -69,7 +68,7 @@ def fetch_rubygems_data(name, version)
"https://rubygems.org/api/v2/rubygems/#{name}/versions/#{version}.json"
end

response = Faraday.get(api_url)
response = HTTP.get(api_url)

# Try again without the version in case the version does not exist at rubygems for some reason.
# This can happen when using a pre-release Ruby that has a bundled gem newer than the published version.
Expand Down
34 changes: 34 additions & 0 deletions lib/bundle_update_interactive/http.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "net/http"
require "uri"

module BundleUpdateInteractive
module HTTP
module Success
def success?
code.start_with?("2")
end
end

class << self
def get(url)
http(:get, url)
end

def head(url)
http(:head, url)
end

private

def http(method, url_string)
uri = URI(url_string)
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme.end_with?("s")) do |http|
http.public_send(method, uri.request_uri)
end
response.extend(Success)
end
end
end
end

0 comments on commit 359c040

Please sign in to comment.