simple-json-api-serializer 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: 70a2dc4601aee95d0b8ca23f13996aa12f55d697
4
+ data.tar.gz: d9b52897b675d77b5b353195eea2f1bd1c610c33
5
+ SHA512:
6
+ metadata.gz: 01e2a4136d074192f60bb7c54b0f159bec76ae83be357ff97cd2505a6385dec091b6cce68ce23b7a88ab376360d94a70b6fa5b797759e25439edfbb900fd6646
7
+ data.tar.gz: 983f533a18b6c30f28bf4ddc353e3ffc899d389582f60010610bcf610adcc2f1820470811e5f48dad70d376f878fb93e71d5b691db048487059a9434094c8c20
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple-json-api-serializer.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple-json-api-serializer (0.1.0)
5
+ activesupport (~> 4.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.2.4)
11
+ i18n (~> 0.7)
12
+ json (~> 1.7, >= 1.7.7)
13
+ minitest (~> 5.1)
14
+ thread_safe (~> 0.3, >= 0.3.4)
15
+ tzinfo (~> 1.1)
16
+ diff-lcs (1.2.5)
17
+ i18n (0.7.0)
18
+ json (1.8.3)
19
+ minitest (5.8.0)
20
+ rake (10.4.2)
21
+ rspec (3.3.0)
22
+ rspec-core (~> 3.3.0)
23
+ rspec-expectations (~> 3.3.0)
24
+ rspec-mocks (~> 3.3.0)
25
+ rspec-core (3.3.2)
26
+ rspec-support (~> 3.3.0)
27
+ rspec-expectations (3.3.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.3.0)
30
+ rspec-mocks (3.3.2)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.3.0)
33
+ rspec-support (3.3.0)
34
+ thread_safe (0.3.5)
35
+ tzinfo (1.2.2)
36
+ thread_safe (~> 0.1)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ bundler (~> 1.10)
43
+ rake (~> 10.0)
44
+ rspec (~> 3.0)
45
+ simple-json-api-serializer!
46
+
47
+ BUNDLED WITH
48
+ 1.10.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Marten Schilstra
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,39 @@
1
+ # Simple JsonApi::Serializer
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/simple/json/api/serializer`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'simple-json-api-serializer'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install simple-json-api-serializer
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Development
27
+
28
+ 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.
29
+
30
+ 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).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/martndemus/simple-json-api-serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
35
+
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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 "simple/json/api/serializer"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,50 @@
1
+ require 'json_api/serializer'
2
+
3
+ module JSONApi
4
+ class ObjectSerializerDefinition
5
+ class << self
6
+ def serialize(object)
7
+ Serializer.new.to_json(object, {
8
+ id_attribute: id_attribute,
9
+ attributes: attributes,
10
+ relationships: relationships
11
+ })
12
+ end
13
+
14
+ def inherited(specialization)
15
+ specialization.attributes(*attributes)
16
+ specialization.relationships(*relationships)
17
+ specialization.id_attribute(id_attribute)
18
+ end
19
+
20
+ def attributes(*attrs)
21
+ @attributes ||= []
22
+ @attributes |= attrs
23
+ end
24
+
25
+ def id_attribute(attr = @id_attribute)
26
+ @id_attribute = attr
27
+ end
28
+
29
+ def relationship(name, **config)
30
+ config[:name] = name
31
+ relationships(config)
32
+ end
33
+
34
+ def relationships(*configs)
35
+ @relationships ||= []
36
+ @relationships |= configs
37
+ end
38
+
39
+ def has_one(name, **config)
40
+ config[:to] = :one
41
+ relationship(name, config)
42
+ end
43
+
44
+ def has_many(name, **config)
45
+ config[:to] = :many
46
+ relationship(name, config)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,66 @@
1
+ require 'json_api/utils'
2
+
3
+ module JSONApi
4
+ class RelationshipSerializer
5
+ def as_json(object, **options)
6
+ data =
7
+ if options[:to] == :many
8
+ ToManySerializer.new.data_for(object, options)
9
+ else
10
+ ToOneSerializer.new.data_for(object, options)
11
+ end
12
+
13
+ if data.nil? || data == []
14
+ nil
15
+ else
16
+ { data: data }
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def type_for(options)
23
+ options[:type] || options[:name]
24
+ end
25
+
26
+ def key_for(options)
27
+ options[:name].to_s.singularize
28
+ end
29
+
30
+ def relationship_for(object, **options)
31
+ object.send(options[:key] || key_for(options))
32
+ end
33
+
34
+ def resource_identifier_for(type_name, id)
35
+ return nil if id.nil? || id == ""
36
+
37
+ {
38
+ type: Utils.canonicalize_type_name(type_name),
39
+ id: Utils.canonicalize_id(id)
40
+ }
41
+ end
42
+
43
+ class ToManySerializer < RelationshipSerializer
44
+ def key_for(**options)
45
+ "#{super}_ids"
46
+ end
47
+
48
+ def data_for(object, options)
49
+ ids = relationship_for(object, options)
50
+ ids.map { |id| resource_identifier_for(type_for(options), id) }
51
+ .compact
52
+ end
53
+ end
54
+
55
+ class ToOneSerializer < RelationshipSerializer
56
+ def key_for(**options)
57
+ "#{super}_id"
58
+ end
59
+
60
+ def data_for(object, options)
61
+ id = relationship_for(object, options)
62
+ resource_identifier_for(type_for(options), id)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,76 @@
1
+ require 'active_support/json'
2
+ require 'json_api/utils'
3
+ require 'json_api/relationship_serializer'
4
+
5
+
6
+ module JSONApi
7
+ class Serializer
8
+ def to_json(object, **options)
9
+ ActiveSupport::JSON.encode({ data: data_for(object, **options) })
10
+ end
11
+
12
+ private
13
+
14
+ def data_for(object, **options)
15
+ if object.is_a? Array
16
+ object.map { |o| resource_object_for(o, options) }
17
+ else
18
+ resource_object_for(object, options)
19
+ end
20
+ end
21
+
22
+ def resource_object_for(object, **options)
23
+ result = resource_identifier_for(object, options)
24
+
25
+ if options[:attributes] && options[:attributes].any?
26
+ result[:attributes] = attributes_for(object, options)
27
+ end
28
+
29
+ if options[:relationships] && options[:relationships].any?
30
+ result[:relationships] = relationships_for(object, options)
31
+ end
32
+
33
+ result
34
+ end
35
+
36
+ def resource_identifier_for(object, **options)
37
+ {
38
+ type: type_for(object),
39
+ id: id_for(object, options)
40
+ }
41
+ end
42
+
43
+ def relationships_for(object, options)
44
+ relationship_serializer = RelationshipSerializer.new
45
+ options[:relationships].each_with_object({}) do |relationship, hash|
46
+ relationship_key = Utils.canonicalize_attribute_name(relationship[:name])
47
+ data = relationship_serializer.as_json(object, relationship)
48
+ hash[relationship_key] = data unless data.nil?
49
+ end
50
+ end
51
+
52
+ def attributes_for(object, **options)
53
+ options[:attributes].each_with_object({}) do |attribute, hash|
54
+ attribute_key = Utils.canonicalize_attribute_name(attribute)
55
+ hash[attribute_key] = object.send(attribute)
56
+ end
57
+ end
58
+
59
+ def id_for(object, **options)
60
+ id_attribute = options[:id_attribute] || :id
61
+ canonicalize_id(object.send(id_attribute))
62
+ end
63
+
64
+ def type_for(object)
65
+ canonicalize_type_name(object.class.name)
66
+ end
67
+
68
+ def canonicalize_id(id)
69
+ Utils.canonicalize_id(id)
70
+ end
71
+
72
+ def canonicalize_type_name(type_name)
73
+ Utils.canonicalize_type_name(type_name)
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/all'
2
+
3
+ module JSONApi
4
+ module Utils
5
+ class << self
6
+ def canonicalize_id(id)
7
+ id.nil? ? '' : id.to_s
8
+ end
9
+
10
+ def canonicalize_type_name(type_name)
11
+ type_name.to_s
12
+ .demodulize
13
+ .underscore
14
+ .pluralize
15
+ .dasherize
16
+ end
17
+
18
+ def canonicalize_attribute_name(attribute_name)
19
+ attribute_name.to_s
20
+ .dasherize
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ module Simple
2
+ module Json
3
+ module Api
4
+ module Serializer
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require "simple/json/api/serializer/version"
2
+
3
+ module Simple
4
+ module Json
5
+ module Api
6
+ module Serializer
7
+ # Your code goes here...
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple-json-api-serializer"
8
+ spec.version = JSONApi::VERSION
9
+ spec.authors = ["Marten Schilstra"]
10
+ spec.email = ["mail@martenschilstra.nl"]
11
+
12
+ spec.summary = %q{An extremely simple JSON Api serializer}
13
+ spec.description = %q{
14
+ It supports just plain Ruby objects. This serializer does not target any
15
+ specific framework like ActiveRecord or anything like that.
16
+
17
+ Does not (yet) have support for links and sideloading.
18
+ }
19
+ spec.homepage = "https://github.com/martndemus/simple-json-api-serializer"
20
+ spec.license = "MIT"
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "activesupport", "~> 4.2"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.10"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-json-api-serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marten Schilstra
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: "\n It supports just plain Ruby objects. This serializer does not
70
+ target any\n specific framework like ActiveRecord or anything like that.\n\n
71
+ \ Does not (yet) have support for links and sideloading.\n "
72
+ email:
73
+ - mail@martenschilstra.nl
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/json_api/object_serializer_definition.rb
87
+ - lib/json_api/relationship_serializer.rb
88
+ - lib/json_api/serializer.rb
89
+ - lib/json_api/utils.rb
90
+ - lib/simple/json/api/serializer.rb
91
+ - lib/simple/json/api/serializer/version.rb
92
+ - simple-json-api-serializer.gemspec
93
+ homepage: https://github.com/martndemus/simple-json-api-serializer
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.5.1
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: An extremely simple JSON Api serializer
117
+ test_files: []