stockpot 0.3.1 → 0.4.0

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: a2ed98e155357c764f537d390bdf41db905bb5231e33bb7644639c1f309fde33
4
- data.tar.gz: 0cd8008bef691299dc4c2e50343ef9d98002b9857703ca4f9a1cee55f2eed556
3
+ metadata.gz: d3e810f46e006579bd1532c1f2d2fc291110cec1f20fc432b1f073efb7f104f2
4
+ data.tar.gz: 8373caf574a7f15a48ae2139606d85541f91c3ce26b05b7295ef293ddb592974
5
5
  SHA512:
6
- metadata.gz: 00e9c0c1eea751c8f01bdb925592f904a578dea35aa0430278bd93ec7923c564cd7d787499fb8ce77feb07b7f1aad78d67a68884974640b532428ac381e4aa4b
7
- data.tar.gz: e66ffa80fc4138ca3b01e86ab4012043d7909f9735e877d4cc2391f63e34a2b1de066761eb61cea0e19e134f30165584a2d845f8f65e62653479416783407b78
6
+ metadata.gz: 486f9a8a802130c36c0528a9dffb4d7d7dc90686b170058ecc590c04d182a70fabacbebfc73d9f0feb445cbfa55e98c5ea7bb49cc4412748b6c84548a1a71e4d
7
+ data.tar.gz: b18a2ac9341c86fae9d69b91c9e6b008352bc4a5ec989bb14b6d31ed8d8982c01acf28da797f9afd8542667c8b5265891b8d032147343be7f64b82cd86563b82
data/README.md CHANGED
@@ -79,7 +79,7 @@ Query for data. Accepts a array of objects that require at least a model name, b
79
79
 
80
80
  #### POST
81
81
 
82
- Create new data. Accepts an object specifying a single model with additional qualifiers.
82
+ Create new data. Accepts an an array of objects specifying a single model with additional qualifiers.
83
83
 
84
84
  * factory (required) - Specify which factory to create a record with.
85
85
  * list (optional) - Specify a count of items to create, default: 1
@@ -87,7 +87,7 @@ Create new data. Accepts an object specifying a single model with additional qua
87
87
  * attributes (optional) - An array of objects allowing for the specification of data to be used when creating records. Array position matches with list order if multiple records are desired.
88
88
 
89
89
  ```javascript
90
- {
90
+ [{
91
91
  factory: "user",
92
92
  traits: ["having_address"],
93
93
  attributes: [{
@@ -96,7 +96,7 @@ Create new data. Accepts an object specifying a single model with additional qua
96
96
  first_name: "Foo",
97
97
  last_name: "Bar"
98
98
  }]
99
- }
99
+ }]
100
100
  ```
101
101
 
102
102
  #### DELETE
@@ -129,7 +129,7 @@ Clears Rails & Redis caches and truncates Active Records databases. No body requ
129
129
  ### `/stockpot/redis`
130
130
 
131
131
  #### GET
132
-
132
+
133
133
  Query for data. Accepts key or field to use to search cache for record.
134
134
 
135
135
  ```javascript
@@ -139,6 +139,10 @@ Query for data. Accepts key or field to use to search cache for record.
139
139
  }
140
140
  ```
141
141
 
142
+ #### GET - Keys
143
+
144
+ Query for all keys within data. No body or argument required.
145
+
142
146
  #### POST - Create new data
143
147
 
144
148
  Accepts an object specifying a key, field, and value to be inserted into Redis cache.
@@ -168,7 +172,7 @@ Cypress.Commands.add("getRecords", args => {
168
172
  ```
169
173
 
170
174
  Our tests can then call this command like this
171
-
175
+
172
176
  ```javascript
173
177
  cy.getRecords([{ model: "user", id: user.id }])
174
178
  .then(res => {
@@ -8,18 +8,18 @@ module Stockpot
8
8
 
9
9
  case error
10
10
  when NameError
11
- return_error(error.to_s, error.backtrace.first(5), :bad_request)
11
+ return_error(error.message, error.backtrace.first(5), :bad_request)
12
12
  when PG::Error
13
- return_error("Postgres error: #{error}", error.backtrace.first(5), :server_error)
14
- when ActiveRecord::RecordInvalid, ActiveRecord::Validations
15
- return_error("ActiveRecord error: #{error}", error.backtrace.first(5), :server_error)
13
+ return_error("Postgres error: #{error.message}", error.backtrace.first(5), :internal_server_error)
14
+ when ActiveRecord::RecordInvalid, ActiveRecord::Validations, ActiveRecord::RecordNotDestroyed
15
+ return_error("In #{error.record.class} class, #{error.message}", error.backtrace.first(5), :expectation_failed)
16
16
  else
17
- return_error(error.to_s, error.backtrace.first(5),:server_error)
17
+ return_error(error.message, error.backtrace.first(5),:internal_server_error)
18
18
  end
19
19
  end
20
20
 
21
21
  def return_error(message, backtrace, status)
22
- { json: { error: { status: status, backtrace: backtrace, message: message }}, status: status }
22
+ render json: { error: { status: status, backtrace: backtrace, message: message }}, status: status
23
23
  end
24
24
  end
25
25
  end
@@ -5,8 +5,7 @@ module Stockpot
5
5
  include Helper::Errors
6
6
 
7
7
  rescue_from StandardError do |exception|
8
- render rescue_error(exception)
8
+ rescue_error(exception)
9
9
  end
10
- # protect_from_forgery with: :exception
11
10
  end
12
11
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  require "factory_bot_rails"
3
+ require "active_record/persistence"
3
4
 
4
5
  module Stockpot
5
6
  class RecordsController < MainController
@@ -10,58 +11,84 @@ module Stockpot
10
11
  return_error("You need to provide at least one model name as an argument", 400) if params.dig(:models).blank?
11
12
  end
12
13
  before_action only: %i[create] do
13
- return_error("You need to provide at least one factory name as an argument", 400) if params.dig(:factory).blank?
14
+ return_error("You need to provide at least one factory name as an argument", 400) if params.dig(:factories).blank?
15
+ end
16
+ before_action do
17
+ @response_data = {}
14
18
  end
15
19
 
16
20
  def index
17
- obj = {}
18
21
  models.each_with_index do |element, i|
19
- model = element[:model].to_s
22
+ model = element[:model]
20
23
  class_name = find_correct_class_name(model)
24
+ formatted_model = pluralize(model).camelize(:lower).gsub("::", "")
21
25
 
22
- obj[pluralize(model).camelize(:lower)] = class_name.constantize.where(models[i].except(:model))
26
+ @response_data[formatted_model] = [] unless @response_data.key?(formatted_model)
27
+ @response_data[formatted_model].concat(class_name.constantize.where(models[i].except(:model)))
28
+ @response_data[formatted_model].reverse!.uniq! { |obj| obj["id"] }
23
29
  end
24
-
25
- render json: obj, status: :ok
30
+ render json: @response_data, status: :ok
26
31
  end
27
32
 
28
33
  def create
29
- list = params[:list] || 1
30
34
  ActiveRecord::Base.transaction do
31
- list.times do |n|
32
- all_parameters = [ factory, *traits, attributes(n) ].compact
33
- @factory = FactoryBot.create(*all_parameters)
35
+ factories.each do |element|
36
+ ids = []
37
+ list = element[:list] || 1
38
+ factory = element[:factory]
39
+ traits = element[:traits].map(&:to_sym) if element[:traits].present?
40
+ attributes = element[:attributes].to_h if element[:attributes].present?
41
+
42
+ list.times do
43
+ factory_arguments = [ factory, *traits, attributes ].compact
44
+ @factory_instance = FactoryBot.create(*factory_arguments)
45
+ ids << @factory_instance.id
46
+ end
47
+
48
+ factory_name = pluralize(factory).camelize(:lower)
49
+
50
+ @response_data[factory_name] = [] unless @response_data.key?(factory_name)
51
+ @response_data[factory_name].concat(@factory_instance.class.name.constantize.where(id: ids))
34
52
  end
35
53
  end
36
- obj = @factory.class.name.constantize.last(list)
37
-
38
- render json: obj, status: :created
54
+ render json: @response_data, status: :accepted
39
55
  end
40
56
 
41
57
  def destroy
42
- obj = {}
43
58
  ActiveRecord::Base.transaction do
44
59
  models.each_with_index do |element, i|
45
- model = element[:model].to_s
60
+ model = element[:model]
46
61
  class_name = find_correct_class_name(model)
47
- obj[pluralize(model).camelize(:lower)] = class_name.constantize.where(models[i].except(:model)).destroy_all
62
+ formatted_model = pluralize(model).camelize(:lower).gsub("::", "")
63
+
64
+ class_name.constantize.where(models[i].except(:model)).each do |record|
65
+ record.destroy!
66
+ @response_data[formatted_model] = [] unless @response_data.key?(formatted_model)
67
+ @response_data[formatted_model] << record
68
+ end
48
69
  end
49
70
  end
50
-
51
- render json: obj, status: :accepted
71
+ render json: @response_data, status: :accepted
52
72
  end
53
73
 
54
74
  def update
55
- obj = {}
56
75
  ActiveRecord::Base.transaction do
57
76
  models.each_with_index do |element, i|
58
- model = element[:model].to_s
77
+ model = element[:model]
59
78
  class_name = find_correct_class_name(model)
60
79
  update_params = params.permit![:models][i][:update].to_h
61
- obj[pluralize(model).camelize(:lower)] = class_name.constantize.where(models[i].except(:model, :update)).update(update_params)
80
+ attributes_to_search = models[i].except(:model, :update)
81
+ formatted_model = pluralize(model).camelize(:lower).gsub("::", "")
82
+
83
+ class_name.constantize.where(attributes_to_search).each do |record|
84
+ record.update!(update_params)
85
+ @response_data[formatted_model] = [] unless @response_data.key?(formatted_model)
86
+ @response_data[formatted_model] << class_name.constantize.find(record.id)
87
+ @response_data[formatted_model].reverse!.uniq! { |obj| obj["id"] }
88
+ end
62
89
  end
63
90
  end
64
- render json: obj, status: :accepted
91
+ render json: @response_data, status: :accepted
65
92
  end
66
93
 
67
94
  private
@@ -70,29 +97,19 @@ module Stockpot
70
97
  # We are getting the class name from the factory or we default to whatever we send in.
71
98
  # Something to keep in mind "module/class_name".camelize will translate into "Module::ClassName"
72
99
  # which is perfect for namespaces in case there is no factory associated with a specific model
73
- FactoryBot.factories.registered?(model) ? FactoryBot.build_stubbed(model).class.name : model.camelize
74
- end
75
-
76
- def traits
77
- return if params[:traits].blank?
78
-
79
- params[:traits].map(&:to_sym)
80
- end
81
-
82
- def factory
83
- params[:factory].to_sym
84
- end
85
-
86
- # rubocop:disable Naming/UncommunicativeMethodParamName
87
- def attributes(n)
88
- # rubocop:enable Naming/UncommunicativeMethodParamName
89
- return if params[:attributes].blank?
90
-
91
- params.permit![:attributes][n].to_h
100
+ if FactoryBot.factories.registered?(model)
101
+ FactoryBot.factories.find(model).build_class.to_s
102
+ else
103
+ model.camelize
104
+ end
92
105
  end
93
106
 
94
107
  def models
95
108
  params.permit![:models].map(&:to_h)
96
109
  end
110
+
111
+ def factories
112
+ params.permit![:factories].map(&:to_h)
113
+ end
97
114
  end
98
115
  end
@@ -24,5 +24,11 @@ module Stockpot
24
24
 
25
25
  render json: { status: 201 }
26
26
  end
27
+
28
+ def keys
29
+ record = REDIS.keys
30
+
31
+ render json: record.to_json, status: :ok
32
+ end
27
33
  end
28
34
  end
@@ -8,8 +8,9 @@ Stockpot::Engine.routes.draw do
8
8
 
9
9
  delete "/clean_database", to: "database_cleaner#index"
10
10
 
11
- get "/redis", to: "redis#index"
12
11
  post "/redis", to: "redis#create"
12
+ get "/redis", to: "redis#index"
13
+ get "/redis/keys", to: "redis#keys"
13
14
 
14
15
  get "/healthz", to: "healthz#index"
15
16
  end
@@ -4,11 +4,5 @@ module Stockpot
4
4
  class Engine < ::Rails::Engine
5
5
  isolate_namespace Stockpot
6
6
  config.generators.api_only = true
7
-
8
- config.generators do |generators|
9
- generators.test_framework :rspec
10
- generators.fixture_replacement :factory_bot
11
- generators.factory_bot dir: 'spec/factories'
12
- end
13
7
  end
14
8
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stockpot
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  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.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jayson Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-06 00:00:00.000000000 Z
11
+ date: 2020-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails