praxis 0.14.0 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -1
- data/Gemfile +5 -0
- data/bin/praxis +7 -4
- data/lib/api_browser/package.json +1 -1
- data/lib/praxis/action_definition.rb +10 -5
- data/lib/praxis/application.rb +30 -0
- data/lib/praxis/bootloader_stages/subgroup_loader.rb +2 -2
- data/lib/praxis/bootloader_stages/warn_unloaded_files.rb +7 -2
- data/lib/praxis/file_group.rb +1 -1
- data/lib/praxis/handlers/json.rb +32 -0
- data/lib/praxis/handlers/www_form.rb +20 -0
- data/lib/praxis/handlers/xml.rb +78 -0
- data/lib/praxis/links.rb +4 -1
- data/lib/praxis/media_type.rb +55 -0
- data/lib/praxis/media_type_identifier.rb +198 -0
- data/lib/praxis/multipart/part.rb +16 -0
- data/lib/praxis/request.rb +34 -25
- data/lib/praxis/response.rb +22 -3
- data/lib/praxis/response_definition.rb +11 -36
- data/lib/praxis/restful_doc_generator.rb +1 -1
- data/lib/praxis/simple_media_type.rb +6 -1
- data/lib/praxis/types/media_type_common.rb +8 -3
- data/lib/praxis/types/multipart.rb +6 -6
- data/lib/praxis/version.rb +1 -1
- data/lib/praxis.rb +7 -1
- data/praxis.gemspec +1 -1
- data/spec/functional_spec.rb +3 -1
- data/spec/praxis/application_spec.rb +58 -1
- data/spec/praxis/handlers/json_spec.rb +37 -0
- data/spec/praxis/handlers/xml_spec.rb +155 -0
- data/spec/praxis/media_type_identifier_spec.rb +209 -0
- data/spec/praxis/media_type_spec.rb +50 -3
- data/spec/praxis/plugins/praxis_mapper_plugin_spec.rb +2 -2
- data/spec/praxis/request_spec.rb +39 -1
- data/spec/praxis/response_definition_spec.rb +12 -9
- data/spec/praxis/response_spec.rb +37 -6
- data/spec/praxis/types/collection_spec.rb +2 -2
- data/spec/praxis/types/multipart_spec.rb +17 -0
- data/spec/spec_app/app/controllers/instances.rb +1 -1
- data/spec/support/spec_media_types.rb +19 -0
- metadata +11 -6
- data/lib/praxis/content_type_parser.rb +0 -62
- data/spec/praxis/content_type_parser_spec.rb +0 -91
@@ -1,62 +0,0 @@
|
|
1
|
-
module Praxis
|
2
|
-
module ContentTypeParser
|
3
|
-
REGEXP = /^
|
4
|
-
\s*
|
5
|
-
(?<type>[^+;\s]+)
|
6
|
-
(\+(?<sub_type>[^;\s]+))?
|
7
|
-
(\s*;\s*)?
|
8
|
-
(?<params>.*?)?
|
9
|
-
(\s*;\s*)?
|
10
|
-
$/x
|
11
|
-
|
12
|
-
# Parses Content type
|
13
|
-
#
|
14
|
-
# @param [String] Content type to parse
|
15
|
-
# @return [Hash] A hash with keys: :type, :sub_type(optional) and :params(optional)
|
16
|
-
# @raise [ArgumentError] It fails when blank or weird content type is provided
|
17
|
-
#
|
18
|
-
# @example
|
19
|
-
# parse(nil) #=> Exception: Content type does not have any type defined (ArgumentError)
|
20
|
-
# parse('+json') #=> Exception: Content type does not have any type defined (ArgumentError)
|
21
|
-
# parse(';p1=11') #=> Exception: Content type does not have any type defined (ArgumentError)
|
22
|
-
#
|
23
|
-
# @example
|
24
|
-
# parse('text/xml') #=>
|
25
|
-
# {:type => "text/xml",
|
26
|
-
# :sub_type => nil,
|
27
|
-
# :params => {}}
|
28
|
-
#
|
29
|
-
# @example
|
30
|
-
# parse('application/vnd.something+json') #=>
|
31
|
-
# {:type => "application/vnd.something",
|
32
|
-
# :sub_type => "json",
|
33
|
-
# :params => {}}
|
34
|
-
#
|
35
|
-
# @example
|
36
|
-
# parse('application/vnd.something+json;p1=1.0;param_with_noval;p2=a13') #=>
|
37
|
-
# {:type => "application/vnd.something",
|
38
|
-
# :sub_type => "json",
|
39
|
-
# :params => {"p1"=>"1.0", "param_with_noval"=>nil, "p2"=>"a13"}}
|
40
|
-
#
|
41
|
-
def self.parse(content_type)
|
42
|
-
parsed = REGEXP.match(content_type.to_s)
|
43
|
-
raise(ArgumentError, 'Content type does not have any type defined') unless parsed
|
44
|
-
|
45
|
-
result = {
|
46
|
-
type: parsed[:type]
|
47
|
-
}
|
48
|
-
result[:sub_type] = parsed[:sub_type] if parsed[:sub_type]
|
49
|
-
if parsed[:params]
|
50
|
-
params = {}
|
51
|
-
parsed[:params].split(';').each do |param|
|
52
|
-
key, value = param.split('=')
|
53
|
-
key = key.to_s.strip
|
54
|
-
next if key.empty?
|
55
|
-
params[key.strip] = value && value.strip
|
56
|
-
end
|
57
|
-
result[:params] = params unless params.empty?
|
58
|
-
end
|
59
|
-
result
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
@@ -1,91 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Praxis::ContentTypeParser do
|
4
|
-
|
5
|
-
describe '.parse' do
|
6
|
-
context 'when content type is blank' do
|
7
|
-
it 'complains' do
|
8
|
-
expect{ subject.parse(nil) }.to raise_error(ArgumentError)
|
9
|
-
expect{ subject.parse('') }.to raise_error(ArgumentError)
|
10
|
-
expect{ subject.parse(' ') }.to raise_error(ArgumentError)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'when content type is weird' do
|
15
|
-
it 'complains' do
|
16
|
-
expect{ subject.parse('+json') }.to raise_error(ArgumentError)
|
17
|
-
expect{ subject.parse(';p1=2') }.to raise_error(ArgumentError)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'when content type has only type' do
|
22
|
-
it 'returns the type only' do
|
23
|
-
content_type = 'application/json'
|
24
|
-
expectation = {
|
25
|
-
type: 'application/json'
|
26
|
-
}
|
27
|
-
expect(subject.parse(content_type)).to eq(expectation)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context 'when content type has type and subtype' do
|
32
|
-
it 'returns both the type and the sub_type' do
|
33
|
-
content_type = 'application/vnd.something+json'
|
34
|
-
expectation = {
|
35
|
-
type: 'application/vnd.something',
|
36
|
-
sub_type: 'json'
|
37
|
-
}
|
38
|
-
expect(subject.parse(content_type)).to eq(expectation)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context 'when content type has type and params' do
|
43
|
-
it 'returns both the type and the params' do
|
44
|
-
content_type = 'application/json;p1=2;no_value;foo=bar'
|
45
|
-
expectation = {
|
46
|
-
type: 'application/json',
|
47
|
-
params: {"p1"=>"2", "no_value"=>nil, "foo"=>"bar"}
|
48
|
-
}
|
49
|
-
expect(subject.parse(content_type)).to eq(expectation)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
context 'when content type has type, sub_type and params' do
|
54
|
-
it 'returns them all' do
|
55
|
-
content_type = 'application/vnd.something+json;p1=2;no_value;foo=bar'
|
56
|
-
expectation = {
|
57
|
-
type: 'application/vnd.something',
|
58
|
-
sub_type: 'json',
|
59
|
-
params: {"p1"=>"2", "no_value"=>nil, "foo"=>"bar"}
|
60
|
-
}
|
61
|
-
expect(subject.parse(content_type)).to eq(expectation)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
|
66
|
-
context 'when there are spaces all around it should ignore them' do
|
67
|
-
it 'returns them all' do
|
68
|
-
content_type = ' application/vnd.something+json ; p1=2 ;no_value ; foo=bar ; '
|
69
|
-
expectation = {
|
70
|
-
type: 'application/vnd.something',
|
71
|
-
sub_type: 'json',
|
72
|
-
params: {"p1"=>"2", "no_value"=>nil, "foo"=>"bar"}
|
73
|
-
}
|
74
|
-
expect(subject.parse(content_type)).to eq(expectation)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
|
79
|
-
context 'when there are multiple ;;;;;;' do
|
80
|
-
it 'ignores them' do
|
81
|
-
content_type = 'application/vnd.something+json;;;; ; ;;;; ;;'
|
82
|
-
expectation = {
|
83
|
-
type: 'application/vnd.something',
|
84
|
-
sub_type: 'json'
|
85
|
-
}
|
86
|
-
expect(subject.parse(content_type)).to eq(expectation)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
91
|
-
end
|