petit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .rvmrc
2
+ .autotest
3
+ *.gem
4
+ .bundle
5
+ Gemfile.lock
6
+ pkg/*
data/COPYING ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2010-2011 Greg Osuri <gosuri@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Petit
2
+
3
+ simple, fast, rack-based url routing server. Links are stored in a yaml
4
+ file
5
+
6
+ ## Highlights
7
+
8
+ * Your own url-shortner in minutes
9
+ * Links are precached, extremly fast routing
10
+ * Rack-based -> Deploy with heroku in minutes
11
+ * No Database setup/maintenance
12
+
13
+
14
+ ## Getting Started
15
+
16
+ ### Install the Gem
17
+
18
+ gem install petit
19
+
20
+ ### Initialize in the app directory
21
+
22
+ This will create links.yml in the current directory
23
+
24
+ petit init
25
+
26
+ ### Add your links to links.yml
27
+
28
+ Links are in simple YAML format
29
+
30
+ foo: http://bar.com # => will redirect http://yourdomain/foo to http://bar.com
31
+
32
+ ### Deploy
33
+
34
+ A config.ru will be generated during the initialization, or simply
35
+ place this in your rackup file
36
+
37
+ require 'petit'
38
+ run Petit::Router.builder
39
+
40
+ ## Copying
41
+
42
+ Copyright (c) 2011 [Greg Osuri](http://gregosuri.com/about)
43
+
44
+ This is Free Software distributed under the terms of the MIT license.
45
+ See the file COPYING for information of licensing and distribution.
46
+
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'rake/testtask'
5
+ require 'rdoc/task'
6
+
7
+ desc 'Run Petit unit tests'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'lib'
10
+ t.libs << 'test'
11
+ t.pattern = 'test/**/test_*.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Run tests by default'
16
+ task :default => %w(test)
data/bin/petit ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'thor'
4
+ require 'petit'
5
+
6
+ class PetitCommand < Thor
7
+ include Thor::Actions
8
+
9
+ desc "init", "initializes petit for your project"
10
+ def init # :nodoc:
11
+ generate_links_file
12
+ generate_rackup_file
13
+ end
14
+
15
+ desc "server", "starts the petit routing server"
16
+ def server # :nodoc:
17
+ Petit::Router.server
18
+ end
19
+
20
+ no_tasks do
21
+
22
+ def generate_rackup_file # :nodoc:
23
+ create_file "config.ru" do
24
+ <<-CONTENT
25
+ require 'petit'
26
+ run Petit::Router.builder
27
+ CONTENT
28
+ end
29
+ end
30
+
31
+ def generate_config_file # :nodoc:
32
+ create_file "petit.rb" do
33
+ <<-CONTENT
34
+ Petit.setup do |config|
35
+ # YML file where links are stored
36
+ # config.link_source = "links.yml"
37
+ end
38
+ CONTENT
39
+ end
40
+ end
41
+
42
+ def generate_links_file # :nodoc:
43
+ create_file "links.yml" do
44
+ <<-CONTENT
45
+ # Links go here
46
+ # foo: http://bar.com # => will redirect yourdomain/foo to http://bar.com
47
+ CONTENT
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+
54
+ PetitCommand.start
@@ -0,0 +1,20 @@
1
+ module Petit
2
+ module RackHelper
3
+ class << self
4
+
5
+ # Generated a three part response for redirects
6
+ def redirect_response(url)
7
+ [301, redirect_headers(url), [""]]
8
+ end
9
+
10
+ # Generates a CGI-like redirect header hash
11
+ # for the given url
12
+ def redirect_headers(url)
13
+ {
14
+ "Content-Type" => "text/plain",
15
+ "Location" => url
16
+ }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ require 'rack'
2
+
3
+ module Petit
4
+ class Router
5
+ class << self
6
+
7
+ # Generates a Rack::Builder object for URLs mapped redirects
8
+ # +links+ a hash of links to redirect
9
+ def builder(links = Petit.links)
10
+ @builder ||= Rack::Builder.new do
11
+ links.each do |key,url|
12
+ map "/#{key}" do
13
+ run Proc.new {|env| RackHelper.redirect_response(url)}
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ # starts the petit routing server
20
+ def server(port = 4200)
21
+ Rack::Server.new(:app => builder, :Port => port, :server => 'webrick').start
22
+ end
23
+
24
+ end
25
+ end
26
+ end
data/lib/petit.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'yaml'
2
+
3
+ module Petit
4
+
5
+ autoload :Router, 'petit/router'
6
+ autoload :RackHelper, 'petit/rack_helper'
7
+
8
+ class << self
9
+
10
+ def link_source=(source)
11
+ @@link_source = source
12
+ end
13
+
14
+ def link_source
15
+ @@link_source ||= "links.yml"
16
+ end
17
+
18
+ def links(source = link_source)
19
+ @links ||= YAML::load(File.open(source))
20
+ end
21
+
22
+ # Default way to setup petit
23
+ def config
24
+ yield self
25
+ end
26
+
27
+ end
28
+ end
29
+
data/petit.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "petit"
6
+ s.version = "0.0.1"
7
+ s.authors = ["Greg Osuri"]
8
+ s.email = ["gosuri@gmail.com"]
9
+ s.homepage = "http://gosuri.github.com/petit"
10
+ s.summary = %q{a simple, fast, rack-based url shortening server}
11
+ s.description = %q{a simple, fast, rack-based url shortening server}
12
+
13
+ s.rubyforge_project = "petit"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = %w(petit)
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_runtime_dependency "rack"
21
+ s.add_runtime_dependency "thor"
22
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "test/unit"
2
+ require "petit"
3
+
4
+ TEST_ROOT = File.expand_path(File.dirname(__FILE__))
5
+ Petit.link_source = File.join(TEST_ROOT,"links.yml")
6
+
data/test/links.yml ADDED
@@ -0,0 +1,4 @@
1
+ foo: http://bar.com
2
+ beth: http://beatty.net
3
+ m: http://morissettedibbert.name
4
+ h: http://hamill.name
@@ -0,0 +1,2 @@
1
+ require 'petit'
2
+ run Petit::Router.builder
@@ -0,0 +1,4 @@
1
+ g: http://gregosuri.com
2
+ gb: http://gridbag.com
3
+ # Links go here
4
+ # foo: http://bar.com # => will redirect yourdomain/foo to http://bar.com
@@ -0,0 +1,13 @@
1
+ require 'helper'
2
+
3
+ class TestPetit < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @links = Petit.links
7
+ end
8
+
9
+ def test_load_link_files
10
+ assert @links.has_key? 'foo'
11
+ assert_equal @links['foo'], 'http://bar.com'
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ class TestRackHelper < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @url = "http://foo.com"
7
+ end
8
+
9
+ def test_redirect
10
+ response = Petit::RackHelper.redirect_response(@url)
11
+ assert_equal response, [301, {"Content-Type" => "text/plain",
12
+ "Location" => @url}, [""]
13
+ ]
14
+ end
15
+
16
+ def test_redirect_headers
17
+ headers = Petit::RackHelper.redirect_headers(@url)
18
+ assert_equal headers["Content-Type"], "text/plain"
19
+ assert_equal headers["Location"], @url
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+ require 'rack/mock'
3
+
4
+ class TestRouter < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @links = {"foo" => "http://bar.com"}
8
+ end
9
+
10
+ def test_builder
11
+ assertion = Petit::Router.builder(@links)
12
+ response = Rack::MockRequest.new(assertion).get("/foo")
13
+ assert_equal response.status, 301
14
+ assert_equal response.headers["Location"], "http://bar.com"
15
+ end
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: petit
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Greg Osuri
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-04 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: thor
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ description: a simple, fast, rack-based url shortening server
38
+ email:
39
+ - gosuri@gmail.com
40
+ executables:
41
+ - petit
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - COPYING
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - bin/petit
53
+ - lib/petit.rb
54
+ - lib/petit/rack_helper.rb
55
+ - lib/petit/router.rb
56
+ - petit.gemspec
57
+ - test/helper.rb
58
+ - test/links.yml
59
+ - test/sample_app/config.ru
60
+ - test/sample_app/links.yml
61
+ - test/test_petit.rb
62
+ - test/test_rack_helper.rb
63
+ - test/test_router.rb
64
+ homepage: http://gosuri.github.com/petit
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: petit
87
+ rubygems_version: 1.8.10
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: a simple, fast, rack-based url shortening server
91
+ test_files:
92
+ - test/helper.rb
93
+ - test/links.yml
94
+ - test/sample_app/config.ru
95
+ - test/sample_app/links.yml
96
+ - test/test_petit.rb
97
+ - test/test_rack_helper.rb
98
+ - test/test_router.rb