simple_json_api 0.0.2

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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.overcommit.yml +31 -0
  4. data/.rubocop.yml +8 -0
  5. data/.travis.yml +11 -0
  6. data/Gemfile +21 -0
  7. data/LICENSE +22 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +37 -0
  10. data/Rakefile +22 -0
  11. data/lib/simple_json_api.rb +27 -0
  12. data/lib/simple_json_api/active_record_refinements.rb +21 -0
  13. data/lib/simple_json_api/array_refinements.rb +14 -0
  14. data/lib/simple_json_api/array_serializer.rb +16 -0
  15. data/lib/simple_json_api/association.rb +10 -0
  16. data/lib/simple_json_api/attribute.rb +9 -0
  17. data/lib/simple_json_api/dsl.rb +61 -0
  18. data/lib/simple_json_api/field_list.rb +33 -0
  19. data/lib/simple_json_api/include_list.rb +20 -0
  20. data/lib/simple_json_api/json_api_builder.rb +92 -0
  21. data/lib/simple_json_api/resource_serializer.rb +68 -0
  22. data/lib/simple_json_api/serializer.rb +52 -0
  23. data/lib/simple_json_api/version.rb +4 -0
  24. data/simple_json_api.gemspec +34 -0
  25. data/test/fixtures/projects.yml +5 -0
  26. data/test/integration/render_basic_test.rb +71 -0
  27. data/test/integration/render_fields_test.rb +78 -0
  28. data/test/integration/render_include_test.rb +133 -0
  29. data/test/integration/render_nested_include_test.rb +116 -0
  30. data/test/integration/serializers_test.rb +41 -0
  31. data/test/setup/data.rb +24 -0
  32. data/test/setup/models.rb +49 -0
  33. data/test/setup/schema.rb +38 -0
  34. data/test/setup/serializers.rb +57 -0
  35. data/test/test_helper.rb +33 -0
  36. data/test/test_setup.rb +19 -0
  37. data/test/tmp/schema.rb +47 -0
  38. data/test/unit/association_test.rb +28 -0
  39. data/test/unit/attribute_test.rb +22 -0
  40. data/test/unit/field_list_test.rb +28 -0
  41. data/test/unit/include_list_test.rb +23 -0
  42. metadata +241 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f79e962fb92a428343041ed39759f6e78564fce
4
+ data.tar.gz: 31d9bdcdfd654d50f96309080991040c0126ba59
5
+ SHA512:
6
+ metadata.gz: 52ca5d094eb111beb2388bb9d57965b8f4d04aedbeb4169c4124d54d42b4ba5e05da2b70e90bce5ebdca243c8bbbe69214c4e960dac564762fe21a333994d4e9
7
+ data.tar.gz: e4304c2ed873421a9a82b0ef78e9e7d1edc54bb47f6e20b53a450e5ec8e5a1ffa84584a14499b4afc9892f95fa339454ef01d6dc03f55772af4be414b7883998
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /rdoc/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/.overcommit.yml ADDED
@@ -0,0 +1,31 @@
1
+ # Use this file to configure the Overcommit hooks you wish to use. This will
2
+ # extend the default configuration defined in:
3
+ # https://github.com/causes/overcommit/blob/master/config/default.yml
4
+ #
5
+ # At the topmost level of this YAML file is a key representing type of hook
6
+ # being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
7
+ # customize each hook, such as whether to only run it on certain files (via
8
+ # `include`), whether to only display output if it fails (via `quiet`), etc.
9
+ #
10
+ # For a complete list of hooks, see:
11
+ # https://github.com/causes/overcommit/tree/master/lib/overcommit/hook
12
+ #
13
+ # For a complete list of options that you can use to customize hooks, see:
14
+ # https://github.com/causes/overcommit#configuration
15
+ #
16
+ # Uncomment the following lines to make the configuration take effect.
17
+
18
+ #PreCommit:
19
+ # Rubocop:
20
+ # on_warn: fail # Treat all warnings as failures
21
+ #
22
+ # TrailingWhitespace:
23
+ # exclude:
24
+ # - '**/db/structure.sql' # Ignore trailing whitespace in generated files
25
+ #
26
+ #PostCheckout:
27
+ # ALL: # Special hook name that customizes all hooks of this type
28
+ # quiet: true # Change all post-checkout hooks to only display output on failure
29
+ #
30
+ # IndexTags:
31
+ # enabled: true # Generate a tags file with `ctags` each time HEAD changes
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ Exclude:
3
+ - test/tmp/*
4
+
5
+ Style/PredicateName:
6
+ NamePrefix:
7
+ - is_
8
+ - have_
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ env:
5
+ - 'RAILS_VERSION=4.0'
6
+ - 'RAILS_VERSION=4.1'
7
+ - 'RAILS_VERSION=4.2'
8
+ - 'RAILS_VERSION=master'
9
+ matrix:
10
+ allow_failures:
11
+ - env: 'RAILS_VERSION=master'
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_json_api.gemspec
4
+ gemspec
5
+
6
+ version = ENV['RAILS_VERSION'] || '4.1'
7
+
8
+ case version
9
+ when 'master'
10
+ gem 'rails', github: 'rails/rails'
11
+
12
+ # Learned from AMS
13
+ # ugh https://github.com/rails/rails/issues/16063#issuecomment-48090125
14
+ gem 'arel', github: 'rails/arel'
15
+ when '4.0', '4.1', '4.2'
16
+ gem 'rails', "~> #{version}.0"
17
+ else
18
+ fail GemfileError, "Unsupported Rails version - #{version}"
19
+ end
20
+
21
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Gary Gordon
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Gary Gordon
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # SimpleJsonApi
2
+
3
+ [![Build Status](https://travis-ci.org/ggordon/simple_json_api.svg?branch=master)](https://travis-ci.org/ggordon/simple_json_api) [![Code Climate](https://codeclimate.com/github/ggordon/simple_json_api/badges/gpa.svg)](https://codeclimate.com/github/ggordon/simple_json_api) [![Test Coverage](https://codeclimate.com/github/ggordon/simple_json_api/badges/coverage.svg)](https://codeclimate.com/github/ggordon/simple_json_api)
4
+
5
+ A gem to render json following the [jsonapi](http://jsonapi.org) spec.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'simple_json_api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install simple_json_api
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+
28
+ ## TODO
29
+
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/[my-github-username]/simple_json_api/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+ namespace :coverage do
6
+ desc 'Delete previous coverage results'
7
+ task :clean do
8
+ rm_rf 'coverage'
9
+ end
10
+ end
11
+
12
+ Rake::TestTask.new do |t|
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.libs.push 'test'
15
+ end
16
+ task test: 'coverage:clean'
17
+ task default: :test
18
+
19
+ RDoc::Task.new do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,27 @@
1
+ require 'simple_json_api/version'
2
+
3
+ require 'simple_json_api/active_record_refinements'
4
+ require 'simple_json_api/array_refinements'
5
+ require 'simple_json_api/association'
6
+ require 'simple_json_api/attribute'
7
+ require 'simple_json_api/dsl'
8
+ require 'simple_json_api/field_list'
9
+ require 'simple_json_api/include_list'
10
+
11
+ require 'simple_json_api/serializer'
12
+ require 'simple_json_api/array_serializer'
13
+ require 'simple_json_api/resource_serializer'
14
+
15
+ require 'simple_json_api/json_api_builder'
16
+
17
+ # SimpleJsonApi
18
+ module SimpleJsonApi
19
+ # Main hook to generate json
20
+ def self.render(model:, serializer:, options: {})
21
+ JsonApiBuilder.new(
22
+ model: model,
23
+ serializer: serializer,
24
+ options: options
25
+ ).to_json
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_record'
2
+
3
+ # SimpleJsonApi
4
+ module SimpleJsonApi
5
+ # Refinements on ActiveRecord
6
+ module ActiveRecordRefinements
7
+ refine ActiveRecord::Base do
8
+ def typed_json_id
9
+ [self.class.to_s.downcase.pluralize, json_id]
10
+ end
11
+
12
+ def json_id
13
+ send(json_pk).to_s
14
+ end
15
+
16
+ def json_pk
17
+ self.class.primary_key.to_sym
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_record'
2
+
3
+ # SimpleJsonApi
4
+ module SimpleJsonApi
5
+ # Refinements on Hash
6
+ module ArrayRefinements
7
+ refine Array do
8
+ using ActiveRecordRefinements
9
+ def already_linked?(key:, id:)
10
+ find { |elem| elem[key] == id }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ # SimpleJsonApi
2
+ module SimpleJsonApi
3
+ # The ArraySerializer will serialize a collection
4
+ class ArraySerializer < Serializer
5
+ def serialize
6
+ _object.map do |object|
7
+ serializer = _each_serializer.new(object: object, builder: _builder)
8
+ serializer.serialize
9
+ end
10
+ end
11
+
12
+ def _root_name
13
+ _each_serializer._root_name
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ # SimpleJsonApi
2
+ module SimpleJsonApi
3
+ # Wrapper for a linked association
4
+ Association = Struct.new(:name, :type, :serializer, :polymorphic, :key) do
5
+ def key
6
+ name_s = name.to_s
7
+ (type == :has_many) ? name_s.pluralize.to_sym : name_s.singularize.to_sym
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ # SimpleJsonApi
2
+ module SimpleJsonApi
3
+ # Attribute
4
+ Attribute = Struct.new(:name, :key) do
5
+ def key
6
+ self[:key] || self[:name]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,61 @@
1
+ # SimpleJsonApi
2
+ module SimpleJsonApi
3
+ # Define the public API for creating serializers
4
+ module DSL
5
+ attr_accessor :_associations
6
+ attr_accessor :_attributes
7
+ attr_reader :_root_name
8
+
9
+ def inherited(base)
10
+ base._associations = []
11
+ base._attributes = []
12
+ end
13
+
14
+ def serializes(name, options = {})
15
+ ResourceSerializer.register_serializer(
16
+ { serializer: self, resource: name },
17
+ options
18
+ )
19
+ @_root_name = name
20
+ end
21
+
22
+ def attribute(name, options = {})
23
+ register_attribute(name, options)
24
+ end
25
+
26
+ def belongs_to(name, options = {})
27
+ register_association(name, :belongs_to, options)
28
+ end
29
+
30
+ def has_many(name, options = {})
31
+ register_association(name, :has_many, options)
32
+ end
33
+
34
+ def has_one(name, options = {})
35
+ register_association(name, :has_one, options)
36
+ end
37
+
38
+ def default_fields
39
+ @_attributes.map(&:first).join(',')
40
+ end
41
+
42
+ private
43
+
44
+ def register_association(name, type, options)
45
+ serializer = options.fetch(:serializer, nil)
46
+ polymorphic = options.fetch(:polymorphic, false)
47
+ association = SimpleJsonApi::Association.new(
48
+ name, type, serializer, polymorphic
49
+ )
50
+ _associations << association
51
+ def_delegator :_object, association.name
52
+ end
53
+
54
+ def register_attribute(name, options)
55
+ key = options.fetch(:key, nil)
56
+ attribute = SimpleJsonApi::Attribute.new(name, key)
57
+ _attributes << attribute
58
+ def_delegator :_object, attribute.name
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,33 @@
1
+ # SimpleJsonApi
2
+ module SimpleJsonApi
3
+ # List of Fields for a resource
4
+ class FieldList
5
+ def initialize(fields:, root_serializer:)
6
+ @fields = fields
7
+ @root_serializer = root_serializer
8
+ end
9
+
10
+ def field_list
11
+ @field_list ||= parse
12
+ end
13
+
14
+ def parse
15
+ result = {}
16
+ case @fields
17
+ when Hash
18
+ @fields.each do |resource, fields|
19
+ result[resource.to_sym] = FIELD_LIST_PARSER.call(fields)
20
+ end
21
+ when String
22
+ result[@root_serializer._root_name] = FIELD_LIST_PARSER.call(@fields)
23
+ end
24
+ result
25
+ end
26
+
27
+ def fields_for(resource)
28
+ field_list.fetch(resource, nil)
29
+ end
30
+
31
+ FIELD_LIST_PARSER = ->(list) { list.split(',').map(&:to_s) }
32
+ end
33
+ end
@@ -0,0 +1,20 @@
1
+ # SimpleJsonApi
2
+ module SimpleJsonApi
3
+ # List of Included associations for a resource
4
+ class IncludeList
5
+ def initialize(include:)
6
+ @include = include
7
+ @include_list = []
8
+ end
9
+
10
+ def parse
11
+ @include_list = @include.split(',').map(&:to_s) if @include.is_a? String
12
+ self
13
+ end
14
+
15
+ def include?(resource, parent = [])
16
+ path = [parent, resource].flatten.compact.join('.')
17
+ @include_list.include?(path)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,92 @@
1
+ # SimpleJsonApi
2
+ module SimpleJsonApi
3
+ # The Builder to walk the hierarchy and contruct the JSON
4
+ class JsonApiBuilder
5
+ using ActiveRecordRefinements
6
+ using ArrayRefinements
7
+
8
+ extend Forwardable
9
+
10
+ attr_reader :serializer
11
+ attr_reader :model
12
+ attr_reader :_included_resources
13
+ attr_reader :include
14
+ attr_reader :field_list
15
+ attr_reader :result
16
+
17
+ def_delegators :@field_list, :fields_for
18
+
19
+ # TODO: sort: nil
20
+ def initialize(model:, serializer:, options: {})
21
+ @model = model
22
+ handle_options(options, serializer)
23
+ @serializer = get_serializer(serializer)
24
+ @_included_resources = {}
25
+ @result = {}
26
+ @linked = {}
27
+ end
28
+
29
+ def handle_options(options, serializer)
30
+ @field_list = FieldList.new(
31
+ fields: options.fetch(:fields, nil),
32
+ root_serializer: serializer
33
+ )
34
+ @include = IncludeList.new(
35
+ include: options.fetch(:include, nil)
36
+ ).parse
37
+ end
38
+
39
+ def as_json(options = nil)
40
+ build.as_json(options)
41
+ end
42
+
43
+ def build
44
+ result[@serializer._root_name] = @serializer.serialize
45
+ @result[:linked] = @linked unless @linked.empty?
46
+ @result
47
+ end
48
+
49
+ def add_linked_elem(elem, obj, association, base)
50
+ resource_name = elem.to_s.pluralize
51
+ obj = Array(obj) unless obj.respond_to?(:each)
52
+ obj.each do |item|
53
+ serialize_association(item, resource_name, association, base)
54
+ end
55
+ end
56
+
57
+ def serialize_association(item, resource_name, association, base)
58
+ # TODO: don't include middle unless explicitly requested.
59
+ return unless include.include?(resource_name, base)
60
+ assoc_base = [base, resource_name].compact.join('.')
61
+ serializer = ResourceSerializer.for(item, association)
62
+ add_to_linked(assoc_base, item, serializer)
63
+ end
64
+
65
+ def add_to_linked(assoc_base, item, serializer)
66
+ linked = @linked[serializer._root_name] ||= []
67
+ return if linked.already_linked?(key: item.json_pk, id: item.json_id)
68
+ linked << serializer.new(
69
+ object: item, builder: self, base: assoc_base
70
+ ).serialize
71
+ end
72
+
73
+ private
74
+
75
+ def get_serializer(serializer)
76
+ if use_array_serializer?
77
+ ArraySerializer.new(
78
+ object: @model,
79
+ each_serializer: serializer,
80
+ builder: self
81
+ )
82
+ else
83
+ serializer.new(object: @model, builder: self)
84
+ end
85
+ end
86
+
87
+ def use_array_serializer?
88
+ @model.is_a?(Array) ||
89
+ @model.is_a?(ActiveRecord::Associations::CollectionProxy)
90
+ end
91
+ end
92
+ end