hoc_utils 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 37c0907c531c99595c93af77a2265257d24390b5
4
+ data.tar.gz: a282780241a4d3b2accffcc5446adc6cb02f7ccf
5
+ SHA512:
6
+ metadata.gz: 2494e4a797cceeaf9de3f621fb362915a95a89498d5ad920b21f5823bf50bd49c8642e1bd9aa5feb1aa4928fd7bbb9642c73176db880c3d602d2ab5da2a351fc
7
+ data.tar.gz: 018d8ddf9283f118b16430c97512af321b0e0448b1cd45a6d1b89d1ef4ee591d0914c3d62e07392d35d4a15052ebc2440236b4686d5b9677a92adecc31b3ae60
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in hoc_utils.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Gert Lavsen
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.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # HocUtils
2
+
3
+ This is a collection of utilities for a HoC API application
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'hoc_utils'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install hoc_utils
19
+
20
+
21
+ ## Contributing
22
+
23
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hoc_utils.
24
+
25
+ ## License
26
+
27
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hoc_utils"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/hoc_utils.gemspec ADDED
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hoc_utils/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hoc_utils"
8
+ spec.version = HocUtils::VERSION
9
+ spec.authors = ["Gert Lavsen"]
10
+ spec.email = ["gert@houseofcode.io"]
11
+
12
+ spec.summary = "Collection of utilities for the House of Code API project"
13
+ spec.description = "Collection of utilities for the House of Code API project"
14
+ spec.homepage = "https://github.com/house-of-code/hoc_utils"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+ spec.add_development_dependency "rails-rspec"
24
+ spec.add_development_dependency "trestle"
25
+ spec.add_development_dependency "bundler", "~> 1.16.a"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,13 @@
1
+ Description:
2
+ This generator can scaffold a model, api controller, admin controller and stubs for api specification in swagger format
3
+
4
+ Example:
5
+ rails generate hoc_utils:api_scaffold NAME field[:type][:index] field[:type][:index] --api_version=v2
6
+
7
+ This will create:
8
+ A model: /app/models/name.rb
9
+ A migration for model
10
+ A controller: /app/controllers/api/v2/names_controller.rb
11
+ Routes for api_controller
12
+ A administration controller: /app/admin/names_admin.rb
13
+ Swagger documentation stubs: /spec/integration/names_spec.rb
@@ -0,0 +1,99 @@
1
+
2
+ require 'rails/generators/resource_helpers'
3
+ require "rails/generators/model_helpers"
4
+
5
+ module HocUtils
6
+ module Generators
7
+ class ApiScaffoldGenerator < Rails::Generators::NamedBase
8
+
9
+ include Rails::Generators::ModelHelpers
10
+ include Rails::Generators::ResourceHelpers
11
+
12
+ desc "Generate a model with api controller and optional administration interface and routes for api controller"
13
+ argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
14
+
15
+ class_option :admin, type: :boolean, default: true, desc: "Generate administration interface"
16
+ class_option :routes, type: :boolean, default: true, desc: "Generate routes"
17
+ class_option :api_version, type: :string, default: "v1", desc: "Version of namespace"
18
+
19
+ source_root File.expand_path('templates', __dir__)
20
+
21
+
22
+ def api_version
23
+ options.api_version.downcase
24
+ end
25
+
26
+ # Generates the model and the migration. Overrides the model definition to add acts_as_api block
27
+ def generate_model
28
+ say "Generates app/models/#{singular_table_name}.rb", :bold
29
+ invoke :model
30
+ inject_into_class "app/models/#{singular_table_name}.rb", singular_table_name.camelize do
31
+ <<-ACTS
32
+
33
+ acts_as_api
34
+ api_accessible :basic do |t|
35
+ #{attributes_names.map { |name| "\tt.add :#{name}" }.join("\n")}
36
+ t.add :created_at
37
+ t.add :updated_at
38
+ end
39
+
40
+ ACTS
41
+ end
42
+ end
43
+
44
+ # Scaffolds the api controller
45
+ def generate_api_controller
46
+ say "Generates app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb", :bold
47
+ template "api_controller.rb.tt", "app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb"
48
+ end
49
+
50
+ # Generates routes in config/routes.rb. Will namespace resources to api/[api_version].
51
+ def generate_routes
52
+ return unless options.routes?
53
+ say "Generates routes. You may want to merge api/#{api_version} namespaces in config/routes.rb", :bold
54
+ generate "resource_route api/#{api_version.downcase}/#{plural_table_name}"
55
+ end
56
+
57
+ # Generates spec stubs for swagger.
58
+ def generate_specs
59
+ say "Generates spec/integration/#{plural_table_name}_spec.rb", :bold
60
+ template "spec.rb.tt", "spec/integration/#{plural_table_name}_spec.rb"
61
+ say "Adds definitions to spec/swagger_helper.rb", :bold
62
+ insert_into_file "spec/swagger_helper.rb", :after => "definitions: {\n" do
63
+ %{
64
+ # AUTO GENERATED STUB TODO: update with correct fields
65
+ #{singular_table_name}_input: {
66
+ description: 'TODO: replace with correct description',
67
+ type: 'object',
68
+ properties: {
69
+
70
+ },
71
+ required: [] #TODO require
72
+ },
73
+ # AUTO GENERATED STUB TODO: update with correct fields
74
+ #{singular_table_name}: {
75
+ description: 'TODO: replace with correct description',
76
+ type: 'object',
77
+ properties: {
78
+ id: { type: "integer"},
79
+ created_at: { type: "string"},
80
+ updated_at: { type: "string"},
81
+ }
82
+ },
83
+ }
84
+ end
85
+ end
86
+
87
+ # Generates admin controllers in app/admin
88
+ def generate_admin_controllers
89
+ return unless options.admin?
90
+ say "Generates app/admin/#{plural_table_name}_admin.rb", :bold
91
+ generate "trestle:resource #{singular_table_name}"
92
+ end
93
+
94
+ def migrate
95
+ rails_command 'db:migrate'
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,52 @@
1
+ class Api::<%= api_version.upcase %>::<%= plural_table_name.camelize %>Controller < Api::<%= api_version.upcase %>::ApiController
2
+
3
+ before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
4
+
5
+ # GET /api/<%= api_version %><%= route_url %>
6
+ def index
7
+ @<%= plural_table_name %> = <%= class_name %>.all
8
+ render_result({ <%= plural_table_name %>: @<%= plural_table_name %>.as_api_response(:basic) })
9
+ end
10
+
11
+ # GET /api/<%= api_version %><%= route_url %>/1
12
+ def show
13
+ render_result(@<%= singular_table_name %>.as_api_response(:basic))
14
+ end
15
+
16
+ # POST /api/<%= api_version %><%= route_url %>
17
+ def create
18
+ @<%= "#{singular_table_name} = #{class_name}.new(#{singular_table_name}_params)" %>
19
+
20
+ if @<%= singular_table_name %>
21
+ render_result(@<%= singular_table_name %>.as_api_response(:basic), :created)
22
+ else
23
+ render_error(@<%= singular_table_name %>.errors.full_messages.first, :unprocessable_entity)
24
+ end
25
+ end
26
+
27
+ # PATCH/PUT /api/<%= api_version %><%= route_url %>/1
28
+ def update
29
+ if @<%= singular_table_name %>.update(<%= singular_table_name %>_params)
30
+ render_result(@<%= singular_table_name %>.as_api_response(:basic))
31
+ else
32
+ render_error(@<%= singular_table_name %>.errors.full_messages.first, :unprocessable_entity)
33
+ end
34
+ end
35
+
36
+ # DELETE /api/<%= api_version %><%= route_url %>/1
37
+ def destroy
38
+ @<%= singular_table_name %>.destroy
39
+ end
40
+
41
+ private
42
+
43
+ # Use callbacks to share common setup or constraints between actions.
44
+ def set_<%= singular_table_name %>
45
+ @<%= singular_table_name %> = <%= class_name %>.find(params[:id])
46
+ end
47
+
48
+ # Only allow a trusted parameter "white list" through.
49
+ def <%= "#{singular_table_name}_params" %>
50
+ params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
51
+ end
52
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+ require 'swagger_helper'
3
+ describe 'House of Code API' do
4
+ path '/api/<%= "#{api_version}/#{plural_table_name}" %>' do
5
+ get 'Get <%= plural_table_name %>' do
6
+ tags '<%= plural_table_name.camelize %>'
7
+ description 'Gets <%= plural_table_name %>'
8
+ produces 'application/json'
9
+ consumes 'application/json'
10
+ parameter name: :Authorization, in: :header, required: true, type: :string
11
+ response '200', '<%= plural_table_name.camelize %>' do
12
+ schema '$ref' => '#/definitions/<%= plural_table_name %>'
13
+ run_test!
14
+ end
15
+ response '401', 'Not authorized' do
16
+ schema '$ref' => '#/definitions/error'
17
+ run_test!
18
+ end
19
+ end
20
+
21
+ post 'creates a profile' do
22
+ tags '<%= singular_table_name.camelize %>'
23
+ description 'Creates <%= singular_table_name %> with given input'
24
+ produces 'application/json'
25
+ consumes 'application/json'
26
+ parameter name: :<%= singular_table_name %>, in: :body, schema: {
27
+ '$ref' => '#/definitions/<%= singular_table_name %>_input'
28
+ }
29
+
30
+ response '201', '<%= singular_table_name.camelize %> created' do
31
+ schema '$ref' => '#/definitions/<%= singular_table_name %>'
32
+ run_test!
33
+ end
34
+ response '401', 'Not authorized' do
35
+ schema '$ref' => '#/definitions/error'
36
+ run_test!
37
+ end
38
+ end
39
+ end
40
+
41
+ path '/api/<%= "#{api_version}/#{plural_table_name}" %>/{id}' do
42
+ get 'Get <%= singular_table_name %>' do
43
+ tags '<%= plural_table_name %>'
44
+ description 'Gets <%= singular_table_name %> with :id'
45
+ produces 'application/json'
46
+ consumes 'application/json'
47
+ parameter name: :id, in: :path, type: :integer
48
+ parameter name: :Authorization, in: :header, required: true, type: :string
49
+ response '200', '<%= singular_table_name.camelize %>' do
50
+ schema '$ref' => '#/definitions/<%= singular_table_name %>'
51
+ run_test!
52
+ end
53
+ response '401', 'Not authorized' do
54
+ schema '$ref' => '#/definitions/error'
55
+ run_test!
56
+ end
57
+ end
58
+
59
+ put 'Update <%= singular_table_name %>' do
60
+ tags '<%= plural_table_name %>'
61
+ description 'Updates <%= singular_table_name %> with :id'
62
+ produces 'application/json'
63
+ consumes 'application/json'
64
+ parameter name: :id, in: :path, type: :integer
65
+ parameter name: :Authorization, in: :header, required: true, type: :string
66
+ response '200', '<%= singular_table_name.camelize %>' do
67
+ schema '$ref' => '#/definitions/<%= singular_table_name %>_input'
68
+ run_test!
69
+ end
70
+ response '401', 'Not authorized' do
71
+ schema '$ref' => '#/definitions/error'
72
+ run_test!
73
+ end
74
+ end
75
+
76
+ delete 'Delete <%= singular_table_name %> with :id' do
77
+ tags '<%= plural_table_name %>'
78
+ description 'Deletes <%= singular_table_name %> with :id'
79
+ produces 'application/json'
80
+ consumes 'application/json'
81
+ parameter name: :Authorization, in: :header, required: true, type: :string
82
+ response '200', '<%= singular_table_name.camelize %> deleted' do
83
+ run_test!
84
+ end
85
+ response '401', 'Not authorized' do
86
+ schema '$ref' => '#/definitions/error'
87
+ run_test!
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module HocUtils
2
+ VERSION = "0.1.1"
3
+ end
data/lib/hoc_utils.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "hoc_utils/version"
2
+
3
+ module HocUtils
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hoc_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Gert Lavsen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails-rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: trestle
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.16.a
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.16.a
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Collection of utilities for the House of Code API project
70
+ email:
71
+ - gert@houseofcode.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - hoc_utils.gemspec
84
+ - lib/generators/hoc_utils/api_scaffold/USAGE
85
+ - lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb
86
+ - lib/generators/hoc_utils/api_scaffold/templates/api_controller.rb.tt
87
+ - lib/generators/hoc_utils/api_scaffold/templates/spec.rb.tt
88
+ - lib/hoc_utils.rb
89
+ - lib/hoc_utils/version.rb
90
+ homepage: https://github.com/house-of-code/hoc_utils
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.6.13
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Collection of utilities for the House of Code API project
114
+ test_files: []