Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jgorset committed Jan 18, 2012
1 parent baf50d4 commit 597d938
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
10 changes: 5 additions & 5 deletions lib/gitio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ def self.shorten(url, code=nil)
request = Net::HTTP::Post.new "/"
request.content_type = "application/x-www-form-urlencoded"

query = {}.tap do |hash|
hash[:url] = url
hash[:code] = code if code
query = Hash.new.tap do |h|
h[:url] = url
h[:code] = code if code
end

request.body = URI.encode_www_form(query)

response = http.request(request)

if short_url = response["Location"]
return short_url
if response.key? "Location"
return response["Location"]
else
raise Error, response.body
end
Expand Down
11 changes: 5 additions & 6 deletions lib/gitio/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ module Gitio
class CLI

def initialize(arguments)
begin
url, options = parse!(arguments)
rescue OptionParser::InvalidOption => e
error e
end
url, options = parse!(arguments)

if options[:code]
if options.include? :code
puts Gitio.shorten(url, options[:code])
else
puts Gitio.shorten(url)
Expand Down Expand Up @@ -40,8 +36,11 @@ def parse!(arguments)
error "You must specify an URL to shorten" if arguments.empty?

[arguments.first, options]
rescue OptionParser::InvalidOption => e
error e
end

# Print <tt>message</tt> and exit with the given <tt>code</tt>.
def error(message, code=1)
puts "git.io: #{message}"
exit code
Expand Down

0 comments on commit 597d938

Please sign in to comment.