rack-spec 0.1.7 → 0.1.8
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 +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +52 -0
- data/bin/specup +1 -0
- data/lib/rack/spec.rb +2 -0
- data/lib/rack/spec/docs.rb +38 -0
- data/lib/rack/spec/version.rb +1 -1
- data/rack-spec.gemspec +1 -0
- data/spec/fixtures/schema.json +1 -0
- data/spec/rack/spec/docs_spec.rb +93 -0
- data/spec/rack/spec/mock_spec.rb +4 -0
- metadata +18 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: f9795c070d0186c9a6438b83e16f99d451efd2e0
         | 
| 4 | 
            +
              data.tar.gz: 9bc28b54878df5ff26bea3d3db7f4162ccaa39c0
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: ddea8433e0f5c03b399acd9e3e6d91c73d24ad8c56ed6a9ce0409b022d1683a1464123e337257f0e5baf5124331899475aed2498afbc199172124f2d99bc8b7a
         | 
| 7 | 
            +
              data.tar.gz: b49bd48bd82e0387e64496cf9ac599b633937fa4bad9b5323607208eb66853dbfb096c47e904f883dd9d5c088ea05e06d41342d5a10a89afd49ae2d890606474
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -6,6 +6,7 @@ | |
| 6 6 | 
             
            str = File.read("schema.json")
         | 
| 7 7 | 
             
            schema = JSON.parse(str)
         | 
| 8 8 |  | 
| 9 | 
            +
            use Rack::Spec::Docs, schema: schema
         | 
| 9 10 | 
             
            use Rack::Spec::ErrorHandler
         | 
| 10 11 | 
             
            use Rack::Spec::RequestValidation, schema: schema
         | 
| 11 12 | 
             
            use Rack::Spec::ResponseValidation, schema: schema if ENV["RACK_ENV"] == "test"
         | 
| @@ -146,3 +147,54 @@ Rack::Spec::Error | |
| 146 147 | 
             
               |
         | 
| 147 148 | 
             
               `--Rack::Spec::ResponseValidation::InvalidResponseType
         | 
| 148 149 | 
             
            ```
         | 
| 150 | 
            +
             | 
| 151 | 
            +
            ### Rack::Spec::Docs
         | 
| 152 | 
            +
            Returns API documentation as a text/plain content, rendered in GitHub flavored Markdown.
         | 
| 153 | 
            +
             | 
| 154 | 
            +
            * You can give `path` option to change default path: `GET /docs`
         | 
| 155 | 
            +
            * API documentation is powered by [jdoc](https://github.com/r7kamura/jdoc) gem
         | 
| 156 | 
            +
            * This middleware is also bundled in the `specup` executable command
         | 
| 157 | 
            +
             | 
| 158 | 
            +
            ```sh
         | 
| 159 | 
            +
            $ specup schema.json
         | 
| 160 | 
            +
            [2014-06-06 23:01:35] INFO  WEBrick 1.3.1
         | 
| 161 | 
            +
            [2014-06-06 23:01:35] INFO  ruby 2.0.0 (2013-06-27) [x86_64-darwin12.5.0]
         | 
| 162 | 
            +
            [2014-06-06 23:01:35] INFO  WEBrick::HTTPServer#start: pid=24303 port=8080
         | 
| 163 | 
            +
             | 
| 164 | 
            +
            $ curl :8080/docs -i
         | 
| 165 | 
            +
            HTTP/1.1 200 OK
         | 
| 166 | 
            +
            Content-Type: text/plain; charset=utf-8
         | 
| 167 | 
            +
            Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
         | 
| 168 | 
            +
            Date: Sat, 07 Jun 2014 19:58:04 GMT
         | 
| 169 | 
            +
            Content-Length: 2175
         | 
| 170 | 
            +
            Connection: Keep-Alive
         | 
| 171 | 
            +
             | 
| 172 | 
            +
            # Example API
         | 
| 173 | 
            +
            * [App](#app)
         | 
| 174 | 
            +
             * [GET /apps](#get-apps)
         | 
| 175 | 
            +
             * [POST /apps](#post-apps)
         | 
| 176 | 
            +
             * [GET /apps/:id](#get-appsid)
         | 
| 177 | 
            +
             * [PATCH /apps/:id](#patch-appsid)
         | 
| 178 | 
            +
             * [DELETE /apps/:id](#delete-appsid)
         | 
| 179 | 
            +
            * [Recipe](#recipe)
         | 
| 180 | 
            +
             * [GET /recipes](#get-recipes)
         | 
| 181 | 
            +
             | 
| 182 | 
            +
            ## App
         | 
| 183 | 
            +
            An app is a program to be deployed.
         | 
| 184 | 
            +
             | 
| 185 | 
            +
            ### Properties
         | 
| 186 | 
            +
            * id - unique identifier of app
         | 
| 187 | 
            +
             * Example: `01234567-89ab-cdef-0123-456789abcdef`
         | 
| 188 | 
            +
             * Type: string
         | 
| 189 | 
            +
             * Format: uuid
         | 
| 190 | 
            +
             * ReadOnly: true
         | 
| 191 | 
            +
            * name - unique name of app
         | 
| 192 | 
            +
             * Example: `example`
         | 
| 193 | 
            +
             * Type: string
         | 
| 194 | 
            +
             * Patern: `(?-mix:^[a-z][a-z0-9-]{3,50}$)`
         | 
| 195 | 
            +
             | 
| 196 | 
            +
            ### GET /apps
         | 
| 197 | 
            +
            List existing apps.
         | 
| 198 | 
            +
             | 
| 199 | 
            +
            ...
         | 
| 200 | 
            +
            ```
         | 
    
        data/bin/specup
    CHANGED
    
    
    
        data/lib/rack/spec.rb
    CHANGED
    
    | @@ -1,3 +1,4 @@ | |
| 1 | 
            +
            require "jdoc"
         | 
| 1 2 | 
             
            require "json"
         | 
| 2 3 | 
             
            require "json_schema"
         | 
| 3 4 | 
             
            require "multi_json"
         | 
| @@ -5,6 +6,7 @@ require "rack" | |
| 5 6 |  | 
| 6 7 | 
             
            require "rack/spec/base_request_handler"
         | 
| 7 8 | 
             
            require "rack/spec/error"
         | 
| 9 | 
            +
            require "rack/spec/docs"
         | 
| 8 10 | 
             
            require "rack/spec/error_handler"
         | 
| 9 11 | 
             
            require "rack/spec/mock"
         | 
| 10 12 | 
             
            require "rack/spec/request_validation"
         | 
| @@ -0,0 +1,38 @@ | |
| 1 | 
            +
            module Rack
         | 
| 2 | 
            +
              module Spec
         | 
| 3 | 
            +
                class Docs
         | 
| 4 | 
            +
                  DEFAULT_PATH = "/docs"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  # Behaves as a rack-middleware
         | 
| 7 | 
            +
                  # @param app [Object] Rack application
         | 
| 8 | 
            +
                  # @param path [String, nil] URL path to return document (default: /docs)
         | 
| 9 | 
            +
                  # @param schema [Hash] Schema object written in JSON schema format
         | 
| 10 | 
            +
                  def initialize(app, path: nil, schema: nil)
         | 
| 11 | 
            +
                    @app = app
         | 
| 12 | 
            +
                    @path = path
         | 
| 13 | 
            +
                    @document = Jdoc::Generator.call(schema)
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  # Returns rendered document for document request
         | 
| 17 | 
            +
                  # @param env [Hash] Rack env
         | 
| 18 | 
            +
                  def call(env)
         | 
| 19 | 
            +
                    if env["REQUEST_METHOD"] == "GET" && env["PATH_INFO"] == path
         | 
| 20 | 
            +
                      [
         | 
| 21 | 
            +
                        200,
         | 
| 22 | 
            +
                        { "Content-Type" => "text/plain; charset=utf-8" },
         | 
| 23 | 
            +
                        [@document],
         | 
| 24 | 
            +
                      ]
         | 
| 25 | 
            +
                    else
         | 
| 26 | 
            +
                      @app.call(env)
         | 
| 27 | 
            +
                    end
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                  private
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  # @return [String] Path to return document
         | 
| 33 | 
            +
                  def path
         | 
| 34 | 
            +
                    @path || DEFAULT_PATH
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
            end
         | 
    
        data/lib/rack/spec/version.rb
    CHANGED
    
    
    
        data/rack-spec.gemspec
    CHANGED
    
    | @@ -17,6 +17,7 @@ Gem::Specification.new do |spec| | |
| 17 17 | 
             
              spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
         | 
| 18 18 | 
             
              spec.require_paths = ["lib"]
         | 
| 19 19 |  | 
| 20 | 
            +
              spec.add_dependency "jdoc", ">= 0.0.3"
         | 
| 20 21 | 
             
              spec.add_dependency "json_schema"
         | 
| 21 22 | 
             
              spec.add_dependency "multi_json"
         | 
| 22 23 | 
             
              spec.add_dependency "rack"
         | 
    
        data/spec/fixtures/schema.json
    CHANGED
    
    
| @@ -0,0 +1,93 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Rack::Spec::Docs do
         | 
| 4 | 
            +
              include Rack::Test::Methods
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              let(:app) do
         | 
| 7 | 
            +
                local_docs_path = docs_path
         | 
| 8 | 
            +
                local_schema = schema
         | 
| 9 | 
            +
                Rack::Builder.app do
         | 
| 10 | 
            +
                  use Rack::Spec::Docs, path: local_docs_path, schema: local_schema
         | 
| 11 | 
            +
                  run ->(env) do
         | 
| 12 | 
            +
                    [
         | 
| 13 | 
            +
                      200,
         | 
| 14 | 
            +
                      {},
         | 
| 15 | 
            +
                      ["dummy"],
         | 
| 16 | 
            +
                    ]
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              let(:schema) do
         | 
| 22 | 
            +
                str = File.read(schema_path)
         | 
| 23 | 
            +
                JSON.parse(str)
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              let(:schema_path) do
         | 
| 27 | 
            +
                File.expand_path("../../../fixtures/schema.json", __FILE__)
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              let(:docs_path) do
         | 
| 31 | 
            +
                nil
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              let(:response) do
         | 
| 35 | 
            +
                last_response
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              let(:env) do
         | 
| 39 | 
            +
                {}
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              let(:params) do
         | 
| 43 | 
            +
                {}
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              subject do
         | 
| 47 | 
            +
                send(verb, path, params, env)
         | 
| 48 | 
            +
                response.status
         | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              describe "#call" do
         | 
| 52 | 
            +
                let(:verb) do
         | 
| 53 | 
            +
                  :get
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                context "without :path option" do
         | 
| 57 | 
            +
                  let(:path) do
         | 
| 58 | 
            +
                    "/docs"
         | 
| 59 | 
            +
                  end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                  it "generates API documentation and returns it to request to GET /docs" do
         | 
| 62 | 
            +
                    should == 200
         | 
| 63 | 
            +
                    response.body.should include("Example API")
         | 
| 64 | 
            +
                  end
         | 
| 65 | 
            +
                end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                context "with :path option" do
         | 
| 68 | 
            +
                  let(:docs_path) do
         | 
| 69 | 
            +
                    "/api_document"
         | 
| 70 | 
            +
                  end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                  let(:path) do
         | 
| 73 | 
            +
                    "/api_document"
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                  it "responds to specified path" do
         | 
| 77 | 
            +
                    should == 200
         | 
| 78 | 
            +
                    response.body.should_not == "dummy"
         | 
| 79 | 
            +
                  end
         | 
| 80 | 
            +
                end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                context "without targeted request" do
         | 
| 83 | 
            +
                  let(:path) do
         | 
| 84 | 
            +
                    "/apps"
         | 
| 85 | 
            +
                  end
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                  it "delegates request to inner app" do
         | 
| 88 | 
            +
                    should == 200
         | 
| 89 | 
            +
                    response.body.should == "dummy"
         | 
| 90 | 
            +
                  end
         | 
| 91 | 
            +
                end
         | 
| 92 | 
            +
              end
         | 
| 93 | 
            +
            end
         | 
    
        data/spec/rack/spec/mock_spec.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: rack-spec
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.8
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Ryo Nakamura
         | 
| @@ -10,6 +10,20 @@ bindir: bin | |
| 10 10 | 
             
            cert_chain: []
         | 
| 11 11 | 
             
            date: 2014-06-07 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: jdoc
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - ">="
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: 0.0.3
         | 
| 20 | 
            +
              type: :runtime
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - ">="
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: 0.0.3
         | 
| 13 27 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 28 | 
             
              name: json_schema
         | 
| 15 29 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -168,6 +182,7 @@ files: | |
| 168 182 | 
             
            - lib/rack-spec.rb
         | 
| 169 183 | 
             
            - lib/rack/spec.rb
         | 
| 170 184 | 
             
            - lib/rack/spec/base_request_handler.rb
         | 
| 185 | 
            +
            - lib/rack/spec/docs.rb
         | 
| 171 186 | 
             
            - lib/rack/spec/error.rb
         | 
| 172 187 | 
             
            - lib/rack/spec/error_handler.rb
         | 
| 173 188 | 
             
            - lib/rack/spec/mock.rb
         | 
| @@ -177,6 +192,7 @@ files: | |
| 177 192 | 
             
            - lib/rack/spec/version.rb
         | 
| 178 193 | 
             
            - rack-spec.gemspec
         | 
| 179 194 | 
             
            - spec/fixtures/schema.json
         | 
| 195 | 
            +
            - spec/rack/spec/docs_spec.rb
         | 
| 180 196 | 
             
            - spec/rack/spec/mock_spec.rb
         | 
| 181 197 | 
             
            - spec/rack/spec_spec.rb
         | 
| 182 198 | 
             
            - spec/spec_helper.rb
         | 
| @@ -206,6 +222,7 @@ specification_version: 4 | |
| 206 222 | 
             
            summary: JSON Schema based Rack middlewares
         | 
| 207 223 | 
             
            test_files:
         | 
| 208 224 | 
             
            - spec/fixtures/schema.json
         | 
| 225 | 
            +
            - spec/rack/spec/docs_spec.rb
         | 
| 209 226 | 
             
            - spec/rack/spec/mock_spec.rb
         | 
| 210 227 | 
             
            - spec/rack/spec_spec.rb
         | 
| 211 228 | 
             
            - spec/spec_helper.rb
         |