<commit_before># GrapeApeRails

[![Gem Version](https://badge.fury.io/rb/grape_ape_rails.svg)](http://badge.fury.io/rb/grape_ape_rails)
[![Code Climate](https://codeclimate.com/github/mepatterson/grape_ape_rails/badges/gpa.svg)](https://codeclimate.com/github/mepatterson/grape_ape_rails)
[![Build Status](https://semaphoreapp.com/api/v1/projects/dbb9cbd7-0767-4215-b3f8-faa25510b708/231133/shields_badge.png)](https://semaphoreapp.com/mepatterson/grape_ape_rails)

The general purpose of this gem is to wrap the various best practices of integrating GrapeAPI within the context of a Rails app
into an easy-to-use macro-framework and DSL, plus some opinionated value-adds and features. Basically, Grape and Rails play
together great, but can be tricky to integrate in a robust, clean way; GrapeApeRails (hopefully) makes your life easier.

## Features/Opinions

GrapeApeRails is opinionated. The goal is to make integration easier, so GrapeApeRails makes a number of integration decisions for you:

* API endpoints respond with JSON
* API endpoints expect serialized JSON strings for POST and PUT bodies
* JSON responses are wrapped in a structure that mostly resembles the JSON-RPC spec
* GrapeApeRails APIs are header-versioned using the 'Accept' HTTP header
* API endpoints automatically handle locale if provided (either via params[:locale] or the 'Accept-Language' header) and use the `http_accept_language` middleware
* GrapeApeRails provides an ActiveSupport::Notification that can be suscribed to for logging/injection into the Rails log
* Pagination support is already included for all endpoints via the [grape-kaminari gem](https://github.com/monterail/grape-kaminari)
* Rails cache support is already included for all endpoints via the [grape-rails-cache gem](https://github.com/monterail/grape-rails-cache)
* API endpoints are automagically documented into [Swagger API XML docs](https://helloreverb.com/developers/swagger), presented by your API via mounted endpoints
* Swagger-documented APIs are automatically separated into different URIs by API version

If these opinions and features align closely to what you're planning to build for your API, I think you'll find GrapeApeRails very useful.
If you're intending to build something very different from the above, you're probably better off integrating Grape on your own, or looking at alternate projects
like [Rails::API](https://github.com/rails-api/rails-api).

## Installation

Add this line to your application's Gemfile:

    gem 'grape_ape_rails'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install grape_ape_rails

## Setup

First, GrapeApeRails needs an initializer as `config/initializers/grape_ape_rails.rb`

The easiest way to do this is to run the handy rails generator command:

```ruby
rails g grape_ape_rails:setup
```

This will:
* create your initializer with some default settings, based on how you answer the questions
* add a `tilt.root` Rack config to your application.rb for use with Rabl templates

Next, you'll need to create your `API::Base` class, which serves as the starting point for all
of your API endpoints and all versions of your API. By default, GrapeApeRails looks for a
base.rb file in `app/controllers/api/base.rb`

```ruby
# app/controllers/api/base.rb
module API
  class Base < GrapeApeRails::API
    grape_apis do
      # always mount admins first and public-facing versions after
      # with newest version lowest
      api "V1" do
        # ... mounts go here
      end
    end
  end
end
```

NOTE: the api name needs to look like a class name, with camelcase (e.g. "V1" or "AdminV1").
This actually _is_ the class that GrapeApeRails will then expect you to define for exposing your endpoints.
Internally, Grape will translate this to an under-dotted version name (e.g "v1" or "admin.v1")
and this is what will be expected in the Accept header string provided by the requestor.

## Usage

Now that you've got your base.rb ready, you need to mount your various resources/endpoints. This is
done via the `grape_mount` command. These should generally be mounted as pluralized resource names.

```ruby<commit_msg>remove confusing text that doesn't really matter<commit_after># GrapeApeRails

[![Gem Version](https://badge.fury.io/rb/grape_ape_rails.svg)](http://badge.fury.io/rb/grape_ape_rails)
[![Code Climate](https://codeclimate.com/github/mepatterson/grape_ape_rails/badges/gpa.svg)](https://codeclimate.com/github/mepatterson/grape_ape_rails)
[![Build Status](https://semaphoreapp.com/api/v1/projects/dbb9cbd7-0767-4215-b3f8-faa25510b708/231133/shields_badge.png)](https://semaphoreapp.com/mepatterson/grape_ape_rails)

The general purpose of this gem is to wrap the various best practices of integrating GrapeAPI within the context of a Rails app
into an easy-to-use macro-framework and DSL, plus some opinionated value-adds and features. Basically, Grape and Rails play
together great, but can be tricky to integrate in a robust, clean way; GrapeApeRails (hopefully) makes your life easier.

## Features/Opinions

GrapeApeRails is opinionated. The goal is to make integration easier, so GrapeApeRails makes a number of integration decisions for you:

* API endpoints respond with JSON
* API endpoints expect serialized JSON strings for POST and PUT bodies
* JSON responses are wrapped in a structure that mostly resembles the JSON-RPC spec
* GrapeApeRails APIs are header-versioned using the 'Accept' HTTP header
* API endpoints automatically handle locale if provided (either via params[:locale] or the 'Accept-Language' header) and use the `http_accept_language` middleware
* GrapeApeRails provides an ActiveSupport::Notification that can be suscribed to for logging/injection into the Rails log
* Pagination support is already included for all endpoints via the [grape-kaminari gem](https://github.com/monterail/grape-kaminari)
* Rails cache support is already included for all endpoints via the [grape-rails-cache gem](https://github.com/monterail/grape-rails-cache)
* API endpoints are automagically documented into [Swagger API XML docs](https://helloreverb.com/developers/swagger), presented by your API via mounted endpoints
* Swagger-documented APIs are automatically separated into different URIs by API version

If these opinions and features align closely to what you're planning to build for your API, I think you'll find GrapeApeRails very useful.
If you're intending to build something very different from the above, you're probably better off integrating Grape on your own, or looking at alternate projects
like [Rails::API](https://github.com/rails-api/rails-api).

## Installation

Add this line to your application's Gemfile:

    gem 'grape_ape_rails'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install grape_ape_rails

## Setup

First, GrapeApeRails needs an initializer as `config/initializers/grape_ape_rails.rb`

The easiest way to do this is to run the handy rails generator command:

```ruby
rails g grape_ape_rails:setup
```

This will:
* create your initializer with some default settings, based on how you answer the questions
* add a `tilt.root` Rack config to your application.rb for use with Rabl templates

Next, you'll need to create your `API::Base` class, which serves as the starting point for all
of your API endpoints and all versions of your API. By default, GrapeApeRails looks for a
base.rb file in `app/controllers/api/base.rb`

```ruby
# app/controllers/api/base.rb
module API
  class Base < GrapeApeRails::API
    grape_apis do
      api "V1" do
        # ... mounts go here
      end
    end
  end
end
```

NOTE: the api name needs to look like a class name, with camelcase (e.g. "V1" or "AdminV1").
This actually _is_ the class that GrapeApeRails will then expect you to define for exposing your endpoints.
Internally, Grape will translate this to an under-dotted version name (e.g "v1" or "admin.v1")
and this is what will be expected in the Accept header string provided by the requestor.

## Usage

Now that you've got your base.rb ready, you need to mount your various resources/endpoints. This is
done via the `grape_mount` command. These should generally be mounted as pluralized resource names.

```ruby
{'fraction_non_alphanumeric': 0.07291286912139992, 'fraction_numerical': 0.0077773727062826585, 'mean_word_length': 4.715277777777778, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 18, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4056931', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2334, 'n_words': 999}