stockpot 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 988e38a908f21076e0eb727da147dd47ce9bd8958265ee914f9a37445504dc79
4
- data.tar.gz: 81c617349f3bdff403877c6b9cbca27802fe54d9a16382d18e6c75679f69de10
3
+ metadata.gz: 636f8c0d7d7a02d00509575fe7aa7c96a09d8e1e6622ed70d83e7afc9c348806
4
+ data.tar.gz: 85c587ba47f767349fc18632ab11bf7aad856fe82a9cd128ef40d990d33d725c
5
5
  SHA512:
6
- metadata.gz: 90c2d16c82aa6988a1e5e4cde8b166bee04db205c934eca52246a009cc7aacb52f81a29d7c86e875a4252efc330ce67c9f1b99b18d477ef549efb6a94686e747
7
- data.tar.gz: 2cf43996d38975b6427eb19512dddf1e5f59824c8d490e75af3016d7fa6b0c7b2b6976f41124698151a857a2ef1c158a6e7951553187d780895ac2c49f958b9d
6
+ metadata.gz: 24b2cd80a3e5ecc68cdafe5653832c381f3c1e5e36ab88b1782cb2600cbadf180b3c9da5fb564afc4f8077a4f9e6c43629ae97039bd66f0d0c5c554ef9b16d80
7
+ data.tar.gz: 4623905f392bf7d7d9879ce44599f81d5108b1b8a3be284a76ea850daaecc11e5f7befdea83c6fe2c2137441f1f89f6656feaea1c747b37e733d669ba0555fc4
data/README.md CHANGED
@@ -48,6 +48,10 @@ gem install stockpot
48
48
 
49
49
  ## Rails App
50
50
 
51
+ ## !! Warning !!
52
+
53
+ **You should only enable this in environments that are **NOT** production. If you choose to ignore this warning, do so at your own risk!! Wrap the following in a check for environments with something like `Rails.env.test?` if you don't have anything else in place.**
54
+
51
55
  Add the `Stockpot` engine to your `/config/routes.rb` file, changing the base path if you'd like to:
52
56
 
53
57
  ```ruby
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require "bundler/setup"
5
+ rescue LoadError
6
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
7
+ end
8
+
9
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
10
+
11
+ require File.expand_path("../spec/dummy/config/application", __FILE__)
12
+ require "bundler/gem_tasks"
13
+ require "rspec/core/rake_task"
14
+
15
+ load "rails/tasks/engine.rake"
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ task :release do
20
+ sh "bundle exec rake release"
21
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stockpot
4
+ class ApplicationController < ActionController::API
5
+ # protect_from_forgery with: :exception
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "database_cleaner"
4
+
5
+ require_dependency "stockpot/application_controller"
6
+
7
+ module Stockpot
8
+ class DatabaseCleanerController < ApplicationController
9
+ # Clean database before, between, and after tests by clearing Rails
10
+ # and REDIS caches and truncating the active record database.
11
+ def index
12
+ clear_cache
13
+ clean_active_record
14
+ render json: { status: 204 }
15
+ end
16
+
17
+ private
18
+
19
+ def clear_cache
20
+ DatabaseCleaner[:redis].clean_with(:truncation)
21
+ REDIS.flushdb
22
+ Rails.cache.clear
23
+ Timecop.return
24
+ end
25
+
26
+ def clean_active_record
27
+ DatabaseCleaner[:active_record].clean_with(:truncation)
28
+ DatabaseCleaner.clean_with(:truncation)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_dependency "stockpot/application_controller"
4
+
5
+ require "factory_bot_rails"
6
+
7
+ module Stockpot
8
+ class RecordsController < ApplicationController
9
+ include ActiveSupport::Inflector
10
+
11
+ def index
12
+ return_error("You need to provide at least one model name as an argument", 400) && return if params.dig(:models).blank?
13
+
14
+ obj = {}
15
+ models.each_with_index do |element, i|
16
+ model = element[:model].to_s
17
+ obj[pluralize(model).camelize(:lower)] = model.camelize.constantize.where(models[i].except(:model))
18
+ end
19
+
20
+ render json: obj.to_json, status: :ok
21
+ end
22
+
23
+ def create
24
+ return_error("You need to provide at least one factory name as an argument", 400) && return if params.dig(:factory).blank?
25
+
26
+ list = params[:list] || 1
27
+ list.times do |n|
28
+ if params[:traits].present? && params[:attributes].present?
29
+ FactoryBot.create(factory, *traits, attributes[n])
30
+ elsif params[:traits].blank? && params[:attributes].blank?
31
+ FactoryBot.create!(factory)
32
+ elsif params[:attributes].blank?
33
+ FactoryBot.create(factory, *traits)
34
+ elsif params[:traits].blank?
35
+ FactoryBot.create(factory, attributes[n])
36
+ end
37
+ end
38
+ obj = factory.to_s.camelize.constantize.last(list)
39
+ render json: obj.to_json, status: :created
40
+ end
41
+
42
+ def destroy
43
+ return_error("You need to provide at least one model name as an argument", 400) && return if params.dig(:models).blank?
44
+
45
+ obj = {}
46
+ models.each_with_index do |element, i|
47
+ model = element[:model].to_s
48
+ obj[pluralize(model).camelize(:lower)] = model.camelize.constantize.where(models[i].except(:model)).destroy_all
49
+ end
50
+
51
+ render json: obj.to_json, status: :accepted
52
+ end
53
+
54
+ def update
55
+ return_error("You need to provide at least one model name as an argument", 400) && return if params.dig(:models).blank?
56
+
57
+ obj = {}
58
+ models.each_with_index do |element, i|
59
+ model = element[:model].to_s
60
+ update_params = params.permit![:models][i][:update].to_h
61
+ obj[pluralize(model).camelize(:lower)] = model.camelize.constantize.where(models[i].except(:model, :update)).update(update_params)
62
+ end
63
+
64
+ render json: obj.to_json, status: :accepted
65
+ end
66
+
67
+ private
68
+
69
+ def return_error(message, status)
70
+ render json: { "error": { "status": status, "message": message }}.to_json, status: status
71
+ end
72
+
73
+ def traits
74
+ params[:traits].map(&:to_sym)
75
+ end
76
+
77
+ def factory
78
+ params[:factory].to_sym
79
+ end
80
+
81
+ def attributes
82
+ params.permit![:attributes].map(&:to_h)
83
+ end
84
+
85
+ def models
86
+ params.permit![:models].map(&:to_h)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stockpot
4
+ class RedisController < ActionController::Base
5
+ def index
6
+ if params[:field].present?
7
+ # Returns the value associated with :field in the hash stored at :key
8
+ record = REDIS.hget(params[:key], params[:field])
9
+ else
10
+ # Returns the value of :key
11
+ record = REDIS.get(params[:key])
12
+ end
13
+
14
+ render json: record.to_json, status: :ok
15
+ end
16
+
17
+ def create
18
+ if params[:field].present?
19
+ # Sets :field in the hash stored at :key to :value
20
+ REDIS.hset(params[:key], params[:field], params[:value])
21
+ else
22
+ # Sets :key to hold the string :value
23
+ REDIS.set(params[:key], params[:value])
24
+ end
25
+
26
+ render json: { status: 201 }
27
+ end
28
+ end
29
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ Stockpot::Engine.routes.draw do
4
+ get "/records", to: "records#index"
5
+ post "/records", to: "records#create"
6
+ delete "/records", to: "records#destroy"
7
+ put "/records", to: "records#update"
8
+
9
+ delete "/clean_database", to: "database_cleaner#index"
10
+
11
+ get "/redis", to: "redis#index"
12
+ post "/redis", to: "redis#create"
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stockpot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jayson Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-26 00:00:00.000000000 Z
11
+ date: 2019-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -211,12 +211,13 @@ executables: []
211
211
  extensions: []
212
212
  extra_rdoc_files: []
213
213
  files:
214
- - LICENSE.txt
215
214
  - README.md
216
- - lib/stockpot.rb
217
- - lib/stockpot/engine.rb
218
- - lib/stockpot/version.rb
219
- - lib/tasks/stockpot_tasks.rake
215
+ - Rakefile
216
+ - app/controllers/stockpot/application_controller.rb
217
+ - app/controllers/stockpot/database_cleaner_controller.rb
218
+ - app/controllers/stockpot/records_controller.rb
219
+ - app/controllers/stockpot/redis_controller.rb
220
+ - config/routes.rb
220
221
  homepage: https://github.com/Freshly/stockpot
221
222
  licenses:
222
223
  - MIT
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2019 Jayson Smith
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.
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stockpot
4
- class Engine < ::Rails::Engine
5
- isolate_namespace Stockpot
6
- config.generators.api_only = true
7
- end
8
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stockpot
4
- VERSION = "0.1.2"
5
- end
data/lib/stockpot.rb DELETED
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "stockpot/engine"
4
-
5
- module Stockpot
6
- # Your code goes here...
7
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # desc "Explaining what the task does"
4
- # task :stockpot do
5
- # # Task goes here
6
- # end