Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parsing a Gemfile
def self.create_from_gemfile_url ( url )
return nil if url.nil?
if url.match(/^https:\/\/github.com\//)
url = url.gsub("https://github.com", "https://raw.github.com")
url = url.gsub("/blob/", "/")
end
uri = URI.parse( url )
http = Net::HTTP.new(uri.host, uri.port)
if uri.port == 443
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(uri.request_uri)
gemfile = http.request(request).body
return nil if gemfile.nil?
project = Project.new
project.dependencies = Array.new
gemfile.each_line do |line|
# if it starts not with gem skip the line
line = line.strip
if line.match(/^gem/).nil?
next
end
line = line.gsub("gem ", "")
line_elements = line.split(",")
package = line_elements[0].strip
package = package.gsub('"', '')
package = package.gsub("'", "")
dependency = Projectdependency.new
dependency.name = package
product = Product.find_by_key(package)
if !product.nil?
dependency.prod_key = product.prod_key
end
update_version_from_gemfile(line_elements, dependency, product)
dependency.update_outdated
if dependency.outdated?
project.out_number = project.out_number + 1
end
project.dependencies << dependency
end
project.dep_number = project.dependencies.count
project
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment