jsonapi_exception 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e75f978fc1a34dd12d6dac2c3f6046986c9013a
4
+ data.tar.gz: 51570bdb8232a89a73532c440b117144123b5a55
5
+ SHA512:
6
+ metadata.gz: a609307688946e0ae639c44f244372d03d5490034b78029900e989f9536a89165964655e6f9f624ef3311f18f938c956de3fcf8eaa6e2dc3eb487e379db2fa90
7
+ data.tar.gz: fe4ad4f0bcb310ec5244cdc95fc455fc232849f24e54c0e7b801ba5fea116c2afcb8d5b75c523ce1933384e723594ec0feebd98e80420cfad583d2d924b4ac7e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,71 @@
1
+ # Built-in config:
2
+ # https://github.com/bbatsov/rubocop/blob/master/config/default.yml
3
+
4
+ # We do not typically use/need class documentation
5
+ Documentation:
6
+ Enabled: false
7
+
8
+ Metrics/LineLength:
9
+ Max: 120
10
+
11
+ Metrics/MethodLength:
12
+ Max: 15
13
+
14
+ Metrics/ModuleLength:
15
+ Exclude:
16
+ - 'spec/**/*_spec.rb'
17
+
18
+ Style/RaiseArgs:
19
+ Enabled: false
20
+
21
+ Metrics/ParameterLists:
22
+ Max: 6
23
+
24
+ Style/SignalException:
25
+ EnforcedStyle: semantic
26
+
27
+ Style/MultilineArrayBraceLayout:
28
+ Enabled: false
29
+
30
+ Style/MultilineHashBraceLayout:
31
+ Enabled: false
32
+
33
+ Style/MultilineMethodCallBraceLayout:
34
+ Enabled: false
35
+
36
+ Style/StringLiterals:
37
+ EnforcedStyle: double_quotes
38
+
39
+ Style/StringLiteralsInInterpolation:
40
+ EnforcedStyle: double_quotes
41
+
42
+ Style/Semicolon:
43
+ Enabled: false
44
+
45
+ Style/DoubleNegation:
46
+ Enabled: false
47
+
48
+ # Run rails specific checks
49
+ # https://github.com/bbatsov/rubocop#rails
50
+ Rails:
51
+ Enabled: true
52
+
53
+ AllCops:
54
+ TargetRubyVersion: 2.2
55
+ Include:
56
+ # Include important files that don't end in .rb
57
+ - '**/Rakefile'
58
+ - '**/config.ru'
59
+ Exclude:
60
+ # exclude files in /vendor because some CI systems put gems in here e.g.
61
+ # TravisCI
62
+ - 'vendor/**/*'
63
+ # exclude the autogenerated schema file as we don't have any control over
64
+ # its format
65
+ - 'db/schema.rb'
66
+ - 'bin/**/*'
67
+ - 'doc/**/*'
68
+ - 'db/migrate/**/*'
69
+ - 'db/seeds.rb'
70
+ - 'features/**/*.rb'
71
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jsonapi_exception.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 James Harton
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,48 @@
1
+ # JsonapiException
2
+
3
+ We're constantly having to rescue exceptions and format them as JSONAPI, so here's an object that does it for you. :sparkles:
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'jsonapi_exception'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jsonapi_exception
20
+
21
+ ## Usage
22
+
23
+ In your Rails controller you can do the following:
24
+
25
+ ```ruby
26
+ class WidgetController < ApplicationController::Base
27
+
28
+ rescue_from WidgetService::InsufficientWidgets do |exception|
29
+ render JsonapiException.new(exception, status: 416).for_render
30
+ end
31
+
32
+ end
33
+ ```
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rabid/jsonapi_exception.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
48
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jsonapi_exception"
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
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
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jsonapi_exception/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jsonapi_exception"
8
+ spec.version = JsonapiException::VERSION
9
+ spec.authors = ["James Harton"]
10
+ spec.email = ["james@rabidtech.co.nz"]
11
+
12
+ spec.summary = "Simple wrapper to turn exceptions into JSONAPI compatible hashes"
13
+ spec.description = "We keep needing this, so we made it."
14
+ spec.homepage = "https://github.com/rabid/jsonapi_exceptions"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "rubocop", "~> 0.42"
26
+ spec.add_development_dependency "faker"
27
+ end
@@ -0,0 +1,3 @@
1
+ class JsonapiException
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,88 @@
1
+ require "jsonapi_exception/version"
2
+
3
+ class JsonapiException
4
+ DEFAULT_STATUS = 422
5
+ MIME_TYPE = "application/vnd.api+json".freeze
6
+
7
+ def initialize(exception, opts = {})
8
+ @exception = exception
9
+ @opts = opts
10
+ end
11
+
12
+ def title
13
+ opts.fetch(:title)
14
+ rescue KeyError
15
+ exception
16
+ .class
17
+ .to_s
18
+ .split("::")
19
+ .last
20
+ .scan(/([A-Z][^A-Z]*)/)
21
+ .join(" ")
22
+ .sub(/ (Error|Exception)$/, "")
23
+ end
24
+
25
+ def detail
26
+ opts.fetch(:detail, exception.message)
27
+ end
28
+
29
+ def status
30
+ opts.fetch(:status, DEFAULT_STATUS)
31
+ end
32
+
33
+ def id
34
+ opts.fetch(:id, exception.object_id)
35
+ end
36
+
37
+ def code
38
+ opts[:code]
39
+ end
40
+
41
+ def links
42
+ opts[:links]
43
+ end
44
+
45
+ def meta
46
+ return opts[:meta] if opts[:meta]
47
+ return unless show_exceptions?
48
+ {
49
+ class: exception.class.to_s,
50
+ message: exception.message,
51
+ backtrace: exception.backtrace
52
+ }
53
+ end
54
+
55
+ def as_json(*_)
56
+ {
57
+ errors: [to_h]
58
+ }
59
+ end
60
+
61
+ def to_h
62
+ {
63
+ title: title,
64
+ links: links,
65
+ detail: detail,
66
+ status: status.to_s,
67
+ id: id,
68
+ code: code
69
+ }.reject { |_, v| !v }
70
+ end
71
+
72
+ def for_render
73
+ {
74
+ json: as_json,
75
+ status: status,
76
+ content_type: MIME_TYPE
77
+ }
78
+ end
79
+
80
+ private
81
+
82
+ attr_reader :exception, :opts
83
+
84
+ def show_exceptions?
85
+ return false unless defined?(Rails)
86
+ Rails.application.config.action_dispatch.show_exceptions
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi_exception
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Harton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.42'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.42'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faker
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: We keep needing this, so we made it.
84
+ email:
85
+ - james@rabidtech.co.nz
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - jsonapi_exception.gemspec
100
+ - lib/jsonapi_exception.rb
101
+ - lib/jsonapi_exception/version.rb
102
+ homepage: https://github.com/rabid/jsonapi_exceptions
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.5.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Simple wrapper to turn exceptions into JSONAPI compatible hashes
126
+ test_files: []