Skip to content

Commit

Permalink
Add a runnable binary. Closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
veelenga committed Dec 26, 2015
1 parent eed3dfd commit 77525ad
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions bin/shorturl
@@ -0,0 +1,3 @@
require "../src/shorturl/cli.cr"

ShortURL::Cli.run(ARGV)
6 changes: 6 additions & 0 deletions shard.lock
@@ -0,0 +1,6 @@
version: 1.0
shards:
webmock:
github: manastech/webmock.cr
version: 0.4.0

2 changes: 1 addition & 1 deletion spec/shorturl_spec.cr
Expand Up @@ -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

Expand Down
12 changes: 7 additions & 5 deletions src/shorturl.cr
Expand Up @@ -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.
#
Expand All @@ -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

Expand Down
49 changes: 49 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion src/shorturl/version.cr
@@ -1,3 +1,3 @@
module ShortURL
VERSION = "0.1.1"
VERSION = "0.2.0"
end

0 comments on commit 77525ad

Please sign in to comment.