Skip to content

Commit f728e55

Browse files
committed
Add gem details fetching
1 parent 8cb3145 commit f728e55

File tree

3 files changed

+160
-37
lines changed

3 files changed

+160
-37
lines changed

lib/dependency_parser/ruby/parse.rb

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,58 @@
55
module DependencyParser
66
module Ruby
77
class Parse
8+
attr_reader :errors
9+
810
def initialize(content)
911
@content = content
12+
@errors = []
13+
@direct = []
1014
end
1115

1216
def call
13-
deps = Bundler::LockfileParser.new(content)
14-
parsed = deps.specs.map do |spec|
15-
{ repos: [{ name: spec.name }], language: "ruby" }
16-
end
17-
JSON.pretty_generate(parsed)
17+
validate_lockfile!(content)
18+
return if error?
19+
20+
@direct = Bundler::LockfileParser.new(content).specs.map do |spec|
21+
fetch_spec(name: spec.name, version: spec.version)
22+
end.compact
23+
end
24+
25+
def direct
26+
{ repos: @direct, language: "ruby" }
27+
end
28+
29+
def success?
30+
errors.empty?
31+
end
32+
33+
def error?
34+
errors.any?
1835
end
1936

2037
private
2138

2239
attr_reader :content
2340

24-
def self.valid_gemfile?(content)
25-
Bundler::LockfileParser.new(content).specs.any?
41+
def validate_lockfile!(content)
42+
@errors << 'No specs found' unless Bundler::LockfileParser.new(content).specs.any?
43+
end
44+
45+
def fetch_spec(name:, version: nil)
46+
full_spec = fetcher.fetch_spec([name, version])
47+
{ name: full_spec.name, url: extract_url(full_spec), description: full_spec.description }
2648
rescue
27-
false
49+
@errors << "Invalid spec #{name}"
50+
nil
51+
end
52+
53+
def extract_url(full_spec)
54+
url = full_spec.metadata["source_code_uri"] || full_spec.homepage
55+
url if url.include? "https://github.com/"
56+
end
57+
58+
def fetcher
59+
@fetcher ||= Bundler::Fetcher.new(Bundler::Source::Rubygems::Remote.new('https://rubygems.org'))
2860
end
2961
end
3062
end

test/dependency_parsers/ruby_parser_test.rb

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,55 @@
33
require 'test_helper'
44
require_relative '../../lib/dependency_parser/ruby/parse'
55

6-
class UserMailerTest < ActiveSupport::TestCase
6+
class RubyParserTest < ActiveSupport::TestCase
77
test "returns nothing on invalid Gemfile.lock" do
8-
deps = DependencyParser::Ruby::Parse.new("invalid").call
9-
assert_equal [], JSON.parse(deps)
8+
parser = DependencyParser::Ruby::Parse.new("invalid")
9+
parser.call
10+
refute parser.success?
11+
assert_equal({ repos: [], language: "ruby" }, parser.direct)
1012
end
1113

1214
test "returns the list of gems for a small Gemfle" do
13-
gemfile_lock = <<-LOCKFILE
14-
GEM
15-
remote: https://rubygems.org/
16-
specs:
17-
actioncable (6.1.4.1)
18-
actionpack (= 6.1.4.1)
19-
activesupport (= 6.1.4.1)
20-
nio4r (~> 2.0)
21-
websocket-driver (>= 0.6.1)
22-
23-
PLATFORMS
24-
ruby
25-
26-
DEPENDENCIES
27-
actioncable
28-
29-
RUBY VERSION
30-
ruby 3.0.4p208
31-
32-
BUNDLED WITH
33-
2.3.21
34-
35-
LOCKFILE
36-
37-
deps = DependencyParser::Ruby::Parse.new(gemfile_lock).call
38-
assert_equal [{ "repos" => [{ "name" => "actioncable" }], "language" => "ruby" }], JSON.parse(deps)
15+
gemfile_lock = <<~LOCKFILE
16+
GEM
17+
remote: https://rubygems.org/
18+
specs:
19+
solid_use_case (2.2.0)
20+
actionpack (= 6.1.4.1)
21+
activesupport (= 6.1.4.1)
22+
nio4r (~> 2.0)
23+
websocket-driver (>= 0.6.1)
24+
25+
PLATFORMS
26+
ruby
27+
28+
DEPENDENCIES
29+
actioncable
30+
31+
RUBY VERSION
32+
ruby 3.0.4p208
33+
34+
BUNDLED WITH
35+
2.3.21
36+
37+
LOCKFILE
38+
39+
parser = DependencyParser::Ruby::Parse.new(gemfile_lock)
40+
VCR.use_cassette("dependency_parser/rubygems") do
41+
parser.call
42+
end
43+
44+
gem_data = {
45+
repos:
46+
[{
47+
name: "solid_use_case",
48+
url: "https://github.com/mindeavor/solid_use_case",
49+
description: "Create use cases the way they were meant to be. Easily verify inputs at each step and seamlessly fail with custom error data and convenient pattern matching."
50+
}],
51+
language: "ruby"
52+
}
53+
54+
assert parser.success?
55+
assert_equal gem_data, parser.direct
3956
end
4057
end

test/vcr_cassettes/dependency_parser/rubygems.yml

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)