flexirest 1.11.1 → 1.11.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '036069384b97e0fbcf4e73c9e75154569cffbb9cbfcb8dfcda4ccfc4ef75cca2'
4
- data.tar.gz: 3099f07f5284601a68ebba405a60e485975635b5306b72f2ead2090509bdb901
3
+ metadata.gz: '069a842d7b7b6db2e2aa7cff28191a02d70e0ebe3bc6f49aadf569b8006b7cc4'
4
+ data.tar.gz: 9db8212da8ed8698bc4a1d48137fe2e0e9ce8d75c94366d72fe1f78d986855e0
5
5
  SHA512:
6
- metadata.gz: 18d64a00ceea9f1a9ee61dcdaedf64dbcc8eae7b9f4f3d20943e7b9209dd7d98acd08138a9c149d0f7cb7aa03740131030580c45f4d153e2f47df999e6415a61
7
- data.tar.gz: 026a430bdc41b68adc6e46c42dc69983fca9ef431fb3cc068c96fb3714c687209393d15e66879bb46c3f26ba3f9e38dd08b94620a7d8b2f20d392f08690031fd
6
+ metadata.gz: fb73957659a29bb35ab8f08b9127e32f36e733e9a37830c09e42f221774531c18edbbb4767bccd1fcc4713a938fb9c195daa4863af39c128b312d582b6245fc2
7
+ data.tar.gz: b9e91c14da366548a6a618eae2d634ad26cf60e1ed20d957b577bb4e1e777c32eea594ebc2a37990f61eeb59c41e34912ed591477f0a80005bfc7435dac7aa3d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.11.2
4
+
5
+ Bugfix:
6
+
7
+ - 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)
8
+
3
9
  ## 1.11.1
4
10
 
5
11
  Enhancement:
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
- When mapping the method, passing a list of attributes will cause any requests for those attributes to mapped to the URLs given in their responses. The response for the attribute may be one of the following:
89
+ If the call for an attribute should
90
90
 
91
- ```ruby
92
- "attribute" : "URL"
93
- "attribute" : ["URL", "URL"]
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
- It is required that the URL is a complete URL including a protocol starting with "http". To configure this use code like:
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 => [:orders, :refunds]
103
+ get :find, "/people/:id", :lazy => { books: Book }
106
104
  end
107
105
  ```
108
106
 
109
- And use it like this:
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
- # Makes a call to the first URL found in the "books":[...] array in the article response
116
- # only makes the HTTP request when first used though
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.
@@ -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
- overridden_name = name
716
+ parent_name = name
717
717
  object = @method[:options][:has_many][name].new
718
718
  elsif @method[:options][:has_one][name]
719
- overridden_name = name
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
- overridden_name = select_name(k, overridden_name)
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
 
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.11.1"
2
+ VERSION = "1.11.2"
3
3
  end
@@ -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.1
4
+ version: 1.11.2
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-06-15 00:00:00.000000000 Z
11
+ date: 2023-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler