napa_rails 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fda91e4a7d1a915d873bb6fe531c4b230eb2b92
4
- data.tar.gz: fafc16def4dca5a2e41ca5c591748d8e141bd3b3
3
+ metadata.gz: 960c38e805018236509ecb3b466e50c9e094d8a4
4
+ data.tar.gz: 57f36adcac7d40c73e009ee81c5a69b0df61b519
5
5
  SHA512:
6
- metadata.gz: 82f28e3bc77ad004bdfdfc72009f1f1b940abb697cfca2b40680e3506b4fc2999d377a0b1b008352d8fab5ef2c9c781a278bc11f0f8e3fe4ff859c113459e227
7
- data.tar.gz: ded28d65ff3438d38ecc48398bd8302e5c87392245f008d3612bcb355def4acb30842d55fc3a8530334073702e3021a95bdc3184920717a7ee78e6e70c29172d
6
+ metadata.gz: 4ddb83d14fa62c93ae37f13ae58faa6e088d3291a337fd9ac8454ff03b156fb1540eb2eeb3ae27779a75fba195fa17842d1dc6da65e87d306fc27a4b78cacbe5
7
+ data.tar.gz: 651adacb65d252a03d8e32e7292f4e524753e5631705d41bd15a96fb8c22a670b408595a97476555e668386df7fa3c9a2e9988750553643da2446e3af71f2d57
@@ -16,3 +16,7 @@ master
16
16
  0.1.2
17
17
  ===
18
18
  * Require napa in napa_rails file
19
+
20
+ 0.1.3
21
+ ===
22
+ * Require napa/grape_extensions/grape_extenders
@@ -8,6 +8,7 @@ require "napa/output_formatters/representer"
8
8
  require "napa/output_formatters/pagination"
9
9
  require "napa/grape_extensions/grape_helpers"
10
10
  require "napa/grape_extensions/error_formatter"
11
+ require "napa/grape_extensions/grape_extenders"
11
12
  require "napa/json_error"
12
13
 
13
14
  module Napa
@@ -0,0 +1,32 @@
1
+ if defined?(Rails)
2
+ module Napa
3
+ module GrapeExtenders
4
+ def self.extended(modified_class)
5
+ # when extended, set the exceptions to handle
6
+
7
+ # if AR is being used, rescue from common AR errors
8
+ if defined?(::ActiveRecord)
9
+ modified_class.rescue_from ::ActiveRecord::RecordNotFound do
10
+ err = Napa::JsonError.new(:record_not_found, 'record not found')
11
+ Rails.logger.debug({ status: 404, headers: {}, response: err.try(:first) }.map { |k, v| "#{k}=#{v}" }.join(' '))
12
+ rack_response(err.to_json, 404)
13
+ end
14
+ modified_class.rescue_from ::ActiveRecord::RecordInvalid do |e|
15
+ err = Napa::JsonError.new(:unprocessable_entity, e.message, e.record.errors.messages)
16
+ Rails.logger.debug({ status: 422, headers: {}, response: err.try(:first) }.map { |k, v| "#{k}=#{v}" }.join(' '))
17
+ rack_response(err.to_json, 422)
18
+ end
19
+ end
20
+
21
+ # if AASM is being used, rescue from invalid transitions
22
+ if defined?(::AASM)
23
+ modified_class.rescue_from ::AASM::InvalidTransition do |e|
24
+ err = Napa::JsonError.new(:unprocessable_entity, e.message)
25
+ Rails.logger.debug({ status: 422, headers: {}, response: err.try(:first) }.map { |k, v| "#{k}=#{v}" }.join(' '))
26
+ rack_response(err.to_json, 422)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module Napa
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: napa_rails
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
  - Darby Frey, Flori Garcia
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-13 00:00:00.000000000 Z
11
+ date: 2016-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -120,6 +120,7 @@ files:
120
120
  - bin/setup
121
121
  - lib/napa.rb
122
122
  - lib/napa/grape_extensions/error_formatter.rb
123
+ - lib/napa/grape_extensions/grape_extenders.rb
123
124
  - lib/napa/grape_extensions/grape_helpers.rb
124
125
  - lib/napa/json_error.rb
125
126
  - lib/napa/output_formatters/include_nil.rb