sinatra-pagin 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ doc
2
+ pkg
3
+ rcov
4
+ vendor
5
+ bin
data/Changelog.md ADDED
@@ -0,0 +1,3 @@
1
+ ### sinatra-pagin - 0.0.1 / 2010-02-14
2
+
3
+ * Initial Release
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+
2
+ only :test do
3
+ gem 'sinatra', '>= 0.9.4'
4
+ # these should be installed on your testing location
5
+ #gem 'rack-test', '>= 0.5.2'
6
+ #gem 'rspec', '>= 1.2.8'
7
+ #gem 'webrat', '>= 0.7.0'
8
+ end
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ Sinatra::Pagin*
2
+ =
3
+
4
+ Small utility to process paginated urls without modifying the mapped paths in your Sinatra app
5
+
6
+ ---
7
+
8
+ ### FEATURES/PROBLEMS:
9
+
10
+ * Parses ../page/# off your urls to allow simple route mappings
11
+ * Saves page # for internal use
12
+
13
+ ### SYNOPSIS:
14
+
15
+ Given you have mapped paths as such:
16
+
17
+ get "/" do
18
+ "hello world"
19
+ end
20
+
21
+ # => "http://example.org/"
22
+
23
+ get "/a/pathed/path" do
24
+ "foo bar"
25
+ end
26
+
27
+ # => "http://example.org/a/pathed/path"
28
+
29
+ Without changing those paths, you can run a paginated url.
30
+
31
+ http://example.org/page/2
32
+ # => get "/"
33
+
34
+ http://example.org/a/pathed/path/page/45
35
+ # => get "/a/pathed/path"
36
+
37
+ Use the helper method `page` to get the provide page number.
38
+
39
+ http://example.org/page/2
40
+
41
+ get "/" do
42
+ "hello world, you asked for page #{page}"
43
+ end
44
+
45
+ # => hello world, you asked for page 2
46
+
47
+ `page` returns 1 as a default.
48
+
49
+ It also supports `.:format` in your path.
50
+
51
+ get "/a/pathed/path.:format" do
52
+ "foo bar"
53
+ end
54
+
55
+ http://example.org/a/pathed/path/page/45.js
56
+ # => path_info == /a/pathed/path.js
57
+ # => page == 45
58
+
59
+ ### REQUIREMENTS:
60
+
61
+ * [Sinatra](http://www.sinatrarb.com/)
62
+
63
+ ### INSTALL:
64
+
65
+ Install the gem:
66
+
67
+ sudo gem install sinatra-pagin
68
+
69
+ Require in your app:
70
+
71
+ require 'sinatra/pagin'
72
+
73
+ ---
74
+ ### LICENSE:
75
+
76
+ (The MIT License)
77
+
78
+ Copyright (c) 2010 Yung Hwa Kwon (yung.kwon@nowk.net)
79
+
80
+ Permission is hereby granted, free of charge, to any person obtaining
81
+ a copy of this software and associated documentation files (the
82
+ 'Software'), to deal in the Software without restriction, including
83
+ without limitation the rights to use, copy, modify, merge, publish,
84
+ distribute, sublicense, and/or sell copies of the Software, and to
85
+ permit persons to whom the Software is furnished to do so, subject to
86
+ the following conditions:
87
+
88
+ The above copyright notice and this permission notice shall be
89
+ included in all copies or substantial portions of the Software.
90
+
91
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
92
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
93
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
94
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
95
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
96
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
97
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+
3
+ # GEM
4
+ begin
5
+ require 'jeweler'
6
+
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "sinatra-pagin"
9
+ gem.summary = %Q{Small utility to process paginated urls without modifying the mapped paths in your Sinatra app}
10
+ gem.description = %Q{Small utility to process paginated urls without modifying the mapped paths in your Sinatra app}
11
+ gem.email = "yung.kwon@nowk.net"
12
+ gem.homepage = "http://github.com/nowk/sinatra-pagin"
13
+ gem.authors = ["Yung Hwa Kwon"]
14
+ gem.add_dependency "sinatra", ">= 0.9.4"
15
+
16
+ { 'rack-test' => '>= 0.5.2',
17
+ 'rspec' => '>= 1.2.8',
18
+ 'webrat' => '>= 0.7.0'
19
+ }.each_pair do |g, v|
20
+ gem.add_development_dependency g, v
21
+ end
22
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
23
+ end
24
+
25
+ Jeweler::GemcutterTasks.new
26
+ rescue LoadError
27
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
28
+ end
29
+
30
+ # Test Rakes
31
+ begin
32
+ require 'spec'
33
+ require 'spec/rake/spectask'
34
+
35
+ task :default => :spec
36
+
37
+ desc "Run Rspec tests"
38
+ Spec::Rake::SpecTask.new(:spec) do |t|
39
+ #t.spec_opts = ['spec/spec.opts']
40
+ t.spec_files = FileList['spec/*_spec.rb']
41
+ end
42
+
43
+ desc "Run CI tests"
44
+ task :ci => 'setup:gem_bundle' do
45
+ Rake::Task[:spec].invoke
46
+ end
47
+
48
+ namespace :setup do
49
+ desc "Setup required gems/goodies for CI test. Server has only the essential minimums."
50
+ task :gem_bundle do
51
+ sh "gem bundle --only test"
52
+ require "vendor/gems/environment"
53
+ end
54
+ end
55
+
56
+ rescue LoadError
57
+ puts "I <3 Rspec, or any good testing unit for that matter."
58
+ puts "RSpec required. To install Rspec, please run sudo gem install rspec."
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,32 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Pagin
5
+ def self.registered(app)
6
+ app.helpers Pagin::Helpers
7
+
8
+ app.before do
9
+ page_pattern = /\/page\/(\d+)(\/)?(\.[^\.\/]+)?$/
10
+ request.path_info.match(page_pattern)
11
+
12
+ if $1
13
+ page $1
14
+ request.path_info = request.path_info.gsub!(page_pattern, '')+$3.to_s
15
+ request.path_info.gsub!(/^(?!\/)/) { |s| "/"+s } # force the first slash if not avail
16
+ end
17
+ end
18
+ end
19
+
20
+ module Helpers
21
+ def page(pg = 1)
22
+ unless pg == 1
23
+ @page = pg.to_i
24
+ end
25
+
26
+ @page || 1
27
+ end
28
+ end
29
+ end
30
+
31
+ Sinatra::Application.register Pagin
32
+ end
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sinatra-pagin}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Yung Hwa Kwon"]
12
+ s.date = %q{2010-02-15}
13
+ s.description = %q{Small utility to process paginated urls without modifying the mapped paths in your Sinatra app}
14
+ s.email = %q{yung.kwon@nowk.net}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "Changelog.md",
21
+ "Gemfile",
22
+ "README.md",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/sinatra/pagin.rb",
26
+ "sinatra-pagin.gemspec",
27
+ "spec/pagin_spec.rb",
28
+ "spec/spec.opts",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/nowk/sinatra-pagin}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{Small utility to process paginated urls without modifying the mapped paths in your Sinatra app}
36
+ s.test_files = [
37
+ "spec/pagin_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_runtime_dependency(%q<sinatra>, [">= 0.9.4"])
47
+ s.add_development_dependency(%q<rspec>, [">= 1.2.8"])
48
+ s.add_development_dependency(%q<webrat>, [">= 0.7.0"])
49
+ s.add_development_dependency(%q<rack-test>, [">= 0.5.2"])
50
+ else
51
+ s.add_dependency(%q<sinatra>, [">= 0.9.4"])
52
+ s.add_dependency(%q<rspec>, [">= 1.2.8"])
53
+ s.add_dependency(%q<webrat>, [">= 0.7.0"])
54
+ s.add_dependency(%q<rack-test>, [">= 0.5.2"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<sinatra>, [">= 0.9.4"])
58
+ s.add_dependency(%q<rspec>, [">= 1.2.8"])
59
+ s.add_dependency(%q<webrat>, [">= 0.7.0"])
60
+ s.add_dependency(%q<rack-test>, [">= 0.5.2"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,167 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class MyTestApp < Sinatra::Base
4
+ set :environment, :test
5
+
6
+ get "/"do
7
+ "Hello World!"
8
+ end
9
+
10
+ get "/a/pathed" do
11
+ "Foo Bar!"
12
+ end
13
+ end
14
+
15
+ describe 'Sinatra' do
16
+
17
+ def app
18
+ @app ||= MyTestApp
19
+ end
20
+
21
+ def get_absolute(uri)
22
+ page_pattern = /\/page\/(\d+)(\/)?(\.[^\.\/]+)?$/
23
+ uri.match(page_pattern)
24
+
25
+ if $1
26
+ uri = uri.gsub(page_pattern, '')+$3.to_s
27
+ uri.gsub!(/^(?!\/)/) { |s| "/"+s }
28
+ end
29
+
30
+ return uri
31
+ end
32
+
33
+ describe "MyTestApp" do
34
+ it "should actually work" do
35
+ get "/" do |response|
36
+ response.should be_ok
37
+ response.should contain "Hello World!"
38
+ end
39
+
40
+ get "/a/pathed" do |response|
41
+ response.should be_ok
42
+ response.should contain "Foo Bar!"
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "without Pagin" do
48
+ ["/", "/a/pathed"].each do |uri|
49
+ it "should respond ok to #{uri}" do
50
+ get uri
51
+ last_response.should be_ok
52
+ last_request.url.should == "http://example.org#{uri}"
53
+ end
54
+ end
55
+
56
+ ["/page/45", "/a/pathed/page/2"].each do |uri|
57
+ it "should not respond ok to #{uri}" do
58
+ get uri
59
+ last_response.should_not be_ok
60
+ last_response.status.should == 404
61
+ last_request.url.should == "http://example.org#{uri}"
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "with Pagin" do
67
+ before(:all) do
68
+ MyTestApp.register Sinatra::Pagin
69
+ end
70
+
71
+ ["/", "/a/pathed"].each do |uri|
72
+ it "should respond ok to #{uri}" do
73
+ get uri
74
+ last_response.should be_ok
75
+ last_request.url.should == "http://example.org#{uri}"
76
+ end
77
+ end
78
+
79
+ ["/page/123", "/a/pathed/page/32"].each do |uri|
80
+ it "should respond ok to #{uri}" do
81
+ get uri
82
+ last_response.should be_ok
83
+ last_request.url.should == "http://example.org#{get_absolute(uri)}"
84
+ end
85
+ end
86
+
87
+ [
88
+ "/page",
89
+ "/page/",
90
+ "/page/a",
91
+ "/a/pathed/page",
92
+ "/a/pathed/page/",
93
+ "/a/pathed/page/1two",
94
+ "/a/pathed/page/one2three"
95
+ ].each do |uri|
96
+ it "should not respond ok to #{uri}" do
97
+ get uri
98
+ last_response.should_not be_ok
99
+ last_response.status.should == 404
100
+ last_request.url.should == "http://example.org#{uri}"
101
+ end
102
+ end
103
+
104
+ it "should not respond ok to multiple interations of /page/# ex: /page/1/page/2" do
105
+ get "/page/1/page/2"
106
+ last_response.should_not be_ok
107
+ last_response.status.should == 404
108
+ last_request.url.should == "http://example.org/page/1"
109
+ end
110
+
111
+ it "should accept a page/0" do
112
+ get "/page/0"
113
+ last_response.should be_ok
114
+ last_request.url.should == "http://example.org/"
115
+ end
116
+
117
+ describe ".:format" do
118
+ before(:all) do
119
+ app.get "/a/pathed/with.:format" do
120
+ "Howdy!"
121
+ end
122
+ end
123
+
124
+ it "should respond ok to a path with a .:format" do
125
+ get "/a/pathed/with.html"
126
+ last_response.should be_ok
127
+ last_response.body.should contain "Howdy!"
128
+ last_request.url.should == "http://example.org/a/pathed/with.html"
129
+ end
130
+
131
+ it "should reattach the .:format back to the url" do
132
+ get "/a/pathed/with/page/3.html"
133
+ last_response.should be_ok
134
+ last_response.body.should contain "Howdy!"
135
+ last_request.url.should == "http://example.org/a/pathed/with.html"
136
+ end
137
+ end
138
+ end
139
+
140
+ describe "Helpers" do
141
+ include Sinatra::Pagin::Helpers
142
+
143
+ describe "current_page" do
144
+ it "should return 1 if @page.nil?" do
145
+ page.should == 1
146
+ end
147
+
148
+ [1, 21, 302, "4", "501", "602"].each do |p|
149
+ it "should return value of page #{p} in it's integer format" do
150
+ page p
151
+ page.should == p.to_i
152
+ end
153
+ end
154
+
155
+ it "should actually work inside the app" do
156
+ app.get "/get/page/value/if" do
157
+ "The page is #{page}"
158
+ end
159
+
160
+ get "/get/page/value/if/page/123"
161
+ last_response.should be_ok
162
+ last_response.body.should == "The page is 123"
163
+ last_request.url.should == "http://example.org/get/page/value/if"
164
+ end
165
+ end
166
+ end
167
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'sinatra'
5
+ require 'sinatra/pagin'
6
+ require 'rack/test'
7
+ require 'spec'
8
+ require 'spec/autorun'
9
+ require "webrat"
10
+ #require 'mocha'
11
+
12
+ Webrat.configure do |config|
13
+ config.mode = :rack
14
+ end
15
+
16
+ Spec::Runner.configure do |config|
17
+ config.include Rack::Test::Methods
18
+ config.include Webrat::Methods
19
+ config.include Webrat::Matchers
20
+ # config.mock_with :mocha
21
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-pagin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yung Hwa Kwon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-15 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.4
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.8
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: webrat
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.7.0
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: rack-test
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.5.2
54
+ version:
55
+ description: Small utility to process paginated urls without modifying the mapped paths in your Sinatra app
56
+ email: yung.kwon@nowk.net
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - README.md
63
+ files:
64
+ - .gitignore
65
+ - Changelog.md
66
+ - Gemfile
67
+ - README.md
68
+ - Rakefile
69
+ - VERSION
70
+ - lib/sinatra/pagin.rb
71
+ - sinatra-pagin.gemspec
72
+ - spec/pagin_spec.rb
73
+ - spec/spec.opts
74
+ - spec/spec_helper.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/nowk/sinatra-pagin
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.5
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Small utility to process paginated urls without modifying the mapped paths in your Sinatra app
103
+ test_files:
104
+ - spec/pagin_spec.rb
105
+ - spec/spec_helper.rb