lib/belajar/terminal/courses.rb in belajar-0.1.1 vs lib/belajar/terminal/courses.rb in belajar-1.0.0
- old
+ new
@@ -1,16 +1,20 @@
+require 'os'
+require 'open-uri'
+require 'zip'
+require_relative 'output'
+
module Belajar
module Terminal
-
- require 'os'
- require 'open-uri'
- require 'zip'
- require_relative 'output'
-
class Courses < Thor
include Terminal::Output
+ GITHUB = /github\.com/
+ MASTER_ZIP_URL = %r{github.com\/(.*)\/archive\/master.zip}
+ URL = /\A#{URI.regexp(%w(http https))}\z/
+ ZIP_FILE = /\.zip/
+
desc 'list', 'List your available belajar courses'
def list
courses = Loading::Courses.load(Belajar.config.courses_path)
say_info courses_list_text(courses)
end
@@ -20,46 +24,46 @@
def download(url = nil, action = 'downloaded')
use_initial_course = url.nil? && options[:github].nil?
url = GithubClient.master_zip_url(Belajar.config.initial_course) if use_initial_course
url = GithubClient.master_zip_url(options[:github]) if options[:github]
- url_given = (url =~ /\A#{URI::regexp(['http', 'https'])}\z/)
- github = use_initial_course || options[:github] || url.match(/github\.com/)
+ url_given = (url =~ URL)
+ github = use_initial_course || options[:github] || url.match(GITHUB)
raise Download::NoUrlError unless url_given
- raise Download::NoZipFileUrlError unless File.basename(url) =~ /\.zip/
+ raise Download::NoZipFileUrlError unless File.basename(url) =~ ZIP_FILE
courses_path = Belajar.config.courses_path
FileUtils.makedirs(courses_path) unless Dir.exist?(courses_path)
file_name = File.join(courses_path, url.split('/').last)
- File.open(file_name, 'w') { |file| file << open(url).read }
+ File.open(file_name, 'w') { |file| file << URI.open(url).read }
course = Course.unzip(file_name, github_repo: github)
if github
- user_and_repo = url.match(/github.com\/(.*)\/archive\/master.zip/).captures.first
+ user_and_repo = url.match(MASTER_ZIP_URL).captures.first
store_repo_data(options[:github] || user_and_repo)
end
QuickStore.store.set(course.key(:url), url)
QuickStore.store.set(course.key(:updated_at), Time.now.to_s)
scaffold_solutions
say_info "Successfully #{action} the course \"#{course.title}\"!"
- rescue Download::NoUrlError => e
+ rescue Download::NoUrlError
print_download_warning(url, "\"#{url}\" is not a valid URL!")
- rescue Download::NoZipFileUrlError => e
+ rescue Download::NoZipFileUrlError
print_download_warning(url, "\"#{url}\" is not a URL of a *.zip file!")
- rescue Exception => e
+ rescue StandardError => e
print_download_warning(url, e.message)
ensure
FileUtils.rm(file_name) if File.exist?(file_name.to_s)
end
method_option :all, type: :boolean, aliases: '-a', desc: 'Update all courses'
- desc 'update [COURSE_NAME] [OPTIONS]', 'Update Daigak courses.'
+ desc 'update [COURSE_NAME] [OPTIONS]', 'Update Belajar courses.'
def update(course_name = nil)
if options[:all]
courses = Loading::Courses.load(Belajar.config.courses_path)
courses.each { |course| update_course(course) }
elsif course_name
@@ -96,11 +100,11 @@
unless Dir.exist?(path)
print_course_not_available(course_name)
return
end
- get_confirm("Are you shure you want to delete the course \"#{course_name}\"?") do
+ get_confirm("Are you sure you want to delete the course \"#{course_name}\"?") do
FileUtils.remove_dir(path)
QuickStore.store.delete(Storeable.key(course_name, prefix: 'courses'))
say_info "The course \"#{course_name}\" was successfully deleted."
end
else
@@ -122,13 +126,13 @@
"#{text}\n#{Terminal.text :hint_course_download}"
end
def store_repo_data(user_and_repo)
- parts = (user_and_repo ||= Belajar.config.initial_course).split('/')
- author = parts.first
- course = parts.second
+ parts = (user_and_repo ||= Belajar.config.initial_course).split('/')
+ author = parts[0]
+ course = parts[1]
course = Course.new(course)
QuickStore.store.set(course.key(:author), author)
QuickStore.store.set(course.key(:github), user_and_repo)
end
@@ -162,18 +166,17 @@
end
def print_course_not_available(course_name)
text = [
"The course \"#{course_name}\" is not available in",
- "\"#{Belajar.config.courses_path}\".\n",
+ "\"#{Belajar.config.courses_path}\".\n"
]
say_warning text.join("\n")
unless Loading::Courses.load(Belajar.config.courses_path).empty?
Terminal::Courses.new.list
end
end
end
-
end
end