<commit_before>require 'gemnasium/parser'

module Bibliothecary
  class Rubygems
    NAME_VERSION = '(?! )(.*?)(?: \(([^-]*)(?:-(.*))?\))?'.freeze
    NAME_VERSION_4 = /^ {4}#{NAME_VERSION}$/

    def self.analyse(folder_path, file_list)
      [
        analyse_gemfile(folder_path, file_list),
        analyse_gemspec(folder_path, file_list),
        analyse_gemfile_lock(folder_path, file_list)
      ]
    end

    def self.analyse_gemfile(folder_path, file_list)
      path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Gemfile$|^gems\.rb$/) }
      return unless path

      manifest = Gemnasium::Parser.send(:gemfile, File.open(path).read)

      {
        platform: 'Rubygems',
        path: path,
        dependencies: parse_manifest(manifest)
      }
    end

    def self.analyse_gemspec(folder_path, file_list)
      path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^[A-Za-z0-9_-]+\.gemspec$/) }
      return unless path

      manifest = Gemnasium::Parser.send(:gemspec, File.open(path).read)

      {
        platform: 'Rubygems',
        path: path,
        dependencies: parse_manifest(manifest)
      }
    end

    def self.analyse_gemfile_lock(folder_path, file_list)
      path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Gemfile\.lock$|^gems\.locked$/) }
      return unless path

      manifest = File.open(path).read

      {
        platform: 'Rubygems',
        path: path,
        dependencies: parse_gemfile_lock(manifest)
      }
    end

    def self.parse_gemfile_lock(manifest)
      manifest.split('\n').map do |line|
        match = line.match(NAME_VERSION_4)
        next unless match
        name = match[1]
        version = match[2].gsub(/\(|\)/,'')
        {
          name: name,
          requirement: version,
          type: 'runtime'
        }
      end.compact
    end

    def self.parse_manifest(manifest)
      manifest.dependencies.inject([]) do |deps, dep|
        deps.push({
          name: dep.name,
          requirement: dep.requirement.to_s,<commit_msg>Fix Gemfile.lock parser<commit_after>require 'gemnasium/parser'

module Bibliothecary
  class Rubygems
    NAME_VERSION = '(?! )(.*?)(?: \(([^-]*)(?:-(.*))?\))?'.freeze
    NAME_VERSION_4 = /^ {4}#{NAME_VERSION}$/

    def self.analyse(folder_path, file_list)
      [
        analyse_gemfile(folder_path, file_list),
        analyse_gemspec(folder_path, file_list),
        analyse_gemfile_lock(folder_path, file_list)
      ]
    end

    def self.analyse_gemfile(folder_path, file_list)
      path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Gemfile$|^gems\.rb$/) }
      return unless path

      manifest = Gemnasium::Parser.send(:gemfile, File.open(path).read)

      {
        platform: 'Rubygems',
        path: path,
        dependencies: parse_manifest(manifest)
      }
    end

    def self.analyse_gemspec(folder_path, file_list)
      path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^[A-Za-z0-9_-]+\.gemspec$/) }
      return unless path

      manifest = Gemnasium::Parser.send(:gemspec, File.open(path).read)

      {
        platform: 'Rubygems',
        path: path,
        dependencies: parse_manifest(manifest)
      }
    end

    def self.analyse_gemfile_lock(folder_path, file_list)
      path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Gemfile\.lock$|^gems\.locked$/) }
      return unless path

      manifest = File.open(path).read

      {
        platform: 'Rubygems',
        path: path,
        dependencies: parse_gemfile_lock(manifest)
      }
    end

    def self.parse_gemfile_lock(manifest)
      manifest.split("\n").map do |line|
        match = line.match(NAME_VERSION_4)
        next unless match
        name = match[1]
        version = match[2].gsub(/\(|\)/,'')
        {
          name: name,
          requirement: version,
          type: 'runtime'
        }
      end.compact
    end

    def self.parse_manifest(manifest)
      manifest.dependencies.inject([]) do |deps, dep|
        deps.push({
          name: dep.name,
          requirement: dep.requirement.to_s,
{'fraction_non_alphanumeric': 0.1543767840152236, 'fraction_numerical': 0.003330161750713606, 'mean_word_length': 2.4242671009771986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4306494', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1404, 'n_words': 231}