http_api_tools 0.3.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.
Files changed (53) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +3 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +479 -0
  7. data/Rakefile +4 -0
  8. data/http_api_tools.gemspec +29 -0
  9. data/lib/http_api_tools/base_json_serializer.rb +103 -0
  10. data/lib/http_api_tools/expanded_relation_includes.rb +77 -0
  11. data/lib/http_api_tools/identity_map.rb +42 -0
  12. data/lib/http_api_tools/json_serializer_dsl.rb +62 -0
  13. data/lib/http_api_tools/model/acts_like_active_model.rb +16 -0
  14. data/lib/http_api_tools/model/attributes.rb +159 -0
  15. data/lib/http_api_tools/model/has_many_array.rb +47 -0
  16. data/lib/http_api_tools/model/transformers/date_time_transformer.rb +31 -0
  17. data/lib/http_api_tools/model/transformers/registry.rb +55 -0
  18. data/lib/http_api_tools/model.rb +2 -0
  19. data/lib/http_api_tools/nesting/json_serializer.rb +45 -0
  20. data/lib/http_api_tools/nesting/relation_loader.rb +89 -0
  21. data/lib/http_api_tools/relation_includes.rb +146 -0
  22. data/lib/http_api_tools/serializer_registry.rb +27 -0
  23. data/lib/http_api_tools/sideloading/json_deserializer.rb +121 -0
  24. data/lib/http_api_tools/sideloading/json_deserializer_mapping.rb +27 -0
  25. data/lib/http_api_tools/sideloading/json_serializer.rb +125 -0
  26. data/lib/http_api_tools/sideloading/relation_sideloader.rb +79 -0
  27. data/lib/http_api_tools/sideloading/sideload_map.rb +54 -0
  28. data/lib/http_api_tools/type_key_resolver.rb +27 -0
  29. data/lib/http_api_tools/version.rb +3 -0
  30. data/lib/http_api_tools.rb +10 -0
  31. data/reports/empty.png +0 -0
  32. data/reports/minus.png +0 -0
  33. data/reports/plus.png +0 -0
  34. data/spec/http_api_tools/expanded_relation_includes_spec.rb +31 -0
  35. data/spec/http_api_tools/identity_map_spec.rb +31 -0
  36. data/spec/http_api_tools/model/attributes_spec.rb +170 -0
  37. data/spec/http_api_tools/model/has_many_array_spec.rb +48 -0
  38. data/spec/http_api_tools/model/transformers/date_time_transformer_spec.rb +36 -0
  39. data/spec/http_api_tools/model/transformers/registry_spec.rb +53 -0
  40. data/spec/http_api_tools/nesting/json_serializer_spec.rb +173 -0
  41. data/spec/http_api_tools/relation_includes_spec.rb +196 -0
  42. data/spec/http_api_tools/sideloading/json_deserializer_spec.rb +93 -0
  43. data/spec/http_api_tools/sideloading/json_serializer_performance_spec.rb +51 -0
  44. data/spec/http_api_tools/sideloading/json_serializer_spec.rb +174 -0
  45. data/spec/http_api_tools/sideloading/sideload_map_spec.rb +59 -0
  46. data/spec/http_api_tools/support/company_deserializer_mapping.rb +11 -0
  47. data/spec/http_api_tools/support/person_deserializer_mapping.rb +9 -0
  48. data/spec/http_api_tools/support/spec_models.rb +89 -0
  49. data/spec/http_api_tools/support/spec_nesting_serializers.rb +41 -0
  50. data/spec/http_api_tools/support/spec_sideloading_serializers.rb +41 -0
  51. data/spec/http_api_tools/type_key_resolver_spec.rb +19 -0
  52. data/spec/spec_helper.rb +8 -0
  53. metadata +214 -0
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'http_api_tools/sideloading/sideload_map'
3
+
4
+ module HttpApiTools
5
+ module Sideloading
6
+
7
+ describe SideloadMap do
8
+
9
+ let(:json) do
10
+ {
11
+ 'meta' => {
12
+ 'root_key' => 'posts'
13
+ },
14
+ 'posts' => [{
15
+ 'id' => 1,
16
+ 'title' => 'Post Title'
17
+ }],
18
+ 'linked' => {
19
+ 'images' => [
20
+ {'id' => 10, 'url' => '1.png' },
21
+ {'id' => 11, 'url' => '2.png' }
22
+ ],
23
+ 'comments' => [
24
+ {'id' => 20, 'text' => 'Comment 1'}
25
+ ]
26
+ }
27
+ }
28
+
29
+ end
30
+
31
+ let(:sideload_map) { SideloadMap.new(json, 'posts') }
32
+
33
+ describe 'getting sideloaded json from the map' do
34
+
35
+ it 'returns object at root key' do
36
+ expect(sideload_map.get('post', 1)['id']).to eql 1
37
+ end
38
+
39
+ it 'retrieves by singular type' do
40
+ expect(sideload_map.get('image', 10)['id']).to eql 10
41
+ end
42
+
43
+ it 'retrieves by plural type' do
44
+ expect(sideload_map.get('images', 11)['id']).to eql 11
45
+ end
46
+
47
+ it 'retrieves with string type' do
48
+ expect(sideload_map.get('comment', 20)['id']).to eql 20
49
+ end
50
+
51
+ it 'returns nil if object not present' do
52
+ expect(sideload_map.get('foo', 100)).to be_nil
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ require 'http_api_tools/sideloading/json_deserializer_mapping'
4
+
5
+ class CompanyDeserializerMapping
6
+
7
+ include HttpApiTools::Sideloading::JsonDeserializerMapping
8
+
9
+ map :employees, Person
10
+
11
+ end
@@ -0,0 +1,9 @@
1
+ require 'http_api_tools/sideloading/json_deserializer_mapping'
2
+
3
+ class PersonDeserializerMapping
4
+
5
+ include HttpApiTools::Sideloading::JsonDeserializerMapping
6
+
7
+ map :employer, Company
8
+
9
+ end
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+
3
+ require 'http_api_tools/model'
4
+ require 'ostruct'
5
+
6
+
7
+ class Person
8
+
9
+ include HttpApiTools::Model::Attributes
10
+
11
+ attribute :id
12
+ attribute :first_name
13
+ attribute :last_name
14
+ attribute :dob
15
+ attribute :email
16
+ belongs_to :employer
17
+ has_many :skills
18
+
19
+ def employer_id
20
+ employer.try(:id)
21
+ end
22
+
23
+ #Act like active record for reflectively interogating type info
24
+ def self.reflections
25
+ {
26
+ employer: OpenStruct.new(class_name: 'Company'),
27
+ skills: OpenStruct.new(class_name: 'Skill')
28
+ }
29
+ end
30
+
31
+ end
32
+
33
+ class Company
34
+
35
+ include HttpApiTools::Model::Attributes
36
+
37
+ attribute :id
38
+ attribute :name
39
+ attribute :brand, read_only: true
40
+ has_many :employees
41
+ has_many :suppliers
42
+ belongs_to :parent_company
43
+ belongs_to :address
44
+
45
+
46
+ #Act like active record for reflectively interogating type info
47
+ def self.reflections
48
+ {
49
+ employees: OpenStruct.new(class_name: 'Person'),
50
+ suppliers: OpenStruct.new(class_name: 'Company'),
51
+ parent_company: OpenStruct.new(class_name: 'Company'),
52
+ address: OpenStruct.new(class_name: 'Address')
53
+ }
54
+ end
55
+
56
+ end
57
+
58
+ class Skill
59
+
60
+ include HttpApiTools::Model::Attributes
61
+
62
+ attribute :id
63
+ attribute :name
64
+ attribute :description
65
+ belongs_to :person
66
+
67
+ def person_id
68
+ person.try(:id)
69
+ end
70
+
71
+ #Act like active record for reflectively interogating type info
72
+ def self.reflections
73
+ {
74
+ person: OpenStruct.new(class_name: 'Person')
75
+ }
76
+ end
77
+
78
+ end
79
+
80
+ class Address
81
+
82
+ attr_accessor :id, :street_address
83
+
84
+ def initialize(attrs)
85
+ @id = attrs[:id]
86
+ @street_address = attrs[:street_address]
87
+ end
88
+
89
+ end
@@ -0,0 +1,41 @@
1
+ require 'http_api_tools/nesting/json_serializer'
2
+
3
+ module HttpApiTools
4
+ module Nesting
5
+ class PersonSerializer
6
+
7
+ include HttpApiTools::Nesting::JsonSerializer
8
+
9
+ serializes Person
10
+ attributes :id, :first_name, :last_name, :full_name, :dob, :email
11
+ has_one :employer
12
+ has_many :skills
13
+
14
+ def full_name
15
+ "#{serializable.first_name} #{serializable.last_name}"
16
+ end
17
+
18
+ end
19
+
20
+ class CompanySerializer
21
+
22
+ include HttpApiTools::Nesting::JsonSerializer
23
+
24
+ serializes Company
25
+ attributes :id, :name
26
+ has_many :employees
27
+
28
+ end
29
+
30
+
31
+ class SkillSerializer
32
+
33
+ include HttpApiTools::Nesting::JsonSerializer
34
+
35
+ serializes Skill
36
+ attributes :id, :name, :description
37
+ has_one :person
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ require 'http_api_tools/sideloading/json_serializer'
2
+
3
+ module HttpApiTools
4
+ module Sideloading
5
+ class PersonSerializer
6
+
7
+ include HttpApiTools::Sideloading::JsonSerializer
8
+
9
+ serializes Person
10
+ attributes :id, :first_name, :last_name, :full_name, :dob, :email
11
+ has_one :employer
12
+ has_many :skills
13
+
14
+ def full_name
15
+ "#{serializable.first_name} #{serializable.last_name}"
16
+ end
17
+
18
+ end
19
+
20
+ class CompanySerializer
21
+
22
+ include HttpApiTools::Sideloading::JsonSerializer
23
+
24
+ serializes Company
25
+ attributes :id, :name
26
+ has_many :employees
27
+
28
+ end
29
+
30
+
31
+ class SkillSerializer
32
+
33
+ include HttpApiTools::Sideloading::JsonSerializer
34
+
35
+ serializes Skill
36
+ attributes :id, :name, :description
37
+ has_one :person
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'http_api_tools/type_key_resolver'
3
+
4
+ module HttpApiTools
5
+
6
+ describe TypeKeyResolver do
7
+
8
+ let(:resolver) { TypeKeyResolver.new }
9
+
10
+ describe 'resolving class names' do
11
+
12
+ it 'correctly resolves the type key multiple times' do
13
+ expect(resolver.for_class(String)).to eq 'strings'
14
+ expect(resolver.for_class(String)).to eq 'strings'
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ require 'http_api_tools/support/spec_models'
4
+ require 'http_api_tools/support/spec_sideloading_serializers'
5
+ require 'http_api_tools/support/spec_nesting_serializers'
6
+ require 'active_support/core_ext/hash/indifferent_access'
7
+ require 'rubygems'
8
+ require 'pry'
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_api_tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Monie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-debugger
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: ruby-prof
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Http API Tools
112
+ email:
113
+ - robmonie@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .rspec
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - http_api_tools.gemspec
125
+ - lib/http_api_tools.rb
126
+ - lib/http_api_tools/base_json_serializer.rb
127
+ - lib/http_api_tools/expanded_relation_includes.rb
128
+ - lib/http_api_tools/identity_map.rb
129
+ - lib/http_api_tools/json_serializer_dsl.rb
130
+ - lib/http_api_tools/model.rb
131
+ - lib/http_api_tools/model/acts_like_active_model.rb
132
+ - lib/http_api_tools/model/attributes.rb
133
+ - lib/http_api_tools/model/has_many_array.rb
134
+ - lib/http_api_tools/model/transformers/date_time_transformer.rb
135
+ - lib/http_api_tools/model/transformers/registry.rb
136
+ - lib/http_api_tools/nesting/json_serializer.rb
137
+ - lib/http_api_tools/nesting/relation_loader.rb
138
+ - lib/http_api_tools/relation_includes.rb
139
+ - lib/http_api_tools/serializer_registry.rb
140
+ - lib/http_api_tools/sideloading/json_deserializer.rb
141
+ - lib/http_api_tools/sideloading/json_deserializer_mapping.rb
142
+ - lib/http_api_tools/sideloading/json_serializer.rb
143
+ - lib/http_api_tools/sideloading/relation_sideloader.rb
144
+ - lib/http_api_tools/sideloading/sideload_map.rb
145
+ - lib/http_api_tools/type_key_resolver.rb
146
+ - lib/http_api_tools/version.rb
147
+ - reports/empty.png
148
+ - reports/minus.png
149
+ - reports/plus.png
150
+ - spec/http_api_tools/expanded_relation_includes_spec.rb
151
+ - spec/http_api_tools/identity_map_spec.rb
152
+ - spec/http_api_tools/model/attributes_spec.rb
153
+ - spec/http_api_tools/model/has_many_array_spec.rb
154
+ - spec/http_api_tools/model/transformers/date_time_transformer_spec.rb
155
+ - spec/http_api_tools/model/transformers/registry_spec.rb
156
+ - spec/http_api_tools/nesting/json_serializer_spec.rb
157
+ - spec/http_api_tools/relation_includes_spec.rb
158
+ - spec/http_api_tools/sideloading/json_deserializer_spec.rb
159
+ - spec/http_api_tools/sideloading/json_serializer_performance_spec.rb
160
+ - spec/http_api_tools/sideloading/json_serializer_spec.rb
161
+ - spec/http_api_tools/sideloading/sideload_map_spec.rb
162
+ - spec/http_api_tools/support/company_deserializer_mapping.rb
163
+ - spec/http_api_tools/support/person_deserializer_mapping.rb
164
+ - spec/http_api_tools/support/spec_models.rb
165
+ - spec/http_api_tools/support/spec_nesting_serializers.rb
166
+ - spec/http_api_tools/support/spec_sideloading_serializers.rb
167
+ - spec/http_api_tools/type_key_resolver_spec.rb
168
+ - spec/spec_helper.rb
169
+ homepage: ''
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ! '>='
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubyforge_project:
189
+ rubygems_version: 2.2.2
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: Provides JSON serialization/deserialization and basic model attribute definition
193
+ for client apps
194
+ test_files:
195
+ - spec/http_api_tools/expanded_relation_includes_spec.rb
196
+ - spec/http_api_tools/identity_map_spec.rb
197
+ - spec/http_api_tools/model/attributes_spec.rb
198
+ - spec/http_api_tools/model/has_many_array_spec.rb
199
+ - spec/http_api_tools/model/transformers/date_time_transformer_spec.rb
200
+ - spec/http_api_tools/model/transformers/registry_spec.rb
201
+ - spec/http_api_tools/nesting/json_serializer_spec.rb
202
+ - spec/http_api_tools/relation_includes_spec.rb
203
+ - spec/http_api_tools/sideloading/json_deserializer_spec.rb
204
+ - spec/http_api_tools/sideloading/json_serializer_performance_spec.rb
205
+ - spec/http_api_tools/sideloading/json_serializer_spec.rb
206
+ - spec/http_api_tools/sideloading/sideload_map_spec.rb
207
+ - spec/http_api_tools/support/company_deserializer_mapping.rb
208
+ - spec/http_api_tools/support/person_deserializer_mapping.rb
209
+ - spec/http_api_tools/support/spec_models.rb
210
+ - spec/http_api_tools/support/spec_nesting_serializers.rb
211
+ - spec/http_api_tools/support/spec_sideloading_serializers.rb
212
+ - spec/http_api_tools/type_key_resolver_spec.rb
213
+ - spec/spec_helper.rb
214
+ has_rdoc: