jsonapi-utils 0.2.2 → 0.3.0

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: 7a230fe7cfdad1a0c6df415f03789bdbadcfcde3
4
- data.tar.gz: 0822570d229169faa5803647b2c26bca0d51ef3a
3
+ metadata.gz: 4b973c25edc256609e9a73af0eb75610ccbdc73c
4
+ data.tar.gz: 2041dfe51b129f936b4bceae828c536f56b60884
5
5
  SHA512:
6
- metadata.gz: dcb7c2e0a664d9a4df465d137ac331157e9780680b44208f8e132dc6cb67924a61ce773dcefa9a151f7c507d2a797851b4f5424e4eb0174dc7023a2090fc4f66
7
- data.tar.gz: 7a8590dfdc101cd752a742078d7fb0c32451a158dbf52c4e722b33147d336315d73c314f28e50599bbec5a83ae0a25c18b5beda4242ed0bcc580753d412c4d00
6
+ metadata.gz: ee52a5cc6aef9f83190b72ce202fca841dc0f394923f32f371a4703a9dca53b5a5067f6910ed5dba5d0ca30a6df057c0711f5ca8b9660ea692efd36c2d476c4a
7
+ data.tar.gz: d32f1cc5923ab3c916040089cdee77c7270b6d1c7c8916c9e90739540f67434880cdb49487679848a7d428f0070d241e309f4728444264f6f822cb353207c281
data/.gitignore CHANGED
@@ -5,6 +5,8 @@
5
5
  /coverage/
6
6
  /doc/
7
7
  /pkg/
8
+ /spec/log/
9
+ /spec/test_db
8
10
  /spec/reports/
9
11
  /tmp/
10
12
  *.bundle
data/lib/jsonapi/utils.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'jsonapi/utils/version'
2
+ require 'jsonapi/utils/exceptions'
2
3
 
3
4
  module JSONAPI
4
5
  module Utils
@@ -10,60 +11,54 @@ module JSONAPI
10
11
 
11
12
  def jsonapi_render(options)
12
13
  if options.has_key?(:json)
13
- response = build_response(options)
14
- render json: response[:body], status: options[:status] || response[:status] || :ok
14
+ response = jsonapi_serialize(options[:json], options[:options] || {})
15
+ render json: response, status: options[:status] || :ok
15
16
  end
16
17
  end
17
18
 
19
+ def jsonapi_errors(exception)
20
+ JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors).as_json
21
+ end
22
+
23
+ def jsonapi_render_internal_server_error
24
+ errors = ::JSONAPI::Utils::Exceptions::InternalServerError.new
25
+ render json: jsonapi_errors(errors), status: 500
26
+ end
27
+
28
+ def jsonapi_render_bad_request
29
+ errors = ::JSONAPI::Utils::Exceptions::BadRequest.new
30
+ render json: jsonapi_errors(errors), status: 400
31
+ end
32
+
18
33
  def jsonapi_render_not_found
19
- jsonapi_render json: nil
34
+ id = extract_ids(@request.params)
35
+ exception = JSONAPI::Exceptions::RecordNotFound.new(id)
36
+ render json: jsonapi_errors(exception), status: 404
20
37
  end
21
38
 
22
39
  def jsonapi_render_not_found_with_null
23
- jsonapi_render json: nil, options: { allow_null: true }
40
+ render json: { data: nil }, status: 200
24
41
  end
25
42
 
26
43
  def jsonapi_serialize(records, options = {})
27
- return build_nil if records.nil? && options[:allow_null]
28
44
  results = JSONAPI::OperationResults.new
29
45
 
30
- if records.nil?
31
- id = extract_ids(@request.params)
32
- record_not_found = JSONAPI::Exceptions::RecordNotFound.new(id)
33
- results.add_result(JSONAPI::ErrorsOperationResult.new(record_not_found.errors[0].code, record_not_found.errors))
46
+ fix_request_options(params, records)
47
+
48
+ if records.respond_to?(:to_ary)
49
+ records = fix_when_hash(records, options) if needs_to_be_fixed?(records)
50
+ @resources = build_collection(records, options)
51
+ results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, result_options(options)))
34
52
  else
35
- fix_request_options(params, records)
36
-
37
- if records.respond_to?(:to_ary)
38
- records = fix_when_hash(records, options) if needs_to_be_fixed?(records)
39
- @resources = build_collection(records, options)
40
- results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, result_options(options)))
41
- else
42
- @resource = turn_into_resource(records, options)
43
- results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource))
44
- end
53
+ @resource = turn_into_resource(records, options)
54
+ results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource))
45
55
  end
46
56
 
47
57
  create_response_document(results).contents
48
58
  end
49
59
 
50
- def jsonapi_error(exception)
51
- JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors).as_json
52
- end
53
-
54
60
  private
55
61
 
56
- def build_response(options)
57
- {
58
- body: jsonapi_serialize(options[:json], options[:options] || {}),
59
- status: options[:json].nil? && !options[:allow_null] ? :not_found : :ok
60
- }
61
- end
62
-
63
- def build_nil
64
- { data: nil }
65
- end
66
-
67
62
  def extract_ids(hash)
68
63
  ids = hash.keys.select { |e| e =~ /id$/i }.map { |e| hash[e] }
69
64
  ids.first rescue '(id not identified)'
@@ -0,0 +1,40 @@
1
+ require 'jsonapi/utils/version'
2
+
3
+ module JSONAPI
4
+ module Utils
5
+ module Exceptions
6
+ class BadRequest < ::JSONAPI::Exceptions::Error
7
+ def code; 400 end
8
+
9
+ def errors
10
+ [JSONAPI::Error.new(code: 400,
11
+ status: :bad_request,
12
+ title: 'Bad Request',
13
+ detail: 'This request is not supported.')]
14
+ end
15
+ end
16
+
17
+ class NotFoundError < ::JSONAPI::Exceptions::Error
18
+ def code; 404 end
19
+
20
+ def errors
21
+ [JSONAPI::Error.new(code: 404,
22
+ status: :not_found,
23
+ title: 'Not Found',
24
+ detail: 'The requested resource was not found.')]
25
+ end
26
+ end
27
+
28
+ class InternalServerError < ::JSONAPI::Exceptions::Error
29
+ def code; 500 end
30
+
31
+ def errors
32
+ [JSONAPI::Error.new(code: 500,
33
+ status: :internal_server_error,
34
+ title: 'Internal Server Error',
35
+ detail: 'An internal error ocurred while processing the request.')]
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Utils
3
- VERSION = '0.2.2'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Guedes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-10-02 00:00:00.000000000 Z
12
+ date: 2015-10-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -73,6 +73,7 @@ files:
73
73
  - bin/setup
74
74
  - jsonapi-utils.gemspec
75
75
  - lib/jsonapi/utils.rb
76
+ - lib/jsonapi/utils/exceptions.rb
76
77
  - lib/jsonapi/utils/version.rb
77
78
  homepage: https://github.com/b2beauty/jsonapi-utils
78
79
  licenses: