Last active
January 5, 2019 18:08
-
-
Save katafrakt/b8ffc84dfc94c8b53c349dc725767f28 to your computer and use it in GitHub Desktop.
This is a showcase for a new API framework under development. Code is not yet publicly available.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'kraftwerk', path: 'kraftwerk' | |
end | |
DATABASE = [] | |
module Measures | |
class Index | |
include Kraftwerk::Endpoint | |
def call(_params) | |
DATABASE | |
end | |
end | |
class Create | |
include Kraftwerk::Endpoint | |
validate do | |
required(:value) { filled? & int? } | |
optional(:tag) { str? } | |
end | |
def call(params) | |
DATABASE << params | |
end | |
end | |
class Average | |
include Kraftwerk::Endpoint | |
validate do | |
optional(:last) { int? } | |
optional(:tag) { str? } | |
end | |
def call(params) | |
values = DATABASE | |
values = values.select { |v| v[:tag] == params[:tag] } if params[:tag] | |
values = values.last(params[:last]) if params[:last] | |
if !values.empty? | |
avg = values.map { |v| v[:value] }.reduce(:+) / values.size.to_f | |
{ average: avg } # normal object returned | |
else | |
# response object | |
Kraftwerk::Response.new(code: 404, body: { error: 'No elements to calculate average from' }) | |
end | |
end | |
end | |
end | |
app = Kraftwerk::App.new | |
app.routing do | |
get '/measures', to: Measures::Index | |
post '/measures', to: Measures::Create | |
get '/measures/average', to: Measures::Average | |
end | |
Rack::Server.start app: app.rack_app, Port: 2300 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is how it would look like in application structure: | |
. | |
├── config.ru | |
├── endpoints | |
│ └── measures | |
│ ├── average.rb | |
│ ├── create.rb | |
│ └── index.rb | |
├── Gemfile | |
├── Gemfile.lock | |
└── routes.rb | |
2 directories, 7 files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> curl -i localhost:2300 | |
HTTP/1.1 404 Not Found | |
X-Cascade: pass | |
Content-Type: application/json | |
Server: WEBrick/1.4.2 (Ruby/2.5.1/2018-03-29) | |
Date: Sat, 05 Jan 2019 18:01:14 GMT | |
Content-Length: 21 | |
Connection: Keep-Alive | |
{"error":"not found"} | |
--------------------------------------------------------- | |
> curl -i localhost:2300/measures | |
HTTP/1.1 200 OK | |
Server: WEBrick/1.4.2 (Ruby/2.5.1/2018-03-29) | |
Date: Sat, 05 Jan 2019 18:01:17 GMT | |
Content-Length: 2 | |
Connection: Keep-Alive | |
[] | |
--------------------------------------------------------- | |
> curl -i localhost:2300/measures/average | |
HTTP/1.1 404 Not Found | |
Server: WEBrick/1.4.2 (Ruby/2.5.1/2018-03-29) | |
Date: Sat, 05 Jan 2019 18:02:13 GMT | |
Content-Length: 49 | |
Connection: Keep-Alive | |
{"error":"No elements to calculate average from"} | |
--------------------------------------------------------- | |
> curl -XPOST localhost:2300/measures -H 'Content-Type: application/json' -d '{}' -i | |
HTTP/1.1 400 Bad Request | |
Server: WEBrick/1.4.2 (Ruby/2.5.1/2018-03-29) | |
Date: Sat, 05 Jan 2019 18:02:54 GMT | |
Content-Length: 24 | |
Connection: Keep-Alive | |
{"value":["is missing"]} | |
--------------------------------------------------------- | |
> url -XPOST localhost:2300/measures -H 'Content-Type: application/json' -d '{"value": 100}' -i | |
HTTP/1.1 201 Created | |
Server: WEBrick/1.4.2 (Ruby/2.5.1/2018-03-29) | |
Date: Sat, 05 Jan 2019 18:03:24 GMT | |
Content-Length: 15 | |
Connection: Keep-Alive | |
[{"value":100}] | |
--------------------------------------------------------- | |
> curl -XPOST localhost:2300/measures -H 'Content-Type: application/json' -d '{"value": 100, "tag": "test"}' -i | |
HTTP/1.1 201 Created | |
Server: WEBrick/1.4.2 (Ruby/2.5.1/2018-03-29) | |
Date: Sat, 05 Jan 2019 18:03:57 GMT | |
Content-Length: 42 | |
Connection: Keep-Alive | |
[{"value":100},{"value":100,"tag":"test"}] | |
--------------------------------------------------------- | |
> curl -XPOST localhost:2300/measures -H 'Content-Type: application/json' -d '{"value": 100, "tag": "test", "admin": true}' -i | |
HTTP/1.1 201 Created | |
Server: WEBrick/1.4.2 (Ruby/2.5.1/2018-03-29) | |
Date: Sat, 05 Jan 2019 18:04:36 GMT | |
Content-Length: 69 | |
Connection: Keep-Alive | |
[{"value":100},{"value":100,"tag":"test"},{"value":100,"tag":"test"}] | |
--------------------------------------------------------- | |
> curl -i localhost:2300/measures/average | |
HTTP/1.1 200 OK | |
Server: WEBrick/1.4.2 (Ruby/2.5.1/2018-03-29) | |
Date: Sat, 05 Jan 2019 18:05:09 GMT | |
Content-Length: 17 | |
Connection: Keep-Alive | |
{"average":100.0} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment