• Stars
    star
    31
  • Rank 820,005 (Top 17 %)
  • Language
    Crystal
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Kemal API Version Extension

Kave (Kemal API Version Extension) Build Status

This shards makes it easier to version your Kemal API

Installation

Add this to your application's shard.yml:

dependencies:
  kave:
    github: jwoertink/kave

Usage

require "kemal"
require "kave"

Kave.configure do |c|
  # These are default config options
  # c.format = :json                # see Formats below
  # c.auth_strategy = nil           # see Auth Strategy below
  # c.token_model = Kave::AuthToken # see Auth Strategy below
  # c.version_strategy = :header    # see Version Strategy below
end

public do
  get "/" do |env|
    "This is a public route"
  end
end

api("v1") do
  get "/" do |env|
    "This is a private route only accessed through the version 1 API"
  end

  before_post "/check" do |env|
    "This is called before POST /v1/check"
  end
  post "/check" do |env|
    "something here"
  end
end

api("v2") do
  get "/" do |env|
    "This is a private route only accessed through the version 2 API"
  end
end

Configuration Options

Formats

The formats are how your data will be returned. By default, Kave will assume you're building a JSON API. Kave will automatically set your response Content-Type to match the type of API you're building.

For now, you must handle the data conversion yourself, but eventually Kave will take care of that for you.

# assumes format is :json
api("v1") do
  get "/users/:id" do |env|
    {"id" => env.params.url["id"], "name" => "jeremy"}.to_json
  end
end

You would make a call to this route by http://localhost:3000/v1/users/1

Additional options later will be :xml, :msgpack, :plain

Auth Strategy

If your API isn't public, then you'll probably want to add some sort of API key. By default, Kave sets the auth_strategy to nil, but you can use a Bearer token authorization. To use this, set the auth_strategy to :bearer, and create a class that inherits from Kave::AuthToken. Set the token_model to your custom class, and make sure that class implements the class method locate.

class MyTokenModel < Kave::AuthToken
  def self.locate(token : String)
    token == "abc123"
  end
end

Kave.configure do |c|
  c.auth_strategy = :bearer
  c.token_model = MyTokenModel
end

api("v1") do
  get "/users/:id" do |env|
    {"id" => env.params.url["id"], "name" => "jeremy"}.to_json
  end
end

To access this route:

$ curl -H "Authorization: Bearer abc123" "http://localhost:3000/v1/users/1"

Version Strategy

By default, Kave will generate paths for your API by prepending the version to your route like /v1/whatever. This option gives you the ability to specify your routes like /whatever and control the version using a header. Later there may be an option like :subdomain to do v1.whatever.com/whatever.

Kave.configure do |c|
  c.version_strategy = :header
end

api("v1") do
  get "/users" do |env|
    [{"id" => 1, "name" => "Jeremy"}].to_json
  end
end

To access this route:

$ curl -H "Accept: application/vnd.api.v1+json" "http://localhost:3000/users"

NOTE In a previous version of Kave you were able to change this halfway through. I realized after lots of trail and error that this was going to cause a huge mess. You can't conditionally add middleware. It's either there, or it's not. I may add that back in later if I can come up with some clever way, but until people start asking for that, it's out for now.

Development

This is going to be a work in progress for a while. If you have a better idea for how something should be implemented, please open an issue, or submit a PR.

Contributing

  1. Fork it ( https://github.com/jwoertink/kave/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Make sure all specs pass
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request

Contributors

More Repositories

1

fez

A Kemal application generator
Crystal
51
star
2

sitemapper

Sitemap generator for Crystal apps
Crystal
24
star
3

medley

A crystal shard for doing music related stuff
Crystal
17
star
4

crono

2D Video Game framework
Crystal
17
star
5

crystal_orm_test

Benchmark different ORMs for crystal and postgres
Crystal
16
star
6

dopewars

Ruby port of the Dopewars / Drugwars game
Ruby
15
star
7

lucky-cluster

Boot multiple lucky processes
Crystal
13
star
8

psst

Send encrypted string between ruby and crystal
Ruby
11
star
9

webrtc-demo

How to get started with WebRTC
JavaScript
9
star
10

Waves

JRuby + jMonkeyEngine 3D fun
Ruby
9
star
11

pcotm

Phone Case of the Monster
Ruby
5
star
12

bootcamp

Defunct project
Ruby
5
star
13

guard-kemal

A Kemal guard watcher plugin
Ruby
5
star
14

maze_craze

jMonkeyEngine & JRuby dynamic maze game
Ruby
5
star
15

node-chat-demo

Learning NodeJS through the node-chat application
JavaScript
4
star
16

ruby-in-water

Example app with a mock sinatra and really fast connection
Ruby
3
star
17

Pathbox

Path(http://www.path.com/) style box from iPhone app
JavaScript
3
star
18

transfer-util

This is a yahoo store web scraper, and RTML transfer util
Ruby
3
star
19

jko_api

api versioning gem for rails
Ruby
3
star
20

vite_lucky

Crystal
3
star
21

nacha

A Crystal shard for parsing and generating Nacha (ACH) files
Crystal
2
star
22

JustTheTip

jQuery Tool Tip Plug-in
JavaScript
2
star
23

omgwtfmmo

game tutorial
JavaScript
2
star
24

tourets

A Rails gem for using RETS
Ruby
2
star
25

gosu-example

Simple Gosu Example
Ruby
2
star
26

JustSlide

jQuery content slider
JavaScript
2
star
27

jmonkeyengine-ruby

A ruby gem for easily including JME3 into your project
Ruby
2
star
28

rw_depot_store

Depot Store for RubyWeekend
Ruby
2
star
29

ruby-encryptor

a fun little encryption app built in ruby
Ruby
1
star
30

blackjack_example

Ruby
1
star
31

rw_depot_theme

Default theme for the RubyWeekend Depot App
Ruby
1
star
32

jqxmaslights

jQuery Christmas lights for a container element
JavaScript
1
star
33

crono-samples

Sample games written using Crono
Crystal
1
star
34

woert.ink

My personal site
Crystal
1
star
35

rspec-interactions-test

Trying to test this bug.
Ruby
1
star
36

kemal-session-client-engine

Kemal session for client side storage
Crystal
1
star
37

JustCopy

jQuery clipboard copy
JavaScript
1
star
38

pirates-ios

A Pirate game based on the Udemy Course Challenge
Objective-C
1
star
39

shellac

HTTP Cache server
Elixir
1
star
40

recursinator

Traverse a directory to do stuff in Ruby or Crystal
Crystal
1
star
41

tictacnode

tic tac toe game written in nodeJS
JavaScript
1
star
42

rails-upgrade-tracker

make upgrading rails a little easier
Ruby
1
star
43

rr10-team-208

Repository for the RailsRumble 2010 Team 208
Ruby
1
star
44

enom-ruby

Ruby wrapper for the Enom reseller API
Ruby
1
star
45

godaddy-api.cr

Simple Godaddy API for crystal
Crystal
1
star
46

json_fields

A Rails add-on to create a simple UI for json fields
Ruby
1
star
47

lucky-basic-auth

HTTP Basic auth shard for Lucky
Crystal
1
star
48

memory-js

JavaScript
1
star
49

bsky

A BlueSky social API for Crystal
Crystal
1
star
50

blocked-numbers

A list of phone number from robo calls to block
Ruby
1
star