http-api-tools 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTExZDgwZjc0ZDQ3M2NiYjlhZDI2NGZlNWIxMWRiOGU2ZGI1MTlkNA==
4
+ YTBjYWE0MmIwNjVlOTJlNGJkOWJmZjJlMzViNGI5YjBlMDQ0YWM4Yg==
5
5
  data.tar.gz: !binary |-
6
- YWU4ZTczYzliMjQwZjhjYWJhMDI0MTEzODlmZTIxM2I2ODhlNmVhYQ==
6
+ YzE2M2U4MGZjMzUzNGViOTE4YjAwYjdmN2FhZDgyZjU3YThkMjBjOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzYxODgxNWEyY2YxYmY5MDVhOTdkNTgxNTdkZjUyNmIyOTM2YWJkOGJkNjRk
10
- YTJiNTYwMzczM2U3NzNmZjczNzU1Y2E5MzczZDk2MDU3ODI5NGE4N2RkNWJi
11
- YzdjOWI1YjdjYWViMWJjYzFiNTU4MzA0Nzg5MTQxMGRmODk2YmU=
9
+ MWY0NGQ4NDQ5NzdiMTExZmM4NzYwNTgxZWY2ZGFiMDk5MmJhNzZlNGZiNjA2
10
+ NjA3MGMwY2QyODQ3MTIzNDAwOTQxYjNhMTg3MTNmMDk1MzcwYTk4Njg2ZGE1
11
+ NGQwY2Y1NmM2ZmQyYjgzNzBlMjdlMjYwZWNiNTA0ZDk3YzJlZmM=
12
12
  data.tar.gz: !binary |-
13
- ZDUyNmQ1YjYxYjI1YTgzN2E0ZjA3ZGI5ZjEzN2VkNTljODM4OTZlODE4M2Jj
14
- MmM5MWUzM2EwZjk0ODM1YTU1MzlkMDEwYTQwNWZmNjZlZmUzZmVlMzQ0Nzc1
15
- MmYwNDc1MDNmZDI4NTMxZmM3OTY1ZjRmZmRhZTQwOGVmYTJiNmU=
13
+ MjRmNTgxNmFjYzEyYzcyNDhlOTZkMTg0OTU1MTcyZDE4ZGFlYzE0YzhjYTc3
14
+ MWUzODkyZjFlNmNmOWEwNmM3MGQ5NzQzZjNlOTlhZWZkODkxZWIxMDBiMTMz
15
+ NWQ2NmU5ZDM2MjZkZDY4ZTc1NWJkYzU2NDBmMWZhNGI3ODM1YTM=
data/README.md CHANGED
@@ -244,15 +244,17 @@ and splat into the serializer includes:
244
244
 
245
245
  and/or active record queries:
246
246
 
247
- `User.find(params[:id]).includes(*user_serializer.includes_for_query)`
247
+ `User.find(params[:id]).includes(*relation_includes.for_query(UserSerializer))`
248
248
 
249
249
  When providing the includes for an active record query, we actually want a deeper set of includes in order to account for the ids fetched for has_many relationships. If we passed the same set of includes to the query as we pass to the serializer, we'd end up with n+1 queries when fetching the ids for the has_many relationships.
250
250
 
251
- Calling `user_serializer.includes_for_query` will figure out the minimum set of includes that are required based on the following:
251
+ Calling `relation_includes.for_query(UserSerializer)` will figure out the minimum set of includes that are required based on the following:
252
252
 
253
253
  * The models and their relationships
254
254
  * The relationships actually being serialized
255
255
 
256
+ **** Note that this particular API is pretty rough at the moment and likely to change once we find a nicer way of describing this feature.
257
+
256
258
  ##### Restricting what is included
257
259
  Once you expose what can be included as a query string parameter you risk exposing too much information or poorly considered api calls that fetch too much. This can be countered by defining what is `includable` for each serializer when it's being used as the root serializer for a json response.
258
260
 
data/lib/hat.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "hat/version"
2
+ require 'hat/nesting/json_serializer'
2
3
  require 'hat/sideloading/json_serializer'
3
4
  require 'hat/sideloading/json_deserializer'
4
5
  require 'hat/model'
@@ -74,10 +74,6 @@ module Hat
74
74
  self.class._includable
75
75
  end
76
76
 
77
- def includes_for_query
78
- RelationIncludes.new(*ExpandedRelationIncludes.new(relation_includes, self))
79
- end
80
-
81
77
  protected
82
78
 
83
79
  attr_accessor :identity_map
@@ -3,22 +3,22 @@
3
3
  module Hat
4
4
  class ExpandedRelationIncludes
5
5
 
6
- def initialize(relation_includes, serializer)
6
+ def initialize(relation_includes, serializer_class)
7
7
  @relation_includes = relation_includes
8
- @serializer = serializer
8
+ @serializer_class = serializer_class
9
9
  end
10
10
 
11
11
  def to_a
12
12
  @expanded_includes ||= begin
13
13
  expanded_includes = []
14
- expand_includes(serializer.class, relation_includes, expanded_includes)
14
+ expand_includes(serializer_class, relation_includes, expanded_includes)
15
15
  expanded_includes
16
16
  end
17
17
  end
18
18
 
19
19
  private
20
20
 
21
- attr_reader :serializer, :relation_includes
21
+ attr_reader :serializer_class, :relation_includes
22
22
 
23
23
  def expand_includes(target_serializer_class, base_includes, expanded_includes)
24
24
 
@@ -77,6 +77,12 @@ module Hat
77
77
  end
78
78
  end
79
79
 
80
+ #Return an expanded version of the includes for use in a query.
81
+ #This api is still pretty rough and likely to change
82
+ def for_query(serializer_class)
83
+ RelationIncludes.new(*ExpandedRelationIncludes.new(self, serializer_class))
84
+ end
85
+
80
86
  private
81
87
 
82
88
  attr_accessor :includes
@@ -1,3 +1,3 @@
1
1
  module Hat
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -6,8 +6,7 @@ module Hat
6
6
 
7
7
  describe "#to_a" do
8
8
 
9
- let(:serializer) { Hat::Sideloading::PersonSerializer.new(Person.new) }
10
- let(:expanded_includes) { ExpandedRelationIncludes.new(includes, serializer) }
9
+ let(:expanded_includes) { ExpandedRelationIncludes.new(includes, Hat::Sideloading::PersonSerializer) }
11
10
 
12
11
  context 'with single-level includes' do
13
12
 
@@ -181,5 +181,16 @@ module Hat
181
181
  end
182
182
  end
183
183
 
184
+ describe "#for_query" do
185
+
186
+ let(:includes) { RelationIncludes.new(:employer, { skills: [:person] }).for_query(Hat::Sideloading::PersonSerializer) }
187
+
188
+ it "creates includes for included relationships and has_many relationships for fetching ids" do
189
+ expect(includes.find(:employer)).to eq({ employer: [:employees] })
190
+ expect(includes.find(:skills)).to eq({ skills: [{ person: [:skills] }] })
191
+ end
192
+
193
+ end
194
+
184
195
  end
185
196
  end
@@ -78,17 +78,6 @@ module Hat
78
78
  expect(serialized[:linked][:skills].first[:name]).to eql person.skills.first.name
79
79
  end
80
80
 
81
- describe "#includes_for_query" do
82
-
83
- let(:includes_for_query) { serializer.includes_for_query }
84
-
85
- it "expands includes for included relationships and has_many relationships for fetching ids" do
86
- expect(includes_for_query.find(:employer)).to eq({ employer: [:employees] })
87
- expect(includes_for_query.find(:skills)).to eq({ skills: [{ person: [:skills] }] })
88
- end
89
-
90
- end
91
-
92
81
  end
93
82
 
94
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-api-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Monie