Class: Ignorify::Utils
- Inherits:
-
Object
- Object
- Ignorify::Utils
- Defined in:
- lib/util.rb
Overview
Utility class. Generates file list and downloads gitignores.
Constant Summary collapse
- REPOSITORY_URL =
"https://github.com/github/gitignore"- REPOSITORY_RAW_URL =
"https://raw.githubusercontent.com/github/gitignore/master/"- FILENAME =
".gitignore"
Class Method Summary collapse
-
.create_file(name) ⇒ Object
Grabs the latest gitignore.
-
.file_list ⇒ Object
Get file list from Github repository.
Class Method Details
.create_file(name) ⇒ Object
Grabs the latest gitignore. Saves .gitignore. We check to see if cURL is installed.
As a fallback, scrape the file. Arguments:
name: (String)
Returns:
Boolean
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/util.rb', line 43 def self.create_file(name) request_url = REPOSITORY_RAW_URL + name + FILENAME system("curl -V > /dev/null") if $? == 0 system("echo Fetching gitignore...") file_created = system("curl --progress -o #{FILENAME} #{request_url}") return file_created else file = Nokogiri::HTML(open(request_url)) file_content = file.css('body p').text File.open(FILENAME, "w") do |file| file.write(file_content) end if File.exist?(FILENAME) && FILENAME.size > 0 return true else return false end end end |
.file_list ⇒ Object
Get file list from Github repository. Crawls the github repository. Generates a hash with lowercase values mapped to crawled values.
Returns:
Hash
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/util.rb', line 20 def self.file_list file_list = {} page = Nokogiri::HTML(open(REPOSITORY_URL)) page.css('td.content span.css-truncate a').each do |tr| if tr.content.end_with?(FILENAME) trimmed_name = tr.content.chomp(FILENAME) file_list[trimmed_name.downcase] = trimmed_name end end return file_list end |