raml-rb 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -3
- data/lib/core_ext/hash.rb +6 -0
- data/lib/raml/body.rb +3 -3
- data/lib/raml/method.rb +16 -0
- data/lib/raml/parser.rb +14 -1
- data/lib/raml/parser/method.rb +12 -8
- data/lib/raml/parser/resource.rb +8 -6
- data/lib/raml/parser/root.rb +1 -0
- data/lib/raml/parser/util.rb +1 -9
- data/lib/raml/resource.rb +4 -0
- data/lib/raml/response.rb +8 -0
- data/lib/raml/root.rb +4 -0
- data/lib/raml/version.rb +1 -1
- data/raml-rb.gemspec +1 -1
- data/spec/lib/core_ext/hash_spec.rb +25 -0
- data/spec/lib/raml/body_spec.rb +8 -8
- data/spec/lib/raml/method_spec.rb +3 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f32149c287b8afb783fe1e9505634c1d913d0a5e
|
4
|
+
data.tar.gz: c4cff4f2e32605588cd5495eb72509cef3387b1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c88c819e3af6f4b49c1e13cb9dac144f0be1f0c999bc6502638e7e2bc100088d1cc2fc40d66ae2850bb1c6163646ebbf32c41320bcf8da0cec9c4bbd1c7c9bbb
|
7
|
+
data.tar.gz: 77596856bd5b3383fb42e33761b2708d78bd634b7ea228f4c1940bd2e00e7a63b145c9cdbfeb0f66291030e08b7f8263ba9ffb8b82beaf78ff57ab3f8f2e2aec
|
data/README.md
CHANGED
@@ -2,18 +2,25 @@
|
|
2
2
|
|
3
3
|
A RAML parser, implemented in Ruby.
|
4
4
|
|
5
|
-
##
|
5
|
+
## Installation
|
6
6
|
|
7
7
|
```
|
8
|
+
gem install raml-rb
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```Ruby
|
8
14
|
raml = Raml::Parser.parse("#%RAML 0.8\ntitle: World Music API\nbaseUri: http://example.api.com/{version}")
|
9
|
-
raml =
|
15
|
+
raml = Raml::Parser.parse_file('path/to/file.raml')
|
10
16
|
```
|
11
17
|
|
12
18
|
## Todo
|
13
19
|
|
14
20
|
0. Parameters for Resource Types and Traits.
|
15
21
|
0. Ensure all attributes are supported.
|
16
|
-
0.
|
22
|
+
0. Documentation generator.
|
23
|
+
0. RAML file generator.
|
17
24
|
|
18
25
|
## Author
|
19
26
|
|
data/lib/raml/body.rb
CHANGED
data/lib/raml/method.rb
CHANGED
@@ -9,5 +9,21 @@ module Raml
|
|
9
9
|
@query_parameters = []
|
10
10
|
end
|
11
11
|
|
12
|
+
def response_codes
|
13
|
+
[].tap do |codes|
|
14
|
+
responses.each do |response|
|
15
|
+
codes << response.code
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def content_types
|
21
|
+
[].tap do |types|
|
22
|
+
responses.each do |response|
|
23
|
+
types << response.content_types
|
24
|
+
end
|
25
|
+
end.flatten.uniq
|
26
|
+
end
|
27
|
+
|
12
28
|
end
|
13
29
|
end
|
data/lib/raml/parser.rb
CHANGED
@@ -9,13 +9,26 @@ require 'raml/parser/query_parameter'
|
|
9
9
|
module Raml
|
10
10
|
class Parser
|
11
11
|
|
12
|
+
def initialize
|
13
|
+
Psych.add_domain_type 'include', 'include' do |_, value|
|
14
|
+
File.read(value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
12
18
|
def parse(yaml)
|
13
19
|
raml = YAML.load(yaml)
|
14
20
|
Raml::Parser::Root.new.parse(raml)
|
15
21
|
end
|
16
22
|
|
17
23
|
def parse_file(path)
|
18
|
-
|
24
|
+
# Change directories so that relative file !includes work properly
|
25
|
+
wd = Dir.pwd
|
26
|
+
Dir.chdir File.dirname(path)
|
27
|
+
|
28
|
+
raml = parse File.read(File.basename(path))
|
29
|
+
|
30
|
+
Dir.chdir wd
|
31
|
+
raml
|
19
32
|
end
|
20
33
|
|
21
34
|
def self.parse(yaml)
|
data/lib/raml/parser/method.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'forwardable'
|
2
|
+
require 'core_ext/hash'
|
2
3
|
require 'raml/method'
|
3
4
|
require 'raml/parser/response'
|
4
5
|
require 'raml/parser/query_parameter'
|
@@ -25,6 +26,7 @@ module Raml
|
|
25
26
|
@attributes = prepare_attributes(attributes)
|
26
27
|
|
27
28
|
apply_parents_traits
|
29
|
+
apply_traits
|
28
30
|
parse_attributes
|
29
31
|
|
30
32
|
method
|
@@ -32,13 +34,11 @@ module Raml
|
|
32
34
|
|
33
35
|
private
|
34
36
|
|
35
|
-
def parse_attributes
|
37
|
+
def parse_attributes
|
36
38
|
attributes.each do |key, value|
|
37
39
|
case key
|
38
40
|
when *BASIC_ATTRIBUTES
|
39
41
|
method.send("#{key}=".to_sym, value)
|
40
|
-
when 'is'
|
41
|
-
apply_traits(value)
|
42
42
|
when 'responses'
|
43
43
|
parse_responses(value)
|
44
44
|
when 'query_parameters'
|
@@ -62,19 +62,23 @@ module Raml
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def apply_parents_traits
|
65
|
-
|
65
|
+
parent.trait_names.each do |name|
|
66
|
+
apply_trait(name)
|
67
|
+
end if parent.trait_names.respond_to?(:each)
|
66
68
|
end
|
67
69
|
|
68
|
-
def apply_traits
|
69
|
-
|
70
|
+
def apply_traits
|
71
|
+
attributes['is'].each do |name|
|
70
72
|
apply_trait(name)
|
71
|
-
end
|
73
|
+
end if attributes['is'].respond_to?(:each)
|
74
|
+
|
75
|
+
attributes.delete('is')
|
72
76
|
end
|
73
77
|
|
74
78
|
def apply_trait(name)
|
75
79
|
unless traits[name].nil?
|
76
80
|
trait_attributes = prepare_attributes(traits[name])
|
77
|
-
|
81
|
+
@attributes = trait_attributes.deep_merge(attributes)
|
78
82
|
end
|
79
83
|
end
|
80
84
|
|
data/lib/raml/parser/resource.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'forwardable'
|
2
|
+
require 'core_ext/hash'
|
2
3
|
require 'raml/resource'
|
3
4
|
require 'raml/parser/method'
|
4
5
|
require 'raml/parser/util'
|
@@ -24,13 +25,14 @@ module Raml
|
|
24
25
|
@parent_node = parent_node
|
25
26
|
@resource = Raml::Resource.new(@parent_node, uri_partial)
|
26
27
|
@attributes = prepare_attributes(attributes)
|
28
|
+
apply_resource_type
|
27
29
|
parse_attributes
|
28
30
|
resource
|
29
31
|
end
|
30
32
|
|
31
33
|
private
|
32
34
|
|
33
|
-
def parse_attributes
|
35
|
+
def parse_attributes
|
34
36
|
attributes.each do |key, value|
|
35
37
|
key = underscore(key)
|
36
38
|
case key
|
@@ -38,8 +40,6 @@ module Raml
|
|
38
40
|
resources << Raml::Parser::Resource.new(self).parse(resource, key, value)
|
39
41
|
when *METHODS
|
40
42
|
resource.methods << Raml::Parser::Method.new(self).parse(key, value)
|
41
|
-
when 'type'
|
42
|
-
apply_resource_type(value)
|
43
43
|
when 'is'
|
44
44
|
@trait_names = value
|
45
45
|
else
|
@@ -48,10 +48,12 @@ module Raml
|
|
48
48
|
end if attributes
|
49
49
|
end
|
50
50
|
|
51
|
-
def apply_resource_type
|
52
|
-
|
51
|
+
def apply_resource_type
|
52
|
+
name = attributes['type']
|
53
|
+
if name and !resource_types[name].nil?
|
53
54
|
resource_attributes = prepare_attributes(resource_types[name])
|
54
|
-
|
55
|
+
@attributes.delete('type')
|
56
|
+
@attributes = resource_attributes.deep_merge(attributes)
|
55
57
|
end
|
56
58
|
end
|
57
59
|
|
data/lib/raml/parser/root.rb
CHANGED
data/lib/raml/parser/util.rb
CHANGED
@@ -7,19 +7,11 @@ module Raml
|
|
7
7
|
def prepare_attributes(attributes)
|
8
8
|
hash = {}
|
9
9
|
attributes.each do |key, value|
|
10
|
-
hash[underscore(key)] =
|
10
|
+
hash[underscore(key)] = value
|
11
11
|
end if attributes.respond_to?(:each)
|
12
12
|
hash
|
13
13
|
end
|
14
14
|
|
15
|
-
def parse_value(value)
|
16
|
-
if value.is_a?(String) && value.strip.start_with?('include!')
|
17
|
-
File.read value.match(/include!(.*)/)[1].strip
|
18
|
-
else
|
19
|
-
value
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
15
|
def underscore(string)
|
24
16
|
string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
25
17
|
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
|
data/lib/raml/resource.rb
CHANGED
data/lib/raml/response.rb
CHANGED
data/lib/raml/root.rb
CHANGED
data/lib/raml/version.rb
CHANGED
data/raml-rb.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Raml::VERSION
|
9
9
|
spec.authors = ['James Brennan']
|
10
10
|
spec.email = ['james@jamesbrennan.ca']
|
11
|
-
spec.summary =
|
11
|
+
spec.summary = 'A RAML parser implemented in Ruby'
|
12
12
|
spec.homepage = 'https://github.com/jpb/raml-rb'
|
13
13
|
spec.license = 'MIT'
|
14
14
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'core_ext/hash'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
|
5
|
+
describe '#deep_merge' do
|
6
|
+
subject { { key: 'left' }.deep_merge({ key: 'right'}) }
|
7
|
+
it { should == { key: 'right' } }
|
8
|
+
|
9
|
+
context 'nested hash' do
|
10
|
+
subject { { key: { key: { key: 'left' } } }.deep_merge({ key: { key: { key: 'right' } } }) }
|
11
|
+
it { should == { key: { key: { key: 'right' } } } }
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'nested hash with nil on original hash' do
|
15
|
+
subject { { key: { key: { key: nil } } }.deep_merge({ key: { key: { key: 'right' } } }) }
|
16
|
+
it { should == { key: { key: { key: 'right' } } } }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'nested hash with nil on merged hash' do
|
20
|
+
subject { { key: { key: { key: 'left' } } }.deep_merge({ key: { key: { key: nil } } }) }
|
21
|
+
it { should == { key: { key: { key: nil } } } }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/spec/lib/raml/body_spec.rb
CHANGED
@@ -3,19 +3,19 @@ require 'raml/body'
|
|
3
3
|
describe Raml::Body do
|
4
4
|
|
5
5
|
describe '.new' do
|
6
|
-
subject { Raml::Body.new('the
|
7
|
-
its(:
|
6
|
+
subject { Raml::Body.new('the content_type') }
|
7
|
+
its(:content_type) { should == 'the content_type' }
|
8
8
|
end
|
9
9
|
|
10
|
-
describe '#
|
11
|
-
let(:body) { Raml::Body.new('the
|
12
|
-
before { body.
|
13
|
-
subject { body.
|
14
|
-
it { should == '
|
10
|
+
describe '#content_type' do
|
11
|
+
let(:body) { Raml::Body.new('the content_type') }
|
12
|
+
before { body.content_type = 'content_type' }
|
13
|
+
subject { body.content_type }
|
14
|
+
it { should == 'content_type' }
|
15
15
|
end
|
16
16
|
|
17
17
|
describe '#schema' do
|
18
|
-
let(:body) { Raml::Body.new('the
|
18
|
+
let(:body) { Raml::Body.new('the content_type') }
|
19
19
|
before { body.schema = 'schema' }
|
20
20
|
subject { body.schema }
|
21
21
|
it { should == 'schema' }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: raml-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Brennan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- LICENSE.txt
|
80
80
|
- README.md
|
81
81
|
- Rakefile
|
82
|
+
- lib/core_ext/hash.rb
|
82
83
|
- lib/raml-rb.rb
|
83
84
|
- lib/raml.rb
|
84
85
|
- lib/raml/body.rb
|
@@ -102,6 +103,7 @@ files:
|
|
102
103
|
- raml-rb.gemspec
|
103
104
|
- spec/fixtures/all-the-things.raml
|
104
105
|
- spec/fixtures/basic.raml
|
106
|
+
- spec/lib/core_ext/hash_spec.rb
|
105
107
|
- spec/lib/raml/body_spec.rb
|
106
108
|
- spec/lib/raml/documentation_spec.rb
|
107
109
|
- spec/lib/raml/method_spec.rb
|
@@ -145,6 +147,7 @@ summary: A RAML parser implemented in Ruby
|
|
145
147
|
test_files:
|
146
148
|
- spec/fixtures/all-the-things.raml
|
147
149
|
- spec/fixtures/basic.raml
|
150
|
+
- spec/lib/core_ext/hash_spec.rb
|
148
151
|
- spec/lib/raml/body_spec.rb
|
149
152
|
- spec/lib/raml/documentation_spec.rb
|
150
153
|
- spec/lib/raml/method_spec.rb
|