shortey 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,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ config.ru
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shoosh.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sascha Wessel
2
+
3
+ MIT License
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # shortey
2
+
3
+ shortey is an very small rack application, that provides url shortening.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'shortey'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install shortey
18
+
19
+ ## Usage
20
+
21
+ TODO: I will provide usage instructions later, when this app is ready for production
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |task|
5
+ task.libs << "spec"
6
+ task.test_files = FileList["spec/**/*_spec.rb"]
7
+ end
8
+
9
+ task :default => [:test, :build]
data/lib/shortey.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rack'
2
+ require 'redis'
3
+ require 'uri'
4
+ require 'zlib'
5
+
6
+ require 'shortey/version'
7
+ require 'shortey/app'
@@ -0,0 +1,39 @@
1
+ class Shortey::App
2
+
3
+ @@redis = Redis.new(:host => 'localhost')
4
+
5
+ def self.call(env)
6
+ request = Rack::Request.new(env)
7
+ response = Rack::Response.new
8
+ response.header['Content-Type'] = "text/plain"
9
+ case request.path
10
+ when "/"
11
+ url = request.params['url']
12
+ uri = URI.parse(url)
13
+ unless uri.kind_of? URI::HTTP or uri.kind_of? URI::HTTPS
14
+ response.status = 500
15
+ response.write "URL is invalid!"
16
+ else
17
+ hash = Zlib.crc32(url)
18
+ unless @@redis.exists(hash)
19
+ @@redis.set(hash, uri.to_s)
20
+ @@redis.hset(:clicks, hash, 0)
21
+ end
22
+ response.status = 200
23
+ response.write "#{request.scheme}://#{request.host_with_port}/#{hash}"
24
+ end
25
+ else
26
+ hash = request.path.delete('/')
27
+ url = @@redis.get(hash)
28
+ unless url.nil?
29
+ @@redis.hincrby(:clicks, hash, 1)
30
+ response.redirect(url, 301)
31
+ else
32
+ response.status = 404
33
+ response.write "URL with hash #{hash} not found!"
34
+ end
35
+ end
36
+ response.finish
37
+ end
38
+
39
+ end
@@ -0,0 +1,3 @@
1
+ module Shortey
2
+ VERSION = "0.0.1"
3
+ end
data/shortey.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shortey/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "shortey"
8
+ gem.version = Shortey::VERSION
9
+ gem.authors = ["Sascha Wessel"]
10
+ gem.email = ["swessel@gr4yweb.de"]
11
+ gem.description = %q{an url-shortener based on rack and redis}
12
+ gem.summary = %q{an url-shortener based on rack and redis}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "rack", "~> 1.4.1"
21
+ gem.add_runtime_dependency "redis", "~> 3.0.2"
22
+ gem.add_development_dependency "minitest", "~> 4.3.3"
23
+ gem.add_development_dependency "rack-test", "~> 0.6.2"
24
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ def app
4
+ Shortey::App
5
+ end
6
+
7
+ describe "Shortey::App" do
8
+
9
+ describe 'when I enter an valid URL' do
10
+
11
+ it 'should return an hash' do
12
+ url = 'http://google.de'
13
+ request '/', :params => { 'url' => url }
14
+ last_response.status == 200
15
+ uri = URI.parse(last_response.body)
16
+ uri.path.delete('/') == Zlib.crc32(url)
17
+ end
18
+
19
+ end
20
+
21
+ describe 'when I enter an invalid URL' do
22
+
23
+ it 'should return error 500' do
24
+ request '/', :params => { 'url' => 'ftp://mapple.com' }
25
+ last_response.status == 500
26
+ last_response.body == "URL is invalid!"
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,8 @@
1
+ require 'minitest/autorun'
2
+ require 'rack/test'
3
+
4
+ class MiniTest::Spec
5
+ include Rack::Test::Methods
6
+ end
7
+
8
+ require 'shortey'
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shortey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sascha Wessel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: redis
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 4.3.3
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 4.3.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: rack-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.6.2
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.6.2
78
+ description: an url-shortener based on rack and redis
79
+ email:
80
+ - swessel@gr4yweb.de
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/shortey.rb
91
+ - lib/shortey/app.rb
92
+ - lib/shortey/version.rb
93
+ - shortey.gemspec
94
+ - spec/shortey/app_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage: ''
97
+ licenses: []
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.24
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: an url-shortener based on rack and redis
120
+ test_files:
121
+ - spec/shortey/app_spec.rb
122
+ - spec/spec_helper.rb
123
+ has_rdoc: