diff --git a/README.md b/README.md index 079dec5..b5067c0 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,24 @@ ShortURL.shorten "http://google.com", :isgd # => "http://is.gd/OwycZW" # expand ShortURL.expand "http://tinyurl.com/2tx" # => "http://google.com" ``` + +## Binary + +It is possible to use it from command line interface: + +```sh +$ crystal build bin/shorturl && ./shorturl -h +Usage: shorturl [arguments] + -u URL, --url=URL URL to be shorten + -s SERVICE, --service=SERVICE Shortening service + -V, --verbose Verbose output + -v, --versoin Version + -h, --help Prints this help + +$ ./shorturl http://google.com +http://tinyurl.com/2tx +``` + ## Available services Here is a list of available shortening services in shorturl.cr: diff --git a/bin/shorturl b/bin/shorturl new file mode 100644 index 0000000..740594d --- /dev/null +++ b/bin/shorturl @@ -0,0 +1,3 @@ +require "../src/shorturl/cli.cr" + +ShortURL::Cli.run(ARGV) diff --git a/shard.lock b/shard.lock new file mode 100644 index 0000000..46e7059 --- /dev/null +++ b/shard.lock @@ -0,0 +1,6 @@ +version: 1.0 +shards: + webmock: + github: manastech/webmock.cr + version: 0.4.0 + diff --git a/spec/shorturl_spec.cr b/spec/shorturl_spec.cr index a70fe2b..721b126 100644 --- a/spec/shorturl_spec.cr +++ b/spec/shorturl_spec.cr @@ -9,7 +9,7 @@ module ShortURL describe ".all_services" do it "returns a list of available services names" do - ShortURL.all_services.should contain :tinyurl + ShortURL.all_services.should contain "tinyurl" end end diff --git a/src/shorturl.cr b/src/shorturl.cr index 45cdc19..70dbc12 100644 --- a/src/shorturl.cr +++ b/src/shorturl.cr @@ -4,10 +4,11 @@ require "http/client" module ShortURL SERVICES = { - isgd: Services::Isgd.new, - tinyurl: Services::TinyURL.new, - vgd: Services::Vgd.new, - } of Symbol => Service + "isgd": Services::Isgd.new, + "tinyurl": Services::TinyURL.new, + "vgd": Services::Vgd.new, + "shorl": Services::Shorl.new, + } of String => Service # Returns all available shortening services. # @@ -26,10 +27,11 @@ module ShortURL # # ... # ``` def self.shorten(url : String, service = :tinyurl) + service = service.to_s if service.is_a? Symbol if SERVICES.has_key? service SERVICES[service].shorten url else - raise InvalidService.new + raise InvalidService.new "Service not exists '#{service}'. Available services: #{SERVICES.keys.join ", "}" end end diff --git a/src/shorturl/cli.cr b/src/shorturl/cli.cr new file mode 100644 index 0000000..c41ecb1 --- /dev/null +++ b/src/shorturl/cli.cr @@ -0,0 +1,49 @@ +require "option_parser" +require "../shorturl" + +module ShortURL + class Cli + def self.run(args = ARGV) + url = nil + service = :tinyurl + verbose = false + + OptionParser.parse(args) do |parser| + parser.banner = "Usage: shorturl [arguments]" + + parser.on("-u URL", "--url=URL", "URL to be shorten") do |u| + url = u + end + + parser.on("-s SERVICE", "--service=SERVICE", "Shortening service") do |s| + service = s.strip.downcase + end + + parser.on("-V", "--verbose", "Verbose output") do + verbose = true + end + + parser.on("-v", "--version", "Version") do + puts ShortURL::VERSION + exit 0 + end + + parser.on("-h", "--help", "Prints this help") do + puts parser + exit 0 + end + end + + url = args.first if url.nil? && !args.empty? + + raise "URL is required" if url.nil? + + puts "Shortening '#{url}'" if verbose + puts ShortURL.shorten url.not_nil!, service + + rescue e : Exception + puts "Error: #{e.message}" + exit 1 + end + end +end diff --git a/src/shorturl/version.cr b/src/shorturl/version.cr index b55b842..0e4a597 100644 --- a/src/shorturl/version.cr +++ b/src/shorturl/version.cr @@ -1,3 +1,3 @@ module ShortURL - VERSION = "0.1.1" + VERSION = "0.2.0" end