Skip to content

Commit

Permalink
API Resource generator
Browse files Browse the repository at this point in the history
  • Loading branch information
stgeneral authored Aug 17, 2019
1 parent ad3094a commit b09ebc5
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ rails new APP_PATH --skip-action-cable --skip-coffee --skip-test --database=post
### Generators

```sh
rails generate auxiliary_rails:api_resource
rails generate auxiliary_rails:install_errors
rails generate auxiliary_rails:install_rubocop
rails generate auxiliary_rails:install_rubocop --no-specify-gems
Expand Down
63 changes: 63 additions & 0 deletions lib/generators/auxiliary_rails/api_resource_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'rails'

module AuxiliaryRails
class ApiResourceGenerator < ::Rails::Generators::NamedBase
desc 'Stubs out a new API resource including an empty entity and spec.'

class_option :api_module,
type: :string,
default: 'app',
desc: 'API module name'
class_option :api_version,
type: :numeric,
default: 1,
desc: 'API version'
class_option :skip_helper,
type: :boolean,
default: true,
desc: 'Indicates if helper generation needs to be skipped'
source_root File.expand_path('templates/apis', __dir__)

def create_api_resource_file
template 'api_resource_template.rb.erb',
"app/#{api_module_path}/resources/#{plural_file_name}_resource.rb"
end

def create_api_entity_file
template 'api_entity_template.rb.erb',
"app/#{api_module_path}/entities/#{file_name}_entity.rb"
end

def create_api_helper_file
return if options[:skip_helper]

template 'api_helper_template.rb.erb',
"app/#{api_module_path}/helpers/#{plural_file_name}_api_helper.rb"
end

def create_api_resource_spec_file
template 'api_resource_spec_template.rb.erb',
"spec/#{api_module_path}/#{plural_file_name}_resource_spec.rb"
end

private

def api_module_name
"#{options[:api_module].camelize}V#{options[:api_version]}API"
end

def api_module_path
"apis/#{options[:api_module]}_v#{options[:api_version]}_api"
end

def api_url_path
api_name = 'api'
api_name += "-#{options[:api_module]}" if options[:api_module] != 'app'
"/#{api_name}/v#{options[:api_version]}/#{plural_name}"
end

def entity_class_name
"#{api_module_name}::Entities::#{class_name}Entity"
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module <%= api_module_name %>::Entities
class <%= class_name %>Entity < Grape::Entity
expose :id
# TODO: define `<%= class_name %>Entity` exposes
expose :created_at, :updated_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module <%= api_module_name %>::Helpers
module <%= plural_name.camelize %>APIHelper
# TODO: define methods for `<%= plural_name.camelize %>Helper`
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rails_helper'

RSpec.describe <%= api_module_name %>::Resources::<%= plural_name.camelize %>Resource, type: :request do
describe 'GET <%= api_url_path %>' do
subject { get '<%= api_url_path %>' }

# TODO: write some tests
skip
end

describe 'POST <%= api_url_path %>' do
subject { post '<%= api_url_path %>' }

# TODO: write some tests
skip
end

describe 'GET <%= api_url_path %>/:id' do
# TODO: write some tests
skip
end

describe 'PUT <%= api_url_path %>/:id' do
# TODO: write some tests
skip
end

describe 'DELETE <%= api_url_path %>/:id' do
# TODO: write some tests
skip
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module <%= api_module_name %>::Resources
class <%= plural_name.camelize %>Resource < Grape::API
helpers do
def collection
# TODO: add `policy_scope`?
@<%= plural_name %> ||= <%= class_name %>.all
end

def resource
@<%= singular_name %> ||= <%= class_name %>.find(params[:id])
end
end

resource :<%= plural_name %> do
desc 'Returns resource collection'
get do
# TODO: authorize resource, :index?
present collection,
with: <%= entity_class_name %>
end

desc 'Creates a <%= class_name %>'
params do
requires :<%= singular_name %>, type: Hash do
# TODO: define resource params or remove `params` block
end
end
post do
@<%= singular_name %> = <%= class_name %>.new(declared(params)[:<%= singular_name %>])
# TODO: authorize resource, :create?
resource.save!
present resource,
with: <%= entity_class_name %>
end

params do
requires :id, type: Integer, desc: '<%= class_name %> ID'
end
route_param :id do
desc 'Returns a <%= class_name %>'
get do
# TODO: authorize resource, :show?
present resource,
with: <%= entity_class_name %>
end

desc 'Updates a <%= class_name %>'
params do
requires :<%= singular_name %>, type: Hash do
# TODO: define resource params or remove `params` block
end
end
put do
resource.assign_attributes(declared(params)[:<%= singular_name %>])
# TODO: authorize resource, :update?
resource.save!
present resource,
with: <%= entity_class_name %>
end

desc 'Deletes a <%= class_name %>'
delete do
# TODO: authorize resource, :destroy?
resource.destroy!
body false
end
end
end
end
end

0 comments on commit b09ebc5

Please sign in to comment.