class GemMiner::Miner
Constants
- GEMFILE_REGEX
- GEMSPEC_REGEX
Public Class Methods
gems_for(github_search_query, github_token)
click to toggle source
# File lib/gem-miner/miner.rb, line 14 def self.gems_for(github_search_query, github_token) new(github_search_query, GithubClient.new(github_token)).gems end
new(github_search_query, github_client = GithubClient.new)
click to toggle source
# File lib/gem-miner/miner.rb, line 18 def initialize(github_search_query, github_client = GithubClient.new) @github_search_query = github_search_query @github_client = github_client end
Public Instance Methods
gems()
click to toggle source
A hash of gems and the repositories they are used in:
{
"pry '~> 1'" => ['reevoo/flakes', 'reevoo/gem-miner'], ...
}
# File lib/gem-miner/miner.rb, line 29 def gems @gems ||= collect_gems! end
Private Instance Methods
collect_gems!()
click to toggle source
Collects gem specifications from Gemfiles and Gemspecs. Returns a hash of gems and the repositories they are used in:
{
"pry '~> 1'" => ['reevoo/flakes', 'reevoo/gem-miner'], ...
}
# File lib/gem-miner/miner.rb, line 42 def collect_gems! gemfiles = parse_sources('Gemfile', GEMFILE_REGEX) gemspecs = parse_sources('gemspec', GEMSPEC_REGEX) gems = merge_hashes(gemfiles, gemspecs) # Invert the PROJECT => [GEMS] hash to a GEM => [PROJECTS] hash gems = invert_hash_of_arrays(gems) # Sort it gems = gems.sort.to_h end
extract_deps(file, regex)
click to toggle source
# File lib/gem-miner/miner.rb, line 72 def extract_deps(file, regex) file.gsub(/\"/, '\'').scan(regex).flatten end
invert_hash_of_arrays(hash)
click to toggle source
Converts a hash of the form A => [Bs] to B => [As]
# File lib/gem-miner/miner.rb, line 92 def invert_hash_of_arrays(hash) hash.reduce({}) do |memo, (key, values)| values.each do |v| memo[v] ||= [] memo[v] << key end memo end end
merge_hashes(hash1, hash2)
click to toggle source
Merge two hashes of lists
h1 = { a: [1, 2, 3], b: [2, 4] } h2 = { b: [1, 3], c: [1, 2, 3] } merge_hashes
(h1, h2) # => { a: [1, 2, 3], b: [1, 2, 3, 4], c: [1, 2, 3] }
From stackoverflow.com/questions/11171834
# File lib/gem-miner/miner.rb, line 87 def merge_hashes(hash1, hash2) hash1.merge(hash2) { |key, oldval, newval| (oldval | newval) } end
parse_sources(filename, regex)
click to toggle source
Collects all of the gemfiles for all Reevoo repositories. Returns a dictionary with the gem as the key and an array of repositories as values.
# File lib/gem-miner/miner.rb, line 58 def parse_sources(filename, regex) results = search("filename:#{filename} #{@github_search_query}") log "Parsing #{results.count} #{filename}s" files = results.reduce({}) do |memo, result| # We might have more than one dep file in a repository... memo[result[:name]] ||= [] memo[result[:name]] += extract_deps(result[:content], regex) log '.' memo end log "done!\n" files end
search(query)
click to toggle source
# File lib/gem-miner/miner.rb, line 76 def search(query) @github_client.files(query) end