raml-rb 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c055a6b5ac11a427a84bf58f3dd22d1d366c91d
4
- data.tar.gz: efeb25b9f8bb5004c9f5f65d50264285739c1bee
3
+ metadata.gz: f32149c287b8afb783fe1e9505634c1d913d0a5e
4
+ data.tar.gz: c4cff4f2e32605588cd5495eb72509cef3387b1c
5
5
  SHA512:
6
- metadata.gz: 0ceb137a50546a1a0ebbd5cdd3b30f3f62214cf46dc3084a5da2bdc3717b042c49a45b584e483089f64db07c5941a26425d1471e2535cf6facf92a86e3879687
7
- data.tar.gz: 561a64c167180a4b151e644692730197c0b565c1d76342387e269a0c341c839ce189734eb14e28eb565cd55f9372dc23add0764ce069d62d49bf92785844ea56
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
- ## Usage
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 = Rank::Parser.parse_file('path/to/file.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. Add documentation generator.
22
+ 0. Documentation generator.
23
+ 0. RAML file generator.
17
24
 
18
25
  ## Author
19
26
 
@@ -0,0 +1,6 @@
1
+ class ::Hash
2
+ def deep_merge(second)
3
+ merger = proc { |_, left, right| left.is_a?(Hash) && right.is_a?(Hash) ? left.merge(right, &merger) : right }
4
+ self.merge(second, &merger)
5
+ end
6
+ end
data/lib/raml/body.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  module Raml
2
2
  class Body
3
- attr_accessor :type, :schema
3
+ attr_accessor :content_type, :schema
4
4
 
5
- def initialize(type)
6
- @type = type
5
+ def initialize(content_type)
6
+ @content_type = content_type
7
7
  end
8
8
 
9
9
  end
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
- parse File.read(path)
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)
@@ -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(attributes = @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
- apply_traits(parent.trait_names) if !parent.trait_names.nil? && parent.trait_names.length
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(names)
69
- names.each do |name|
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
- parse_attributes(trait_attributes)
81
+ @attributes = trait_attributes.deep_merge(attributes)
78
82
  end
79
83
  end
80
84
 
@@ -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(attributes = @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(name)
52
- unless resource_types[name].nil?
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
- parse_attributes(resource_attributes)
55
+ @attributes.delete('type')
56
+ @attributes = resource_attributes.deep_merge(attributes)
55
57
  end
56
58
  end
57
59
 
@@ -15,6 +15,7 @@ module Raml
15
15
 
16
16
  def initialize
17
17
  @traits = {}
18
+ @resource_types = {}
18
19
  end
19
20
 
20
21
  def parse(attributes)
@@ -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)] = parse_value(value)
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
@@ -8,6 +8,10 @@ module Raml
8
8
  @methods = []
9
9
  end
10
10
 
11
+ def path
12
+ File.join(parent.path, uri_partial)
13
+ end
14
+
11
15
  def uri
12
16
  File.join(parent.uri, uri_partial)
13
17
  end
data/lib/raml/response.rb CHANGED
@@ -7,5 +7,13 @@ module Raml
7
7
  @bodies = []
8
8
  end
9
9
 
10
+ def content_types
11
+ [].tap do |types|
12
+ bodies.each do |body|
13
+ types << body.content_type
14
+ end
15
+ end.uniq
16
+ end
17
+
10
18
  end
11
19
  end
data/lib/raml/root.rb CHANGED
@@ -11,5 +11,9 @@ module Raml
11
11
  base_uri.sub('{version}', version.to_s)
12
12
  end
13
13
 
14
+ def path
15
+ ''
16
+ end
17
+
14
18
  end
15
19
  end
data/lib/raml/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Raml
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
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 = %q{A RAML parser implemented in Ruby}
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
@@ -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 type') }
7
- its(:type) { should == 'the type' }
6
+ subject { Raml::Body.new('the content_type') }
7
+ its(:content_type) { should == 'the content_type' }
8
8
  end
9
9
 
10
- describe '#type' do
11
- let(:body) { Raml::Body.new('the type') }
12
- before { body.type = 'type' }
13
- subject { body.type }
14
- it { should == 'type' }
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 type') }
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' }
@@ -14,4 +14,7 @@ describe Raml::Method do
14
14
  it { should == 'the title' }
15
15
  end
16
16
 
17
+ describe '#response_code' do
18
+ end
19
+
17
20
  end
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.2
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-05 00:00:00.000000000 Z
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