flexirest 1.11.1 → 1.11.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -1
- data/docs/associations.md +34 -15
- data/lib/flexirest/request.rb +4 -4
- data/lib/flexirest/result_iterator.rb +2 -2
- data/lib/flexirest/version.rb +1 -1
- data/spec/lib/lazy_association_loader_spec.rb +27 -0
- data/spec/lib/result_iterator_spec.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abc3590ed1c31e53df7d8be038fad0a01f66d4d50dd3effbb17f0ed41256e6fa
|
4
|
+
data.tar.gz: 6049f2be6ecb5c921b4c0d10c1e4e3533dc50adaf98d153bae690e1d01413ecb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 469d6426f9de8945cc94af7c26e06a88e6b7b570e3c0b72487dd4d940518d54eba224ed522efe8abc12f822d80350b89658ac0cc4d7b7fcdc64c581075f44644
|
7
|
+
data.tar.gz: d6d241da12c0049113e692693a4d98b7cbaa45f7ad06f5733a21eb7c516507e6ae9d513620feca19372f8039a2a9c3019bfb98005b8670a1920a860a363a181d
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.11.3
|
4
|
+
|
5
|
+
Bugfix:
|
6
|
+
|
7
|
+
- Forward all Flexirest::ResultIterator#index arguments to allow for code like `result.index { |i| i == "z" }` (thanks to Stevo-S for the PR)
|
8
|
+
|
9
|
+
## 1.11.2
|
10
|
+
|
11
|
+
Bugfix:
|
12
|
+
|
13
|
+
- When a model had multiple lazy loaders specified, they would all have their object class set to the first object class that was found when iterating over them. (thanks to Joshua Samberg for the PR)
|
14
|
+
|
3
15
|
## 1.11.1
|
4
16
|
|
5
17
|
Enhancement:
|
@@ -224,7 +236,7 @@ Features:
|
|
224
236
|
|
225
237
|
Fix:
|
226
238
|
|
227
|
-
- plain requests to URLs that are just the domain e.g. "https://www.example.com" should also work (i.e. without any trailing path, even a '/')
|
239
|
+
- plain requests to URLs that are just the domain e.g. "<https://www.example.com>" should also work (i.e. without any trailing path, even a '/')
|
228
240
|
|
229
241
|
## 1.7.7
|
230
242
|
|
data/docs/associations.md
CHANGED
@@ -86,37 +86,56 @@ When the `:array` option includes an attribute, it is assumed the values were re
|
|
86
86
|
|
87
87
|
## Type 2 - Lazy Loading From Other URLs
|
88
88
|
|
89
|
-
|
89
|
+
If the call for an attribute should
|
90
90
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
"attribute" : { "url" : "URL"}
|
95
|
-
"attribute" : { "href" : "URL"}
|
96
|
-
"attribute" : { "something" : "URL"}
|
97
|
-
```
|
98
|
-
|
99
|
-
The difference between the last 3 examples is that a key of `url` or `href` signifies it's a single object that is lazy loaded from the value specified. Any other keys assume that it's a nested set of URLs (like in the array situation, but accessible via the keys - e.g. object.attribute.something in the above example).
|
91
|
+
+ Use the value held within the specified attribute as a URL(s) from which to load the associated resource(s)
|
92
|
+
+ **THEN**, create an instance of your API object from the result
|
93
|
+
+ **THEN**, call subsequent chained methods on that instance
|
100
94
|
|
101
|
-
|
95
|
+
you can specify this when mapping the method by passing attribtues to the `:lazy` option.
|
102
96
|
|
103
97
|
```ruby
|
98
|
+
class Book < Flexirest::Base
|
99
|
+
get :find, "/books/:name"
|
100
|
+
end
|
101
|
+
|
104
102
|
class Person < Flexirest::Base
|
105
|
-
get :find, "/people/:id", :lazy =>
|
103
|
+
get :find, "/people/:id", :lazy => { books: Book }
|
106
104
|
end
|
107
105
|
```
|
108
106
|
|
109
|
-
|
107
|
+
Use it like this:
|
110
108
|
|
111
109
|
```ruby
|
112
110
|
# Makes a call to /people/1
|
113
111
|
@person = Person.find(1)
|
114
112
|
|
115
|
-
#
|
116
|
-
#
|
113
|
+
# Lazily makes a call to the first URL found in the "books":[...] array in the Person.find response
|
114
|
+
# - Only makes the HTTP request when first used
|
115
|
+
# - Instantiates a Book object from the response and then accesses its "name" property
|
117
116
|
@person.books.first.name
|
118
117
|
```
|
119
118
|
|
119
|
+
### URLs in API responses
|
120
|
+
To provide a URL(s) for lazy loading an attribute, the API response may contain one of the following for the attribute (**`book`** or **`books`** is the attribute in all examples below):
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
# all of the following will lazy load a single object from the specified URL
|
124
|
+
"book" : "https://example.com/books/1"
|
125
|
+
# or
|
126
|
+
"book" : { "url" : "https://example.com/books/1"}
|
127
|
+
# or
|
128
|
+
"book" : { "href" : "https://example.com/books/1"}
|
129
|
+
|
130
|
+
# books will be an array whose elements will be lazy loaded one-by-one, from the URL in the corresponding array position, whenever each element is first accessed
|
131
|
+
"books" : ["https://example.com/books/1", "https://example.com/books/2"]
|
132
|
+
|
133
|
+
# book will be an object where the values will be lazy loaded one-by-one, from the URL in the corresponding key, whenever each key is first accessed (e.g. the first time object.book.author is accessed)
|
134
|
+
"book" : { "author" : "https://example.com/author/1"}
|
135
|
+
```
|
136
|
+
|
137
|
+
It is required that each URL is a complete URL including a protocol starting with `http`.
|
138
|
+
|
120
139
|
## Type 3 - HAL Auto-loaded Resources
|
121
140
|
|
122
141
|
You don't need to define lazy attributes if they are defined using [HAL](http://stateless.co/hal_specification.html) (with an optional embedded representation). If your resource has an `_links` item (and optionally an `_embedded` item) then it will automatically treat the linked resources (with the `_embedded` cache) as if they were defined using `:lazy` as per type 2 above.
|
data/lib/flexirest/request.rb
CHANGED
@@ -713,12 +713,13 @@ module Flexirest
|
|
713
713
|
@method[:options][:has_many] ||= {}
|
714
714
|
name = name.to_sym rescue nil
|
715
715
|
if @method[:options][:has_many][name]
|
716
|
-
|
716
|
+
parent_name = name
|
717
717
|
object = @method[:options][:has_many][name].new
|
718
718
|
elsif @method[:options][:has_one][name]
|
719
|
-
|
719
|
+
parent_name = name
|
720
720
|
object = @method[:options][:has_one][name].new
|
721
721
|
else
|
722
|
+
parent_name = nil
|
722
723
|
object = create_object_instance
|
723
724
|
end
|
724
725
|
|
@@ -735,8 +736,7 @@ module Flexirest
|
|
735
736
|
else
|
736
737
|
k = k.to_sym
|
737
738
|
end
|
738
|
-
|
739
|
-
set_corresponding_value(v, k, object, overridden_name)
|
739
|
+
set_corresponding_value(v, k, object, select_name(k, parent_name))
|
740
740
|
end
|
741
741
|
object.clean! unless object_is_class?
|
742
742
|
|
data/lib/flexirest/version.rb
CHANGED
@@ -12,6 +12,24 @@ class YearExample < Flexirest::Base
|
|
12
12
|
get :find, "/year/:id", lazy: { months: MonthExample }, fake: "{\"months\": [\"http://www.example.com/months/1\"] }"
|
13
13
|
end
|
14
14
|
|
15
|
+
class CenturyExample < Flexirest::Base
|
16
|
+
base_url "http://www.example.com"
|
17
|
+
|
18
|
+
get :find, "century/:id"
|
19
|
+
end
|
20
|
+
|
21
|
+
class DecadeExample < Flexirest::Base
|
22
|
+
base_url "http://www.example.com"
|
23
|
+
|
24
|
+
get :find, "/decade/:id", lazy: { years: YearExample, century: CenturyExample }, fake: %Q{
|
25
|
+
{
|
26
|
+
"years": ["http://www.example.com/years/1"],
|
27
|
+
"century": "http://www.example.com/century/1"
|
28
|
+
}
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
|
15
33
|
describe Flexirest::LazyAssociationLoader do
|
16
34
|
let(:url1) { "http://www.example.com/some/url" }
|
17
35
|
let(:url2) { "http://www.example.com/some/other" }
|
@@ -132,4 +150,13 @@ describe Flexirest::LazyAssociationLoader do
|
|
132
150
|
association = YearExample.find(1)
|
133
151
|
expect(association.months.instance_variable_get('@request').instance_variable_get('@object').class).to eq(MonthExample)
|
134
152
|
end
|
153
|
+
|
154
|
+
context "has multiple associations" do
|
155
|
+
it "should correctly map each association name to the class specified for that association in the 'lazy' declaration " do
|
156
|
+
association = DecadeExample.find(1)
|
157
|
+
expect(association.years.instance_variable_get('@request').instance_variable_get('@object').class).to eq(YearExample)
|
158
|
+
expect(association.century.instance_variable_get('@request').instance_variable_get('@object').class).to eq(CenturyExample)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
135
162
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flexirest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.11.
|
4
|
+
version: 1.11.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Jeffries
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|