jsonapi 0.1.1.beta1

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.
@@ -0,0 +1,59 @@
1
+ require 'jsonapi/include_directive'
2
+
3
+ describe JSONAPI::IncludeDirective::Parser, '.parse_include_args' do
4
+ it 'handles arrays of symbols and hashes' do
5
+ args = [:friends,
6
+ comments: [:author],
7
+ posts: [:author,
8
+ comments: [:author]]]
9
+ hash = JSONAPI::IncludeDirective::Parser.parse_include_args(args)
10
+ expected = {
11
+ friends: {},
12
+ comments: { author: {} },
13
+ posts: { author: {}, comments: { author: {} } }
14
+ }
15
+
16
+ expect(hash).to eq expected
17
+ end
18
+
19
+ it 'handles strings with spaces' do
20
+ str = 'friends, comments.author, posts.author, posts.comments.author'
21
+ hash = JSONAPI::IncludeDirective::Parser.parse_include_args(str)
22
+ expected = {
23
+ friends: {},
24
+ comments: { author: {} },
25
+ posts: { author: {}, comments: { author: {} } }
26
+ }
27
+
28
+ expect(hash).to eq expected
29
+ end
30
+
31
+ it 'handles common prefixes in strings' do
32
+ args = ['friends', 'comments.author', 'posts.author',
33
+ 'posts.comments.author']
34
+ hash = JSONAPI::IncludeDirective::Parser.parse_include_args(args)
35
+ expected = {
36
+ friends: {},
37
+ comments: { author: {} },
38
+ posts: { author: {}, comments: { author: {} } }
39
+ }
40
+
41
+ expect(hash).to eq expected
42
+ end
43
+
44
+ it 'handles an empty string' do
45
+ args = ''
46
+ hash = JSONAPI::IncludeDirective::Parser.parse_include_args(args)
47
+ expected = {}
48
+
49
+ expect(hash).to eq expected
50
+ end
51
+
52
+ it 'handles an empty array' do
53
+ args = []
54
+ hash = JSONAPI::IncludeDirective::Parser.parse_include_args(args)
55
+ expected = {}
56
+
57
+ expect(hash).to eq expected
58
+ end
59
+ end
@@ -0,0 +1,47 @@
1
+ require 'jsonapi/include_directive'
2
+
3
+ describe JSONAPI::IncludeDirective, '.key?' do
4
+ it 'handles existing keys' do
5
+ str = 'posts.comments'
6
+ include_directive = JSONAPI::IncludeDirective.new(str)
7
+
8
+ expect(include_directive.key?(:posts)).to be_truthy
9
+ end
10
+
11
+ it 'handles absent keys' do
12
+ str = 'posts.comments'
13
+ include_directive = JSONAPI::IncludeDirective.new(str)
14
+
15
+ expect(include_directive.key?(:author)).to be_falsy
16
+ end
17
+
18
+ it 'handles wildcards' do
19
+ str = 'posts.*'
20
+ include_directive = JSONAPI::IncludeDirective.new(
21
+ str, allow_wildcard: true)
22
+
23
+ expect(include_directive[:posts].key?(:author)).to be_truthy
24
+ expect(include_directive[:posts][:author].key?(:comments)).to be_falsy
25
+ end
26
+
27
+ it 'handles wildcards' do
28
+ str = 'posts.**'
29
+ include_directive = JSONAPI::IncludeDirective.new(
30
+ str, allow_wildcard: true)
31
+
32
+ expect(include_directive[:posts].key?(:author)).to be_truthy
33
+ expect(include_directive[:posts][:author].key?(:comments)).to be_truthy
34
+ end
35
+ end
36
+
37
+ describe JSONAPI::IncludeDirective, '.to_string' do
38
+ it 'works' do
39
+ str = 'friends,comments.author,posts.author,posts.comments.author'
40
+ include_directive = JSONAPI::IncludeDirective.new(str)
41
+ expected = include_directive.to_hash
42
+ actual = JSONAPI::IncludeDirective.new(include_directive.to_string)
43
+ .to_hash
44
+
45
+ expect(actual).to eq expected
46
+ end
47
+ end
@@ -0,0 +1,97 @@
1
+ require 'jsonapi'
2
+
3
+ describe JSONAPI, '#parse' do
4
+ before(:all) do
5
+ @author_links_hash = {
6
+ 'self' => 'http://example.com/articles/1/relationships/author',
7
+ 'related' => 'http://example.com/articles/1/author'
8
+ }
9
+ @author_data_hash = { 'type' => 'people', 'id' => '9' }
10
+ @comments_data_hash = [
11
+ { 'type' => 'comments', 'id' => '5' },
12
+ { 'type' => 'comments', 'id' => '12' }
13
+ ]
14
+ @article_relationships_hash = {
15
+ 'author' => {
16
+ 'links' => @author_links_hash,
17
+ 'data' => @author_data_hash
18
+ },
19
+ 'journal' => {
20
+ 'data' => nil
21
+ },
22
+ 'comments' => {
23
+ 'links' => {
24
+ 'self' => 'http://example.com/articles/1/relationships/comments',
25
+ 'related' => 'http://example.com/articles/1/comments'
26
+ },
27
+ 'data' => @comments_data_hash
28
+ }
29
+ }
30
+ @article_attributes_hash = { 'title' => 'JSON API paints my bikeshed!' }
31
+ @article_links_hash = { 'self' => 'http://example.com/articles/1' }
32
+ @data_hash = [{
33
+ 'type' => 'articles',
34
+ 'id' => '1',
35
+ 'attributes' => @article_attributes_hash,
36
+ 'links' => @article_links_hash,
37
+ 'relationships' => @article_relationships_hash
38
+ }]
39
+ @meta_hash = {
40
+ 'count' => '13'
41
+ }
42
+
43
+ @payload = {
44
+ 'data' => @data_hash,
45
+ 'meta' => @meta_hash
46
+ }
47
+ end
48
+
49
+ it 'works' do
50
+ document = JSONAPI.parse(@payload)
51
+
52
+ expect(document.meta.keys).to eq ['count']
53
+ expect(document.meta['count']).to eq '13'
54
+ expect(document.data.first.links.keys).to eq ['self']
55
+ expect(document.data.first.links.defined?(:self)).to be_truthy
56
+ expect(document.data.first.links.self.value).to eq 'http://example.com/articles/1'
57
+ expect(document.data.first.attributes.keys).to eq ['title']
58
+ expect(document.data.first.attributes.defined?(:title)).to be_truthy
59
+ expect(document.data.first.attributes.title).to eq 'JSON API paints my bikeshed!'
60
+ expect(document.data.first.relationships.keys).to eq %w(author journal comments)
61
+ expect(document.data.first.relationships.defined?(:author)).to be_truthy
62
+ expect(document.data.first.relationships.author.collection?).to be_falsy
63
+ expect(document.data.first.relationships.author.data.id).to eq '9'
64
+ expect(document.data.first.relationships.author.data.type).to eq 'people'
65
+ expect(document.data.first.relationships.author.links.keys).to eq ['self', 'related']
66
+ expect(document.data.first.relationships.author.links.defined?(:self)).to be_truthy
67
+ expect(document.data.first.relationships.author.links.self.value).to eq 'http://example.com/articles/1/relationships/author'
68
+ expect(document.data.first.relationships.author.links.defined?(:related)).to be_truthy
69
+ expect(document.data.first.relationships.author.links.related.value).to eq 'http://example.com/articles/1/author'
70
+ expect(document.data.first.relationships.defined?(:comments)).to be_truthy
71
+ expect(document.data.first.relationships.comments.collection?).to be_truthy
72
+ expect(document.data.first.relationships.comments.data.size).to eq 2
73
+ expect(document.data.first.relationships.comments.data[0].id).to eq '5'
74
+ expect(document.data.first.relationships.comments.data[0].type).to eq 'comments'
75
+ expect(document.data.first.relationships.comments.data[1].id).to eq '12'
76
+ expect(document.data.first.relationships.comments.data[1].type).to eq 'comments'
77
+ expect(document.data.first.relationships.comments.links.keys).to eq ['self', 'related']
78
+ expect(document.data.first.relationships.comments.links.defined?(:self)).to be_truthy
79
+ expect(document.data.first.relationships.comments.links.self.value).to eq 'http://example.com/articles/1/relationships/comments'
80
+ expect(document.data.first.relationships.comments.links.defined?(:related)).to be_truthy
81
+ expect(document.data.first.relationships.comments.links.related.value).to eq 'http://example.com/articles/1/comments'
82
+ expect(document.data.first.relationships.defined?(:journal)).to be_truthy
83
+ expect(document.data.first.relationships.journal.collection?).to be_falsy
84
+ expect(document.data.first.relationships.journal.data).to eq nil
85
+
86
+ expect(document.to_hash).to eq @payload
87
+ expect(document.data.map(&:to_hash)).to eq @data_hash
88
+
89
+ expect(document.data.first.attributes.to_hash).to eq @article_attributes_hash
90
+ expect(document.data.first.links.to_hash).to eq @article_links_hash
91
+ expect(document.data.first.relationships.to_hash).to eq @article_relationships_hash
92
+
93
+ expect(document.data.first.relationships.author.links.to_hash).to eq @author_links_hash
94
+ expect(document.data.first.relationships.author.data.to_hash).to eq @author_data_hash
95
+ expect(document.data.first.relationships.comments.data.map(&:to_hash)).to eq @comments_data_hash
96
+ end
97
+ end
@@ -0,0 +1,76 @@
1
+ require 'jsonapi'
2
+
3
+ describe JSONAPI::Resource, '.to_activerecord_hash' do
4
+ before(:all) do
5
+ @payload = {
6
+ 'data' => {
7
+ 'type' => 'articles',
8
+ 'id' => '1',
9
+ 'attributes' => {
10
+ 'title' => 'JSON API paints my bikeshed!',
11
+ 'rating' => '5 stars'
12
+ },
13
+ 'relationships' => {
14
+ 'author' => {
15
+ 'data' => { 'type' => 'people', 'id' => '9' }
16
+ },
17
+ 'referree' => {
18
+ 'data' => nil
19
+ },
20
+ 'publishing-journal' => {
21
+ 'data' => nil
22
+ },
23
+ 'comments' => {
24
+ 'data' => [
25
+ { 'type' => 'comments', 'id' => '5' },
26
+ { 'type' => 'comments', 'id' => '12' }
27
+ ]
28
+ }
29
+ }
30
+ }
31
+ }
32
+ end
33
+
34
+ it 'works' do
35
+ document = JSONAPI.parse(@payload)
36
+
37
+ options = {
38
+ attributes: {
39
+ except: [:rating]
40
+ },
41
+ relationships: {
42
+ only: [:author, :'publishing-journal', :comments],
43
+ polymorphic: [:author]
44
+ },
45
+ key_formatter: ->(x) { x.underscore }
46
+ }
47
+ actual = document.data.to_activerecord_hash(options)
48
+ expected = {
49
+ id: '1',
50
+ title: 'JSON API paints my bikeshed!',
51
+ author_id: '9',
52
+ author_type: 'Person',
53
+ publishing_journal_id: nil,
54
+ comment_ids: ['5', '12']
55
+ }
56
+
57
+ expect(actual).to eq expected
58
+ end
59
+
60
+ it 'whitelists all attributes/relationships by default' do
61
+ document = JSONAPI.parse(@payload)
62
+
63
+ actual = document.data.to_activerecord_hash
64
+ expected = {
65
+ id: '1',
66
+ title: 'JSON API paints my bikeshed!',
67
+ rating: '5 stars',
68
+ author_id: '9',
69
+ referree_id: nil,
70
+ :'publishing-journal_id' => nil,
71
+ comment_ids: ['5', '12']
72
+ }
73
+
74
+ expect(actual).to eq expected
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1.beta1
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Hosseini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
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.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ description: Tools for handling JSON API documents
70
+ email:
71
+ - lucas.hosseini@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - jsonapi_parser.gemspec
84
+ - lib/jsonapi.rb
85
+ - lib/jsonapi/attributes.rb
86
+ - lib/jsonapi/document.rb
87
+ - lib/jsonapi/error.rb
88
+ - lib/jsonapi/exceptions.rb
89
+ - lib/jsonapi/include_directive.rb
90
+ - lib/jsonapi/include_directive/parser.rb
91
+ - lib/jsonapi/jsonapi.rb
92
+ - lib/jsonapi/link.rb
93
+ - lib/jsonapi/links.rb
94
+ - lib/jsonapi/parse.rb
95
+ - lib/jsonapi/relationship.rb
96
+ - lib/jsonapi/relationships.rb
97
+ - lib/jsonapi/resource.rb
98
+ - lib/jsonapi/resource/active_record.rb
99
+ - lib/jsonapi/resource_identifier.rb
100
+ - lib/jsonapi/version.rb
101
+ - spec/duplicates_spec.rb
102
+ - spec/full_linkage_spec.rb
103
+ - spec/include_directive/parser_spec.rb
104
+ - spec/include_directive_spec.rb
105
+ - spec/parser_spec.rb
106
+ - spec/resource/to_activerecord_hash_spec.rb
107
+ homepage: https://github.com/beauby/jsonapi
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.3.1
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.8
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Parse and validate JSON API documents
131
+ test_files:
132
+ - spec/duplicates_spec.rb
133
+ - spec/full_linkage_spec.rb
134
+ - spec/include_directive/parser_spec.rb
135
+ - spec/include_directive_spec.rb
136
+ - spec/parser_spec.rb
137
+ - spec/resource/to_activerecord_hash_spec.rb