caprese 0.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 +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +12 -0
- data/.travis.yml +18 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/caprese.gemspec +38 -0
- data/lib/caprese/adapter/json_api/error.rb +123 -0
- data/lib/caprese/adapter/json_api/json_pointer.rb +52 -0
- data/lib/caprese/adapter/json_api/jsonapi.rb +49 -0
- data/lib/caprese/adapter/json_api/link.rb +88 -0
- data/lib/caprese/adapter/json_api/meta.rb +37 -0
- data/lib/caprese/adapter/json_api/pagination_links.rb +69 -0
- data/lib/caprese/adapter/json_api/relationship.rb +65 -0
- data/lib/caprese/adapter/json_api/resource_identifier.rb +49 -0
- data/lib/caprese/adapter/json_api.rb +509 -0
- data/lib/caprese/concerns/versioning.rb +69 -0
- data/lib/caprese/controller/concerns/callbacks.rb +60 -0
- data/lib/caprese/controller/concerns/errors.rb +42 -0
- data/lib/caprese/controller/concerns/persistence.rb +327 -0
- data/lib/caprese/controller/concerns/query.rb +209 -0
- data/lib/caprese/controller/concerns/relationships.rb +250 -0
- data/lib/caprese/controller/concerns/rendering.rb +43 -0
- data/lib/caprese/controller/concerns/typing.rb +39 -0
- data/lib/caprese/controller.rb +26 -0
- data/lib/caprese/error.rb +121 -0
- data/lib/caprese/errors.rb +69 -0
- data/lib/caprese/record/errors.rb +82 -0
- data/lib/caprese/record.rb +19 -0
- data/lib/caprese/routing/caprese_resources.rb +27 -0
- data/lib/caprese/serializer/concerns/links.rb +96 -0
- data/lib/caprese/serializer/concerns/lookup.rb +37 -0
- data/lib/caprese/serializer/error_serializer.rb +13 -0
- data/lib/caprese/serializer.rb +13 -0
- data/lib/caprese/version.rb +3 -0
- data/lib/caprese.rb +35 -0
- metadata +273 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0f662bf206bc10125464b9779d47c1d6704e0062
|
4
|
+
data.tar.gz: cd8f888d31eed365bc32e9efeb762903c04780b9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85b15e24d4f7f89d67d4741dd1e3d47d0ecf90414a6ce0e6b1499a24aa0d3f58b8aec2e8e47270c1c0e5d82d5cedd11f0525dfe9a4761a263ea2290d1a15cbe7
|
7
|
+
data.tar.gz: a1c2e81f96bce6b52c80c7ca61aed31018121a542b9baa67c819ec9c66565495ee96d434211128cb6cc75399682a251be3d742764e8ab8a9c7bc4d8496006e8d
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
rvm:
|
4
|
+
- 2.1.5
|
5
|
+
- 2.2.2
|
6
|
+
env:
|
7
|
+
- DB=sqlite
|
8
|
+
script:
|
9
|
+
- cd spec/dummy
|
10
|
+
- RAILS_ENV=test bundle exec rake db:migrate --trace
|
11
|
+
- bundle exec rake db:test:prepare
|
12
|
+
- cd ../..
|
13
|
+
- bundle exec rake
|
14
|
+
before_script:
|
15
|
+
- cp spec/dummy/config/database.travis.yml spec/dummy/config/database.yml
|
16
|
+
|
17
|
+
notifications:
|
18
|
+
email: false
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Nick Landgrebe && Peak Labs, LLC DBA Occasion App
|
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,41 @@
|
|
1
|
+
# Caprese
|
2
|
+
|
3
|
+
Caprese is a Rails library for creating RESTful APIs in as few words as possible. It handles all CRUD operations on resources and their associations for you, and you can customize how these operations
|
4
|
+
are carried out by overriding intermediate helper methods and by defining callbacks around any CRUD action.
|
5
|
+
|
6
|
+
What separates Caprese from similar gems is that it is trying to do as little as possible, instead allowing gems that are better at other things to do what they are good at, as opposed to Caprese trying to do it all.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'caprese'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install caprese
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
TODO: Write usage instructions here
|
27
|
+
|
28
|
+
## Development
|
29
|
+
|
30
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
31
|
+
|
32
|
+
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).
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nicklandgrebe/caprese.
|
37
|
+
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "caprese"
|
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
data/caprese.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'caprese/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "caprese"
|
8
|
+
spec.version = Caprese::VERSION
|
9
|
+
spec.authors = ["Nick Landgrebe", "Pelle ten Cate", "Kieran Klaassen"]
|
10
|
+
spec.email = ["nick@landgre.be"]
|
11
|
+
|
12
|
+
spec.summary = "Opinionated Rails library for writing RESTful APIs"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.homepage = 'https://github.com/nicklandgrebe/caprese'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 2.1'
|
23
|
+
|
24
|
+
spec.add_dependency 'active_model_serializers', '~> 0.10.0'
|
25
|
+
spec.add_dependency 'kaminari'
|
26
|
+
spec.add_dependency 'rails', '>= 4.2.0', '~> 4.2.6'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler'
|
29
|
+
spec.add_development_dependency 'factory_girl'
|
30
|
+
spec.add_development_dependency 'coveralls'
|
31
|
+
spec.add_development_dependency 'database_cleaner'
|
32
|
+
spec.add_development_dependency 'pry-byebug'
|
33
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
34
|
+
spec.add_development_dependency 'responders', '~> 2.0'
|
35
|
+
spec.add_development_dependency 'rspec'
|
36
|
+
spec.add_development_dependency 'rspec-rails'
|
37
|
+
spec.add_development_dependency 'sqlite3'
|
38
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'caprese/adapter/json_api/json_pointer'
|
2
|
+
|
3
|
+
module Caprese
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
module Error
|
7
|
+
# rubocop:disable Style/AsciiComments
|
8
|
+
UnknownSourceTypeError = Class.new(ArgumentError)
|
9
|
+
|
10
|
+
# Builds a JSON API Errors Object
|
11
|
+
# {http://jsonapi.org/format/#errors JSON API Errors}
|
12
|
+
#
|
13
|
+
# @param [Caprese::Serializer::ErrorSerializer] error_serializer
|
14
|
+
# @return [Array<Symbol, Array<String>>] i.e. attribute_name, [attribute_errors]
|
15
|
+
def self.resource_errors(error_serializer, options)
|
16
|
+
error_serializer.as_json.flat_map do |attribute_name, attribute_errors|
|
17
|
+
attribute_name = JsonApi.send(:transform_key_casing!, attribute_name,
|
18
|
+
options)
|
19
|
+
attribute_error_objects(error_serializer.object.record, attribute_name, attribute_errors)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# FIXME: param_errors implies multiple errors, but since we use a fail first
|
24
|
+
# strategy, there will only be one param error ever, as reflected by this method's
|
25
|
+
# definition
|
26
|
+
def self.param_errors(error_serializer, options)
|
27
|
+
error_attributes = error_serializer.as_json
|
28
|
+
[
|
29
|
+
{
|
30
|
+
code: error_attributes[:code],
|
31
|
+
detail: error_attributes[:message],
|
32
|
+
source: error_source(:parameter, nil, error_attributes[:field])
|
33
|
+
}
|
34
|
+
]
|
35
|
+
end
|
36
|
+
|
37
|
+
# definition:
|
38
|
+
# JSON Object
|
39
|
+
#
|
40
|
+
# properties:
|
41
|
+
# ☐ id : String
|
42
|
+
# ☐ status : String
|
43
|
+
# ☐ code : String
|
44
|
+
# ☐ title : String
|
45
|
+
# ☑ detail : String
|
46
|
+
# ☐ links
|
47
|
+
# ☐ meta
|
48
|
+
# ☑ error_source
|
49
|
+
#
|
50
|
+
# description:
|
51
|
+
# id : A unique identifier for this particular occurrence of the problem.
|
52
|
+
# status : The HTTP status code applicable to this problem, expressed as a string value
|
53
|
+
# code : An application-specific error code, expressed as a string value.
|
54
|
+
# title : A short, human-readable summary of the problem. It **SHOULD NOT** change from
|
55
|
+
# occurrence to occurrence of the problem, except for purposes of localization.
|
56
|
+
# detail : A human-readable explanation specific to this occurrence of the problem.
|
57
|
+
# structure:
|
58
|
+
# {
|
59
|
+
# title: 'SystemFailure',
|
60
|
+
# detail: 'something went terribly wrong',
|
61
|
+
# status: '500'
|
62
|
+
# }.merge!(errorSource)
|
63
|
+
def self.attribute_error_objects(record, attribute_name, attribute_errors)
|
64
|
+
attribute_errors.map do |attribute_error|
|
65
|
+
{
|
66
|
+
source: error_source(:pointer, record, attribute_name),
|
67
|
+
code: attribute_error[:code],
|
68
|
+
detail: attribute_error[:message]
|
69
|
+
}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# errorSource
|
74
|
+
# description:
|
75
|
+
# oneOf
|
76
|
+
# ☑ pointer : String
|
77
|
+
# ☑ parameter : String
|
78
|
+
#
|
79
|
+
# description:
|
80
|
+
# pointer: A JSON Pointer RFC6901 to the associated entity in the request document e.g. "/data"
|
81
|
+
# for a primary data object, or "/data/attributes/title" for a specific attribute.
|
82
|
+
# https://tools.ietf.org/html/rfc6901
|
83
|
+
#
|
84
|
+
# parameter: A string indicating which query parameter caused the error
|
85
|
+
# structure:
|
86
|
+
# if is_attribute?
|
87
|
+
# {
|
88
|
+
# pointer: '/data/attributes/red-button'
|
89
|
+
# }
|
90
|
+
# else
|
91
|
+
# {
|
92
|
+
# parameter: 'pres'
|
93
|
+
# }
|
94
|
+
# end
|
95
|
+
def self.error_source(source_type, record, attribute_name)
|
96
|
+
case source_type
|
97
|
+
when :pointer
|
98
|
+
if record.has_attribute?(attribute_name)
|
99
|
+
{
|
100
|
+
pointer: JsonApi::JsonPointer.new(:attribute, record, attribute_name)
|
101
|
+
}
|
102
|
+
elsif attribute_name.to_s.split('.').size > 1
|
103
|
+
{
|
104
|
+
pointer: JsonApi::JsonPointer.new(:relationship_attribute, record, attribute_name)
|
105
|
+
}
|
106
|
+
else
|
107
|
+
{
|
108
|
+
pointer: JsonApi::JsonPointer.new(:relationship, record, attribute_name)
|
109
|
+
}
|
110
|
+
end
|
111
|
+
when :parameter
|
112
|
+
{
|
113
|
+
parameter: attribute_name
|
114
|
+
}
|
115
|
+
else
|
116
|
+
fail UnknownSourceTypeError, "Unknown source type '#{source_type}' for attribute_name '#{attribute_name}'"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
# rubocop:enable Style/AsciiComments
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Caprese
|
2
|
+
module Adapter
|
3
|
+
class JsonApi
|
4
|
+
module JsonPointer
|
5
|
+
module_function
|
6
|
+
|
7
|
+
POINTERS = {
|
8
|
+
attribute: '/data/attributes/%s'.freeze,
|
9
|
+
relationship: '/data/relationships/%s'.freeze,
|
10
|
+
primary_data: '/data%s'.freeze
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
# Iterates over the field of an error and converts it to a pointer in JSON API format
|
14
|
+
#
|
15
|
+
# @example
|
16
|
+
# new(:attribute, record, 'name')
|
17
|
+
# => '/data/attributes/name'
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
# new(:relationship, record, 'post')
|
21
|
+
# => '/data/attributes/name'
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
# new(:relationship_attribute, record, 'post.user.name')
|
25
|
+
# => '/data/relationships/post/data/relationships/user/data/attributes/name'
|
26
|
+
#
|
27
|
+
# @param [Symbol] pointer_type the type of pointer: :attribute, :relationship, :primary_data
|
28
|
+
def new(pointer_type, record, value)
|
29
|
+
if pointer_type == :relationship_attribute
|
30
|
+
value.to_s.split('.').inject('') do |pointer, v|
|
31
|
+
pointer +
|
32
|
+
if record.class.reflections.key?(v)
|
33
|
+
record =
|
34
|
+
if record.class.reflections[v].collection?
|
35
|
+
record.send(v).first
|
36
|
+
else
|
37
|
+
record.send(v)
|
38
|
+
end
|
39
|
+
|
40
|
+
format(POINTERS[:relationship], v)
|
41
|
+
else
|
42
|
+
format(POINTERS[:attribute], v)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
else
|
46
|
+
format(POINTERS[pointer_type], value)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Caprese
|
2
|
+
module Adapter
|
3
|
+
class JsonApi
|
4
|
+
# {http://jsonapi.org/format/#document-jsonapi-object Jsonapi Object}
|
5
|
+
|
6
|
+
# toplevel_jsonapi
|
7
|
+
# definition:
|
8
|
+
# JSON Object
|
9
|
+
#
|
10
|
+
# properties:
|
11
|
+
# version : String
|
12
|
+
# meta
|
13
|
+
#
|
14
|
+
# description:
|
15
|
+
# An object describing the server's implementation
|
16
|
+
# structure:
|
17
|
+
# {
|
18
|
+
# version: ActiveModelSerializers.config.jsonapi_version,
|
19
|
+
# meta: ActiveModelSerializers.config.jsonapi_toplevel_meta
|
20
|
+
# }.reject! { |_, v| v.blank? }
|
21
|
+
# prs:
|
22
|
+
# https://github.com/rails-api/active_model_serializers/pull/1050
|
23
|
+
module Jsonapi
|
24
|
+
module_function
|
25
|
+
|
26
|
+
def add!(hash)
|
27
|
+
hash.merge!(object) if include_object?
|
28
|
+
end
|
29
|
+
|
30
|
+
def include_object?
|
31
|
+
ActiveModelSerializers.config.jsonapi_include_toplevel_object
|
32
|
+
end
|
33
|
+
|
34
|
+
# TODO: see if we can cache this
|
35
|
+
def object
|
36
|
+
object = {
|
37
|
+
jsonapi: {
|
38
|
+
version: ActiveModelSerializers.config.jsonapi_version,
|
39
|
+
meta: ActiveModelSerializers.config.jsonapi_toplevel_meta
|
40
|
+
}
|
41
|
+
}
|
42
|
+
object[:jsonapi].reject! { |_, v| v.blank? }
|
43
|
+
|
44
|
+
object
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Caprese
|
2
|
+
module Adapter
|
3
|
+
class JsonApi
|
4
|
+
# link
|
5
|
+
# definition:
|
6
|
+
# oneOf
|
7
|
+
# linkString
|
8
|
+
# linkObject
|
9
|
+
#
|
10
|
+
# description:
|
11
|
+
# A link **MUST** be represented as either: a string containing the link's URL or a link
|
12
|
+
# object."
|
13
|
+
# structure:
|
14
|
+
# if href?
|
15
|
+
# linkString
|
16
|
+
# else
|
17
|
+
# linkObject
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# linkString
|
21
|
+
# definition:
|
22
|
+
# URI
|
23
|
+
#
|
24
|
+
# description:
|
25
|
+
# A string containing the link's URL.
|
26
|
+
# structure:
|
27
|
+
# 'http://example.com/link-string'
|
28
|
+
#
|
29
|
+
# linkObject
|
30
|
+
# definition:
|
31
|
+
# JSON Object
|
32
|
+
#
|
33
|
+
# properties:
|
34
|
+
# href (required) : URI
|
35
|
+
# meta
|
36
|
+
# structure:
|
37
|
+
# {
|
38
|
+
# href: 'http://example.com/link-object',
|
39
|
+
# meta: meta,
|
40
|
+
# }.reject! {|_,v| v.nil? }
|
41
|
+
class Link
|
42
|
+
include ActiveModelSerializers::SerializationContext::UrlHelpers
|
43
|
+
|
44
|
+
def initialize(serializer, value)
|
45
|
+
@_routes ||= nil # handles warning
|
46
|
+
# actionpack-4.0.13/lib/action_dispatch/routing/route_set.rb:417: warning: instance variable @_routes not initialized
|
47
|
+
@serializer = serializer
|
48
|
+
@object = serializer.object
|
49
|
+
@scope = serializer.scope
|
50
|
+
# Use the return value of the block unless it is nil.
|
51
|
+
if value.respond_to?(:call)
|
52
|
+
@value = instance_eval(&value)
|
53
|
+
else
|
54
|
+
@value = value
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def href(value)
|
59
|
+
@href = value
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
|
63
|
+
def meta(value)
|
64
|
+
@meta = value
|
65
|
+
nil
|
66
|
+
end
|
67
|
+
|
68
|
+
def default_url_options
|
69
|
+
Caprese.config.default_url_options
|
70
|
+
end
|
71
|
+
|
72
|
+
def as_json
|
73
|
+
return @value if @value
|
74
|
+
|
75
|
+
hash = {}
|
76
|
+
hash[:href] = @href if defined?(@href)
|
77
|
+
hash[:meta] = @meta if defined?(@meta)
|
78
|
+
|
79
|
+
hash.any? ? hash : nil
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
|
84
|
+
attr_reader :object, :scope, :serializer
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Caprese
|
2
|
+
module Adapter
|
3
|
+
class JsonApi
|
4
|
+
# meta
|
5
|
+
# definition:
|
6
|
+
# JSON Object
|
7
|
+
#
|
8
|
+
# description:
|
9
|
+
# Non-standard meta-information that can not be represented as an attribute or relationship.
|
10
|
+
# structure:
|
11
|
+
# {
|
12
|
+
# attitude: 'adjustable'
|
13
|
+
# }
|
14
|
+
class Meta
|
15
|
+
def initialize(serializer)
|
16
|
+
@object = serializer.object
|
17
|
+
@scope = serializer.scope
|
18
|
+
|
19
|
+
# Use the return value of the block unless it is nil.
|
20
|
+
if serializer._meta.respond_to?(:call)
|
21
|
+
@value = instance_eval(&serializer._meta)
|
22
|
+
else
|
23
|
+
@value = serializer._meta
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def as_json
|
28
|
+
@value
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
attr_reader :object, :scope
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Caprese
|
2
|
+
module Adapter
|
3
|
+
class JsonApi
|
4
|
+
class PaginationLinks
|
5
|
+
MissingSerializationContextError = Class.new(KeyError)
|
6
|
+
FIRST_PAGE = 1
|
7
|
+
|
8
|
+
attr_reader :collection, :context
|
9
|
+
|
10
|
+
def initialize(collection, adapter_options)
|
11
|
+
@collection = collection
|
12
|
+
@adapter_options = adapter_options
|
13
|
+
@context = adapter_options.fetch(:serialization_context) do
|
14
|
+
fail MissingSerializationContextError, <<-EOF.freeze
|
15
|
+
JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext.
|
16
|
+
Please pass a ':serialization_context' option or
|
17
|
+
override CollectionSerializer#paginated? to return 'false'.
|
18
|
+
EOF
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def as_json
|
23
|
+
per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
|
24
|
+
pages_from.each_with_object({}) do |(key, value), hash|
|
25
|
+
params = query_parameters.merge(page: { number: value, size: per_page }).to_query
|
26
|
+
|
27
|
+
hash[key] = "#{url(adapter_options)}?#{params}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
attr_reader :adapter_options
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def pages_from
|
38
|
+
return {} if collection.total_pages <= FIRST_PAGE
|
39
|
+
|
40
|
+
{}.tap do |pages|
|
41
|
+
pages[:self] = collection.current_page
|
42
|
+
|
43
|
+
unless collection.current_page == FIRST_PAGE
|
44
|
+
pages[:first] = FIRST_PAGE
|
45
|
+
pages[:prev] = collection.current_page - FIRST_PAGE
|
46
|
+
end
|
47
|
+
|
48
|
+
unless collection.current_page == collection.total_pages
|
49
|
+
pages[:next] = collection.current_page + FIRST_PAGE
|
50
|
+
pages[:last] = collection.total_pages
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def url(options)
|
56
|
+
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
|
57
|
+
end
|
58
|
+
|
59
|
+
def request_url
|
60
|
+
@request_url ||= context.request_url
|
61
|
+
end
|
62
|
+
|
63
|
+
def query_parameters
|
64
|
+
@query_parameters ||= context.query_parameters
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Caprese
|
2
|
+
module Adapter
|
3
|
+
class JsonApi
|
4
|
+
class Relationship
|
5
|
+
# {http://jsonapi.org/format/#document-resource-object-related-resource-links Document Resource Object Related Resource Links}
|
6
|
+
# {http://jsonapi.org/format/#document-links Document Links}
|
7
|
+
# {http://jsonapi.org/format/#document-resource-object-linkage Document Resource Relationship Linkage}
|
8
|
+
# {http://jsonapi.org/format/#document-meta Document Meta}
|
9
|
+
def initialize(parent_serializer, serializable_resource_options, association, included_associations)
|
10
|
+
@parent_serializer = parent_serializer
|
11
|
+
@association = association
|
12
|
+
@serializable_resource_options = serializable_resource_options
|
13
|
+
@included_associations = included_associations
|
14
|
+
end
|
15
|
+
|
16
|
+
def as_json
|
17
|
+
hash = {}
|
18
|
+
|
19
|
+
# Only render a relationship's data if it was included, if Caprese.config.optimize_relationships
|
20
|
+
if association.options[:include_data] &&
|
21
|
+
(!Caprese.config.optimize_relationships || included_associations[association.name])
|
22
|
+
hash[:data] = data_for(association)
|
23
|
+
end
|
24
|
+
|
25
|
+
links = links_for(association)
|
26
|
+
hash[:links] = links if links.any?
|
27
|
+
|
28
|
+
meta = meta_for(association)
|
29
|
+
hash[:meta] = meta if meta
|
30
|
+
|
31
|
+
hash
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
attr_reader :parent_serializer, :serializable_resource_options, :association, :included_associations
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def data_for(association)
|
41
|
+
serializer = association.serializer
|
42
|
+
if serializer.respond_to?(:each)
|
43
|
+
serializer.map { |s| ResourceIdentifier.new(s, serializable_resource_options).as_json }
|
44
|
+
elsif (virtual_value = association.options[:virtual_value])
|
45
|
+
virtual_value
|
46
|
+
elsif serializer && serializer.object
|
47
|
+
ResourceIdentifier.new(serializer, serializable_resource_options).as_json
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def links_for(association)
|
52
|
+
association.links.each_with_object({}) do |(key, value), hash|
|
53
|
+
result = Link.new(parent_serializer, value).as_json
|
54
|
+
hash[key] = result if result
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def meta_for(association)
|
59
|
+
meta = association.meta
|
60
|
+
meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|