git.io 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in git.io.gemspec
4
+ gemspec
@@ -0,0 +1,8 @@
1
+ # Git.io
2
+
3
+ Command-line client for GitHub's URL shortener.
4
+
5
+ ## Usage
6
+
7
+ $ git.io http://github.com/jgorset/git.io
8
+ http://git.io/J6MbsQ
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "gitio"
4
+
5
+ Gitio::CLI.new(ARGV)
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gitio/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "git.io"
7
+ s.version = Gitio::VERSION
8
+ s.authors = ["Johannes Gorset"]
9
+ s.email = ["jgorset@gmail.com"]
10
+ s.homepage = "http://github.com/jgorset/git.io"
11
+ s.summary = "Command-line client for git.io"
12
+ s.description = "Command-line client for GitHub's URL shortener, git.io."
13
+
14
+ s.rubyforge_project = "git.io"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,42 @@
1
+ require "net/http"
2
+ require "uri"
3
+
4
+ module Gitio
5
+ autoload :VERSION, "gitio/version"
6
+ autoload :Error, "gitio/errors"
7
+ autoload :CLI, "gitio/cli"
8
+
9
+ def self.shorten(url, code=nil)
10
+ Net::HTTP.start "git.io", 80 do |http|
11
+ request = Net::HTTP::Post.new "/"
12
+ request.content_type = "application/x-www-form-urlencoded"
13
+
14
+ query = {}.tap do |hash|
15
+ hash[:url] = url
16
+ hash[:code] = code if code
17
+ end
18
+
19
+ request.body = URI.encode_www_form(query)
20
+
21
+ response = http.request(request)
22
+
23
+ if short_url = response["Location"]
24
+ return short_url
25
+ else
26
+ raise Error, response.body
27
+ end
28
+ end
29
+ end
30
+
31
+ def self.lengthen(url)
32
+ url = URI(url)
33
+
34
+ Net::HTTP.start url.host, 80 do |http|
35
+ request = Net::HTTP::Get.new url.path
36
+
37
+ response = http.request(request)
38
+ return response["Location"]
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,47 @@
1
+ require "optparse"
2
+ require "gitio"
3
+
4
+ module Gitio
5
+ class CLI
6
+
7
+ def initialize(arguments)
8
+ url, options = parse!(arguments)
9
+
10
+ if options[:code]
11
+ puts Gitio.shorten(url, options[:code])
12
+ else
13
+ puts Gitio.shorten(url)
14
+ end
15
+
16
+ rescue Gitio::Error => e
17
+ error e
18
+ end
19
+
20
+ private
21
+
22
+ # Parse the command-line arguments, returning the URL and a hash of options.
23
+ def parse!(arguments)
24
+ options = {}
25
+
26
+ OptionParser.new do |opts|
27
+ opts.program_name = "git.io"
28
+ opts.banner = "Usage: git.io [options] <url>"
29
+ opts.version = Gitio::VERSION
30
+
31
+ opts.on "-c", "--code CODE", "Generate a short url with the given code" do |code|
32
+ options[:code] = code
33
+ end
34
+ end.parse!(arguments)
35
+
36
+ error "You must specify an URL to shorten" if arguments.empty?
37
+
38
+ [arguments.first, options]
39
+ end
40
+
41
+ def error(message, code=1)
42
+ puts "git.io: #{message}"
43
+ exit code
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,6 @@
1
+ module Gitio
2
+
3
+ class Error < StandardError
4
+ end
5
+
6
+ end
@@ -0,0 +1,3 @@
1
+ module Gitio
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,17 @@
1
+ require "gitio"
2
+
3
+ describe Gitio::CLI do
4
+
5
+ before do
6
+ $stdout = StringIO.new
7
+ end
8
+
9
+ it "accepts an URL" do
10
+ Gitio::CLI.new ["http://github.com/jgorset/git.io"]
11
+ end
12
+
13
+ it "accepts an URL with a code" do
14
+ Gitio::CLI.new ["http://github.com/jgorset/git.io", "git.io"]
15
+ end
16
+
17
+ end
@@ -0,0 +1,29 @@
1
+ require "net/http"
2
+ require "gitio"
3
+
4
+ describe Gitio do
5
+
6
+ it "shortens URLs randomly" do
7
+ original_url = "https://github.com/jgorset/git.io"
8
+
9
+ short_url = Gitio.shorten(original_url)
10
+ long_url = Gitio.lengthen(short_url)
11
+
12
+ original_url.should eq long_url
13
+ end
14
+
15
+ it "shortens URLs with a given code" do
16
+ original_url = "https://github.com/jgorset/git.io"
17
+
18
+ short_url = Gitio.shorten(original_url, "git.io")
19
+
20
+ short_url.should eq "http://git.io/git.io"
21
+ end
22
+
23
+ it "should raise an exception upon trying to shorten an invalid URL" do
24
+ original_url = "http://google.com"
25
+
26
+ expect { Gitio.shorten(original_url) }.to raise_error Gitio::Error
27
+ end
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git.io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Johannes Gorset
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70294673264120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70294673264120
25
+ description: Command-line client for GitHub's URL shortener, git.io.
26
+ email:
27
+ - jgorset@gmail.com
28
+ executables:
29
+ - git.io
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - README.md
36
+ - Rakefile
37
+ - bin/git.io
38
+ - git.io.gemspec
39
+ - lib/gitio.rb
40
+ - lib/gitio/cli.rb
41
+ - lib/gitio/errors.rb
42
+ - lib/gitio/version.rb
43
+ - spec/cli_spec.rb
44
+ - spec/gitio_spec.rb
45
+ homepage: http://github.com/jgorset/git.io
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project: git.io
65
+ rubygems_version: 1.8.11
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Command-line client for git.io
69
+ test_files:
70
+ - spec/cli_spec.rb
71
+ - spec/gitio_spec.rb