Skip to content

Commit 9dd6bb4

Browse files
committed
Initial sample application (app, specs, docs, etc)
Sample Grape API with Grape-Entity, Grape-OAuth2, ActiveRecord 5, Puma, PostgreSQL, Dotenv, Rubocop and RSpec
1 parent d832b54 commit 9dd6bb4

36 files changed

+847
-4
lines changed

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DATABASE_HOST=
2+
DATABASE_NAME=
3+
DATABASE_USER=
4+
DATABASE_PASSWORD=

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
/test/tmp/
1010
/test/version_tmp/
1111
/tmp/
12+
/log/*.log
1213

1314
# Used by dotenv library to load environment variables.
14-
# .env
15+
.env.*
1516

1617
## Specific to RubyMotion:
1718
.dat*
@@ -48,3 +49,5 @@ build-iPhoneSimulator/
4849

4950
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
5051
.rvmrc
52+
/Gemfile.lock
53+
.idea/

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--colour

.rubocop.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
LineLength:
2+
Max: 120
3+
AllCops:
4+
Exclude:
5+
- 'spec/**/*'
6+
- 'Rakefile'
7+
- db/**/*
8+
DisplayCopNames: true
9+
Rails:
10+
Enabled: false
11+
Documentation:
12+
Enabled: false
13+
Style/EndOfLine:
14+
Enabled: false

Gemfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'pg'
4+
5+
gem 'grape', '~> 0.17.0'
6+
gem 'grape-entity'
7+
gem 'grape_oauth2', git: 'https://github.com/nbulaj/grape_oauth2.git'
8+
9+
gem 'rake'
10+
gem 'rack-cors', require: 'rack/cors'
11+
12+
gem 'dotenv'
13+
14+
gem 'bcrypt'
15+
gem 'activerecord', '~> 5.0.0'
16+
gem 'otr-activerecord', '~> 1.1.0'
17+
18+
gem 'puma'
19+
20+
group :test do
21+
gem 'rspec-rails', '~> 3.5'
22+
gem 'database_cleaner'
23+
gem 'coveralls', require: false
24+
gem 'rack-test', require: 'rack/test'
25+
end
26+
27+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2016 grape-oauth2
3+
Copyright (c) 2016 Grape-OAuth2 maintainers.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: bundle exec puma -e $RACK_ENV -b unix:///tmp/web_server.sock --pidfile /tmp/web_server.pid -d -C config/puma.rb

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1-
# grape-oauth2-sample
2-
Sample application using Grape and GrapeOAuth2 gem
1+
# Grape & Grape::OAuth2 sample application
2+
[![License](http://img.shields.io/badge/license-MIT-brightgreen.svg)](#license)
3+
4+
This is an example of using GrapeOAuth2 gem with the Grape API project.
5+
6+
This app is ready to deploy to Heroku [![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy?template=https://github.com/grape-oauth2/grape-oauth2-sample).
7+
8+
Project stack includes: **Grape, Grape-Entity, Grape-OAuth2, ActiveRecord 5, Puma, PostgreSQL, Dotenv, Rubocop, RSpec**.
9+
10+
## Implemented features
11+
12+
* API endpoints (GET, POST);
13+
* Resource Owner password credentials authentication;
14+
* Protected endpoints access with OAuth Tokens;
15+
* Generate new Access Token via Refresh Token.
16+
17+
## Run
18+
19+
To run the application do the following from your command-line:
20+
21+
`> bundle exec rackup config.ru`
22+
23+
## Routes
24+
25+
To print all the API routes you can run a special Rake task:
26+
27+
`bundle exec rake grape:routes`

Rakefile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'bundler/setup'
2+
require_relative 'app/config/application'
3+
load 'tasks/otr-activerecord.rake'
4+
5+
namespace :db do
6+
task :environment do
7+
end
8+
end
9+
10+
namespace :grape do
11+
desc 'Print Grape routes'
12+
task :routes do
13+
mapping = method_mapping
14+
15+
grape_klasses = ObjectSpace.each_object(Class).select { |klass| klass < Grape::API }
16+
routes = grape_klasses.
17+
flat_map(&:routes).
18+
uniq { |r| r.send(mapping[:path]) + r.send(mapping[:method]).to_s }
19+
20+
method_width, path_width, version_width, desc_width = widths(routes, mapping)
21+
22+
routes.each do |api|
23+
method = api.send(mapping[:method]).to_s.rjust(method_width)
24+
path = api.send(mapping[:path]).to_s.ljust(path_width)
25+
version = api.send(mapping[:version]).to_s.ljust(version_width)
26+
desc = api.send(mapping[:description]).to_s.ljust(desc_width)
27+
puts " #{method} | #{path} | #{version} | #{desc}"
28+
end
29+
end
30+
31+
def widths(routes, mapping)
32+
[
33+
routes.map { |r| r.send(mapping[:method]).try(:length) }.compact.max || 0,
34+
routes.map { |r| r.send(mapping[:path]).try(:length) }.compact.max || 0,
35+
routes.map { |r| r.send(mapping[:version]).try(:length) }.compact.max || 0,
36+
routes.map { |r| r.send(mapping[:description]).try(:length) }.compact.max || 0
37+
]
38+
end
39+
40+
def method_mapping
41+
{
42+
method: 'request_method',
43+
path: 'path',
44+
version: 'version',
45+
description: 'description'
46+
}
47+
end
48+
end

app.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "GrapeOAuth2Sample",
3+
"description": "GrapeOAuth2 Sample application",
4+
"repository": "https://github.com/grape-oauth2/grape-oauth2-sample",
5+
"keywords": ["grape_oauth2", "grape", "rest", "api", "OAuth"],
6+
"scripts": {
7+
"postdeploy": "bundle exec rake db:migrate"
8+
},
9+
"addons": ["heroku-postgresql"]
10+
}

app/api.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module GrapeOAuth2Sample
2+
class API < ::Grape::API
3+
format :json
4+
prefix :api
5+
6+
include GrapeOAuth2.api
7+
8+
mount ::GrapeOAuth2Sample::Endpoints::Posts
9+
10+
desc 'Root'
11+
12+
get '/' do
13+
{ error: ['Please check API documentation'] }
14+
end
15+
end
16+
end

app/config/application.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'bundler/setup'
2+
Bundler.setup
3+
4+
require 'dotenv'
5+
Dotenv.load(File.expand_path('.env.local'), File.expand_path('.env'))
6+
7+
require 'otr-activerecord'
8+
require 'grape'
9+
require 'grape-entity'
10+
require 'grape_oauth2'
11+
12+
# Initializers
13+
Dir[File.expand_path('../initializers/*.rb', __FILE__)].each do |initializer|
14+
require_relative initializer
15+
end
16+
17+
# Database
18+
load File.expand_path('../database.rb', __FILE__)
19+
20+
# Models
21+
require_relative '../models/application_record'
22+
23+
%w(access_token access_code application post user).each do |model|
24+
require_relative "../models/#{model}"
25+
end
26+
27+
# Entities
28+
Dir[File.expand_path('../../entities/*.rb', __FILE__)].each do |entity|
29+
require_relative entity
30+
end
31+
32+
# Endpoints
33+
Dir[File.expand_path('../../endpoints/*.rb', __FILE__)].each do |endpoint|
34+
require_relative endpoint
35+
end
36+
37+
require_relative '../../app/api'

app/config/database.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Dotenv required to be boot first!
2+
OTR::ActiveRecord.configure_from_file!(File.expand_path('../database.yml', __FILE__))
3+
4+
env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'
5+
6+
::ActiveRecord::Base.default_timezone = :utc
7+
::ActiveRecord::Base.logger = Logger.new(File.expand_path("../../log/#{env}.log", __FILE__))
8+
9+
::ActiveRecord::Migration.verbose = false

app/config/database.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
default: &default
2+
adapter: postgresql
3+
encoding: unicode
4+
pool: 5
5+
6+
development:
7+
<<: *default
8+
host: <%= ENV['DATABASE_HOST'] %>
9+
database: <%= ENV['DATABASE_NAME'] %>
10+
username: <%= ENV['DATABASE_USER'] %>
11+
password: <%= ENV['DATABASE_PASSWORD'] %>
12+
13+
production:
14+
<<: *default
15+
host: <%= ENV['DATABASE_HOST'] %>
16+
database: <%= ENV['DATABASE_NAME'] %>_production
17+
username: <%= ENV['DATABASE_USER'] %>
18+
password: <%= ENV['DATABASE_PASSWORD'] %>
19+
20+
test:
21+
<<: *default
22+
host: <%= ENV['DATABASE_HOST'] %>
23+
database: <%= ENV['DATABASE_NAME'] %>_test
24+
username: <%= ENV['DATABASE_USER'] %>
25+
password: <%= ENV['DATABASE_PASSWORD'] %>
26+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
GrapeOAuth2.configure do |config|
2+
config.client_class_name = 'Application'
3+
config.access_token_class_name = 'AccessToken'
4+
config.access_grant_class_name = 'AccessCode'
5+
config.resource_owner_class_name = 'User'
6+
7+
config.realm = 'Sample Grape::OAuth2'
8+
9+
config.allowed_grant_types << 'refresh_token'
10+
end

0 commit comments

Comments
 (0)