jsonapi-object-mapper 0.5.0 → 0.6.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: 8f1ffe31b2bc6266364e9ce22de8f3326fe91d7b6209f421a12a70d9cbe68cdc
4
- data.tar.gz: 8603e6c43ae05a4159811a64fe041e2470cdb011b24223b759d405ddbf2e8e01
3
+ metadata.gz: cc15e84ef11d68ce266929e2a420eefb0526b05eabd8a63065747721fef0c3a3
4
+ data.tar.gz: a3a9a57a7d7492124f505cad9c05e509cd5deb5a33f1afec59c7c01295153a5e
5
5
  SHA512:
6
- metadata.gz: 1d6a3abc3c030e77d4ca61c26610c46772e3fa1f98b7add1f486502e42036998d7f76a7c7896375efd76f336558b6e0e6d0c1bec7611a76727b7cb6101194a27
7
- data.tar.gz: 05226150aac758f77f1d8e0af7b13624b97194e42dd075f4e80b544cb674414563a69ed3fc609c42f44585bd47f44c1c8440320c5f79860f15e716d5eb7f02f1
6
+ metadata.gz: 7b18f64a3c70061e4860862fd8d8b766bbc5a78de8bf728f52b34b04eeadfc3a1ed9ac0a6e598d1bc8b083e7f82ffb9d85ab3c1a1bd00a62b536c02c2fe72f19
7
+ data.tar.gz: 43ba9a81a83f4824ed4aedddc6e947cd7812cd1775efc4d925c6e14a5a41d3ed7a86ce3317516429da17a90c42427ce29742f6168fb557fd08678565473b0c8c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jsonapi-object-mapper (0.5.0)
4
+ jsonapi-object-mapper (0.6.0)
5
5
  oj (~> 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # JsonapiObjectMapper
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jsonapi_object_mapper`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Deserialize's raw or pre-hashed JsonAPI objects into plan ruby objects as well as embeds any included relational resources.
6
4
 
7
5
  ## Installation
8
6
 
@@ -23,13 +21,13 @@ Or install it yourself as:
23
21
  ## Usage
24
22
 
25
23
  ```ruby
26
- require "jsonapi_object_mapper"
24
+ require "jsonapi-object-mapper"
27
25
 
28
- class Photo < JsonAPIObjectMapper::Deserializer::Resource
26
+ class Photo < JsonAPIObjectMapper::Deserialize::Resource
29
27
  attribute :image
30
28
  end
31
29
 
32
- class User < JsonAPIObjectMapper::Deserializer::Resource
30
+ class User < JsonAPIObjectMapper::Deserialize::Resource
33
31
  # Embedding with another Resource class, will deserialize the `included` resource with the given class
34
32
  has_one :photo, embed_with: Photo
35
33
 
@@ -51,12 +49,32 @@ class User < JsonAPIObjectMapper::Deserializer::Resource
51
49
  end
52
50
 
53
51
 
54
- User.call_collection(json_payload) #=> [<#User:123>, <#User:432>]
55
52
  user = User.call(json_payload) #=> <#User:123>
56
53
 
57
54
  user.first_name #=> "FOOER"
58
55
  user.last_name #=> "Bar"
59
56
 
57
+ # If json API Payload is a collection of data points
58
+ users = User.call(json_payload) #=> <# JsonAPIObjectMapper::Deserialize::Collection #>
59
+
60
+ users.each do |user|
61
+ user.first_name
62
+ user.last_name
63
+ end
64
+
65
+ ```
66
+
67
+ ### Errors
68
+
69
+ ```ruby
70
+
71
+ user = User.call(json_payload)
72
+
73
+ # Aliases: document_valid?
74
+ # Inverses: errors?, invalid?, document_invalid?
75
+ user.valid? #=> false
76
+ user.errors #=> [<# OpenStruct title:..., detail: ..., source: {...}, ...>, ...]
77
+
60
78
  ```
61
79
 
62
80
  ## Development
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "oj"
4
+ require "jsonapi-object-mapper/exceptions"
4
5
  require "jsonapi-object-mapper/parser/document"
5
- require "jsonapi-object-mapper/deserializer/resource"
6
+ require "jsonapi-object-mapper/deserialize/resource"
6
7
  require "jsonapi-object-mapper/version"
7
8
 
8
9
  module JsonAPIObjectMapper
@@ -3,7 +3,7 @@
3
3
  require "jsonapi-object-mapper/parser/errors"
4
4
 
5
5
  module JsonAPIObjectMapper
6
- module Deserializer
6
+ module Deserialize
7
7
  class Collection
8
8
  include Enumerable
9
9
  include JsonAPIObjectMapper::Parser::Errors
@@ -11,8 +11,8 @@ module JsonAPIObjectMapper
11
11
  attr_accessor :collection_data
12
12
 
13
13
  def initialize(parser, klass:)
14
- raise ArgumentError, "Must provide a valid resource klass" unless klass.is_a?(Class)
15
- raise ArgumentError, "Must provide a parsed document" unless parser.is_a?(JsonAPIObjectMapper::Parser::Document)
14
+ raise InvalidResource, "Must provide a valid resource klass" unless klass.is_a?(Class)
15
+ raise InvalidParser, "Must provide a parsed document" unless parser.is_a?(JsonAPIObjectMapper::Parser::Document)
16
16
  @errors = parser.errors
17
17
  @collection_data =
18
18
  if document_invalid?
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonAPIObjectMapper
4
- module Deserializer
4
+ module Deserialize
5
5
  module DSL
6
6
  DEFAULT_ID_BLOCK = proc { |id| id }
7
7
  DEFAULT_TYPE_BLOCK = proc { |type| type }
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "jsonapi-object-mapper/deserializer/dsl"
4
- require "jsonapi-object-mapper/deserializer/collection"
3
+ require "jsonapi-object-mapper/deserialize/dsl"
4
+ require "jsonapi-object-mapper/deserialize/collection"
5
5
  require "jsonapi-object-mapper/parser/errors"
6
6
 
7
7
  module JsonAPIObjectMapper
8
- module Deserializer
8
+ module Deserialize
9
9
  class Resource
10
10
  include JsonAPIObjectMapper::Parser::Errors
11
11
  extend DSL
@@ -42,7 +42,7 @@ module JsonAPIObjectMapper
42
42
 
43
43
  def initialize(parser, document: nil)
44
44
  super()
45
- raise ArgumentError, "Must provide a parsed document" unless parser.is_a?(JsonAPIObjectMapper::Parser::Document)
45
+ raise InvalidParser, "Must provide a parsed document" unless parser.is_a?(JsonAPIObjectMapper::Parser::Document)
46
46
  @errors = parser.errors
47
47
 
48
48
  if document_valid?
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonAPIObjectMapper
4
+ class InvalidResource < StandardError
5
+ end
6
+
7
+ class InvalidParser < StandardError
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonAPIObjectMapper
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ module JsonAPIObjectMapper
6
+ module Deserialize
7
+ RSpec.describe Collection do
8
+ let(:parser) { JsonAPIObjectMapper::Parser::Document.new(payload) }
9
+ let(:payload) { {} }
10
+
11
+ describe ".new" do
12
+ context "Rising Exceptions" do
13
+ it "is expected to raise an error if the resource isn't a class object" do
14
+ expect { described_class.new(nil, klass: "Class") }.to raise_error(InvalidResource)
15
+ end
16
+
17
+ it "is expected to raise an error if the parser isn't a parsed json document" do
18
+ expect { described_class.new({}, { klass: Class }) }.to raise_error(InvalidParser)
19
+ end
20
+ end
21
+
22
+ context "Creating a collection of resources" do
23
+ let(:payload) do
24
+ {
25
+ "data" => [
26
+ {
27
+ "id" => "1",
28
+ "type" => "foobar",
29
+ "attributes" => {
30
+ "name" => "Bert",
31
+ },
32
+ },
33
+ {
34
+ "id" => "2",
35
+ "type" => "foobar",
36
+ "attributes" => {
37
+ "name" => "Johnson",
38
+ },
39
+ },
40
+ ],
41
+ }
42
+ end
43
+ it "Should collect all resources into a enumerable resource" do
44
+ foobar_klass = Class.new(JsonAPIObjectMapper::Deserialize::Resource) do
45
+ attribute :name
46
+ end
47
+
48
+ result = described_class.new(parser, klass: foobar_klass)
49
+ expect(result.valid?).to be_truthy
50
+ expect(result.count).to eq(2)
51
+ expect(result.map(&:name)).to eq(%w[Bert Johnson])
52
+ end
53
+ end
54
+
55
+ context "Results containing errors" do
56
+ let(:payload) do
57
+ {
58
+ "errors" => [
59
+ {
60
+ "title" => "Invalid name",
61
+ "detail" => "Look like foobar to me",
62
+ "source" => {
63
+ "pointer" => "name",
64
+ },
65
+ },
66
+ ],
67
+ }
68
+ end
69
+
70
+ it "Should return as an invalid or error document" do
71
+ result = described_class.new(parser, klass: Class.new)
72
+ expect(result).to be_invalid
73
+ end
74
+
75
+ it "Should contain all error responses the in #errors field" do
76
+ result = described_class.new(parser, klass: Class.new).errors.first
77
+ expect(result.title).to eq("Invalid name")
78
+ expect(result.detail).to eq("Look like foobar to me")
79
+ expect(result.source).to include("pointer" => "name")
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -3,7 +3,7 @@
3
3
  require "spec_helper"
4
4
 
5
5
  module JsonAPIObjectMapper
6
- module Deserializer
6
+ module Deserialize
7
7
  RSpec.describe Resource do
8
8
  describe "Attributes" do
9
9
  let(:payload) do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-object-mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Protacio-Karaszi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-06 00:00:00.000000000 Z
11
+ date: 2018-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -91,14 +91,16 @@ files:
91
91
  - bin/setup
92
92
  - jsonapi-object-mapper.gemspec
93
93
  - lib/jsonapi-object-mapper.rb
94
- - lib/jsonapi-object-mapper/deserializer/collection.rb
95
- - lib/jsonapi-object-mapper/deserializer/dsl.rb
96
- - lib/jsonapi-object-mapper/deserializer/resource.rb
94
+ - lib/jsonapi-object-mapper/deserialize/collection.rb
95
+ - lib/jsonapi-object-mapper/deserialize/dsl.rb
96
+ - lib/jsonapi-object-mapper/deserialize/resource.rb
97
+ - lib/jsonapi-object-mapper/exceptions.rb
97
98
  - lib/jsonapi-object-mapper/parser/document.rb
98
99
  - lib/jsonapi-object-mapper/parser/errors.rb
99
100
  - lib/jsonapi-object-mapper/parser/included_resources.rb
100
101
  - lib/jsonapi-object-mapper/version.rb
101
- - spec/deserializer/resource_spec.rb
102
+ - spec/deserialize/collection_spec.rb
103
+ - spec/deserialize/resource_spec.rb
102
104
  - spec/jsonapi_object_mapper_spec.rb
103
105
  - spec/spec_helper.rb
104
106
  homepage: https://github.com/GeorgeKaraszi/jsonapi-object-mapper
@@ -126,6 +128,7 @@ signing_key:
126
128
  specification_version: 4
127
129
  summary: Digests JSON-API responses to plain old ruby objects
128
130
  test_files:
129
- - spec/deserializer/resource_spec.rb
131
+ - spec/deserialize/collection_spec.rb
132
+ - spec/deserialize/resource_spec.rb
130
133
  - spec/jsonapi_object_mapper_spec.rb
131
134
  - spec/spec_helper.rb