rack-raml 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86b31081e20fdb366e4f840dc58f44614492b798
4
+ data.tar.gz: 9469fc9a148fb16807efb6151b2100e64c9c096d
5
+ SHA512:
6
+ metadata.gz: c4b0a8f2c93fa04364c77c5b1d4ab4704836fe5a9aecc8aa0f79303905e5b9db2a83fd7ce60d745109ddc50dabe34a4c8ce4b9f5c414c8f51f66a94eabf1bf63
7
+ data.tar.gz: d0ea9ae37ebfa610e60c4787dd8bab21299a969bb14ef55e97007edeb41e79cc8cc1a4f17fc7f8b0beaf84755c214e56a790cf4360617cf9dc84aa952b9a0318
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-raml.gemspec
4
+ gemspec
5
+
6
+ gem 'sinatra', '>= 2.0.0.beta2'
7
+ gem 'rails'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ray Zane
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # Rack::Raml
2
+
3
+ [![Build Status](https://travis-ci.org/rzane/rack-raml.svg?branch=master)](https://travis-ci.org/rzane/rack-raml)
4
+
5
+ A mountable mock server for your [RAML](https://raml.org) specifications based on Rack.
6
+
7
+ __Note:__ This is not intended for use in production environments.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'rack-raml'
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Rack::Raml implements a Rack compatible application that responds to `#call`. Therefore, it can be used with any Rack-based web framework.
20
+
21
+ #### Rails
22
+
23
+ To use with Rails, simply point specific URLs at an instance of `Rack::Raml::App` like so:
24
+
25
+ ```ruby
26
+ MyApplication.routes.draw do
27
+ # ...
28
+
29
+ Rack::Raml.routes Rails.root.join('path/to/spec.raml') do |raml_app|
30
+ get '/api/v1/users/:id', to: raml_app
31
+ end
32
+ end
33
+ ```
34
+
35
+ #### Sinatra
36
+
37
+ To use with Sinatra, you have a few options. The easiest way is to define your route, and invoke `#process`.
38
+
39
+ ```ruby
40
+ raml_file = File.expand_path('../path/to/spec.raml', __FILE__)
41
+ raml_app = Rack::Raml.create(raml_file)
42
+
43
+ get '/api/v1/users/:id' do
44
+ raml_app.process(request)
45
+ end
46
+ ```
47
+
48
+ Otherwise, you might configure `Rack::Raml` in your `config.ru`.
49
+
50
+ #### Example RAML
51
+
52
+ ```yaml
53
+ #%RAML 0.8
54
+ ---
55
+ title: Dummy API
56
+ baseUri: https://dummy-api.com/api/{version}
57
+ version: v1
58
+
59
+ /users:
60
+ /{id}:
61
+ displayName: Find a user
62
+ get:
63
+ responses:
64
+ 200:
65
+ description: User was found.
66
+ body:
67
+ application/json:
68
+ example: |-
69
+ {
70
+ "id": 1,
71
+ "first_name": "John",
72
+ "last_name": "Doe"
73
+ }
74
+ ```
75
+
76
+ ## Status Codes
77
+
78
+ In RAML, you'll often specify multiple status codes that your server might respond with. By default, Rack::Raml will respond with the lowest status code that you've specified. So, for example, if you've declared a 200 and a 401 response, it will respond with the 200 response code unless otherwise instructed.
79
+
80
+ You can force Rack::Raml to use a specific status code by requesting the url with the `_code` query parameter. For example:
81
+
82
+ ```
83
+ $ curl http://localhost:3000/api/v1/users?_code=401
84
+ ```
85
+
86
+ ## Content Types
87
+
88
+ The server will respond with the content type that matches the request. If the request does not specify a content type, Rack::Raml will assume you want `application/json`.
89
+
90
+ You can force Rack::Raml to use a specific content type by requesting the url with the `_type` query parameter. For example:
91
+
92
+ ```
93
+ $ curl http://localhost:3000/api/v1/users?_type=text/plain
94
+ ```
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rack-raml.
99
+
100
+
101
+ ## License
102
+
103
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/examples/rack.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rack'
2
+ require 'rack/raml'
3
+
4
+ raml_file = File.expand_path('../../spec/fixtures/api.raml', __FILE__)
5
+ raml_app = Rack::Raml.create(raml_file)
6
+
7
+ app = Rack::Builder.new do
8
+ map '/' do
9
+ run proc { |env|
10
+ [200, {'Content-Type' => 'text/plain'}, ['This is a Rack demo']]
11
+ }
12
+ end
13
+
14
+ map '/api/v1' do
15
+ run raml_app
16
+ end
17
+ end
18
+
19
+ Rack::Server.start app: app
data/examples/rails.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'rack/raml'
2
+ require 'action_controller/railtie'
3
+
4
+ Rails.logger = Logger.new(STDOUT)
5
+
6
+ class App < Rails::Application
7
+ config.secret_key_base = '123456789'
8
+ config.raml_file = File.expand_path('../../spec/fixtures/api.raml', __FILE__)
9
+ end
10
+
11
+ class PagesController < ActionController::Base
12
+ def index
13
+ render text: 'This is the Rails demo'
14
+ end
15
+ end
16
+
17
+ App.routes.draw do
18
+ Rack::Raml.routes(App.config.raml_file) do |raml_app|
19
+ get '/api/v1/users/:id', to: raml_app
20
+ end
21
+
22
+ root to: 'pages#index'
23
+ end
24
+
25
+ Rack::Server.start app: App
@@ -0,0 +1,13 @@
1
+ require 'sinatra'
2
+ require 'rack/raml'
3
+
4
+ raml_file = File.expand_path('../../spec/fixtures/api.raml', __FILE__)
5
+ raml_app = Rack::Raml.create(raml_file)
6
+
7
+ get '/' do
8
+ 'This is a Sinatra demo'
9
+ end
10
+
11
+ get '/api/v1/users/:id' do
12
+ raml_app.process(request)
13
+ end
@@ -0,0 +1,43 @@
1
+ require 'rack/response'
2
+ require 'rack/raml/response'
3
+
4
+ module Rack
5
+ module Raml
6
+ class App
7
+ attr_reader :raml_file
8
+
9
+ def initialize(raml_file)
10
+ @raml_file = raml_file
11
+ end
12
+
13
+ def raml
14
+ @raml ||= ::Raml.parse_file(raml_file)
15
+ end
16
+
17
+ def call(env)
18
+ process Rack::Request.new(env)
19
+ end
20
+
21
+ def process(request)
22
+ response_for(request).to_rack
23
+ end
24
+
25
+ def resources
26
+ @resources ||= flatten_resources(raml)
27
+ end
28
+
29
+ private
30
+
31
+ def response_for(request)
32
+ Rack::Raml::Response.new(resources, request)
33
+ end
34
+
35
+ def flatten_resources(node)
36
+ node.children.inject([]) do |acc, child|
37
+ acc << child if child.kind_of?(::Raml::Resource)
38
+ acc + flatten_resources(child)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,87 @@
1
+ require 'uri_template'
2
+
3
+ module Rack
4
+ module Raml
5
+ class Response
6
+ attr_reader :resources, :request
7
+
8
+ def initialize(resources, request)
9
+ @resources = resources
10
+ @request = request
11
+ end
12
+
13
+ def matches?
14
+ !response.nil?
15
+ end
16
+
17
+ def to_rack
18
+ if matches?
19
+ [code, { 'Content-Type' => type }, [body]]
20
+ else
21
+ [404, { 'Content-Type' => type }, [not_found]]
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def path
28
+ request.env['PATH_INFO']
29
+ end
30
+
31
+ def verb
32
+ request.request_method.downcase
33
+ end
34
+
35
+ def type
36
+ request.params['_type'] || request.content_type || 'application/json'
37
+ end
38
+
39
+ def code
40
+ if value = request.params['_code']
41
+ value.to_i
42
+ elsif possible_responses
43
+ possible_responses.keys.min
44
+ end
45
+ end
46
+
47
+ def body
48
+ response.example if matches?
49
+ end
50
+
51
+ def not_found
52
+ {
53
+ error: 'RAML not found',
54
+ code: code,
55
+ type: type,
56
+ path: path,
57
+ verb: verb
58
+ }.to_json
59
+ end
60
+
61
+ def response
62
+ @response ||= (
63
+ possible_responses &&
64
+ possible_responses[code] &&
65
+ possible_responses[code].bodies[type]
66
+ )
67
+ end
68
+
69
+ def possible_responses
70
+ @possible_responses ||= find_possible_responses
71
+ end
72
+
73
+ def find_possible_responses
74
+ resource = resources.find do |res|
75
+ pattern = uri_pattern(res.resource_path)
76
+ res.methods[verb] && pattern.match(path)
77
+ end
78
+
79
+ resource.methods[verb].responses if resource
80
+ end
81
+
82
+ def uri_pattern(template)
83
+ URITemplate::RFC6570.new("{/prefix*}#{template}")
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module Raml
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/rack/raml.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'raml'
2
+ require 'rack/raml/version'
3
+ require 'rack/raml/app'
4
+ require 'rack/raml/response'
5
+
6
+ module Rack
7
+ module Raml
8
+ def self.create(file)
9
+ Rack::Raml::App.new(file)
10
+ end
11
+
12
+ def self.routes(file, &block)
13
+ create(file).tap(&block)
14
+ end
15
+ end
16
+ end
data/lib/rack-raml.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rack/raml'
data/rack-raml.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/raml/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-raml"
8
+ spec.version = Rack::Raml::VERSION
9
+ spec.authors = ["Ray Zane"]
10
+ spec.email = ["ray@promptworks.com"]
11
+
12
+ spec.summary = %q{A mock RAML server for Rack-based applications.}
13
+ spec.description = %q{A mock RAML server for Rack-based applications.}
14
+ spec.homepage = "https://github.com/rzane/rack-raml"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'rack'
23
+ spec.add_dependency 'raml_ruby'
24
+ spec.add_dependency 'uri_template'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.12'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec'
29
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-raml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ray Zane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: raml_ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: uri_template
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A mock RAML server for Rack-based applications.
98
+ email:
99
+ - ray@promptworks.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - examples/rack.rb
112
+ - examples/rails.rb
113
+ - examples/sinatra.rb
114
+ - lib/rack-raml.rb
115
+ - lib/rack/raml.rb
116
+ - lib/rack/raml/app.rb
117
+ - lib/rack/raml/response.rb
118
+ - lib/rack/raml/version.rb
119
+ - rack-raml.gemspec
120
+ homepage: https://github.com/rzane/rack-raml
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.7
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: A mock RAML server for Rack-based applications.
144
+ test_files: []