api blueprint minitest rails code example

Example 1: api blueprint minitest rails

$ bin/rails api:schemas
Generating api schemas from docs/api/documentation.md
yarn run v1.7.0
$ node_modules/.bin/apib2json --pretty -i docs/api/documentation.md -o test/support/schemas/schemas.json
Done in 0.31s.
Writing GET-Users-200
Writing POST-Users-201
Writing POST-Users-422
Schemas are ready at test/support/schemas

Example 2: api blueprint minitest rails

require 'json_matchers/minitest/assertions'
JsonMatchers.schema_root = 'test/support/schemas'
Minitest::Test.send(:include, JsonMatchers::Minitest::Assertions)

Example 3: api blueprint minitest rails

require 'test_helper'
class UsersApiTest < ActionDispatch::IntegrationTest
  test 'Users List' do
    get '/users', headers: { Accept: 'application/vnd.api-test.v1+json' }
    assert_response :success
    assert_matches_json_schema response, 'GET-Users-200'
  end
  test 'Create new User successfully' do
    post '/users', headers: { Accept: 'application/vnd.api-test.v1+json' }, params: user_payload
    assert_response :created
    assert_matches_json_schema response, 'POST-Users-201'
  end
  test 'Fails to create new User' do
    post '/users', headers: { Accept: 'application/vnd.api-test.v1+json' },
                   params: user_payload(email: nil, first_name: nil)
    assert_response :unprocessable_entity
    assert_matches_json_schema response, 'POST-Users-422'
  end
  def user_payload(attrs = {})
    {
      email: '[email protected]',
      first_name: 'Jane',
      last_name: 'Doe'
    }.merge(attrs)
  end
end

Example 4: api blueprint minitest rails

#: failed schema #: "links" wasn't supplied. 
---
expected
{
}
to match schema "POST-Users-201": 
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "data": {
      "type": "array",
      "items": {
...

Example 5: api blueprint minitest rails

FORMAT: 1A
HOST: http://polls.apiblueprint.org/
# Sample API
Polls is a simple API allowing consumers to view polls and vote in them.
## Questions Collection [/questions]
### List All Questions [GET]
+ Response 200 (application/json)
        [
            {
                "question": "Favourite programming language?",
                "published_at": "2015-08-05T08:40:51.620Z",
                "choices": [
                    {
                        "choice": "Swift",
                        "votes": 2048
                    }, {
                        "choice": "Python",
                        "votes": 1024
                    }, {
                        "choice": "Objective-C",
                        "votes": 512
                    }, {
                        "choice": "Ruby",
                        "votes": 256
                    }
                ]
            }
        ]

Example 6: api blueprint minitest rails

- address
    - street
    - city
    - state

Example 7: api blueprint minitest rails

{
    "address" : {
        "street": "",
        "city": "",
        "state": ""
    }
}

Example 8: api blueprint minitest rails

$ bin/yarn run aglio -i docs/api/documentation.md -o public/documentation.html

Example 9: api blueprint minitest rails

$ bin/yarn add aglio

Example 10: api blueprint minitest rails

$ bin/yarn add "https://github.com/mariochavez/apib2json.git#additional-metadata"

Tags:

Ruby Example