raml-rb 0.0.2

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.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +3 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +20 -0
  7. data/Rakefile +9 -0
  8. data/lib/raml/body.rb +10 -0
  9. data/lib/raml/documentation.rb +5 -0
  10. data/lib/raml/errors/unknown_attribute_error.rb +4 -0
  11. data/lib/raml/method.rb +13 -0
  12. data/lib/raml/parser/body.rb +38 -0
  13. data/lib/raml/parser/documentation.rb +37 -0
  14. data/lib/raml/parser/method.rb +83 -0
  15. data/lib/raml/parser/query_parameter.rb +39 -0
  16. data/lib/raml/parser/resource.rb +60 -0
  17. data/lib/raml/parser/response.rb +42 -0
  18. data/lib/raml/parser/root.rb +73 -0
  19. data/lib/raml/parser/util.rb +31 -0
  20. data/lib/raml/parser.rb +30 -0
  21. data/lib/raml/query_parameter.rb +10 -0
  22. data/lib/raml/resource.rb +16 -0
  23. data/lib/raml/response.rb +11 -0
  24. data/lib/raml/root.rb +15 -0
  25. data/lib/raml/version.rb +3 -0
  26. data/lib/raml-rb.rb +1 -0
  27. data/lib/raml.rb +12 -0
  28. data/raml-rb.gemspec +24 -0
  29. data/spec/fixtures/all-the-things.raml +54 -0
  30. data/spec/fixtures/basic.raml +41 -0
  31. data/spec/lib/raml/body_spec.rb +24 -0
  32. data/spec/lib/raml/documentation_spec.rb +19 -0
  33. data/spec/lib/raml/method_spec.rb +17 -0
  34. data/spec/lib/raml/parser/body_spec.rb +21 -0
  35. data/spec/lib/raml/parser/documentation_spec.rb +15 -0
  36. data/spec/lib/raml/parser/method_spec.rb +35 -0
  37. data/spec/lib/raml/parser/query_parameter_spec.rb +23 -0
  38. data/spec/lib/raml/parser/resource_spec.rb +49 -0
  39. data/spec/lib/raml/parser/response_spec.rb +30 -0
  40. data/spec/lib/raml/parser/root_spec.rb +35 -0
  41. data/spec/lib/raml/parser.rb +49 -0
  42. data/spec/lib/raml/query_parameter_spec.rb +40 -0
  43. data/spec/lib/raml/resource_spec.rb +36 -0
  44. data/spec/lib/raml/response_spec.rb +25 -0
  45. data/spec/lib/raml/root_spec.rb +36 -0
  46. data/spec/spec_helper.rb +14 -0
  47. metadata +164 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c055a6b5ac11a427a84bf58f3dd22d1d366c91d
4
+ data.tar.gz: efeb25b9f8bb5004c9f5f65d50264285739c1bee
5
+ SHA512:
6
+ metadata.gz: 0ceb137a50546a1a0ebbd5cdd3b30f3f62214cf46dc3084a5da2bdc3717b042c49a45b584e483089f64db07c5941a26425d1471e2535cf6facf92a86e3879687
7
+ data.tar.gz: 561a64c167180a4b151e644692730197c0b565c1d76342387e269a0c341c839ce189734eb14e28eb565cd55f9372dc23add0764ce069d62d49bf92785844ea56
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in raml-rb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 James Brennan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # raml-rb
2
+
3
+ A RAML parser, implemented in Ruby.
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ 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')
10
+ ```
11
+
12
+ ## Todo
13
+
14
+ 0. Parameters for Resource Types and Traits.
15
+ 0. Ensure all attributes are supported.
16
+ 0. Add documentation generator.
17
+
18
+ ## Author
19
+
20
+ James Brennan (james@jamesbrennan.ca)
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :console do
4
+ require 'irb'
5
+ require 'irb/completion'
6
+ require 'raml'
7
+ ARGV.clear
8
+ IRB.start
9
+ end
data/lib/raml/body.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Raml
2
+ class Body
3
+ attr_accessor :type, :schema
4
+
5
+ def initialize(type)
6
+ @type = type
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Raml
2
+ class Documentation
3
+ attr_accessor :title, :content
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module Raml
2
+ class UnknownAttributeError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module Raml
2
+ class Method
3
+
4
+ attr_accessor :method, :description, :headers, :responses, :query_parameters
5
+
6
+ def initialize(method)
7
+ @method = method
8
+ @responses = []
9
+ @query_parameters = []
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ require 'raml/body'
2
+ require 'raml/parser/util'
3
+ require 'raml/errors/unknown_attribute_error'
4
+
5
+ module Raml
6
+ class Parser
7
+ class Body
8
+ include Raml::Parser::Util
9
+
10
+ BASIC_ATTRIBUTES = %w[schema]
11
+
12
+ attr_accessor :body, :attributes
13
+
14
+ def parse(type, attributes)
15
+ @body = Raml::Body.new(type)
16
+ @attributes = prepare_attributes(attributes)
17
+
18
+ parse_attributes
19
+
20
+ body
21
+ end
22
+
23
+ private
24
+
25
+ def parse_attributes
26
+ attributes.each do |key, value|
27
+ case key
28
+ when *BASIC_ATTRIBUTES
29
+ body.send("#{key}=".to_sym, value)
30
+ else
31
+ raise UnknownAttributeError.new "Unknown body key: #{key}"
32
+ end
33
+ end if attributes
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require 'raml/documentation'
2
+ require 'raml/errors/unknown_attribute_error'
3
+
4
+ module Raml
5
+ class Parser
6
+ class Documentation
7
+ include Raml::Parser::Util
8
+
9
+ BASIC_ATTRIBUTES = %w[title content]
10
+
11
+ attr_accessor :documentation, :attributes
12
+
13
+ def parse(attributes)
14
+ @documentation = Raml::Documentation.new
15
+ @attributes = prepare_attributes(attributes)
16
+
17
+ parse_attributes
18
+
19
+ documentation
20
+ end
21
+
22
+ private
23
+
24
+ def parse_attributes
25
+ attributes.each do |key, value|
26
+ case key
27
+ when *BASIC_ATTRIBUTES
28
+ documentation.send("#{key}=".to_sym, value)
29
+ else
30
+ raise UnknownAttributeError.new "Unknown documentation key: #{key}"
31
+ end
32
+ end if attributes
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,83 @@
1
+ require 'forwardable'
2
+ require 'raml/method'
3
+ require 'raml/parser/response'
4
+ require 'raml/parser/query_parameter'
5
+ require 'raml/parser/util'
6
+ require 'raml/errors/unknown_attribute_error'
7
+
8
+ module Raml
9
+ class Parser
10
+ class Method
11
+ extend Forwardable
12
+ include Raml::Parser::Util
13
+
14
+ BASIC_ATTRIBUTES = %w[description headers]
15
+
16
+ attr_accessor :method, :parent, :attributes
17
+ def_delegators :@parent, :traits
18
+
19
+ def initialize(parent)
20
+ @parent = parent
21
+ end
22
+
23
+ def parse(the_method, attributes)
24
+ @method = Raml::Method.new(the_method)
25
+ @attributes = prepare_attributes(attributes)
26
+
27
+ apply_parents_traits
28
+ parse_attributes
29
+
30
+ method
31
+ end
32
+
33
+ private
34
+
35
+ def parse_attributes(attributes = @attributes)
36
+ attributes.each do |key, value|
37
+ case key
38
+ when *BASIC_ATTRIBUTES
39
+ method.send("#{key}=".to_sym, value)
40
+ when 'is'
41
+ apply_traits(value)
42
+ when 'responses'
43
+ parse_responses(value)
44
+ when 'query_parameters'
45
+ parse_query_parameters(value)
46
+ else
47
+ raise UnknownAttributeError.new "Unknown method key: #{key}"
48
+ end
49
+ end if attributes
50
+ end
51
+
52
+ def parse_responses(responses)
53
+ responses.each do |code, response_attributes|
54
+ method.responses << Raml::Parser::Response.new.parse(code, response_attributes)
55
+ end
56
+ end
57
+
58
+ def parse_query_parameters(query_parameters)
59
+ query_parameters.each do |name, parameter_attributes|
60
+ method.query_parameters << Raml::Parser::QueryParameter.new.parse(name, parameter_attributes)
61
+ end
62
+ end
63
+
64
+ def apply_parents_traits
65
+ apply_traits(parent.trait_names) if !parent.trait_names.nil? && parent.trait_names.length
66
+ end
67
+
68
+ def apply_traits(names)
69
+ names.each do |name|
70
+ apply_trait(name)
71
+ end
72
+ end
73
+
74
+ def apply_trait(name)
75
+ unless traits[name].nil?
76
+ trait_attributes = prepare_attributes(traits[name])
77
+ parse_attributes(trait_attributes)
78
+ end
79
+ end
80
+
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,39 @@
1
+ require 'raml/parser/util'
2
+ require 'raml/query_parameter'
3
+ require 'raml/errors/unknown_attribute_error'
4
+
5
+ module Raml
6
+ class Parser
7
+ class QueryParameter
8
+ include Raml::Parser::Util
9
+
10
+ BASIC_ATTRIBUTES = %w[description type example]
11
+
12
+ attr_accessor :query_parameter, :attributes
13
+
14
+ def parse(name, attributes)
15
+ @query_parameter = Raml::QueryParameter.new(name)
16
+
17
+ @attributes = prepare_attributes(attributes)
18
+ parse_attributes(attributes)
19
+
20
+ query_parameter
21
+ end
22
+
23
+ private
24
+
25
+ def parse_attributes(attributes)
26
+ attributes.each do |key, value|
27
+ key = underscore(key)
28
+ case key
29
+ when *BASIC_ATTRIBUTES
30
+ query_parameter.send("#{key}=".to_sym, value)
31
+ else
32
+ raise UnknownAttributeError.new "Unknown query paramter key: #{key}"
33
+ end
34
+ end if attributes
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,60 @@
1
+ require 'forwardable'
2
+ require 'raml/resource'
3
+ require 'raml/parser/method'
4
+ require 'raml/parser/util'
5
+ require 'raml/errors/unknown_attribute_error'
6
+
7
+ module Raml
8
+ class Parser
9
+ class Resource
10
+ extend Forwardable
11
+ include Raml::Parser::Util
12
+
13
+ METHODS = %w[get put post delete]
14
+
15
+ attr_accessor :parent_node, :resource, :trait_names, :attributes
16
+ def_delegators :@parent_node, :resources
17
+ def_delegators :@parent, :traits, :resource_types
18
+
19
+ def initialize(parent)
20
+ @parent = parent
21
+ end
22
+
23
+ def parse(parent_node, uri_partial, attributes)
24
+ @parent_node = parent_node
25
+ @resource = Raml::Resource.new(@parent_node, uri_partial)
26
+ @attributes = prepare_attributes(attributes)
27
+ parse_attributes
28
+ resource
29
+ end
30
+
31
+ private
32
+
33
+ def parse_attributes(attributes = @attributes)
34
+ attributes.each do |key, value|
35
+ key = underscore(key)
36
+ case key
37
+ when /^\//
38
+ resources << Raml::Parser::Resource.new(self).parse(resource, key, value)
39
+ when *METHODS
40
+ resource.methods << Raml::Parser::Method.new(self).parse(key, value)
41
+ when 'type'
42
+ apply_resource_type(value)
43
+ when 'is'
44
+ @trait_names = value
45
+ else
46
+ raise UnknownAttributeError.new "Unknown resource key: #{key}"
47
+ end
48
+ end if attributes
49
+ end
50
+
51
+ def apply_resource_type(name)
52
+ unless resource_types[name].nil?
53
+ resource_attributes = prepare_attributes(resource_types[name])
54
+ parse_attributes(resource_attributes)
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,42 @@
1
+ require 'raml/response'
2
+ require 'raml/parser/util'
3
+ require 'raml/parser/body'
4
+ require 'raml/errors/unknown_attribute_error'
5
+
6
+ module Raml
7
+ class Parser
8
+ class Response
9
+ include Raml::Parser::Util
10
+
11
+ attr_accessor :response, :attributes
12
+
13
+ def parse(code, attributes)
14
+ @response = Raml::Response.new(code)
15
+ @attributes = prepare_attributes(attributes)
16
+ parse_attributes
17
+ response
18
+ end
19
+
20
+ private
21
+
22
+ def parse_attributes
23
+ attributes.each do |key, value|
24
+ key = underscore(key)
25
+ case key
26
+ when 'body'
27
+ parse_bodies(value)
28
+ else
29
+ raise UnknownAttributeError.new "Unknown response key: #{key}"
30
+ end
31
+ end if attributes
32
+ end
33
+
34
+ def parse_bodies(bodies)
35
+ bodies.each do |type, body_attributes|
36
+ response.bodies << Raml::Parser::Body.new.parse(type, body_attributes)
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,73 @@
1
+ require 'raml/root'
2
+ require 'raml/parser/resource'
3
+ require 'raml/parser/documentation'
4
+ require 'raml/parser/util'
5
+ require 'raml/errors/unknown_attribute_error'
6
+
7
+ module Raml
8
+ class Parser
9
+ class Root
10
+ include Raml::Parser::Util
11
+
12
+ BASIC_ATTRIBUTES = %w[title base_uri version]
13
+
14
+ attr_accessor :root, :traits, :resource_types, :attributes
15
+
16
+ def initialize
17
+ @traits = {}
18
+ end
19
+
20
+ def parse(attributes)
21
+ @root = Raml::Root.new
22
+ @attributes = prepare_attributes(attributes)
23
+ parse_attributes
24
+ root
25
+ end
26
+
27
+ private
28
+
29
+ def parse_attributes
30
+ attributes.each do |key, value|
31
+ key = underscore(key)
32
+ case key
33
+ when *BASIC_ATTRIBUTES
34
+ root.send("#{key}=".to_sym, value)
35
+ when 'traits'
36
+ parse_traits(value)
37
+ when 'resource_types'
38
+ parse_resource_types(value)
39
+ when 'documentation'
40
+ parse_documentation(value)
41
+ when /^\//
42
+ root.resources << Raml::Parser::Resource.new(self).parse(root, key, value)
43
+ else
44
+ raise UnknownAttributeError.new "Unknown root key: #{key}"
45
+ end
46
+ end if attributes
47
+ end
48
+
49
+ def parse_documentation(documentations)
50
+ documentations.each do |documentation_attributes|
51
+ root.documentations << Raml::Parser::Documentation.new.parse(documentation_attributes)
52
+ end
53
+ end
54
+
55
+ def parse_resource_types(resource_types)
56
+ resource_types.each do |type|
57
+ type.each do |name, type_attributes|
58
+ @resource_types[name] = type_attributes
59
+ end
60
+ end
61
+ end
62
+
63
+ def parse_traits(traits)
64
+ traits.each do |trait|
65
+ trait.each do |name, trait_attributes|
66
+ @traits[name] = trait_attributes
67
+ end
68
+ end
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,31 @@
1
+ module Raml
2
+ class Parser
3
+ module Util
4
+
5
+ private
6
+
7
+ def prepare_attributes(attributes)
8
+ hash = {}
9
+ attributes.each do |key, value|
10
+ hash[underscore(key)] = parse_value(value)
11
+ end if attributes.respond_to?(:each)
12
+ hash
13
+ end
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
+ def underscore(string)
24
+ string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
25
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
26
+ .downcase
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ require 'yaml'
2
+ require 'raml/parser/root'
3
+ require 'raml/parser/resource'
4
+ require 'raml/parser/method'
5
+ require 'raml/parser/response'
6
+ require 'raml/parser/body'
7
+ require 'raml/parser/query_parameter'
8
+
9
+ module Raml
10
+ class Parser
11
+
12
+ def parse(yaml)
13
+ raml = YAML.load(yaml)
14
+ Raml::Parser::Root.new.parse(raml)
15
+ end
16
+
17
+ def parse_file(path)
18
+ parse File.read(path)
19
+ end
20
+
21
+ def self.parse(yaml)
22
+ self.new.parse(yaml)
23
+ end
24
+
25
+ def self.parse_file(path)
26
+ self.new.parse_file(path)
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ module Raml
2
+ class QueryParameter
3
+ attr_accessor :name, :description, :type, :example
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ module Raml
2
+ class Resource
3
+ attr_accessor :parent, :methods, :uri_partial
4
+
5
+ def initialize(parent, uri_partial)
6
+ @parent = parent
7
+ @uri_partial = uri_partial
8
+ @methods = []
9
+ end
10
+
11
+ def uri
12
+ File.join(parent.uri, uri_partial)
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module Raml
2
+ class Response
3
+ attr_accessor :code, :bodies
4
+
5
+ def initialize(code)
6
+ @code = code
7
+ @bodies = []
8
+ end
9
+
10
+ end
11
+ end
data/lib/raml/root.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Raml
2
+ class Root
3
+ attr_accessor :title, :base_uri, :version, :resources, :documentation
4
+
5
+ def initialize
6
+ @resources = []
7
+ @documentation = []
8
+ end
9
+
10
+ def uri
11
+ base_uri.sub('{version}', version.to_s)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Raml
2
+ VERSION = '0.0.2'
3
+ end
data/lib/raml-rb.rb ADDED
@@ -0,0 +1 @@
1
+ require 'raml'
data/lib/raml.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'raml/version'
2
+ require 'raml/parser'
3
+ require 'raml/errors/unknown_attribute_error'
4
+ require 'raml/root'
5
+ require 'raml/resource'
6
+ require 'raml/method'
7
+ require 'raml/response'
8
+ require 'raml/body'
9
+ require 'raml/query_parameter'
10
+
11
+ module Raml
12
+ end
data/raml-rb.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'raml/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'raml-rb'
8
+ spec.version = Raml::VERSION
9
+ spec.authors = ['James Brennan']
10
+ spec.email = ['james@jamesbrennan.ca']
11
+ spec.summary = %q{A RAML parser implemented in Ruby}
12
+ spec.homepage = 'https://github.com/jpb/raml-rb'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files`.split("\n")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.6'
21
+ spec.add_development_dependency 'rake', '~>10.3'
22
+ spec.add_development_dependency 'rspec', '~> 3.0'
23
+ spec.add_development_dependency 'rspec-its', '~> 1.0'
24
+ end