json_api_errors 0.1.4 → 0.1.5

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: 4efbecc44a6f1b6d441e174b89002870d943f0c6
4
- data.tar.gz: b26b6d1af206276ce292be1b45032833e72545a1
3
+ metadata.gz: 85d06f167f7623264a1e0da8cf01dccba411f60a
4
+ data.tar.gz: 5c00220d6f9251084aef53416805de62f2b0a718
5
5
  SHA512:
6
- metadata.gz: 0f659defbe116065118a2de7941d2bff3764945388fc4a1faeda85d1ef5aac1db3a0e9716b55555e04f5027efc54ffb8d7bbfd476da00bf9ff100e636255ede8
7
- data.tar.gz: a504608277be45cbf8b6ed5df8adc5303dddf98c2d568f3717c4a73db09cee0388d66daf9c9b172e712f586824e38d52b368e7f1e3f8a9b56f78a9066e5517e9
6
+ metadata.gz: fbb9c009e3ca161a76249452e1d20994f9b08a22aeef1b414ed5516771d3c12a46775aeab98dfb3143c9fded10a7f72f07ff90bed55d608f8a9a00709d9d22dd
7
+ data.tar.gz: a96ac527f00904eb7e8f41554d2b646b0ba683bb78eb539a0f4f08add6ca1d6df3b83d019bccefb0866c39110b03e5e905073fd8b365b63d334d5659a3f59c04
data/README.md CHANGED
@@ -76,7 +76,7 @@ So you want an error object? Well, all you need to do is this:
76
76
  @title=#<JsonApiErrors::Default::Title:0x007ff753941568>>
77
77
  ```
78
78
 
79
- A `JsonApiError::Error` implements `#call`. So to get a hash representation
79
+ A `JsonApiErrors::Error` implements `#call`. So to get a hash representation
80
80
  of your error, which can be serialized into JSON, you need to send the `#call`
81
81
  message to your error object:
82
82
 
@@ -220,7 +220,7 @@ override either the `#to_s` or `#to_h`. (This project was built using Ruby 2.2.2
220
220
 
221
221
  The [Json API spec](http://jsonapi.org/format/#error-objects) mentions that all
222
222
  errors MUST have the keyword `errors` at the root. For this purpose there is
223
- `JsonApiError::ErrorCollection`. To get a fully qualified hash representation
223
+ `JsonApiErrors::ErrorCollection`. To get a fully qualified hash representation
224
224
  of your error you can:
225
225
 
226
226
  ```ruby
Binary file
@@ -1,12 +1,6 @@
1
- require "json_api_errors/default/id"
2
- require "json_api_errors/default/code"
3
- require "json_api_errors/default/status"
4
- require "json_api_errors/default/title"
5
- require "json_api_errors/default/detail"
6
- require "json_api_errors/default/links"
7
- require "json_api_errors/default/source"
8
- require "json_api_errors/default/meta"
9
- require "json_api_errors/default/error"
1
+ require "json_api_errors/templates"
2
+ require "json_api_errors/templates/default"
3
+ require "forwardable"
10
4
 
11
5
  # JsonApiErrors::Error
12
6
  # ====================
@@ -22,109 +16,33 @@ require "json_api_errors/default/error"
22
16
  #
23
17
  # There are two parts to configuring an error object:
24
18
  #
25
- # First, you'll need to create a template.
26
- #
27
- # Second, you'll need to configure the attributes you want to be displayed
28
- # in the template.
29
- #
30
- # The easiest way to do this is by creating a lambda whose body is a hash
31
- # representing the template you wish to be displayed. Then pass a block to the
32
- # initializer:
33
- #
34
- # template = ->(error) do
35
- # {
36
- # status: error.status,
37
- # code: error.code
38
- # }
39
- # end
40
- #
41
- # error = JsonApiErrors::Error.new do |config|
42
- # config.error = template
43
- # config.status = "400"
44
- # config.code = "Bad Request"
45
- # end
46
- #
47
- # error.call => { status: "400", code: "Bad Request" }
48
- #
49
- #
50
- # You can also pass in literal values for the attributes for the attributes
51
- # as well as inject them into the initializer:
52
- #
53
- # JsonApiErrors::Error.new( error: CustomError.new,
54
- # status: CustomStatus.new,
55
- # code: CustomCode.new,
56
- # links: CustomLinks.new )
57
- #
58
- #
59
- # error.call => { status: "400",
60
- # code: "Bad Request",
61
- # links: {
62
- # about: "www.example.org"
63
- # }
64
- # }
65
- #
66
- # Here's how you would define those classes:
67
- #
68
- # class CustomError
69
- # def call(error)
70
- # {
71
- # status: error.status.to_s,
72
- # code: error.code.to_s,
73
- # links: error.links.to_h
74
- # }
75
- # end
76
- # end
77
- #
78
- # class CustomStatus
79
- # def to_s; "400"; end
80
- # end
81
- #
82
- # class CustomCode
83
- # def to_s; "Bad Request"; end
84
- # end
85
- #
86
- # class CustomLinks
87
- # def to_h; { about: "www.example.com" }; end
88
- # end
89
- #
90
- # Note:
91
- # =====
92
- # The error class must implement #call and it receives an error object as an argument.
93
- # Classes that define id, status, code, title, detail must implement #to_s.
94
- # Classes that define links, source, and meta must implement #to_h.
95
- #
19
+ # TODO: I've overhauled the mechanism for creating error objects. I've opted to
20
+ # inject error templates into the error object. The initializer no longer takes
21
+ # an implicit block. I need to document this change.
22
+
96
23
 
97
24
  module JsonApiErrors
98
25
  class Error
99
26
 
100
- attr_accessor :error, :id, :status, :links, :code, :title, :detail,
101
- :source, :meta
27
+ extend Forwardable
102
28
 
103
- def initialize( error: JsonApiErrors::Default::Error.new,
104
- id: JsonApiErrors::Default::Id.new,
105
- status: JsonApiErrors::Default::Status.new,
106
- code: JsonApiErrors::Default::Code.new,
107
- links: JsonApiErrors::Default::Links.new,
108
- title: JsonApiErrors::Default::Title.new,
109
- detail: JsonApiErrors::Default::Detail.new,
110
- source: JsonApiErrors::Default::Source.new,
111
- meta: JsonApiErrors::Default::Meta.new )
29
+ attr_accessor :template
112
30
 
113
- @error = error
114
- @id = id
115
- @status = status
116
- @code = code
117
- @links = links
118
- @title = title
119
- @detail = detail
120
- @source = source
121
- @meta = meta
122
-
123
- yield(self) if block_given?
31
+ def initialize(template: JsonApiErrors::Templates::Default.new)
32
+ @template = template
124
33
  end
125
34
 
35
+ def_delegators :template, :id,
36
+ :status,
37
+ :code,
38
+ :links,
39
+ :title,
40
+ :detail,
41
+ :source,
42
+ :meta
43
+
126
44
  def call
127
- error.call(self)
45
+ template.call
128
46
  end
129
47
 
130
48
  def status_code
@@ -0,0 +1,57 @@
1
+ module JsonApiErrors
2
+ module Templates
3
+ class Default
4
+ def call
5
+ {
6
+ id: id,
7
+ status: status,
8
+ code: code,
9
+ links: links,
10
+ title: title,
11
+ detail: detail,
12
+ source: source,
13
+ meta: meta
14
+ }
15
+ end
16
+
17
+ def id
18
+ "default-id"
19
+ end
20
+
21
+ def status
22
+ "default_status"
23
+ end
24
+
25
+ def code
26
+ "default-code"
27
+ end
28
+
29
+ def links
30
+ {
31
+ about: "default-links"
32
+ }
33
+ end
34
+
35
+ def title
36
+ "default-title"
37
+ end
38
+
39
+ def detail
40
+ "default_detail"
41
+ end
42
+
43
+ def source
44
+ {
45
+ pointer: "default-pointer",
46
+ parameter: "default-parameter"
47
+ }
48
+ end
49
+
50
+ def meta
51
+ {
52
+ meta: "default-meta"
53
+ }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,4 @@
1
+ module JsonApiErrors
2
+ module Templates
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module JsonApiErrors
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_errors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - byelipk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-20 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,21 +70,13 @@ files:
70
70
  - Rakefile
71
71
  - bin/console
72
72
  - bin/setup
73
- - json_api_errors-0.1.2.gem
74
- - json_api_errors-0.1.3.gem
73
+ - json_api_errors-0.1.4.gem
75
74
  - json_api_errors.gemspec
76
75
  - lib/json_api_errors.rb
77
- - lib/json_api_errors/default/code.rb
78
- - lib/json_api_errors/default/detail.rb
79
- - lib/json_api_errors/default/error.rb
80
- - lib/json_api_errors/default/id.rb
81
- - lib/json_api_errors/default/links.rb
82
- - lib/json_api_errors/default/meta.rb
83
- - lib/json_api_errors/default/source.rb
84
- - lib/json_api_errors/default/status.rb
85
- - lib/json_api_errors/default/title.rb
86
76
  - lib/json_api_errors/error.rb
87
77
  - lib/json_api_errors/error_collection.rb
78
+ - lib/json_api_errors/templates.rb
79
+ - lib/json_api_errors/templates/default.rb
88
80
  - lib/json_api_errors/version.rb
89
81
  homepage: https://github.com/byelipk/json_api_errors
90
82
  licenses:
Binary file
Binary file
@@ -1,21 +0,0 @@
1
- # DefaultCode
2
- # ===========
3
- #
4
- # This is the default error code. To override this you can create your own
5
- # class that implements #to_s and inject it into the constructor
6
- # like this:
7
- #
8
- # JsonAPI::Error.new(status: MySpecialErrorCodeClass.new)
9
- #
10
- # From the api docs:
11
- #
12
- # an application-specific error code, expressed as a string value.
13
- module JsonApiErrors
14
- module Default
15
- class Code
16
- def to_s
17
- "default-code"
18
- end
19
- end
20
- end
21
- end
@@ -1,21 +0,0 @@
1
- # DefaultDetail
2
- # =============
3
- #
4
- # This is the default error detail. To override this you can create your own
5
- # class that implements #to_s and inject it into the constructor
6
- # like this:
7
- #
8
- # JsonAPI::Error.new(detail: MySpecialErrorDetailClass.new)
9
- #
10
- # From the api docs:
11
- #
12
- # a human-readable explanation specific to this occurrence of the problem.
13
- module JsonApiErrors
14
- module Default
15
- class Detail
16
- def to_s
17
- "default-detail"
18
- end
19
- end
20
- end
21
- end
@@ -1,18 +0,0 @@
1
- module JsonApiErrors
2
- module Default
3
- class Error
4
- def call(error)
5
- {
6
- id: error.id.to_s,
7
- links: error.links.to_h,
8
- status: error.status.to_s,
9
- code: error.code.to_s,
10
- title: error.title.to_s,
11
- detail: error.detail.to_s,
12
- source: error.source.to_h,
13
- meta: error.meta.to_h
14
- }
15
- end
16
- end
17
- end
18
- end
@@ -1,21 +0,0 @@
1
- # DefaultId
2
- # =========
3
- #
4
- # This is the default error id. To override this you can create your own
5
- # class that implements #to_s and inject it into the constructor
6
- # like this:
7
- #
8
- # JsonAPI::Error.new(id: MySpecialErrorIdClass.new)
9
- #
10
- # From the api docs:
11
- #
12
- # a unique identifier for this particular occurrence of the problem.
13
- module JsonApiErrors
14
- module Default
15
- class Id
16
- def to_s
17
- "default-id"
18
- end
19
- end
20
- end
21
- end
@@ -1,26 +0,0 @@
1
- # DefaultLinks
2
- # ============
3
- #
4
- # This is the default error links. To override this you can create your own
5
- # class that implements #to_hash and inject it into the constructor
6
- # like this:
7
- #
8
- # JsonAPI::Error.new(links: MySpecialErrorLinksClass.new)
9
- #
10
- # From the api docs:
11
- #
12
- # a links object containing the following members:
13
- #
14
- # about: a link that leads to further details about this particular
15
- # occurrence of the problem.
16
- module JsonApiErrors
17
- module Default
18
- class Links
19
- def to_h
20
- {
21
- about: "default-links"
22
- }
23
- end
24
- end
25
- end
26
- end
@@ -1,23 +0,0 @@
1
- # DefaultMeta
2
- # ===========
3
- #
4
- # This is the default error meta information. To override this you can create your own
5
- # class that implements #to_hash and inject it into the constructor
6
- # like this:
7
- #
8
- # JsonAPI::Error.new(meta: MySpecialErrorMetaClass.new)
9
- #
10
- # From the api docs:
11
- #
12
- # a meta object containing non-standard meta-information about the error.
13
- module JsonApiErrors
14
- module Default
15
- class Meta
16
- def to_h
17
- {
18
- extra_info: "default-meta"
19
- }
20
- end
21
- end
22
- end
23
- end
@@ -1,31 +0,0 @@
1
- # DefaultSource
2
- # =============
3
- #
4
- # This is the default error source. To override this you can create your own
5
- # class that implements #to_hash and inject it into the constructor
6
- # like this:
7
- #
8
- # JsonAPI::Error.new(source: MySpecialErrorSourceClass.new)
9
- #
10
- # From the api docs:
11
- #
12
- # an object containing references to the source of the error, optionally
13
- # including any of the following members:
14
- #
15
- # pointer: a JSON Pointer [RFC6901] to the associated entity in the
16
- # request document [e.g. "/data" for a primary data object,
17
- # or "/data/attributes/title" for a specific attribute].
18
- #
19
- # parameter: a string indicating which query parameter caused the error.
20
- module JsonApiErrors
21
- module Default
22
- class Source
23
- def to_h
24
- {
25
- pointer: "default-pointer",
26
- parameter: "default-parameter"
27
- }
28
- end
29
- end
30
- end
31
- end
@@ -1,21 +0,0 @@
1
- # DefaultStatus
2
- # =============
3
- #
4
- # This is the default error status code. To override this you can create your own
5
- # class that implements #to_s and inject it into the constructor
6
- # like this:
7
- #
8
- # JsonAPI::Error.new(status: MySpecialErrorStatusClass.new)
9
- #
10
- # From the api docs:
11
- #
12
- # the HTTP status code applicable to this problem, expressed as a string value.
13
- module JsonApiErrors
14
- module Default
15
- class Status
16
- def to_s
17
- "422"
18
- end
19
- end
20
- end
21
- end
@@ -1,23 +0,0 @@
1
- # DefaultTitle
2
- # ============
3
- #
4
- # This is the default error title. To override this you can create your own
5
- # class that implements #to_s and inject it into the constructor
6
- # like this:
7
- #
8
- # JsonAPI::Error.new(title: MySpecialErrorTitleClass.new)
9
- #
10
- # From the api docs:
11
- #
12
- # a short, human-readable summary of the problem that SHOULD NOT change
13
- # from occurrence to occurrence of the problem, except for purposes of
14
- # localization.
15
- module JsonApiErrors
16
- module Default
17
- class Title
18
- def to_s
19
- "default-title"
20
- end
21
- end
22
- end
23
- end