rack-matrix_params 0.0.1 → 0.0.5

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/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby
data/README.markdown CHANGED
@@ -1,18 +1,23 @@
1
1
  Rack::MatrixParams
2
2
  ==================
3
3
 
4
- Just simple Rack middleware to enable 'matrix' params
4
+ [![Build Status](https://travis-ci.org/jphastings/rack-matrix_params.png)](https://travis-ci.org/jphastings/rack-matrix_params)
5
5
 
6
- FEATURES:
7
- ---------
6
+
7
+ Just simple Rack middleware to enable 'matrix' params.
8
+
9
+ *Originally developed by [Michal Fojtik](https://github.com/mifo/rack-matrix-params).*
10
+
11
+ Features
12
+ --------
8
13
 
9
14
  - Allow you to use Matrix URLs:
10
15
  - http://localhost:9393/library;category=biology/book;author=Bond;hardcover=yes
11
16
  - http://localhost:9393/library/book;author=Bond;hardcover=yes
12
17
  - http://localhost:9393/library;category=biology/book?id=123
13
18
 
14
- EXAMPLES:
15
- ---------
19
+ Examples
20
+ --------
16
21
 
17
22
  ### Example Sinatra server
18
23
 
@@ -22,7 +27,7 @@ EXAMPLES:
22
27
  use Rack::MatrixParams
23
28
 
24
29
  get '/' do
25
- "Visit <a href=\"/library;category=biology/book;author=Bond;hardcover=yes\">/library;category=biology/book;author=Bond;hardcover=yes</a>"
30
+ "Visit <a href=\"/library;category=biology/book;author=Bond;hardcover=yes\">a matrix URL</a>."
26
31
  end
27
32
 
28
33
  get '/library/book' do
@@ -44,7 +49,14 @@ http://localhost:9393/library;category=biology/book?id=123
44
49
  params['library']['category']='biology'
45
50
  params['id']=123
46
51
 
47
- ## LICENSE
52
+ To Do
53
+ -----
54
+
55
+ * Write more tests!
56
+ * Investigate which environment variables are changed, so that hopefully the Rack Log (eg. in Sinatra) doesn't have the hacked query string visible.
57
+
58
+ LICENSE
59
+ -------
48
60
 
49
61
  Licensed to the Apache Software Foundation (ASF) under one or more
50
62
  contributor license agreements. See the NOTICE file distributed with
data/TODO ADDED
@@ -0,0 +1 @@
1
+ * Look at the POST section - something'sweird there!
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.5
@@ -40,50 +40,52 @@ module Rack
40
40
  # regular <form> parameters.
41
41
 
42
42
  def call(env)
43
- # Copy PATH_INFO to REQUEST_URI if Rack::Test
44
- env['REQUEST_URI'] = env['PATH_INFO'] if env['rack.test']
45
-
46
43
  # Split URI to components and then extract ;var=value pairs
47
- uri_components = env['REQUEST_URI'].split('/')
44
+ uri_components = env['PATH_INFO'].split('/')
48
45
  matrix_params = {}
49
46
  uri_components.each do |component|
50
- sub_components, value = component.split(/\;(\w+)\=/), nil
51
- next unless sub_components.first # Skip subcomponent if it's empty (usually /)
52
- while param=sub_components.pop do
53
- if value
54
- matrix_params[sub_components.first] ||= {}
55
- matrix_params[sub_components.first].merge!(
56
- param => value
57
- )
58
- value=nil
59
- next
60
- else
61
- value = param
47
+ sub_components, value = component.split(/\;([\w:]+)\=/), nil
48
+ next unless sub_components.first # Skip subcomponent if it's empty (usually /)
49
+ while param = sub_components.pop do
50
+ if value
51
+ # There are matrix components to this path element
52
+ (matrix_params[sub_components.first] ||= {}).merge!( param => value )
53
+
54
+ value = nil
55
+ next
56
+ else
57
+ # There are no matrix components to this path element
58
+ value = param
59
+ end
62
60
  end
63
61
  end
64
- end
62
+
63
+ # No need for anything else if there are no matrix params
64
+ return @app.call(env) if matrix_params.keys.empty?
65
65
 
66
66
  # If request method is POST, simply include matrix params in form_hash
67
67
  env['rack.request.form_hash'].merge!(matrix_params) if env['rack.request.form_hash']
68
68
 
69
69
  # For other methods it's a way complicated ;-)
70
- if env['REQUEST_METHOD']!='POST' and not matrix_params.keys.empty?
70
+ if env['REQUEST_METHOD'] != 'POST'
71
71
 
72
- # Rewrite current path and query string and strip all matrix params from it
73
- env['REQUEST_PATH'], env['PATH_INFO'] = env['REQUEST_URI'].gsub(/;([^\/]*)/, '').gsub(/\?(.*)$/, '')
74
- env['PATH_INFO'] = env['REQUEST_PATH']
75
- env['QUERY_STRING'] = env['QUERY_STRING'].gsub(/;([^\/]*)/, '').freeze
72
+ # Rewrite current path and query string and strip all query params from it
73
+ env['PATH_INFO'].gsub!(/;([^\/]*)/, '').gsub(/\?(.*)$/, '')
74
+
75
+ env['QUERY_STRING'].gsub!(/;([^\/]*)/, '').freeze
76
76
 
77
77
  new_params = matrix_params.collect do |component, params|
78
- params.collect { |k,v| "#{component}[#{k}]=#{CGI::escape(v.to_s)}" }
78
+ params.collect do |k,v|
79
+ "#{component}[#{k}]=#{CGI::escape(v.to_s)}"
80
+ end
79
81
  end.flatten
80
82
 
81
83
  # Add matrix params as a regular GET params
82
84
  env['QUERY_STRING'] += '&' if not env['QUERY_STRING'].empty?
83
85
  env['QUERY_STRING'] += "#{new_params.join('&')}"
84
86
  end
87
+
85
88
  @app.call(env)
86
89
  end
87
90
  end
88
-
89
91
  end
@@ -1,11 +1,10 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'rack/matrix_params/version'
5
4
 
6
5
  Gem::Specification.new do |gem|
7
6
  gem.name = "rack-matrix_params"
8
- gem.version = Rack::MatrixParams::VERSION
7
+ gem.version = open('./VERSION').read
9
8
  gem.authors = ["JP Hastings-Spital","Michal Fojtik"]
10
9
  gem.email = ["jphastings@gmail.com"]
11
10
  gem.description = %q{Rack middleware that populates the params variable with the contents of matrix parameters in a URL}
@@ -16,7 +15,10 @@ Gem::Specification.new do |gem|
16
15
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
16
  gem.require_paths = ["lib"]
18
17
 
19
- gem.add_development_dependency 'rspec'
18
+ gem.add_dependency 'rack'
19
+
20
20
  gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
21
22
  gem.add_development_dependency 'bundler'
23
+ gem.add_development_dependency 'rack-test'
22
24
  end
@@ -0,0 +1,64 @@
1
+ require File.join(File.dirname(__FILE__),'spec_helper')
2
+
3
+ describe Rack::MatrixParams do
4
+ include Rack::Test::Methods
5
+
6
+ let(:inner_app) do
7
+ lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['All good!'] ]}
8
+ end
9
+
10
+ let(:app) { described_class.new(inner_app) }
11
+
12
+ it 'should not change a normal GET query' do
13
+ get "/?key=value"
14
+
15
+ last_request.env['PATH_INFO'].should == "/"
16
+ last_request.env['REQUEST_METHOD'].should == "GET"
17
+ last_request.env['QUERY_STRING'].should == "key=value"
18
+ last_response.should be_ok
19
+ end
20
+
21
+ it 'should not change a normal POST query' do
22
+ post "/",{'key' => "value"}
23
+
24
+ last_request.env['PATH_INFO'].should == "/"
25
+ last_request.env['REQUEST_METHOD'].should == "POST"
26
+ last_request.env['rack.input'].read.should == "key=value"
27
+ last_response.should be_ok
28
+ end
29
+
30
+ it 'should add variables to the params when a matrix URL is used on a GET request' do
31
+ get "/path/info;key=value/more"
32
+
33
+ last_request.env['PATH_INFO'].should == "/path/info/more"
34
+ last_request.env['QUERY_STRING'].should == "info[key]=value"
35
+ last_response.should be_ok
36
+ end
37
+
38
+ it 'should not erase data in a query parameter if a matrix parameter has the same keyname' do
39
+ get '/path/info;key=value/more?info[key]=somethingelse'
40
+
41
+ last_request.env['QUERY_STRING'].should == "info[key]=somethingelse&info[key]=value"
42
+ last_response.should be_ok
43
+ end
44
+
45
+ it 'should put matrix parameters after query parameters' do
46
+ get '/path/info;key=value/more?queryparams'
47
+
48
+ last_request.env['QUERY_STRING'].should == "queryparams&info[key]=value"
49
+ last_request.env['QUERY_STRING'].should_not == "info[key]=value&queryparams"
50
+ last_response.should be_ok
51
+ end
52
+
53
+ it 'should provide access to the matrix parameters via the params variable' do
54
+ get '/path/info;key=value/more?queryparams'
55
+
56
+ last_request.params.should == {'queryparams'=>nil, 'info'=>{'key'=>'value'}}
57
+ end
58
+
59
+ it 'should work in the blinkbox books format' do
60
+ get '/client;img:w=200/server;v=0/test/9780111222333.sample.epub/OEBPS/chapter_001.html'
61
+
62
+ last_request.params.should == {'client'=>{'img:w' => '200'}, 'server'=>{'v'=>'0'}}
63
+ end
64
+ end
@@ -0,0 +1,7 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'rspec'
4
+ require 'rack/test'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'rack/matrix_params'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-matrix_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,17 +10,17 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-22 00:00:00.000000000 Z
13
+ date: 2013-02-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rspec
16
+ name: rack
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
21
21
  - !ruby/object:Gem::Version
22
22
  version: '0'
23
- type: :development
23
+ type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  none: false
@@ -44,6 +44,22 @@ dependencies:
44
44
  - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
47
63
  - !ruby/object:Gem::Dependency
48
64
  name: bundler
49
65
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +76,22 @@ dependencies:
60
76
  - - ! '>='
61
77
  - !ruby/object:Gem::Version
62
78
  version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: rack-test
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
63
95
  description: Rack middleware that populates the params variable with the contents
64
96
  of matrix parameters in a URL
65
97
  email:
@@ -69,12 +101,16 @@ extensions: []
69
101
  extra_rdoc_files: []
70
102
  files:
71
103
  - .gitignore
104
+ - .travis.yml
72
105
  - Gemfile
73
106
  - README.markdown
74
107
  - Rakefile
108
+ - TODO
109
+ - VERSION
75
110
  - lib/rack/matrix_params.rb
76
- - lib/rack/matrix_params/version.rb
77
111
  - rack-matrix_params.gemspec
112
+ - spec/matrix_params_spec.rb
113
+ - spec/spec_helper.rb
78
114
  homepage: http://github.com/jphastings/rack-matrix_params
79
115
  licenses: []
80
116
  post_install_message:
@@ -87,22 +123,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
123
  - - ! '>='
88
124
  - !ruby/object:Gem::Version
89
125
  version: '0'
90
- segments:
91
- - 0
92
- hash: 2501651430979619619
93
126
  required_rubygems_version: !ruby/object:Gem::Requirement
94
127
  none: false
95
128
  requirements:
96
129
  - - ! '>='
97
130
  - !ruby/object:Gem::Version
98
131
  version: '0'
99
- segments:
100
- - 0
101
- hash: 2501651430979619619
102
132
  requirements: []
103
133
  rubyforge_project:
104
- rubygems_version: 1.8.23
134
+ rubygems_version: 1.8.24
105
135
  signing_key:
106
136
  specification_version: 3
107
137
  summary: Allows the use of matrix URLs in a Rack environment
108
- test_files: []
138
+ test_files:
139
+ - spec/matrix_params_spec.rb
140
+ - spec/spec_helper.rb
141
+ has_rdoc:
@@ -1,5 +0,0 @@
1
- module Rack
2
- class MatrixParams
3
- VERSION = '0.0.1'
4
- end
5
- end