Giter Club home page Giter Club logo

grape-on-rack's Introduction

Grape API on Rack

Test Code Climate

A Grape API mounted on Rack.

Run

$ bundle install
$ rackup

Loading NewRelic in developer mode ...
Puma starting in single mode...
* Puma version: 6.4.0 (ruby 2.7.7-p221) ("The Eagle of Durango")
*  Min threads: 0
*  Max threads: 5
*  Environment: development
*          PID: 82944
* Listening on http://127.0.0.1:9292
* Listening on http://[::1]:9292

List Routes

rake routes

Explore the API

Explore the API using Swagger UI. Run the application and navigate to http://locahost:9292/swagger/index.html.

Examples

A hello world example that returns a JSON document.

$ curl http://localhost:9292/api/ping

{"ping":"pong"}

A simple POST and PUT example.

curl -XPOST -d '' http://localhost:9292/api/ring 

{"rang":7}
curl -XPUT -d '{"count":2}' -H "Content-Type:application/json" http://localhost:9292/api/ring 

{"rang":9}

An example that shows a POST of JSON data.

$ curl -XPOST http://localhost:9292/api/spline -d '{"reticulated":"lots"}' -H "Content-Type:application/json"

{"reticulated":"lots"}

An example that pre-processes params sent as JSON data.

$ curl http://localhost:9292/api/reticulated_splines?splines=[{"id":1,"reticulated":true},{"id":2,"reticulated":false}]

[{"id":1,"reticulated":false},{"id":2,"reticulated":true}]

An example of rescue_from that wraps all exceptions in an HTTP error code 500.

$ curl -i http://localhost:9292/api/raise
HTTP/1.1 500 Internal Server Error
Last-Modified: Wed, 15 Jun 2022 01:12:37 GMT
Content-Type: text/html
Content-Length: 234
Vary: Origin
Server: WEBrick/1.4.2 (Ruby/2.6.5/2019-10-01)
Date: Sat, 18 Jun 2022 01:51:25 GMT
Connection: Keep-Alive

<html>
 <head>
  <title>Unexpected Error</title>
</head>
 <body>
  <h1>Ouch...</h1>
  <a href="http://rack.rubyforge.org/">
   <img src="/images/rack-logo.png">
  </a>
  <p>
    Something went terribly wrong.
  </p>
 </body>
</html>

An example that uses path-based versioning.

$ curl http://localhost:9292/api/vendor

{"path":"acme"}

An example that uses vendor header-based versioning.

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

{"header":"acme"}

A middleware that wraps all responses and always returns HTTP code 200.

$ curl http://localhost:9292/api/decorated/ping

{"body":{"ping":"pong"},"status":200}

An example that overrides the default Content-Type or returns data in both JSON and XML formats.

$ curl http://localhost:9292/api/plain_text

A red brown fox jumped over the road.
$ curl http://localhost:9292/api/mixed

{"data":"A red brown fox jumped over the road."}
$ curl http://localhost:9292/api/mixed.xml

<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <data>A red brown fox jumped over the road.</data>
</hash>

An example that demonstrates a file upload and download.

$ curl -X POST -i -F image_file=@spec/fixtures/grape_logo.png http://localhost:9292/api/avatar

{"filename":"grape_logo.png","size":4272}
$ curl -X POST -i -F file=@spec/fixtures/grape_logo.png http://localhost:9292/api/download.png

HTTP/1.1 201 Created
Content-Type: image/png
Content-Disposition: attachment; filename*=UTF-8''grape_logo.png
Vary: Origin
Content-Length: 4272
Server: WEBrick/1.4.2 (Ruby/2.6.5/2019-10-01)
Date: Sat, 18 Jun 2022 02:12:21 GMT
Connection: Keep-Alive
$ curl -X POST -i -F file=@api/ping.rb http://localhost:9292/api/download.rb

HTTP/1.1 201 Created
Content-Type: application/x-ruby
Content-Disposition: attachment; filename*=UTF-8''ping.rb
Vary: Origin
Content-Length: 115
Server: WEBrick/1.4.2 (Ruby/2.6.5/2019-10-01)
Date: Sat, 18 Jun 2022 02:12:47 GMT
Connection: Keep-Alive

module Acme
  class Ping < Grape::API
    format :json
    get '/ping' do
      { ping: 'pong' }
    end
  end
end

An example of using grape-entity.

$ curl http://localhost:9292/api/entities/1

{"tool":{"id":"1","length":10,"weight":"20kg"}}

Demonstrates header case-sensitive handling.

$ curl http://localhost:9292/api/headers/Host

{"Host":"localhost:9292"}

An example of streaming data.

curl http://localhost:9292/api/stream --no-buffer
1
2
3
...

New Relic

The application is setup with NewRelic w/ Developer Mode. Navigate to http://localhost:9292/newrelic after making some API calls.

grape-on-rack's People

Contributors

dblock avatar defp avatar dependabot[bot] avatar duffn avatar hayesgm avatar humzashah avatar rjayroach avatar wprater avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grape-on-rack's Issues

not seeing index.html or proper 404 page

I'm not seeing any 404 error pages like when I run through integration tests. The error pages that show up on index.html, invalid, etc all show "Not Found". However, in the integration tests the 404 or public pages are actually being shown. Is there something I'm missing besides from just running "bundle exec rackup"? I'm on Ruby 2.0.0-p247

Nested groups/mounts

My API has some resources addressable in multiple ways. I was hoping to factor this DRY-style by having two groups with before blocks that resolve params to the internal object, and then mount the same resource management code for viewing/updating etc.

I am blocked on that approach - it seems to be that the most obvious nested mounting approach does not work. The paths in the inner mount are apparently not being registered with rack, and I get a 404 from the rack server (the Grape service is not even getting called). I cannot find any examples or documentation in Grape that would point me at an alternative approach or missing syntax.

Example code that demonstrates my problem, stripped down to a minimal version here: https://gist.github.com/Neil-Aframe/5161270

newrelic dashboard shows 'idle application'

I have a Rack based grape setup at which I added newrelic-grape to Gemfile and newrelic.yml to config, NewRelic::Agent.manual_start to configu.ru, and bundle install.
But I all I get in new relic is 'idle application'.

The only way I can get to see anything at newrelic is by adding Rack instrumentation after API class declaration (I saw this somewhere I can't remember now):

class API < Grape::API
include NewRelic::Agent::Instrumentation::Rack
...
end

Am I missing something? :(

Gemfile.lock shows:

newrelic-grape (1.3.1)
  grape
  newrelic_rpm
newrelic_rpm (3.6.5.130)

NoMethodError: undefined method `[]' for nil:NilClass

  • I tried this locally on Ruby 2.6.5
$ ruby -V
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin18]
$ bundle install
$ rackup
$ curl http://localhost:9292/api/ping
[2022-06-14 19:23:08] INFO  WEBrick 1.4.2
[2022-06-14 19:23:08] INFO  ruby 2.6.5 (2019-10-01) [x86_64-darwin18]
[2022-06-14 19:23:08] INFO  WEBrick::HTTPServer#start: pid=75523 port=9292
NoMethodError: undefined method `[]' for nil:NilClass
  1) Acme::API plain_text returns plain text
     Failure/Error: get '/api/plain_text'
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/router.rb:163:in `cascade?'
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/router.rb:95:in `transaction'
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/router.rb:72:in `identity'
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/router.rb:57:in `block in call'
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/router.rb:137:in `with_optimization'
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/router.rb:56:in `call'
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/api/instance.rb:154:in `call'
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/api/instance.rb:70:in `call!'
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/api/instance.rb:65:in `call'
     # ./vendor/bundle/ruby/2.6.0/gems/grape-1.2.4/lib/grape/api.rb:51:in `call'
     # ./vendor/bundle/ruby/2.6.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
     # ./vendor/bundle/ruby/2.6.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
     # ./vendor/bundle/ruby/2.6.0/gems/rack-test-1.1.0/lib/rack/test.rb:129:in `custom_request'
     # ./vendor/bundle/ruby/2.6.0/gems/rack-test-1.1.0/lib/rack/test.rb:58:in `get'
     # ./spec/api/content_type_spec.rb:12:in `block (3 levels) in <top (required)>'

Swagger UI does not show api methods

When I enter the localhost url in the petstore swagger example app I see is this:
[ base url: http://localhost:9292 , api version: v1 ] but when I click Raw, it appears to show the correct json.

hot reloading in dev

Is there an easy way to configure this project to hot reload when running in development mode?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.