lib/daigaku/terminal/courses.rb in daigaku-0.3.0 vs lib/daigaku/terminal/courses.rb in daigaku-0.4.0

- old
+ new

@@ -1,16 +1,20 @@ +require 'os' +require 'open-uri' +require 'zip' +require_relative 'output' + module Daigaku 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 daigaku courses' def list courses = Loading::Courses.load(Daigaku.config.courses_path) say_info courses_list_text(courses) end @@ -20,39 +24,39 @@ def download(url = nil, action = 'downloaded') use_initial_course = url.nil? && options[:github].nil? url = GithubClient.master_zip_url(Daigaku.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 = Daigaku.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 } 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 @@ -122,11 +126,11 @@ "#{text}\n#{Terminal.text :hint_course_download}" end def store_repo_data(user_and_repo) - parts = (user_and_repo ||= Daigaku.config.initial_course).split('/') + parts = (user_and_repo ||= Daigaku.config.initial_course).split('/') author = parts.first course = parts.second course = Course.new(course) QuickStore.store.set(course.key(:author), author) @@ -162,18 +166,17 @@ end def print_course_not_available(course_name) text = [ "The course \"#{course_name}\" is not available in", - "\"#{Daigaku.config.courses_path}\".\n", + "\"#{Daigaku.config.courses_path}\".\n" ] say_warning text.join("\n") unless Loading::Courses.load(Daigaku.config.courses_path).empty? Terminal::Courses.new.list end end end - end end