roda-parse-request 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e3b856b38214108afc6d6189d04eff69891a185
4
+ data.tar.gz: 610dd77acdbd59c68ea77c61a85cb3955bc793a7
5
+ SHA512:
6
+ metadata.gz: 0de60c292612009d318286cd624a36f70d8c487678b06cda38be2d9350b9e92ae5abee99456d5daec53c4ac5b251afdf2ad15cad7aa86b683b2518ced4f53bd9
7
+ data.tar.gz: c11050feec31265706b6ca688324dd75ae8aca6d0f0dc82d688e99aa88118d39c39c97ffa460dbc464a75086c77dade693385899d04c0dfb3b7415d22dc1a619
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in roda-parser.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ roda-parse-request (0.0.1)
5
+ roda (~> 1.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ rack (1.6.0)
12
+ rack-test (0.6.3)
13
+ rack (>= 1.0)
14
+ rake (10.4.2)
15
+ roda (1.3.0)
16
+ rack
17
+ rspec (3.2.0)
18
+ rspec-core (~> 3.2.0)
19
+ rspec-expectations (~> 3.2.0)
20
+ rspec-mocks (~> 3.2.0)
21
+ rspec-core (3.2.1)
22
+ rspec-support (~> 3.2.0)
23
+ rspec-expectations (3.2.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.2.0)
26
+ rspec-mocks (3.2.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.2.0)
29
+ rspec-support (3.2.2)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler (~> 1.7)
36
+ rack-test
37
+ rake (~> 10.0)
38
+ roda-parse-request!
39
+ rspec (~> 3.1)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 3scale
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # roda-parse-request
2
+
3
+ [roda](http://roda.jeremyevans.net/) plugin which automatically parses JSON and url-encoded requests
4
+
5
+
6
+ ## Usage
7
+
8
+ In your `Gemfile`:
9
+
10
+ ```ruby
11
+ gem 'roda-parse-request'
12
+ ```
13
+
14
+
15
+ In your roda app:
16
+
17
+ ```ruby
18
+ require 'roda'
19
+ require 'roda-parse-request'
20
+
21
+ class App < Roda
22
+ plugin :parse_request
23
+
24
+ route do |r|
25
+
26
+ r.post do
27
+ # You can use request.parsed_body here
28
+ # By default, it will contain response.body parsed as a json or url-encoded
29
+ # request (if the request was sent with the appropiate content-type headers)
30
+ end
31
+ end
32
+ end
33
+ ```
34
+
35
+
36
+ ## Customization
37
+
38
+ It is possible to add your own parsers to the plugin, like so:
39
+
40
+ ```ruby
41
+ require 'roda'
42
+ require 'roda-parse-request'
43
+
44
+ class App < Roda
45
+ plugin :parse_request, parsers: {
46
+ 'upcase' => ->(data) { data.upcase },
47
+ }
48
+
49
+ route do |r|
50
+
51
+ r.post do
52
+ # You can still use json and url-encoded parsed requests in request.parsed_body
53
+ # But now, if you get a request with Content-Type = 'upcase', the body
54
+ # will be automatically set to all caps.
55
+ end
56
+ end
57
+ end
58
+ ```
59
+
60
+ See the specs for more examples
61
+
62
+ ## Running specs
63
+
64
+ Clone the repo, run `bundle` and then `rspec`.
65
+
66
+
67
+ ## License
68
+
69
+ MIT License (see LICENSE)
70
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1 @@
1
+ require 'roda/parse_request'
@@ -0,0 +1,50 @@
1
+ require 'roda'
2
+ require 'roda/parse_request/version'
3
+
4
+ require 'uri'
5
+ require 'json'
6
+
7
+ class Roda
8
+ module ParseRequest
9
+
10
+ OPTS = {}.freeze
11
+
12
+ DEFAULT_PARSERS = {
13
+ 'application/x-www-form-urlencoded' => ->(data) { URI.decode_www_form(data) },
14
+ 'application/json' => ->(data) { JSON.parse(data) }
15
+ }.freeze
16
+
17
+ IDENTITY_PARSER = (->(data) { data }).freeze
18
+
19
+ # Set the classes to automatically convert to JSON
20
+ def self.configure(app, opts=OPTS)
21
+ parsers = DEFAULT_PARSERS.merge(opts[:parsers] || {})
22
+ app.opts[:parse_request_parsers] ||= parsers
23
+ app.opts[:parse_request_parsers].freeze
24
+ end
25
+
26
+ module ClassMethods
27
+ def parse_request_parsers
28
+ opts[:parse_request_parsers]
29
+ end
30
+ end
31
+
32
+ module RequestMethods
33
+ def parsed_body
34
+ @parsed_body ||= parse_body(body)
35
+ end
36
+
37
+ private
38
+
39
+ def parse_body(input)
40
+ data = input.read; input.rewind
41
+
42
+ parser = roda_class.parse_request_parsers[media_type] || IDENTITY_PARSER
43
+
44
+ parser.call(data)
45
+ end
46
+ end
47
+ end
48
+
49
+ RodaPlugins.register_plugin(:parse_request, ParseRequest)
50
+ end
@@ -0,0 +1,5 @@
1
+ class Roda
2
+ module ParseRequest
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'roda/parse_request/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "roda-parse-request"
8
+ spec.version = Roda::ParseRequest::VERSION
9
+ spec.authors = ["Michal Cichra", "Enrique García"]
10
+ spec.email = ["michal@o2h.cz"]
11
+ spec.summary = %q{Roda plugin which parses the request body of JSON and url-encoded requests automatically}
12
+ spec.description = %q{It also accepts custom transformations via configuration.}
13
+ spec.homepage = "https://github.com/3scale/roda-parse-request"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.1"
24
+ spec.add_development_dependency "rack-test", "~> 0.6"
25
+ spec.add_dependency "roda", "~> 1.3"
26
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Roda::ParseRequest do
4
+
5
+ context "default parsers" do
6
+
7
+ let(:app) do
8
+ Class.new(Roda) do
9
+ plugin :parse_request
10
+
11
+ route do |r|
12
+ r.post do
13
+ request.parsed_body.to_json
14
+ end
15
+ end
16
+ end.app.freeze
17
+ end
18
+
19
+ it 'parses json' do
20
+ post '/', '{"valid":true}', 'CONTENT_TYPE' => 'application/json'
21
+ expect(last_response.status).to eq(200)
22
+ expect(last_response.body).to eq('{"valid":true}')
23
+ end
24
+
25
+ it 'parses urlencoded' do
26
+ post '/', 'a=b&c=d', 'CONTENT_TYPE' => 'application/x-www-form-urlencoded'
27
+ expect(last_response.status).to eq(200)
28
+ expect(last_response.body).to eq('[["a","b"],["c","d"]]')
29
+ end
30
+
31
+ it 'works when no parser is found' do
32
+ post '/', 'hello', 'CONTENT_TYPE' => 'something/not-expected'
33
+ expect(last_response.status).to eq(200)
34
+ expect(last_response.body).to eq('"hello"')
35
+ end
36
+
37
+ end
38
+
39
+ context "custom parsers" do
40
+ let(:app) do
41
+ Class.new(Roda) do
42
+ plugin :parse_request, parsers: {
43
+ 'hodor' => ->(data) { 'hodor' },
44
+ 'upcase' => ->(data) { data.upcase },
45
+ 'application/json' => ->(data) { '<xml>troll</xml>' }
46
+ }
47
+
48
+ route do |r|
49
+ r.post do
50
+ request.parsed_body.to_json
51
+ end
52
+ end
53
+ end.app.freeze
54
+ end
55
+
56
+ it 'works with the simplest case' do
57
+ post '/', 'hey', 'CONTENT_TYPE' => 'hodor'
58
+ expect(last_response.status).to eq(200)
59
+ expect(last_response.body).to eq('"hodor"')
60
+ end
61
+
62
+ it 'can access the data' do
63
+ post '/', 'hey', 'CONTENT_TYPE' => 'upcase'
64
+ expect(last_response.status).to eq(200)
65
+ expect(last_response.body).to eq('"HEY"')
66
+ end
67
+
68
+ it 'can use the default parsers' do
69
+ post '/', 'a=b&c=d', 'CONTENT_TYPE' => 'application/x-www-form-urlencoded'
70
+ expect(last_response.status).to eq(200)
71
+ expect(last_response.body).to eq('[["a","b"],["c","d"]]')
72
+ end
73
+
74
+ it 'can override default parsers' do
75
+ post '/', '{"valid":true}', 'CONTENT_TYPE' => 'application/json'
76
+ expect(last_response.status).to eq(200)
77
+ expect(last_response.body).to eq('"<xml>troll</xml>"')
78
+ end
79
+
80
+ end
81
+
82
+
83
+ end
@@ -0,0 +1,75 @@
1
+ require 'roda/parse_request'
2
+ require 'rack/test'
3
+
4
+ RSpec.configure do |config|
5
+ # rspec-expectations config goes here. You can use an alternate
6
+ # assertion/expectation library such as wrong or the stdlib/minitest
7
+ # assertions if you prefer.
8
+ config.expect_with :rspec do |expectations|
9
+ # This option will default to `true` in RSpec 4. It makes the `description`
10
+ # and `failure_message` of custom matchers include text for helper methods
11
+ # defined using `chain`, e.g.:
12
+ # be_bigger_than(2).and_smaller_than(4).description
13
+ # # => "be bigger than 2 and smaller than 4"
14
+ # ...rather than:
15
+ # # => "be bigger than 2"
16
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
17
+ end
18
+
19
+ # rspec-mocks config goes here. You can use an alternate test double
20
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
21
+ config.mock_with :rspec do |mocks|
22
+ # Prevents you from mocking or stubbing a method that does not exist on
23
+ # a real object. This is generally recommended, and will default to
24
+ # `true` in RSpec 4.
25
+ mocks.verify_partial_doubles = true
26
+ end
27
+
28
+
29
+ config.include Rack::Test::Methods
30
+
31
+ # These two settings work together to allow you to limit a spec run
32
+ # to individual examples or groups you care about by tagging them with
33
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
34
+ # get run.
35
+ config.filter_run :focus
36
+ config.run_all_when_everything_filtered = true
37
+
38
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
39
+ # For more details, see:
40
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
41
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
42
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
43
+ config.disable_monkey_patching!
44
+
45
+ # This setting enables warnings. It's recommended, but in some cases may
46
+ # be too noisy due to issues in dependencies.
47
+ config.warnings = true
48
+
49
+ # Many RSpec users commonly either run the entire suite or an individual
50
+ # file, and it's useful to allow more verbose output when running an
51
+ # individual spec file.
52
+ if config.files_to_run.one?
53
+ # Use the documentation formatter for detailed output,
54
+ # unless a formatter has already been configured
55
+ # (e.g. via a command-line flag).
56
+ config.default_formatter = 'doc'
57
+ end
58
+
59
+ # Print the 10 slowest examples and example groups at the
60
+ # end of the spec run, to help surface which specs are running
61
+ # particularly slow.
62
+ config.profile_examples = 4
63
+
64
+ # Run specs in random order to surface order dependencies. If you find an
65
+ # order dependency and want to debug it, you can fix the order by providing
66
+ # the seed, which is printed after each run.
67
+ # --seed 1234
68
+ config.order = :random
69
+
70
+ # Seed global randomization in this process using the `--seed` CLI option.
71
+ # Setting this allows you to use `--seed` to deterministically reproduce
72
+ # test failures related to randomization by passing the same `--seed` value
73
+ # as the one that triggered the failure.
74
+ Kernel.srand config.seed
75
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roda-parse-request
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michal Cichra
8
+ - Enrique García
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-03-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.1'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.1'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rack-test
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.6'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.6'
70
+ - !ruby/object:Gem::Dependency
71
+ name: roda
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '1.3'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.3'
84
+ description: It also accepts custom transformations via configuration.
85
+ email:
86
+ - michal@o2h.cz
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - lib/roda-parse-request.rb
98
+ - lib/roda/parse_request.rb
99
+ - lib/roda/parse_request/version.rb
100
+ - roda-parse-request.gemspec
101
+ - spec/roda_parse_request_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/3scale/roda-parse-request
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.5
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Roda plugin which parses the request body of JSON and url-encoded requests
127
+ automatically
128
+ test_files:
129
+ - spec/roda_parse_request_spec.rb
130
+ - spec/spec_helper.rb
131
+ has_rdoc: