Skip to content

Commit

Permalink
update ruby sinatra samples
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Apr 21, 2021
1 parent d4748a7 commit 9256108
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 768 deletions.
10 changes: 10 additions & 0 deletions samples/server/petstore/ruby-sinatra/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Dockerfile
Gemfile
README.md
api/pet_api.rb
api/store_api.rb
api/user_api.rb
config.ru
lib/openapiing.rb
my_app.rb
openapi.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.1-SNAPSHOT
5.1.1-SNAPSHOT
32 changes: 32 additions & 0 deletions samples/server/petstore/ruby-sinatra/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Build libraries
FROM ruby:3.0-alpine as rubydev

## for thin or falcon
#RUN apk --no-cache add make g++ libc-dev
## for puma
#RUN apk --no-cache add make gcc libc-dev

ADD . /app
WORKDIR /app

RUN bundle config set path lib
RUN bundle install

## Build Runtime image
FROM ruby:3.0-alpine

RUN apk --no-cache add tzdata ## ca-certificates

COPY --from=rubydev /app /app
WORKDIR /app

ENV SINATRA_HOST 0.0.0.0
ENV SINATRA_PORT 8080
EXPOSE $SINATRA_PORT

RUN addgroup sinatra
RUN adduser -S -G sinatra sinatra
USER sinatra
RUN bundle config set path lib

CMD bundle exec rackup --host $SINATRA_HOST -p $SINATRA_PORT
3 changes: 2 additions & 1 deletion samples/server/petstore/ruby-sinatra/Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source 'https://rubygems.org'

gem "webrick"
gem "sinatra"
gem "sinatra-cross_origin"
gem "sinatra-cross_origin"
52 changes: 44 additions & 8 deletions samples/server/petstore/ruby-sinatra/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
# Swagger for Sinatra
# OpenAPI for Sinatra

## Overview
This is a project to provide Swagger support inside the [Sinatra](http://www.sinatrarb.com/) framework. You can find
out more about both the spec and the framework at http://swagger.io. For more information about
Wordnik's APIs, please visit http://developer.wordnik.com.

## Prerequisites
You need to install ruby 1.9.3 and the following gems:
As of ruby 3.0.0, the webrick web server library was removed.
You need to install a rack-supported web server, such as webrick and thin.

The default Gemfile is as follows.
Update the name of the web server as your prefer.

```
sinatra
sinatra-cross_origin
source 'https://rubygems.org'
gem "webrick"
gem "sinatra"
gem "sinatra-cross_origin"
```

## Getting started
This sample was generated with the [OpenAPI Generator](https://github.com/openapitools/openapi-generator) project.
To generate a ruby-sinatra server for petstore.yaml, please run the following:

```
openapi-generator-cli generate \
-i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml \
-g ruby-sinatra -o code
```

To run the generated server, please run the following:

```
cd code/
bundle config set path lib
bundle install
bundle exec rackup -p 8080
```

You can access the application by the following URL:

```
http://localhost:8080/v2/store/inventory
```

## Docker
If you want to use a web server other than webrick, you need to edit the generated Dockerfile to prepare the compiler and the make command. Please check the comment of the Dockerfile.

To run the code on docker, you can use the Dockerfile as follows:

### Build the docker image
The "container_name" can be changed for your preferences.

```
rackup -p 4567 config.ru
docker build . --tag "container_name"
```

In your [swagger ui](https://github.com/swagger-api/swagger-ui), put in the following URL:
### Run the docker image

```
http://localhost:4567/resources.json
docker run -it --rm -p 8080:8080 "container_name"
```

Voila!
4 changes: 2 additions & 2 deletions samples/server/petstore/ruby-sinatra/api/pet_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"resourcePath" => "/Pet",
"summary" => "Add a new pet to the store",
"nickname" => "add_pet",
"responseClass" => "void",
"responseClass" => "Pet",
"endpoint" => "/pet",
"notes" => "",
"parameters" => [
Expand Down Expand Up @@ -123,7 +123,7 @@
"resourcePath" => "/Pet",
"summary" => "Update an existing pet",
"nickname" => "update_pet",
"responseClass" => "void",
"responseClass" => "Pet",
"endpoint" => "/pet",
"notes" => "",
"parameters" => [
Expand Down
Loading

0 comments on commit 9256108

Please sign in to comment.