json_api_ruby 0.0.2 → 0.0.3
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 +4 -4
- data/.gitignore +37 -0
- data/CHANGELOG.md +3 -0
- data/json_api_ruby.gemspec +2 -2
- data/lib/json_api_ruby/resource.rb +7 -3
- data/lib/json_api_ruby/resources/base.rb +6 -8
- data/lib/json_api_ruby/resources/discovery.rb +10 -3
- data/lib/json_api_ruby/resources/relationships.rb +8 -5
- data/lib/json_api_ruby/serializer.rb +10 -14
- data/lib/json_api_ruby/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a42efcc7659bcd6df482b2f0a82f9434e2e65d7e
|
4
|
+
data.tar.gz: 3b111dbabfd9f66e0893af2c0b21f53a480d5711
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78d370d9fb0cbecbb2e46265610d7b8eee525d953bd0c786954d603cebe2800934a8995d944ff4fa9554888d697d43cc360dca217ab1ba5fe891a781f0c9be4f
|
7
|
+
data.tar.gz: ccdf63438c842cd0c6964633b755b45465246be9fb963f670e94af24dcb59334f3f2784cbf5d9a3ff2f7d07294748dc72bf33ebecaac18c67be3fd29ca455190
|
data/.gitignore
CHANGED
@@ -1 +1,38 @@
|
|
1
1
|
pkg
|
2
|
+
|
3
|
+
*.gem
|
4
|
+
*.rbc
|
5
|
+
/.config
|
6
|
+
/coverage/
|
7
|
+
/InstalledFiles
|
8
|
+
/pkg/
|
9
|
+
/spec/reports/
|
10
|
+
/spec/examples.txt
|
11
|
+
/test/tmp/
|
12
|
+
/test/version_tmp/
|
13
|
+
/tmp/
|
14
|
+
|
15
|
+
## Specific to RubyMotion:
|
16
|
+
.dat*
|
17
|
+
.repl_history
|
18
|
+
build/
|
19
|
+
|
20
|
+
## Documentation cache and generated files:
|
21
|
+
/.yardoc/
|
22
|
+
/_yardoc/
|
23
|
+
/doc/
|
24
|
+
/rdoc/
|
25
|
+
|
26
|
+
## Environment normalization:
|
27
|
+
/.bundle/
|
28
|
+
/vendor/bundle
|
29
|
+
/lib/bundler/man/
|
30
|
+
|
31
|
+
# for a library or gem, you might want to ignore these files since the code is
|
32
|
+
# intended to run in multiple environments; otherwise, check them in:
|
33
|
+
# Gemfile.lock
|
34
|
+
# .ruby-version
|
35
|
+
# .ruby-gemset
|
36
|
+
|
37
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
38
|
+
.rvmrc
|
data/CHANGELOG.md
CHANGED
data/json_api_ruby.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = JsonApi::VERSION
|
9
9
|
spec.authors = ["Tracey Eubanks"]
|
10
10
|
spec.email = ["tracey@bypassmobile.com"]
|
11
|
-
spec.description = %q{
|
12
|
-
spec.summary = %q{
|
11
|
+
spec.description = %q{Extremely lightweight implementation of JSON API}
|
12
|
+
spec.summary = %q{Extremely lightweight implementation of JSON API}
|
13
13
|
spec.homepage = "https://github.com/teubanks/jsonapi_ruby"
|
14
14
|
spec.license = "Free For All"
|
15
15
|
|
@@ -13,9 +13,9 @@ module JsonApi
|
|
13
13
|
|
14
14
|
# Can be set using `id_field` in the created resource class like so:
|
15
15
|
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
16
|
+
# class ObjectResource < JsonApi::Resource
|
17
|
+
# id_field :uuid
|
18
|
+
# end
|
19
19
|
#
|
20
20
|
# defaults to :id
|
21
21
|
def id
|
@@ -42,8 +42,12 @@ module JsonApi
|
|
42
42
|
_model
|
43
43
|
end
|
44
44
|
|
45
|
+
# The model that is used to fill out the data and attributes objects
|
45
46
|
attr_accessor :_model
|
46
47
|
|
48
|
+
# Includes can be passed in from a request
|
49
|
+
# See:
|
50
|
+
# http://jsonapi.org/format/#fetching-includes
|
47
51
|
attr_reader :includes
|
48
52
|
|
49
53
|
def initialize(model, options={})
|
@@ -4,9 +4,7 @@ module JsonApi
|
|
4
4
|
|
5
5
|
module Base
|
6
6
|
attr_reader :relationships
|
7
|
-
def to_hash
|
8
|
-
options.symbolize_keys
|
9
|
-
|
7
|
+
def to_hash
|
10
8
|
resource_hash = identifier_hash
|
11
9
|
resource_hash['attributes'] = attributes_hash if attributes_hash.any?
|
12
10
|
|
@@ -23,7 +21,7 @@ module JsonApi
|
|
23
21
|
# "article.comments" and "article-comments", so, leaving the method here
|
24
22
|
# but only supporting the most basic of things
|
25
23
|
def parse_for_includes(includes)
|
26
|
-
Array(includes).map
|
24
|
+
Array(includes).map(&:to_s)
|
27
25
|
end
|
28
26
|
|
29
27
|
def identifier_hash
|
@@ -39,11 +37,11 @@ module JsonApi
|
|
39
37
|
end
|
40
38
|
|
41
39
|
def attributes_hash
|
42
|
-
attrs
|
43
|
-
|
44
|
-
attrs[attr.to_s] =
|
40
|
+
Array(self.class.fields).inject({}) do |attrs, attr|
|
41
|
+
meth = method(attr)
|
42
|
+
attrs[attr.to_s] = meth.call
|
43
|
+
attrs
|
45
44
|
end
|
46
|
-
attrs
|
47
45
|
end
|
48
46
|
|
49
47
|
# Builds relationship resource classes
|
@@ -2,16 +2,23 @@ module JsonApi
|
|
2
2
|
module Resources
|
3
3
|
class Discovery
|
4
4
|
def self.resource_for_name(model, options={})
|
5
|
+
@discovered_classes ||= {}
|
5
6
|
namespace = options.fetch(:namespace, nil)
|
6
7
|
klass = options.fetch(:resource_class, nil)
|
7
8
|
parent = options.fetch(:parent_resource, nil)
|
8
9
|
|
9
10
|
if klass.blank?
|
10
|
-
|
11
|
-
|
11
|
+
cached_class = @discovered_classes[model.class.to_s]
|
12
|
+
if cached_class.blank?
|
13
|
+
klass = resource_class(model.class.to_s.underscore, namespace: namespace, parent: parent)
|
14
|
+
else
|
15
|
+
return cached_class
|
16
|
+
end
|
12
17
|
end
|
13
18
|
|
14
|
-
Object.const_get(klass)
|
19
|
+
const = Object.const_get(klass)
|
20
|
+
@discovered_classes[model.class.to_s] ||= const
|
21
|
+
const
|
15
22
|
rescue NameError
|
16
23
|
fail ::JsonApi::ResourceNotFound.new("Could not find resource class `#{klass}'")
|
17
24
|
end
|
@@ -3,11 +3,13 @@ module JsonApi
|
|
3
3
|
|
4
4
|
class RelationshipMeta
|
5
5
|
# The name of this relationship.
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
6
|
+
#
|
7
|
+
# This name comes from the resource object that defines the
|
8
|
+
# relationship. Example:
|
9
|
+
#
|
10
|
+
# class ArticleResource < JsonApi::Resource
|
11
|
+
# has_one :author # this is the name of this relationship
|
12
|
+
# end
|
11
13
|
attr_reader :name
|
12
14
|
|
13
15
|
attr_reader :cardinality
|
@@ -105,6 +107,7 @@ module JsonApi
|
|
105
107
|
{'data' => data}
|
106
108
|
end
|
107
109
|
|
110
|
+
# Build the related resources
|
108
111
|
def build_resources(options)
|
109
112
|
return unless included?
|
110
113
|
|
@@ -64,36 +64,32 @@ module JsonApi
|
|
64
64
|
|
65
65
|
def to_hash
|
66
66
|
serialized = {}
|
67
|
-
|
67
|
+
included_resources = []
|
68
68
|
|
69
69
|
data_array = @objects.map do |object|
|
70
70
|
resource_name = "#{object.class.to_s.underscore}_resource".classify
|
71
71
|
klass_name = @resource_class || resource_name
|
72
72
|
resource_klass = Resources::Discovery.resource_for_name(object, resource_class: klass_name)
|
73
73
|
resource = resource_klass.new(object, include: @includes)
|
74
|
-
|
74
|
+
included_resources += resource.relationships.select {|rel| rel.included?}.flat_map {|rel| rel.resources }
|
75
|
+
|
75
76
|
resource.to_hash
|
76
77
|
end
|
77
78
|
|
79
|
+
included_resources.uniq! do |rel|
|
80
|
+
rel.id + rel.type
|
81
|
+
end
|
82
|
+
|
78
83
|
serialized['data'] = data_array
|
79
84
|
|
80
85
|
serialized['meta'] = @meta if @meta
|
81
|
-
|
82
|
-
if included_data.present?
|
83
|
-
included_data.uniq! do |inc_data|
|
84
|
-
inc_data['id'] + inc_data['type']
|
85
|
-
end
|
86
|
-
serialized['included'] = included_data
|
87
|
-
end
|
86
|
+
serialized['included'] = assemble_included_data(included_resources)
|
88
87
|
|
89
88
|
serialized
|
90
89
|
end
|
91
90
|
|
92
|
-
def assemble_included_data(
|
93
|
-
|
94
|
-
next if relationship.resources.blank?
|
95
|
-
relationship.resources.map(&:to_hash)
|
96
|
-
end.compact
|
91
|
+
def assemble_included_data(included_resources)
|
92
|
+
included_resources.map(&:to_hash)
|
97
93
|
end
|
98
94
|
end
|
99
95
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tracey Eubanks
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -94,7 +94,7 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '4'
|
97
|
-
description:
|
97
|
+
description: Extremely lightweight implementation of JSON API
|
98
98
|
email:
|
99
99
|
- tracey@bypassmobile.com
|
100
100
|
executables: []
|
@@ -151,7 +151,7 @@ rubyforge_project:
|
|
151
151
|
rubygems_version: 2.4.5.1
|
152
152
|
signing_key:
|
153
153
|
specification_version: 4
|
154
|
-
summary:
|
154
|
+
summary: Extremely lightweight implementation of JSON API
|
155
155
|
test_files:
|
156
156
|
- spec/json_api_ruby/resource_spec.rb
|
157
157
|
- spec/json_api_ruby/resources/discover_spec.rb
|
@@ -160,3 +160,4 @@ test_files:
|
|
160
160
|
- spec/spec_helper.rb
|
161
161
|
- spec/support/json_api_matchers.rb
|
162
162
|
- spec/support/resource_objects.rb
|
163
|
+
has_rdoc:
|