grape-api-generator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e3658b3e980b2e434eec0d2a60bdcf49735f6ae
4
+ data.tar.gz: 447fb0d2639ac4fa47249b03d99716115ae9fefa
5
+ SHA512:
6
+ metadata.gz: 907faf96e7a87fa7b9fae828c94b2116b53c1ec56aedb30dd082317419831a02e5774f399d881efd07ee19411df5480f2a68c40510f38dfa7b8bc1ef82031e2e
7
+ data.tar.gz: 37dab1afba2c0145974ef8d054c500b32e64130a2c526c1952126dbc4d97c76b13ec0aec79a05e45d3ff11feb3740074f887875ecaf5f43bbedf4b805e0f4215
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grape-api-generator.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 ducvinh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # Grape API Generators
2
+
3
+ A collection all Rails generator scripts for Grape.
4
+
5
+ ## Getting started
6
+
7
+ Add this line to your Rails application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'grape-api-generator'
11
+ ```
12
+
13
+ Then run `bundle` command to install it.
14
+
15
+ After you installed the gem, you could run the install generator to generate basic `MyApp` API structure:
16
+
17
+ rails generate grape:install my_app
18
+
19
+ You could also specify the API version and authentication model in the generator:
20
+
21
+ rails generate grape:install my_app --version 2 --model_name customer
22
+
23
+ ## Scaffolding new resource
24
+
25
+ To scaffold a resource API, e.g. `Post`
26
+
27
+ rails generate grape:scaffold my_app post
28
+
29
+ It will create the resource API, entity and spec/test files.
30
+
31
+ You could also specify list of attributes for the resource API:
32
+
33
+ rails generate grape:scaffold my_app post title:string:r content published_at:date_time
34
+
35
+ It will expose `tilte`, `content` and `published_at` in the entity as well as define the params list as following:
36
+
37
+ ```ruby
38
+ requires :title, type: String
39
+ optional :content, type: String
40
+ optional :published_at, type: DateTime
41
+ ```
42
+
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( https://github.com/vinh0604/grape-api-generator/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+
5
+ # You can add fixtures and/or initialization code here to make experimenting
6
+ # with your gem easier. You can also use a different console, if you like.
7
+
8
+ # (If you use this, don't forget to add pry to your Gemfile!)
9
+ # require "pry"
10
+ # Pry.start
11
+
12
+ require "irb"
13
+ IRB.start
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "grape-api-generator"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Vinh Bachsy"]
9
+ spec.email = ["bsdvinhuit@gmail.com"]
10
+
11
+ spec.summary = %q{A collection of Rails generator scripts for Grape.}
12
+ spec.description = %q{A collection of Rails generator scripts for Grape.}
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "grape", ">= 0.11.0"
21
+ spec.add_runtime_dependency "grape-entity"
22
+ spec.add_runtime_dependency "grape-kaminari"
23
+ spec.add_runtime_dependency "grape-swagger"
24
+ spec.add_runtime_dependency "hashie-forbidden_attributes"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.9"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency 'rails', '~> 4.2'
29
+ end
@@ -0,0 +1,48 @@
1
+ module Grape
2
+ class InstallGenerator < Rails::Generators::Base
3
+ argument :vendor, required: true, desc: 'The API vendor name'
4
+
5
+ class_option :version, type: :string, default: '1', description: "API version"
6
+ class_option :model_name, type: :string, default: 'user', description: "Authentication model name"
7
+
8
+ source_root File.expand_path("../templates", __FILE__)
9
+
10
+ def modify_config
11
+ application "config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]"
12
+ application "config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')"
13
+ end
14
+
15
+ def create_base_api
16
+ template "api/api.rb",
17
+ "app/api/#{vendor}/api.rb"
18
+ end
19
+
20
+ def create_version_api
21
+ template "api/version.rb",
22
+ "app/api/#{vendor}/#{version}.rb"
23
+ end
24
+
25
+ def create_api_helper
26
+ template "api/api_helpers.rb",
27
+ "app/api/#{vendor}/api_helpers.rb"
28
+ end
29
+
30
+ def add_route_entry
31
+ route "mount #{vendor.classify}::#{version.upcase} => '/'"
32
+ end
33
+
34
+ def version
35
+ @version ||= version_valid? ? "v#{options.version}" : "v1"
36
+ end
37
+
38
+ def model_name
39
+ options.model_name.parameterize
40
+ end
41
+
42
+ private
43
+
44
+ def version_valid?
45
+ options.version.to_i > 0
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,54 @@
1
+ module Grape
2
+ class ScaffoldGenerator < Rails::Generators::Base
3
+ argument :vendor, required: true, desc: 'The API vendor name'
4
+ argument :resource, required: true, desc: 'The resource API name'
5
+ argument :raw_attributes, required: false, type: :array, desc: 'List of attributes of the resource API'
6
+
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ def create_api_file
10
+ template "api/resource_api.rb",
11
+ "app/api/#{vendor}/#{resource}_api.rb"
12
+ end
13
+
14
+ def create_base_entity_file
15
+ template "entities/base.rb",
16
+ "app/api/#{vendor}/entities/base.rb"
17
+ end
18
+
19
+ def create_resource_entity_file
20
+ template "entities/resource.rb",
21
+ "app/api/#{vendor}/entities/#{resource}.rb"
22
+ end
23
+
24
+ def attributes
25
+ @raw_attributes ||= []
26
+ raw_attributes.map do |attr|
27
+ parts = attr.split(':')
28
+ {
29
+ name: parts[0],
30
+ type: (parts[1].presence || 'string').classify,
31
+ required: parts[2] == 'r'
32
+ }
33
+ end
34
+ end
35
+
36
+ def attributes_names
37
+ attributes.map { |attr| attr[:name] }
38
+ end
39
+
40
+ def attributes_params
41
+ params = attributes.map do |attr|
42
+ line = attr[:required] ? 'requires' : 'optional'
43
+ line += " :#{attr[:name]}"
44
+ line += ", type: #{attr[:type]}"
45
+
46
+ line
47
+ end
48
+
49
+ params.join("\n ")
50
+ end
51
+
52
+ hook_for :test_framework, as: :grape_scaffold
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ module <%= vendor.classify %>
2
+ class API < Grape::API
3
+ include Grape::Kaminari
4
+
5
+ content_type :json, "application/json;charset=UTF-8"
6
+ format :json
7
+
8
+ helpers <%= vendor.classify %>::ApiHelpers
9
+
10
+ rescue_from ActiveRecord::RecordNotFound do |e|
11
+ error!({ error: "404 Not found." }, 404)
12
+ end
13
+
14
+ if Rails.env.production?
15
+ rescue_from :all do |e|
16
+ Rails.logger.error e.message + "\n " + e.backtrace.join("\n ")
17
+ error!({ error: "500 Internal server error." }, 500)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ module <%= vendor.classify %>
2
+ module ApiHelpers
3
+ extend Grape::API::Helpers
4
+
5
+ params :pagination do
6
+ optional :page, type: Integer, default: 1
7
+ optional :per_page, type: Integer, default: 20
8
+ end
9
+
10
+ def current_<%= model_name %>
11
+ # Implement your API authentication here
12
+ # @current_<%= model_name %> ||= <%= model_name.classify %>.authenticate!(env)
13
+ end
14
+
15
+ def authenticate!
16
+ error!({ error: "401 Unauthorized." }, 401) unless current_<%= model_name %>
17
+ end
18
+
19
+ def permitted_params
20
+ declared(params, include_missing: false)
21
+ end
22
+
23
+ def resource_error!(resource)
24
+ error!({ error: user.errors.full_messages.join(', ') }, 422)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ module <%= vendor.classify %>
2
+ class <%= resource.classify %>API < <%= vendor.classify %>::API
3
+ resource :<%= resource.pluralize %> do
4
+ paginate per_page: 20
5
+
6
+ get do
7
+ <%= resource.pluralize %> = <%= resource.classify %>.all
8
+ present paginate(<%= resource.pluralize %>), with: <%= vendor.classify %>::Entities::<%= resource.classify %>
9
+ end
10
+
11
+ params do
12
+ <%= attributes_params %>
13
+ end
14
+ post do
15
+ <%= resource %> = <%= resource.classify %>.new(permitted_params)
16
+
17
+ if <%= resource %>.save
18
+ present <%= resource %>, with: <%= vendor.classify %>::Entities::<%= resource.classify %>
19
+ else
20
+ resource_error!(<%= resource %>)
21
+ end
22
+ end
23
+
24
+ params do
25
+ <%= attributes_params %>
26
+ end
27
+ put ":id" do
28
+ <%= resource %> = <%= resource.classify %>.find(params[:id])
29
+
30
+ if <%= resource %>.update_attributes(permitted_params)
31
+ present <%= resource %>, with: <%= vendor.classify %>::Entities::<%= resource.classify %>
32
+ else
33
+ resource_error!(<%= resource %>)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ module <%= vendor.classify %>
2
+ class <%= version.upcase %> < <%= vendor.classify %>::API
3
+ version "<%= version %>", using: :path
4
+ prefix :api
5
+
6
+ get do
7
+ { version: "<%= version %>" }
8
+ end
9
+
10
+ add_swagger_documentation hide_format: true, api_version: "api/<%= version %>"
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module <%= vendor.classify %>
2
+ module Entities
3
+ class Base < Grape::Entity
4
+ format_with(:iso_timestamp) { |dt| dt ? dt.iso8601 : nil }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module <%= vendor.classify %>
2
+ module Entities
3
+ class <%= resource.classify %> < <%= vendor.classify %>::Entities::Base
4
+ expose :id
5
+ <% attributes_names.each do |name| %> expose :<%= name %>
6
+ <% end %>
7
+ with_options(format_with: :iso_timestamp) do
8
+ expose :created_at
9
+ expose :updated_at
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Rspec
2
+ module Generators
3
+ class GrapeScaffoldGenerator < Rails::Generators::Base
4
+ argument :vendor, required: true, desc: 'The API vendor name'
5
+ argument :resource, required: true, desc: 'The resource API name'
6
+
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ def create_spec_file
10
+ template "resource_spec.rb",
11
+ "spec/api/#{vendor}/#{resource}_api_spec.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ require "rails_helper"
2
+
3
+ describe <%= vendor.classify %>::<%= resource.classify %>API do
4
+ end
@@ -0,0 +1,15 @@
1
+ module TestUnit
2
+ module Generators
3
+ class GrapeScaffoldGenerator < Rails::Generators::Base
4
+ argument :vendor, required: true, desc: 'The API vendor name'
5
+ argument :resource, required: true, desc: 'The resource API name'
6
+
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ def create_spec_file
10
+ template "resource_test.rb",
11
+ "test/api/#{vendor}/#{resource}_api_test.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ require 'test_helper'
2
+
3
+ module <%= vendor.classify %>
4
+ class <%= resource.classify %>APITest < ActionDispatch::IntegrationTest
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ require 'grape'
2
+ require 'grape-entity'
3
+ require 'grape-kaminari'
4
+ require 'grape-swagger'
5
+ require 'hashie-forbidden_attributes'
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-api-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vinh Bachsy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.11.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: grape-entity
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: grape-kaminari
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: grape-swagger
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: hashie-forbidden_attributes
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.2'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.2'
125
+ description: A collection of Rails generator scripts for Grape.
126
+ email:
127
+ - bsdvinhuit@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".travis.yml"
134
+ - CODE_OF_CONDUCT.md
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/rake
141
+ - bin/setup
142
+ - grape-api-generator.gemspec
143
+ - lib/generators/grape/install_generator.rb
144
+ - lib/generators/grape/scaffold_generator.rb
145
+ - lib/generators/grape/templates/api/api.rb
146
+ - lib/generators/grape/templates/api/api_helpers.rb
147
+ - lib/generators/grape/templates/api/resource_api.rb
148
+ - lib/generators/grape/templates/api/version.rb
149
+ - lib/generators/grape/templates/entities/base.rb
150
+ - lib/generators/grape/templates/entities/resource.rb
151
+ - lib/generators/rspec/grape_scaffold_generator.rb
152
+ - lib/generators/rspec/templates/resource_spec.rb
153
+ - lib/generators/test_unit/grape_scaffold_generator.rb
154
+ - lib/generators/test_unit/templates/resource_test.rb
155
+ - lib/grape-api-generator.rb
156
+ homepage:
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.4.6
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: A collection of Rails generator scripts for Grape.
180
+ test_files: []