jsonapi_deserializer 1.1.1 → 1.2.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 +5 -5
 - data/jsonapi_deserializer.gemspec +2 -2
 - data/lib/jsonapi_deserializer.rb +47 -15
 - data/lib/jsonapi_deserializer/version.rb +1 -1
 - metadata +12 -7
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 2 
     | 
    
         
            +
            SHA256:
         
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 2e893a9e49dd289acebe86c9f18af0450650659ef36297dfacc219564d6e62e2
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 1cfd8dd28ff6515fa64db6a96dabf1734f4910cccbab1a331c8f790f5be98ba6
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 20ca6743e1ba0412da7c16a493b0046555cd6bc8065982d912fa682be548e76729d0ae2a983f389c05e752a3345f2deb396023df5cf94ee4ca36059021f9586a
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 11cfd77060188941556ca17b62e93e7d05055f746900be994683c773b6099524b59c7edc3ad7f35eb79f4b5296f82760c83766c17ab65bd70e21d286702a0940
         
     | 
| 
         @@ -7,7 +7,7 @@ Gem::Specification.new do |spec| 
     | 
|
| 
       7 
7 
     | 
    
         
             
              spec.name          = "jsonapi_deserializer"
         
     | 
| 
       8 
8 
     | 
    
         
             
              spec.version       = JSONApi::Deserializer::VERSION
         
     | 
| 
       9 
9 
     | 
    
         
             
              spec.authors       = ["Trek Glowacki"]
         
     | 
| 
       10 
     | 
    
         
            -
              spec.email         = ["trek@ 
     | 
| 
      
 10 
     | 
    
         
            +
              spec.email         = ["trek.glowacki@gmail.com"]
         
     | 
| 
       11 
11 
     | 
    
         | 
| 
       12 
12 
     | 
    
         
             
              spec.summary       = "Deserialize your json-api payloads into easy-to-use hashes with attribute properties."
         
     | 
| 
       13 
13 
     | 
    
         
             
              spec.homepage      = "https://github.com/PopularPays/jsonapi_deserializer"
         
     | 
| 
         @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| 
     | 
|
| 
       18 
18 
     | 
    
         
             
              spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
         
     | 
| 
       19 
19 
     | 
    
         
             
              spec.require_paths = ["lib"]
         
     | 
| 
       20 
20 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
              spec.add_development_dependency "bundler", " 
     | 
| 
      
 21 
     | 
    
         
            +
              spec.add_development_dependency "bundler", ">= 1.10", "< 3.0"
         
     | 
| 
       22 
22 
     | 
    
         
             
              spec.add_development_dependency "rake", "~> 10.0"
         
     | 
| 
       23 
23 
     | 
    
         
             
              spec.add_development_dependency "rspec"
         
     | 
| 
       24 
24 
     | 
    
         | 
    
        data/lib/jsonapi_deserializer.rb
    CHANGED
    
    | 
         @@ -9,7 +9,14 @@ module JSONApi 
     | 
|
| 
       9 
9 
     | 
    
         
             
                  include Hashie::Extensions::IndifferentAccess
         
     | 
| 
       10 
10 
     | 
    
         | 
| 
       11 
11 
     | 
    
         
             
                  def initialize(source_hash = nil, default = nil, &blk)
         
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                    if source_hash[:id]
         
     | 
| 
      
 14 
     | 
    
         
            +
                      source_hash[:id] = source_hash[:id].to_s
         
     | 
| 
      
 15 
     | 
    
         
            +
                    end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                    if source_hash[:lid]
         
     | 
| 
      
 18 
     | 
    
         
            +
                      source_hash[:lid] = source_hash[:lid].to_s
         
     | 
| 
      
 19 
     | 
    
         
            +
                    end
         
     | 
| 
       13 
20 
     | 
    
         
             
                    super
         
     | 
| 
       14 
21 
     | 
    
         
             
                  end
         
     | 
| 
       15 
22 
     | 
    
         
             
                end
         
     | 
| 
         @@ -18,11 +25,43 @@ module JSONApi 
     | 
|
| 
       18 
25 
     | 
    
         
             
                  include Hashie::Extensions::MethodAccess
         
     | 
| 
       19 
26 
     | 
    
         
             
                  include Hashie::Extensions::IndifferentAccess
         
     | 
| 
       20 
27 
     | 
    
         
             
                  include Hashie::Extensions::DeepFetch
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                  def initialize(source_hash = nil, default = nil, &blk)
         
     | 
| 
      
 30 
     | 
    
         
            +
                    if source_hash[:id]
         
     | 
| 
      
 31 
     | 
    
         
            +
                      source_hash[:id] = source_hash[:id].to_s
         
     | 
| 
      
 32 
     | 
    
         
            +
                    end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                    if source_hash[:lid]
         
     | 
| 
      
 35 
     | 
    
         
            +
                      source_hash[:lid] = source_hash[:lid].to_s
         
     | 
| 
      
 36 
     | 
    
         
            +
                    end
         
     | 
| 
      
 37 
     | 
    
         
            +
                    super
         
     | 
| 
      
 38 
     | 
    
         
            +
                  end
         
     | 
| 
      
 39 
     | 
    
         
            +
                end
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                class Store
         
     | 
| 
      
 42 
     | 
    
         
            +
                  def initialize
         
     | 
| 
      
 43 
     | 
    
         
            +
                    @db = {}
         
     | 
| 
      
 44 
     | 
    
         
            +
                  end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                  def set(type, id, lid, record)
         
     | 
| 
      
 47 
     | 
    
         
            +
                    per_type = @db[type] ||= {}
         
     | 
| 
      
 48 
     | 
    
         
            +
                    identifier = id || lid
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                    per_type[identifier.to_s] = record
         
     | 
| 
      
 51 
     | 
    
         
            +
                  end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                  def get(type, id, lid)
         
     | 
| 
      
 54 
     | 
    
         
            +
                    per_type = @db[type]
         
     | 
| 
      
 55 
     | 
    
         
            +
                    return nil unless per_type
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                    identifier = id || lid
         
     | 
| 
      
 58 
     | 
    
         
            +
                    per_type[identifier.to_s]
         
     | 
| 
      
 59 
     | 
    
         
            +
                  end
         
     | 
| 
       21 
60 
     | 
    
         
             
                end
         
     | 
| 
       22 
61 
     | 
    
         | 
| 
       23 
62 
     | 
    
         
             
                def initialize(response)
         
     | 
| 
       24 
63 
     | 
    
         
             
                  @response = response.is_a?(Hash) ? response.with_indifferent_access : response
         
     | 
| 
       25 
     | 
    
         
            -
                  @store =  
     | 
| 
      
 64 
     | 
    
         
            +
                  @store = Store.new
         
     | 
| 
       26 
65 
     | 
    
         | 
| 
       27 
66 
     | 
    
         
             
                  all_data = ([] << @response[:data] << @response[:included]).flatten.compact
         
     | 
| 
       28 
67 
     | 
    
         
             
                  store_records(all_data)
         
     | 
| 
         @@ -32,9 +71,9 @@ module JSONApi 
     | 
|
| 
       32 
71 
     | 
    
         
             
                def deserialized_hash
         
     | 
| 
       33 
72 
     | 
    
         
             
                  data = @response[:data]
         
     | 
| 
       34 
73 
     | 
    
         
             
                  if data.is_a? Array
         
     | 
| 
       35 
     | 
    
         
            -
                    data.map { |datum| @store 
     | 
| 
      
 74 
     | 
    
         
            +
                    data.map { |datum| @store.get(datum[:type], datum[:id], datum[:lid]) }
         
     | 
| 
       36 
75 
     | 
    
         
             
                  else
         
     | 
| 
       37 
     | 
    
         
            -
                    @store 
     | 
| 
      
 76 
     | 
    
         
            +
                    @store.get(data[:type], data[:id], data[:lid])
         
     | 
| 
       38 
77 
     | 
    
         
             
                  end
         
     | 
| 
       39 
78 
     | 
    
         
             
                end
         
     | 
| 
       40 
79 
     | 
    
         | 
| 
         @@ -42,26 +81,19 @@ module JSONApi 
     | 
|
| 
       42 
81 
     | 
    
         | 
| 
       43 
82 
     | 
    
         
             
                def store_records(data)
         
     | 
| 
       44 
83 
     | 
    
         
             
                  data.each do |datum|
         
     | 
| 
       45 
     | 
    
         
            -
                     
     | 
| 
       46 
     | 
    
         
            -
                     
     | 
| 
       47 
     | 
    
         
            -
                    record = Record.new(id: datum[:id].to_s).merge(datum[:attributes] || {})
         
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
     | 
    
         
            -
                    per_type[record.id] = record
         
     | 
| 
      
 84 
     | 
    
         
            +
                    record = Record.new(id: datum[:id], lid: datum[:lid]).merge(datum[:attributes] || {})
         
     | 
| 
      
 85 
     | 
    
         
            +
                    @store.set(datum[:type], datum[:id], datum[:lid], record)
         
     | 
| 
       50 
86 
     | 
    
         
             
                  end
         
     | 
| 
       51 
87 
     | 
    
         
             
                end
         
     | 
| 
       52 
88 
     | 
    
         | 
| 
       53 
89 
     | 
    
         
             
                def find_association(association)
         
     | 
| 
       54 
90 
     | 
    
         
             
                  return nil unless association
         
     | 
| 
       55 
     | 
    
         
            -
                   
     | 
| 
       56 
     | 
    
         
            -
                    @store[association[:type]][association[:id].to_s]
         
     | 
| 
       57 
     | 
    
         
            -
                  else
         
     | 
| 
       58 
     | 
    
         
            -
                    Assocation.new(association)
         
     | 
| 
       59 
     | 
    
         
            -
                  end
         
     | 
| 
      
 91 
     | 
    
         
            +
                  @store.get(association[:type], association[:id], association[:lid]) || Assocation.new(association)
         
     | 
| 
       60 
92 
     | 
    
         
             
                end
         
     | 
| 
       61 
93 
     | 
    
         | 
| 
       62 
94 
     | 
    
         
             
                def build_associations(data)
         
     | 
| 
       63 
95 
     | 
    
         
             
                  data.each do |datum|
         
     | 
| 
       64 
     | 
    
         
            -
                    record = @store 
     | 
| 
      
 96 
     | 
    
         
            +
                    record = @store.get(datum[:type], datum[:id], datum[:lid])
         
     | 
| 
       65 
97 
     | 
    
         | 
| 
       66 
98 
     | 
    
         
             
                    relationships = datum[:relationships] || {}
         
     | 
| 
       67 
99 
     | 
    
         
             
                    relationships.each do |relationship_name, relationship|
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,29 +1,35 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: jsonapi_deserializer
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.2.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Trek Glowacki
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: exe
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date:  
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2021-03-29 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: bundler
         
     | 
| 
       15 
15 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
       16 
16 
     | 
    
         
             
                requirements:
         
     | 
| 
       17 
     | 
    
         
            -
                - - " 
     | 
| 
      
 17 
     | 
    
         
            +
                - - ">="
         
     | 
| 
       18 
18 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       19 
19 
     | 
    
         
             
                    version: '1.10'
         
     | 
| 
      
 20 
     | 
    
         
            +
                - - "<"
         
     | 
| 
      
 21 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 22 
     | 
    
         
            +
                    version: '3.0'
         
     | 
| 
       20 
23 
     | 
    
         
             
              type: :development
         
     | 
| 
       21 
24 
     | 
    
         
             
              prerelease: false
         
     | 
| 
       22 
25 
     | 
    
         
             
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
       23 
26 
     | 
    
         
             
                requirements:
         
     | 
| 
       24 
     | 
    
         
            -
                - - " 
     | 
| 
      
 27 
     | 
    
         
            +
                - - ">="
         
     | 
| 
       25 
28 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       26 
29 
     | 
    
         
             
                    version: '1.10'
         
     | 
| 
      
 30 
     | 
    
         
            +
                - - "<"
         
     | 
| 
      
 31 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 32 
     | 
    
         
            +
                    version: '3.0'
         
     | 
| 
       27 
33 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       28 
34 
     | 
    
         
             
              name: rake
         
     | 
| 
       29 
35 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
         @@ -82,7 +88,7 @@ dependencies: 
     | 
|
| 
       82 
88 
     | 
    
         
             
                    version: '0'
         
     | 
| 
       83 
89 
     | 
    
         
             
            description: 
         
     | 
| 
       84 
90 
     | 
    
         
             
            email:
         
     | 
| 
       85 
     | 
    
         
            -
            - trek@ 
     | 
| 
      
 91 
     | 
    
         
            +
            - trek.glowacki@gmail.com
         
     | 
| 
       86 
92 
     | 
    
         
             
            executables: []
         
     | 
| 
       87 
93 
     | 
    
         
             
            extensions: []
         
     | 
| 
       88 
94 
     | 
    
         
             
            extra_rdoc_files: []
         
     | 
| 
         @@ -119,8 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       119 
125 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       120 
126 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       121 
127 
     | 
    
         
             
            requirements: []
         
     | 
| 
       122 
     | 
    
         
            -
             
     | 
| 
       123 
     | 
    
         
            -
            rubygems_version: 2.5.1
         
     | 
| 
      
 128 
     | 
    
         
            +
            rubygems_version: 3.1.4
         
     | 
| 
       124 
129 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       125 
130 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       126 
131 
     | 
    
         
             
            summary: Deserialize your json-api payloads into easy-to-use hashes with attribute
         
     |