simple_json_api 0.0.3 → 0.0.4

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 (47) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +14 -2
  3. data/README.md +1 -1
  4. data/lib/generators/simple_json_api/resource/USAGE +8 -0
  5. data/lib/generators/simple_json_api/resource/resource_generator.rb +73 -0
  6. data/lib/generators/simple_json_api/resource/templates/controller_template.rb.erb +34 -0
  7. data/lib/generators/simple_json_api/resource/templates/serializer_template.rb.erb +14 -0
  8. data/lib/simple_json_api/api_node.rb +68 -0
  9. data/lib/simple_json_api/array_serializer.rb +15 -1
  10. data/lib/simple_json_api/association.rb +16 -0
  11. data/lib/simple_json_api/attribute.rb +9 -0
  12. data/lib/simple_json_api/builder.rb +35 -0
  13. data/lib/simple_json_api/dsl.rb +35 -5
  14. data/lib/simple_json_api/field_list.rb +2 -7
  15. data/lib/simple_json_api/helper.rb +5 -0
  16. data/lib/simple_json_api/include_list.rb +12 -7
  17. data/lib/simple_json_api/json_api_wrapper.rb +46 -0
  18. data/lib/simple_json_api/member/data.rb +14 -0
  19. data/lib/simple_json_api/member/included.rb +38 -0
  20. data/lib/simple_json_api/member/links.rb +9 -0
  21. data/lib/simple_json_api/member/meta.rb +9 -0
  22. data/lib/simple_json_api/refinements/active_record.rb +23 -0
  23. data/lib/simple_json_api/refinements/array.rb +13 -0
  24. data/lib/simple_json_api/refinements/symbol.rb +17 -0
  25. data/lib/simple_json_api/resource.rb +23 -0
  26. data/lib/simple_json_api/resource_serializer.rb +25 -24
  27. data/lib/simple_json_api/serializer.rb +39 -2
  28. data/lib/simple_json_api/serializer_factory.rb +22 -0
  29. data/lib/simple_json_api/version.rb +1 -1
  30. data/lib/simple_json_api.rb +7 -29
  31. data/simple_json_api.gemspec +2 -7
  32. data/test/generators/simple_json_api/resource_generator_test.rb +31 -0
  33. data/test/integration/render_basic_test.rb +10 -7
  34. data/test/integration/render_fields_test.rb +35 -16
  35. data/test/integration/render_include_test.rb +66 -63
  36. data/test/integration/render_nested_include_test.rb +38 -83
  37. data/test/integration/serializers_test.rb +1 -1
  38. data/test/setup/data.rb +7 -3
  39. data/test/setup/serializers.rb +2 -0
  40. data/test/test_helper.rb +3 -9
  41. data/test/unit/field_list_test.rb +0 -10
  42. data/test/unit/include_list_test.rb +35 -5
  43. data/test/unit/symbol_refinement_test.rb +16 -0
  44. metadata +25 -89
  45. data/lib/simple_json_api/active_record_refinements.rb +0 -21
  46. data/lib/simple_json_api/array_refinements.rb +0 -14
  47. data/lib/simple_json_api/json_api_builder.rb +0 -84
@@ -20,14 +20,9 @@ Gem::Specification.new do |spec|
20
20
  spec.required_ruby_version = '~> 2.1'
21
21
 
22
22
  spec.add_dependency 'activerecord', '>= 4.0'
23
+ # spec.add_dependency 'activesupport', '>= 4.0'
24
+ # spec.add_dependency 'railties', '>= 4.0'
23
25
 
24
26
  spec.add_development_dependency 'bundler', '~> 1.7'
25
27
  spec.add_development_dependency 'rake', '~> 10.0'
26
- spec.add_development_dependency 'rdoc'
27
-
28
- spec.add_development_dependency 'awesome_print'
29
- spec.add_development_dependency 'diffy'
30
- spec.add_development_dependency 'minitest'
31
- spec.add_development_dependency 'minitest-reporters'
32
- spec.add_development_dependency 'sqlite3'
33
28
  end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+ require 'rails/generators'
3
+ require 'rails/generators/test_case'
4
+ require 'generators/simple_json_api/resource/resource_generator'
5
+
6
+ module SimpleJsonApi
7
+ module Generators
8
+ # Tests the generation of the resource template files
9
+ class ResourceGeneratorTest < Rails::Generators::TestCase
10
+ tests ResourceGenerator
11
+ destination 'tmp/generators'
12
+ setup :prepare_destination
13
+
14
+ def test_generator_runs
15
+ # No error raised? It passes.
16
+ run_generator %w(Project --namespace=Api::V3)
17
+ end
18
+
19
+ def test_bad_model
20
+ assert_raises(SystemExit) do
21
+ run_generator \
22
+ %w(Project --namespace=Api::V3 --model=ProjectSerializer)
23
+ end
24
+ end
25
+
26
+ def teardown
27
+ FileUtils.rm_rf('tmp/generators')
28
+ end
29
+ end
30
+ end
31
+ end
@@ -4,11 +4,11 @@ require 'test_helper'
4
4
  module SimpleJsonApi
5
5
  describe 'RenderBasicTest' do
6
6
  it 'should match json hash for a project with no options' do
7
- JSON.parse(actual_project).must_equal expected_project
7
+ actual_project.must_match_json expected_project
8
8
  end
9
9
 
10
10
  it 'should match json hash for a project array with no options' do
11
- JSON.parse(actual_projects).must_equal expected_projects
11
+ actual_projects.must_match_json expected_projects
12
12
  end
13
13
 
14
14
  let(:actual_project) do
@@ -19,7 +19,8 @@ module SimpleJsonApi
19
19
  end
20
20
  let(:expected_project) do
21
21
  {
22
- 'projects' => {
22
+ 'data' => {
23
+ 'type' => 'projects',
23
24
  'id' => '100',
24
25
  'name' => 'First Project',
25
26
  'description' => 'The first project',
@@ -27,7 +28,7 @@ module SimpleJsonApi
27
28
  'href' => 'http://example.com/projects/100',
28
29
  'links' => {
29
30
  'todolist' => '200',
30
- 'tags' => ['10']
31
+ 'tags' => %w(10)
31
32
  }
32
33
  }
33
34
  }
@@ -41,8 +42,9 @@ module SimpleJsonApi
41
42
  end
42
43
  let(:expected_projects) do
43
44
  {
44
- 'projects' => [
45
+ 'data' => [
45
46
  {
47
+ 'type' => 'projects',
46
48
  'id' => '100',
47
49
  'name' => 'First Project',
48
50
  'description' => 'The first project',
@@ -50,10 +52,11 @@ module SimpleJsonApi
50
52
  'href' => 'http://example.com/projects/100',
51
53
  'links' => {
52
54
  'todolist' => '200',
53
- 'tags' => ['10']
55
+ 'tags' => %w(10)
54
56
  }
55
57
  },
56
58
  {
59
+ 'type' => 'projects',
57
60
  'id' => '110',
58
61
  'name' => 'Second Project',
59
62
  'description' => 'The second project',
@@ -61,7 +64,7 @@ module SimpleJsonApi
61
64
  'href' => 'http://example.com/projects/110',
62
65
  'links' => {
63
66
  'todolist' => '210',
64
- 'tags' => ['20']
67
+ 'tags' => %w(20)
65
68
  }
66
69
  }
67
70
  ]
@@ -3,35 +3,30 @@ require 'test_helper'
3
3
  # SimpleJsonApi
4
4
  module SimpleJsonApi
5
5
  describe 'RenderFieldsTest' do
6
- it 'should match json hash for a project with fields specified' do
7
- JSON.parse(actual_project).must_equal expected_project
8
- end
9
-
10
6
  it 'should match json hash for a project with fields specified as hash' do
11
- JSON.parse(actual_project_field_hash).must_equal expected_project
7
+ actual_project_field_hash.must_match_json expected_project
12
8
  end
13
9
 
14
10
  it 'should match json hash for a project array with fields specified' do
15
- JSON.parse(actual_projects).must_equal expected_projects
11
+ actual_projects.must_match_json expected_projects
16
12
  end
17
13
 
18
- let(:actual_project) do
19
- SimpleJsonApi.render(
20
- model: Project.first,
21
- serializer: ProjectSerializer,
22
- options: { fields: 'name,description' }
23
- )
14
+ it 'should match hash for todo with default_fields specified' do
15
+ actual_todo.must_match_json expected_todo
24
16
  end
17
+
25
18
  let(:actual_project_field_hash) do
26
19
  SimpleJsonApi.render(
27
20
  model: Project.first,
28
21
  serializer: ProjectSerializer,
29
- options: { fields: { projects: 'name,description' } }
22
+ fields: { projects: 'name,description' }
30
23
  )
31
24
  end
32
25
  let(:expected_project) do
33
26
  {
34
- 'projects' => {
27
+ 'data' => {
28
+ 'type' => 'projects',
29
+ 'id' => '100',
35
30
  'name' => 'First Project',
36
31
  'description' => 'The first project',
37
32
  'href' => 'http://example.com/projects/100',
@@ -47,13 +42,15 @@ module SimpleJsonApi
47
42
  SimpleJsonApi.render(
48
43
  model: Project.all.to_a,
49
44
  serializer: ProjectSerializer,
50
- options: { fields: 'name,description' }
45
+ fields: { 'projects' => 'name,description' }
51
46
  )
52
47
  end
53
48
  let(:expected_projects) do
54
49
  {
55
- 'projects' => [
50
+ 'data' => [
56
51
  {
52
+ 'type' => 'projects',
53
+ 'id' => '100',
57
54
  'name' => 'First Project',
58
55
  'description' => 'The first project',
59
56
  'href' => 'http://example.com/projects/100',
@@ -63,6 +60,8 @@ module SimpleJsonApi
63
60
  }
64
61
  },
65
62
  {
63
+ 'type' => 'projects',
64
+ 'id' => '110',
66
65
  'name' => 'Second Project',
67
66
  'description' => 'The second project',
68
67
  'href' => 'http://example.com/projects/110',
@@ -74,5 +73,25 @@ module SimpleJsonApi
74
73
  ]
75
74
  }
76
75
  end
76
+
77
+ let(:actual_todo) do
78
+ SimpleJsonApi.render(
79
+ model: Todo.first,
80
+ serializer: TodoSerializer
81
+ )
82
+ end
83
+ let(:expected_todo) do
84
+ {
85
+ 'data' => {
86
+ 'type' => 'todos',
87
+ 'id' => '300',
88
+ 'action' => 'Milk',
89
+ 'href' => 'http://example.com/todos/300',
90
+ 'links' => {
91
+ 'tags' => %w(10 30)
92
+ }
93
+ }
94
+ }
95
+ end
77
96
  end
78
97
  end
@@ -4,23 +4,24 @@ require 'test_helper'
4
4
  module SimpleJsonApi
5
5
  describe 'RenderIncludeTest' do
6
6
  it 'should match json hash for a project with include specified' do
7
- JSON.parse(actual_project).must_equal expected_project
7
+ actual_project.must_match_json expected_project
8
8
  end
9
9
 
10
10
  it 'should match json hash for a project array with include specified' do
11
- JSON.parse(actual_projects).must_equal expected_projects
11
+ actual_projects.must_match_json expected_projects
12
12
  end
13
13
 
14
14
  let(:actual_project) do
15
15
  SimpleJsonApi.render(
16
16
  model: Project.first,
17
17
  serializer: ProjectSerializer,
18
- options: { include: 'todolists' }
18
+ include: 'todolists'
19
19
  )
20
20
  end
21
21
  let(:expected_project) do
22
22
  {
23
- 'projects' => {
23
+ 'data' => {
24
+ 'type' => 'projects',
24
25
  'id' => '100',
25
26
  'name' => 'First Project',
26
27
  'description' => 'The first project',
@@ -31,20 +32,18 @@ module SimpleJsonApi
31
32
  'tags' => ['10']
32
33
  }
33
34
  },
34
- 'linked' => {
35
- 'todolists' => [
36
- {
37
- 'id' => '200',
38
- 'description' => 'Groceries',
39
- 'href' => 'http://example.com/todolists/200',
40
- 'links' => {
41
- 'todos' => ['300'],
42
- 'tags' => ['30']
43
- }
35
+ 'included' => [
36
+ {
37
+ 'type' => 'todolists',
38
+ 'id' => '200',
39
+ 'description' => 'Groceries',
40
+ 'href' => 'http://example.com/todolists/200',
41
+ 'links' => {
42
+ 'todos' => %w(300 301),
43
+ 'tags' => ['30']
44
44
  }
45
- ]
46
- }
47
-
45
+ }
46
+ ]
48
47
  }
49
48
  end
50
49
 
@@ -52,13 +51,14 @@ module SimpleJsonApi
52
51
  SimpleJsonApi.render(
53
52
  model: Project.all.to_a,
54
53
  serializer: ProjectSerializer,
55
- options: { include: 'todolists,tags' }
54
+ include: 'todolists,tags'
56
55
  )
57
56
  end
58
57
  let(:expected_projects) do
59
58
  {
60
- 'projects' => [
59
+ 'data' => [
61
60
  {
61
+ 'type' => 'projects',
62
62
  'id' => '100',
63
63
  'name' => 'First Project',
64
64
  'description' => 'The first project',
@@ -70,6 +70,7 @@ module SimpleJsonApi
70
70
  }
71
71
  },
72
72
  {
73
+ 'type' => 'projects',
73
74
  'id' => '110',
74
75
  'name' => 'Second Project',
75
76
  'description' => 'The second project',
@@ -81,52 +82,54 @@ module SimpleJsonApi
81
82
  }
82
83
  }
83
84
  ],
84
- 'linked' => {
85
- 'todolists' => [
86
- {
87
- 'id' => '200',
88
- 'description' => 'Groceries',
89
- 'href' => 'http://example.com/todolists/200',
90
- 'links' => {
91
- 'todos' => ['300'],
92
- 'tags' => ['30']
93
- }
94
- },
95
- {
96
- 'id' => '210',
97
- 'description' => 'Groceries',
98
- 'description' => 'Work',
99
- 'href' => 'http://example.com/todolists/210',
100
- 'links' => {
101
- 'todos' => %w(310 320 330),
102
- 'tags' => []
103
- }
85
+ 'included' => [
86
+ {
87
+ 'type' => 'todolists',
88
+ 'id' => '200',
89
+ 'description' => 'Groceries',
90
+ 'href' => 'http://example.com/todolists/200',
91
+ 'links' => {
92
+ 'todos' => %w(300 301),
93
+ 'tags' => ['30']
94
+ }
95
+ },
96
+ {
97
+ 'type' => 'todolists',
98
+ 'id' => '210',
99
+ 'description' => 'Groceries',
100
+ 'description' => 'Work',
101
+ 'href' => 'http://example.com/todolists/210',
102
+ 'links' => {
103
+ 'todos' => %w(310 320 330),
104
+ 'tags' => []
104
105
  }
105
- ],
106
- 'tags' => [
107
- {
108
- 'guid' => '10',
109
- 'name' => 'Urgent!',
110
- 'links' => {
111
- 'taggables' => [
112
- %w(projects 100),
113
- %w(todos 300),
114
- %w(todos 330)
115
- ]
116
- }
117
- },
118
- {
119
- 'guid' => '20',
120
- 'name' => 'On Hold',
121
- 'links' => {
122
- 'taggables' => [
123
- %w(projects 110),
124
- %w(todos 300)
125
- ]
126
- }
106
+ },
107
+ {
108
+ 'type' => 'tags',
109
+ 'id' => '10',
110
+ 'guid' => '10',
111
+ 'name' => 'Urgent!',
112
+ 'links' => {
113
+ 'taggables' => [
114
+ %w(projects 100),
115
+ %w(todos 300),
116
+ %w(todos 301),
117
+ %w(todos 330)
118
+ ]
127
119
  }
128
- ]
129
- }
120
+ },
121
+ {
122
+ 'type' => 'tags',
123
+ 'id' => '20',
124
+ 'guid' => '20',
125
+ 'name' => 'On Hold',
126
+ 'links' => {
127
+ 'taggables' => [
128
+ %w(projects 110)
129
+ ]
130
+ }
131
+ }
132
+ ]
130
133
  }
131
134
  end
132
135
  end
@@ -4,26 +4,25 @@ require 'test_helper'
4
4
  module SimpleJsonApi
5
5
  describe 'RenderNestedIncludeTest' do
6
6
  it 'should match json hash for a project with nested includes specified' do
7
- compare_json(actual_project, expected_project.to_json)
8
- JSON.parse(actual_project).must_equal expected_project
7
+ actual_project.must_match_json expected_project
8
+ # ap '_' * 20
9
+ # ObjectSpace.each_object(ApiNode).each { |an| ap an.name }
9
10
  end
10
11
 
11
12
  let(:actual_project) do
12
13
  SimpleJsonApi.render(
13
14
  model: Project.first,
14
15
  serializer: ProjectSerializer,
15
- options: {
16
- include: 'todolists'\
16
+ include: 'todolists'\
17
17
  ',todolists.todos'\
18
- ',todolists.todos.tags'\
19
18
  ',todolists.todos.tags.taggables',
20
- fields: { projects: 'id', todolists: 'id', todos: 'id', tags: 'guid' }
21
- }
19
+ fields: { projects: 'id', todolists: 'id', todos: 'id', tags: 'guid' }
22
20
  )
23
21
  end
24
22
  let(:expected_project) do
25
23
  {
26
- 'projects' => {
24
+ 'data' => {
25
+ 'type' => 'projects',
27
26
  'id' => '100',
28
27
  'href' => 'http://example.com/projects/100',
29
28
  'links' => {
@@ -31,85 +30,41 @@ module SimpleJsonApi
31
30
  'tags' => ['10']
32
31
  }
33
32
  },
34
- 'linked' => {
35
- 'todolists' => [
36
- {
37
- 'id' => '200',
38
- 'href' => 'http://example.com/todolists/200',
39
- 'links' => {
40
- 'todos' => ['300'],
41
- 'tags' => ['30']
42
- }
33
+ 'included' => [
34
+ {
35
+ 'type' => 'todolists',
36
+ 'id' => '200',
37
+ 'href' => 'http://example.com/todolists/200',
38
+ 'links' => {
39
+ 'todos' => %w(300 301),
40
+ 'tags' => ['30']
43
41
  }
44
- ],
45
- 'todos' => [
46
- {
47
- 'id' => '300',
48
- 'href' => 'http://example.com/todos/300',
49
- 'links' => {
50
- 'tags' => %w(10 20)
51
- }
52
- },
53
- {
54
- 'id' => '330',
55
- 'href' => 'http://example.com/todos/330',
56
- 'links' => {
57
- 'tags' => ['10']
58
- }
59
- },
60
- # FIXME: duplicate!!!!!
61
- {
62
- 'id' => '300',
63
- 'href' => 'http://example.com/todos/300',
64
- 'links' => {
65
- 'tags' => %w(10 20)
66
- }
42
+ },
43
+ {
44
+ 'type' => 'todos',
45
+ 'id' => '300',
46
+ 'href' => 'http://example.com/todos/300',
47
+ 'links' => {
48
+ 'tags' => %w(10 30)
67
49
  }
68
- ],
69
- 'tags' => [
70
- {
71
- 'guid' => '10',
72
- 'links' => {
73
- 'taggables' => [
74
- %w(projects 100),
75
- %w(todos 300),
76
- %w(todos 330)
77
- ]
78
- }
79
- },
80
- {
81
- 'guid' => '20',
82
- 'links' => {
83
- 'taggables' => [
84
- %w(projects 110),
85
- %w(todos 300)
86
- ]
87
- }
50
+ },
51
+ {
52
+ 'type' => 'todos',
53
+ 'id' => '301',
54
+ 'href' => 'http://example.com/todos/301',
55
+ 'links' => {
56
+ 'tags' => %w(10)
88
57
  }
89
- ],
90
- 'projects' => [
91
- {
92
- 'id' => '100',
93
- 'href' => 'http://example.com/projects/100',
94
- 'links' => {
95
- 'todolist' => '200',
96
- 'tags' => [
97
- '10'
98
- ]
99
- }
100
- },
101
- {
102
- 'id' => '110',
103
- 'href' => 'http://example.com/projects/110',
104
- 'links' => {
105
- 'todolist' => '210',
106
- 'tags' => [
107
- '20'
108
- ]
109
- }
58
+ },
59
+ {
60
+ 'type' => 'todos',
61
+ 'id' => '330',
62
+ 'href' => 'http://example.com/todos/330',
63
+ 'links' => {
64
+ 'tags' => %w(10)
110
65
  }
111
- ]
112
- }
66
+ }
67
+ ]
113
68
  }
114
69
  end
115
70
  end