Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/razum2um/lurker

📖 The ultimate tool for documenting and testing APIs in Rails
https://github.com/razum2um/lurker

documentation documentation-tool json-schema lurker minitest rails rspec ruby

Last synced: 29 days ago
JSON representation

📖 The ultimate tool for documenting and testing APIs in Rails

Lists

README

        

# Lurker

Generates API schemas, validates code against them and creates a handy web interface for testing the API.
Tested on on Rails 4, 5, 6 and Ruby >= 2.6

[![Gem Version](https://badge.fury.io/rb/lurker.svg)](https://badge.fury.io/rb/lurker)
[![Build Status][BS img]][Build Status]

## Usage

Wrap your integration test code, which does request like this

Lurker::Spy.on do
get "/api/v1/users.json"
end

And run the specs and commit your schemas. That's all, easy!

## RSpec usage

Write your RSpec [controller][rspec_controller_spec] or [request][rspec_request_spec] specs as usual,
but add `:lurker` mark (like documented [controller example][controler_spec_example] or [request spec example][request_spec_example]).

it "lists users", :lurker do
get "/api/v1/users.json"
end

NOTE: If you use `rspec-rails`, it should be required first

require 'rspec/rails'
require 'lurker/spec_helper'

## Minitest usage

You can use [minitest-around][minitest_around] to wrap your test classes like this:

class DestroyRepoTest < ActionDispatch::IntegrationTest
def around(&block)
Lurker::Spy.on(&block)
end
end

You also can wrap any block with API requests [manually][minitest_example].

## Schemas

Please, commit your files under `Rails.root/lurker` directory.
Feel free to edit them according to [json-schema][json_schema] standard.
It can be very strict and flexible if you wish: see an [example][json_schema_example],
but scaffolded schemas are pretty good by default.

A lurker/ExampleApp.service.yml
A lurker/api/v1/users-GET.json.yml
A lurker/api/v1/users/__user_id/repos-GET.json.yml

I also advise you to look on [Understanding JSON Schema][json_schema_book] book,
it is up-to-date with draft4 and contains lot of examples.

## Profit!

Now, every test run lurker will look into your requests and [validate them][failed_spec_example],
and it fails if your code changes the API!

Failure/Error: post :create [...]
Lurker::ValidationError:
Request
- The property '#/' contains additional properties ["social_network"] outside of the schema
when none are allowed in schema file:///.../lurker/api/v1/users-POST.json.yml#
Response
- The property '#/user/last_sign_in_at' of type String did not match the following type:
null in schema file:///.../lurker/api/v1/users-POST.json.yml#

The generation of live-documentation is pretty simple:

bin/lurker convert # builds html under `Rails.root/public/lurker` to be served under `/lurker` url
bin/lurker convert -f pdf # builds `Rails.root/public/lurker/snake_cased_name.pdf`

For different document root or serving URL prefix use `-o` and `-u` options accordingly.

If you want to provide additional documentation for your API (and you probably should),
you can use separate Markdown files in the schema folder. To generate documentation
stubs for the current schema:

bin/lurker init_docs # generate documentation stubs for the current schema

Let's run your `rails s` and visit [http://localhost:3000/lurker/](http://localhost:3000/lurker/)
(or see [demo][demo_app] for example)

Now, you can test your API on-line (for real)

## [Demo application][demo_app]

[Custom domain][demo_app] static + demo api production endpoint (in `Sandbox` mode).
You can run this to get the demo running locally:

git clone https://github.com/razum2um/lurker.git
cd lurker
export BUNDLE_GEMFILE=gemfiles/rails_6.gemfile
bundle install
rake build_example_docs
cd tmp/lurker_app_rails_6
bin/rails s

Lurker supports multiple domains (usually `staging` and `production`) and can be deployed
statically everywhere as well as be served by the current `Rails` instance.

## Features

- [Autoscaffolding for non-covered API endpoints][controler_spec_example]
- [Autotesting for covered endpoint once written][failed_spec_example] (both request & response!)
- [Pretty HTML documentation based on your schemas][html_generation_example]
- [Pretty submit form to test API endpoints (live) based on schemas][demo_live] (enter a name & press "Submit")
- [Handling URLs with dynamic segments][nested_controller_spec_example] (such as `api/v1/:user_id/repos`)
- [JSON-Schema partials][partial_example], also in YAML format ([demo][partial_example_demo])
- [Generation PDF documentation][pdf_example] (*NOTE*: add `gem 'pdfkit'` to Gemfile)
- [Multiple docs for many usecases][suffixes_example] (e.g `:lurker => '...'`)
- ERB support inside `.json.yml.erb`
- Insert custom content in Markdown into `index.html` (*NOTE*: add `gem 'kramdown'` to Gemfile)
- Syntax highlighting for sample response (add `gem 'execjs'` to Gemfile)
- Separate API-services generated within one test suite
- Capistrano integration
- JSON-Schema draft-v4 support
- Static site deploy and milti-domain support
- Builtin Rack middleware `Lurker::Server.to_rack` serves cached digested assets
- [RSpec][failed_spec_example] & [Minitest][minitest_example] support

## Related gems

- [Swarker](https://github.com/sponomarev/swarker) converts
Lurker schemas to [Swagger](http://swagger.io) schema

## Token authentication with sandbox

`Lurker::Sandbox` allows you to test services with token authentication:

# make sure it's not production!
# e.g. config/environtents/staging.rb
config.middleware.use Lurker::Sandbox

E.g. demo application on Heroku runs with it: when creating, updating repos or users
ids getting increased, but if you look into GET #index,
new items are NOT showing up. **This is NOT a bug!** Sequences in PostgreSQL
are increasing notwithstanding ROLLBACK is called. As such:

- run all your specs with **the same** testing token
- ensure the same token to be accepted on your demo application
- insert `Lurker::Sandbox` and the recorded examples should be ok to submit again

## Contributions

[![Code Climate][CC img]][Code Climate]
[![Coverage Status][CS img]][Coverage Status]
[![Inline docs](http://inch-pages.github.io/github/razum2um/lurker.png)](http://inch-pages.github.io/github/razum2um/lurker)
[![Stories in Ready](https://badge.waffle.io/razum2um/lurker.png?label=ready&title=Ready&_=1)][waffle]

**NOTE:** to get new version of bundled `bootstrap` or update JS/CSS,
don't touch files under `lib/lurker/templates/public` - they are autogenerated
and copied to static generated site while `bin/lurker convert`

export BUNDLE_GEMFILE=gemfiles/rails_6.gemfile
rake assets:precompile # to build them

Don't commit `lib/lurker/templates/public/**/*` to avoid conflicts.

**NOTE:** if you write features keep in mind to generate different files with aruba
because they are kept in the `lurker_app` directory to be deployed as a demo. Please, write
features in a way to generate **new** relevant `lurker/**/*.json.yml` files

**NOTE:** template partial `submit_form.html.erb` and it's partials is a big `jsx` script for `React`
so there are `` and `