From d7043e233adde038d78a7b3f257052960fc130a0 Mon Sep 17 00:00:00 2001 From: Nemo Date: Sun, 29 Mar 2020 02:44:48 +0530 Subject: [PATCH] Add URL to options --- spec/parser_spec.cr | 13 +++++++++++-- src/errors/missing_link.cr | 4 ++++ src/fetch.cr | 2 +- src/muse-dl.cr | 6 +++++- src/parser.cr | 10 +++++++++- 5 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 src/errors/missing_link.cr diff --git a/spec/parser_spec.cr b/spec/parser_spec.cr index f838323..e49df1a 100644 --- a/spec/parser_spec.cr +++ b/spec/parser_spec.cr @@ -2,7 +2,7 @@ require "./spec_helper" describe Muse::Dl::Parser do it "should parse options" do - parser = Muse::Dl::Parser.new(["--no-bookmarks", "--tmp-dir", "/tmp", "--no-cleanup", "--output", "file.pdf"]) + parser = Muse::Dl::Parser.new(["--no-bookmarks", "--tmp-dir", "/tmp", "--no-cleanup", "--output", "file.pdf", "https://muse.jhu.edu/book/68534"]) parser.bookmarks.should eq false parser.tmp.should eq "/tmp" parser.cleanup.should eq false @@ -10,10 +10,19 @@ describe Muse::Dl::Parser do end it "should have reasonable defaults" do - parser = Muse::Dl::Parser.new + parser = Muse::Dl::Parser.new(["https://muse.jhu.edu/book/68534"]) parser.bookmarks.should eq true parser.cleanup.should eq true parser.tmp.should eq "/tmp" parser.output.should eq "tempfilename.pdf" + parser.url.should eq "https://muse.jhu.edu/book/68534" + end + + it "should raise error on missing options" do + begin + parser = Muse::Dl::Parser.new([] of String) + rescue e : Exception + e.class.should eq Muse::Dl::Errors::MissingLink + end end end diff --git a/src/errors/missing_link.cr b/src/errors/missing_link.cr new file mode 100644 index 0000000..6bd550f --- /dev/null +++ b/src/errors/missing_link.cr @@ -0,0 +1,4 @@ +module Muse::Dl::Errors + class MissingLink < Exception + end +end diff --git a/src/fetch.cr b/src/fetch.cr index 4d4d2a8..d1ea54b 100644 --- a/src/fetch.cr +++ b/src/fetch.cr @@ -3,7 +3,7 @@ require "./errors/*" module Muse::Dl class Fetch - def get_info(url : String) : (Muse::Dl::Book | Muse::Dl::Journal) + def self.get_info(url : String) : (Muse::Dl::Book | Muse::Dl::Journal) match = /https:\/\/muse.jhu.edu\/(book|journal)\/(\d+)/.match url if match begin diff --git a/src/muse-dl.cr b/src/muse-dl.cr index 772f3fd..468cc3b 100644 --- a/src/muse-dl.cr +++ b/src/muse-dl.cr @@ -11,7 +11,11 @@ module Muse::Dl # TODO: Put your code here class Main - def self.run + def self.run(args : Array(String)) + parser = Parser.new(args) + Fetch.get_info(parser.url) end end end + +Muse::Dl::Main.run(ARGV) diff --git a/src/parser.cr b/src/parser.cr index 1700640..766b7a0 100644 --- a/src/parser.cr +++ b/src/parser.cr @@ -1,11 +1,14 @@ +require "./errors/missing_link.cr" + module Muse::Dl class Parser @bookmarks : Bool @tmp : String @cleanup : Bool @output : String + @url : String | Nil - getter :bookmarks, :tmp, :cleanup, :output + getter :bookmarks, :tmp, :cleanup, :output, :url def find_next(arg : Array(String), flag : String, default) search = arg.index flag @@ -21,6 +24,11 @@ module Muse::Dl @cleanup = !arg.index "--no-cleanup" @tmp = find_next(arg, "--tmp-dir", "/tmp") @output = find_next(arg, "--output", "tempfilename.pdf") + begin + @url = arg[-1] + rescue e : Exception + raise Errors::MissingLink.new + end end end end