sinatra-mapping 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,8 @@
1
+ = Changes
2
+
3
+ [0.1.0 - July 2009]
4
+ * This version has been extracted from
5
+ {Postview}[http://postview.rubyforge.org] project.
6
+
7
+ * Add first methods for build maps in base application.
8
+
data/INFO ADDED
@@ -0,0 +1,14 @@
1
+ :name: sinatra-mapping
2
+ :summary:
3
+ Sinatra mapping extension for web application.
4
+ :description:
5
+ Sinatra mapping extension is a minimal module that is useful for create
6
+ map names for Sinatra web application.
7
+ :authors:
8
+ - Hallison Batista
9
+ :email: email@hallisonbatista.com
10
+ :homepage: http://sinatra-mapping.rubyforge.org/
11
+ :dependencies:
12
+ rack: >= 1.0.0
13
+ sinatra: >= 0.9.2
14
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ = The MIT License
2
+
3
+ Copyright (c) 2009 Hallison Batista
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,74 @@
1
+ = Sinatra Mapping - Map easily URLs paths
2
+
3
+ {Sinatra}[http://www.sinatrarb.com]
4
+ {mapping extension}[http://sinatra-mapping.rubyforge.org] is a minimal
5
+ module that is useful for create map names for Sinatra web application.
6
+
7
+ Install stable version gem:
8
+
9
+ gem install sinatra-mapping
10
+
11
+ Or, install development version gem:
12
+
13
+ gem install sinatra-mapping --source http://gems.github.com
14
+
15
+ == Getting start
16
+
17
+ Use extension using registered method in main source of application.
18
+ Example:
19
+
20
+ require 'sinatra'
21
+ require 'sinatra/mapping'
22
+
23
+ class BlogAppication < Sinatra::Base
24
+
25
+ register Sinatra::Mapping
26
+
27
+ map :root, "blog" # => /blog/
28
+ map :posts, "articles" # => /blog/articles
29
+ map :tags, "labels" # => /blog/labels
30
+ map :archive, "archived-articles" # => /blog/archived-articles
31
+
32
+ get root_path do
33
+ # /blog/
34
+ # do anything for root path.
35
+ end
36
+
37
+ get posts_path do
38
+ # /blog/articles
39
+ # do anything for posts path.
40
+ end
41
+
42
+ get posts_path(":year/:month/:day/:permalink") do |year, month, day, permalink|
43
+ # /blog/articles/2009/10/08/permalink-for-your-article
44
+ # do anything for posts path using parameters for find a post.
45
+ end
46
+
47
+ get tags_path do
48
+ # /blog/labels
49
+ # do anything for tags path.
50
+ end
51
+
52
+ get archive_path do
53
+ # do anything for archive path.
54
+ end
55
+
56
+ get archive_path(":year/:month/:day/:permalink") do |year, month, day, permalink|
57
+ # /blog/archived-articles/1978/10/08/permalink-for-your-article
58
+ # do anything for archive path using parameters for find a post.
59
+ end
60
+
61
+ end
62
+
63
+ Easy!!!
64
+
65
+ == More informations
66
+
67
+ * See {changes}[:link:CHANGES.html] for understand the goals of which
68
+ release
69
+ * {License}[:link:LICENSE.html]
70
+
71
+ == Copyright
72
+
73
+ Copyright (c) 2009 Hallison Batista
74
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rdoc'
2
+ require 'rake/testtask'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/rdoctask'
5
+ load 'sinatra-mapping.gemspec'
6
+
7
+ desc "Generate version for release and tagging."
8
+ task :version, [:major, :minor, :patch, :release, :date, :cycle] do |taskspec, version|
9
+ require 'parsedate'
10
+ current = YAML.load_file(taskspec.name.upcase)
11
+ version_date = Date.new(*ParseDate.parsedate(version[:date] || current[:date].to_s).compact) unless version or current
12
+ if current and version_date
13
+ newer = {
14
+ :major => version[:major].to_i || current[:major].to_i,
15
+ :minor => version[:minor].to_i || current[:minor].to_i,
16
+ :patch => version[:patch].to_i || current[:patch].to_i,
17
+ :release => version[:release].to_s.empty? ? nil : version[:release].to_i,
18
+ :date => version_date || Date.today,
19
+ :cycle => version[:cycle] || current[:cycle]
20
+ }
21
+
22
+ newer.merge(current) do |key, new_value, current_value|
23
+ new_value || current_value
24
+ end
25
+
26
+ File.open(taskspec.name.upcase, "w") do |version_file|
27
+ version_file << newer.to_yaml
28
+ end
29
+ end
30
+ end
31
+
32
+ Rake::TestTask.new
33
+
34
+ Rake::GemPackageTask.new(@spec) do |pkg|
35
+ pkg.need_tar_bz2 = true
36
+ end
37
+
38
+ Rake::RDocTask.new("doc") do |rdoc|
39
+ rdoc.title = "Sinatra - Mapping"
40
+ rdoc.main = "README"
41
+ rdoc.options = [ '-SHN', '-f', 'darkfish' ]
42
+ rdoc.rdoc_dir = 'doc'
43
+ rdoc.rdoc_files.include(
44
+ "CHANGES",
45
+ "LICENSE",
46
+ "README",
47
+ "lib/**/*.rb"
48
+ )
49
+ end
50
+
data/VERSION ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ :release:
3
+ :date: 2009-07-15
4
+ :cycle: Development version, pre-alpha
5
+ :major: 0
6
+ :minor: 1
7
+ :patch: 0
@@ -0,0 +1,83 @@
1
+ require 'sinatra/base'
2
+
3
+ # Informations about Sinatra DSL, please, visit the
4
+ # {official site}[http://www.sinatrarb.com/].
5
+ module Sinatra
6
+
7
+ # This extension is useful for any Web application written using
8
+ # Sinatra DSL. The main goal is help developers to write URL path
9
+ # methods.
10
+ module Mapping
11
+
12
+ # Write URL path method for use in HTTP methods.
13
+ #
14
+ # The map method most be used by following syntax:
15
+ #
16
+ # map <name>, <path>
17
+ #
18
+ # If name is equal :root, then returns path ended by slash "/".
19
+ #
20
+ # map :root, "tasks" # => /tasks/
21
+ # map :changes, "last-changes # => /tasks/last-changes
22
+ def map(name, path = nil)
23
+ @root_path ||= ""
24
+ @locations ||= {}
25
+ if name.to_sym == :root
26
+ @root_path = cleanup_paths("/#{path}/")
27
+ metadef "#{name}_path" do |*paths|
28
+ @root_path
29
+ end
30
+ else
31
+ @locations[name.to_sym] = path || name.to_s
32
+ metadef "#{name}_path" do |*paths|
33
+ path_to(@locations[name.to_sym], *paths)
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ # Check arguments. If argument is a symbol and exist map path before
41
+ # setted, then return path mapped by symbol name.
42
+ def path_to(*args)
43
+ path_mapped(*locations_get_from(*args))
44
+ end
45
+
46
+ # Returns all paths mapped by root path in prefix.
47
+ def path_mapped(*args)
48
+ !args.empty? ? cleanup_paths("/#{@root_path}/#{args.join('/')}") : @root_root
49
+ end
50
+
51
+ # Get paths from location maps.
52
+ def locations_get_from(*args)
53
+ args.delete(:root)
54
+ args.collect do |path|
55
+ @locations.has_key?(path) ? @locations[path] : path
56
+ end
57
+ end
58
+
59
+ # Clean all duplicated slashes.
60
+ def cleanup_paths(*paths)
61
+ paths.join('/').gsub(/[\/]{2,}/,'/')
62
+ end
63
+
64
+ end # module Mapping
65
+
66
+ register Mapping
67
+
68
+ end # module Sinatra
69
+
70
+ # private
71
+ #
72
+ # attr_reader :root
73
+ # attr_accessor :locations
74
+ #
75
+ # def root_set_to(path)
76
+ # @root = path_clean("/#{path}/")
77
+ # end
78
+ # alias :root= :root_set_to
79
+ #
80
+ #
81
+ # private
82
+ #
83
+ #
@@ -0,0 +1,18 @@
1
+ module Sinatra
2
+
3
+ # This module contains several helper methods for maps written using
4
+ # {map}[:link:./Sinatra/Mapping.html#map] method.
5
+ module MappingHelpers
6
+
7
+ # Create title using a path mapped. Other else, return just arguments
8
+ # joined by spaces and capitalise.
9
+ def title_path(path, *args)
10
+ title = (options.send("#{path}_path") || path).to_s.gsub('/',' ').strip
11
+ title.gsub!(/\W/,' ') # Cleanup
12
+ (args.empty? ? title : "#{title} #{args.join(' ')}").capitalize
13
+ end
14
+
15
+ end # module MapHelpers
16
+
17
+ end # module Sinatra
18
+
@@ -0,0 +1,28 @@
1
+ @version = YAML.load_file("VERSION")
2
+ @info = YAML.load_file("INFO")
3
+ @manifest = `git ls-files`.split.sort.reject{ |out| out =~ /^\./ || out =~ /^doc/ }
4
+ @spec = Gem::Specification.new do |gemspec|
5
+ gemspec.platform = Gem::Platform::RUBY
6
+ gemspec.version = [ @version[:major], @version[:minor], @version[:patch], @version[:release] ].compact.join('.')
7
+ gemspec.date = @version[:date]
8
+
9
+ @info.each do |info, value|
10
+ gemspec.send("#{info}=", value) if gemspec.respond_to? "#{info}="
11
+ end
12
+
13
+ @info[:dependencies].each do |name, version|
14
+ gemspec.add_dependency name, version
15
+ end
16
+
17
+ gemspec.require_paths = %w[lib]
18
+ gemspec.files = @manifest
19
+ gemspec.test_files = gemspec.files.select{ |path| path =~ /^test\/test_.*.rb/ }
20
+
21
+ gemspec.has_rdoc = true
22
+ gemspec.extra_rdoc_files = %w[README LICENSE]
23
+
24
+ gemspec.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Sinatra - Mapping", "--main", "README"]
25
+ gemspec.rubyforge_project = gemspec.name
26
+ gemspec.rubygems_version = "1.3.3"
27
+ end if @version || @info # Gem::Specification
28
+
@@ -0,0 +1,175 @@
1
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/..")
2
+
3
+ require 'rubygems'
4
+ require 'lib/sinatra/mapping'
5
+ require 'lib/sinatra/mapping_helpers'
6
+ require 'test/unit'
7
+ require 'rack/test'
8
+ require 'ruby-debug'
9
+
10
+ class AppForTest < Sinatra::Base
11
+
12
+ register Sinatra::Mapping
13
+
14
+ map :root # root_path => /
15
+ map :posts, "articles" # posts_path => /articles
16
+ map :archive, "archive/articles" # archive_path => /articles/archive
17
+ map :about # about_path => /about
18
+
19
+ helpers Sinatra::MappingHelpers
20
+
21
+ before do
22
+ @date = Date.today
23
+ end
24
+
25
+ get root_path do
26
+ "#{title_path :root}:#{options.root_path}"
27
+ end
28
+
29
+ get posts_path do
30
+ "#{title_path :posts}:#{options.posts_path}"
31
+ end
32
+
33
+ get posts_path "/" do
34
+ redirect options.posts_path, 301
35
+ end
36
+
37
+ get posts_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
38
+ "#{title_path :posts}:" + options.posts_path("#{@date.to_s.gsub('-','/')}/#{permalink}")
39
+ end
40
+
41
+ get posts_path "/:year/:month/:day/:permalink/" do |year, month, day, permalink|
42
+ redirect options.posts_path(year, month, day, permalink), 301
43
+ end
44
+
45
+ get archive_path do
46
+ "#{title_path :archive}:#{options.archive_path}"
47
+ end
48
+
49
+ get archive_path "/" do
50
+ redirect options.archive_path, 301
51
+ end
52
+
53
+ get archive_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
54
+ "#{title_path :archive}:" + options.archive_path("#{@date.to_s.gsub('-','/')}/#{permalink}")
55
+ end
56
+
57
+ get archive_path "/:year/:month/:day/:permalink/" do |year, month, day, permalink|
58
+ redirect options.archive_path(year, month, day, permalink), 301
59
+ end
60
+
61
+ get about_path do
62
+ "#{title_path :about}:#{options.about_path}"
63
+ end
64
+
65
+ get about_path "/" do
66
+ redirect options.about_path, 301
67
+ end
68
+
69
+ end
70
+
71
+ class TestMapping < Test::Unit::TestCase
72
+
73
+ include Rack::Test::Methods
74
+
75
+ def setup
76
+ @date = Date.today
77
+ @locations = {
78
+ :root_path => "/",
79
+ :posts_path => "/articles",
80
+ :archive_path => "/archive/articles",
81
+ :about_path => "/about"
82
+ }
83
+ end
84
+
85
+ def app
86
+ @app = ::AppForTest
87
+ @app.set :environment, :test
88
+ @app
89
+ end
90
+
91
+ def test_check_map_locations
92
+ @locations.each do |name, location|
93
+ assert_equal "#{location}", app.send(name)
94
+ end
95
+ end
96
+
97
+ def test_should_return_ok_in_root_path
98
+ get "#{@locations[:root_path]}" do |response|
99
+ assert response.ok?
100
+ assert_equal "http://example.org#{@locations[:root_path]}", last_request.url
101
+ assert_equal ":#{@locations[:root_path]}", response.body
102
+ end
103
+ end
104
+
105
+ def test_should_return_ok_in_posts_path
106
+ get "#{@locations[:posts_path]}" do |response|
107
+ assert response.ok?
108
+ assert_equal "http://example.org#{@locations[:posts_path]}", last_request.url
109
+ assert_equal "Articles:#{@locations[:posts_path]}", response.body
110
+ end
111
+
112
+ get "#{@locations[:posts_path]}/" do
113
+ follow_redirect!
114
+ assert last_response.ok?
115
+ assert_equal "http://example.org#{@locations[:posts_path]}", last_request.url
116
+ end
117
+
118
+ path = "#{@locations[:posts_path]}/#{@date.to_s.gsub('-','/')}/post-permalink"
119
+ get path do |response|
120
+ assert response.ok?
121
+ assert_equal "http://example.org#{path}", last_request.url
122
+ assert_equal "Articles:#{path}", response.body
123
+ end
124
+
125
+ get "#{path}/" do
126
+ follow_redirect!
127
+ assert last_response.ok?
128
+ assert_equal "http://example.org#{path}", last_request.url
129
+ assert_equal "Articles:#{path}", last_response.body
130
+ end
131
+ end
132
+
133
+ def test_should_return_ok_in_archive_path
134
+ get "#{@locations[:archive_path]}" do |response|
135
+ assert response.ok?
136
+ assert_equal "http://example.org#{@locations[:archive_path]}", last_request.url
137
+ assert_equal "Archive articles:#{@locations[:archive_path]}", response.body
138
+ end
139
+
140
+ get "#{@locations[:archive_path]}/" do
141
+ follow_redirect!
142
+ assert last_response.ok?
143
+ assert_equal "http://example.org#{@locations[:archive_path]}", last_request.url
144
+ end
145
+
146
+ path = "#{@locations[:archive_path]}/#{@date.to_s.gsub('-','/')}/post-permalink"
147
+ get path do |response|
148
+ assert response.ok?
149
+ assert_equal "http://example.org#{path}", last_request.url
150
+ assert_equal "Archive articles:#{path}", response.body
151
+ end
152
+
153
+ get "#{path}/" do
154
+ follow_redirect!
155
+ assert last_response.ok?
156
+ assert_equal "http://example.org#{path}", last_request.url
157
+ assert_equal "Archive articles:#{path}", last_response.body
158
+ end
159
+ end
160
+
161
+ def test_should_return_ok_in_about_path
162
+ get "#{@locations[:about_path]}" do |response|
163
+ assert response.ok?
164
+ assert_equal "http://example.org#{@locations[:about_path]}", last_request.url
165
+ assert_equal "About:#{@locations[:about_path]}", response.body
166
+ end
167
+
168
+ get "#{@locations[:about_path]}/" do
169
+ follow_redirect!
170
+ assert last_response.ok?
171
+ assert_equal "http://example.org#{@locations[:about_path]}", last_request.url
172
+ end
173
+ end
174
+ end
175
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-mapping
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hallison Batista
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-15 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: sinatra
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2
34
+ version:
35
+ description: Sinatra mapping extension is a minimal module that is useful for create map names for Sinatra web application.
36
+ email: email@hallisonbatista.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ - LICENSE
44
+ files:
45
+ - CHANGES
46
+ - INFO
47
+ - LICENSE
48
+ - README
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/sinatra/mapping.rb
52
+ - lib/sinatra/mapping_helpers.rb
53
+ - sinatra-mapping.gemspec
54
+ - test/test_mapping.rb
55
+ has_rdoc: true
56
+ homepage: http://sinatra-mapping.rubyforge.org/
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --line-numbers
62
+ - --inline-source
63
+ - --title
64
+ - Sinatra - Mapping
65
+ - --main
66
+ - README
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project: sinatra-mapping
84
+ rubygems_version: 1.3.3
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Sinatra mapping extension for web application.
88
+ test_files:
89
+ - test/test_mapping.rb