flexirest 1.9.14 → 1.10.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e675234b5624a923906ecea0ed2f1cdf47230e2bef5f35fbde9731da4bdae062
4
- data.tar.gz: '0237757812519f5b82eb311608b00414cdef1d935feecb462c330e998f960158'
3
+ metadata.gz: 9f713d7d9f8f37de42e54bd20623b4477b9893b6cd40f0d2d5e8c5a06079a821
4
+ data.tar.gz: b5fc1fe7bffbe57a6adf9443aab86d91313fddd90b2fcc2ef35f50f2b41ec942
5
5
  SHA512:
6
- metadata.gz: 9ca1b058118cd12b4ba659332acb5b8cde5e476c0f786b46d803f385c8b59f8661bf65352acca1c1f741dfa1872a510244f704815bb87fb92492c8618d64c027
7
- data.tar.gz: 7ae4402501167aa8a40c02bf446f5267106d9a6b5e50a9e89bfb68798e8ff5b4f20fe2acc9bd9f80cd2a5372a49f9db388fd94bfe90af516a1643c1f97d55749
6
+ metadata.gz: eedfb75a0ecacbb1114d62fad201cf5e478242dd74126b87b29f1081e9c56b4194b600c6d729ac8afa70a7dd4ee8d66a03057974a2e52ee76f575e848027bfca
7
+ data.tar.gz: 955f229836e2e6fbc6273b1505294619ab0a614af05e79d0e5113b956959dbf22bb76ed23b2a169a9dafe207b6ab48b3559f7adeb84f22e3fcff01fbc223b28f
@@ -1,5 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.7.0
3
4
  - 2.6.0
4
5
  - 2.5.0
5
- - 2.4.0
@@ -1,5 +1,35 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.10.0
4
+
5
+ Enhancement:
6
+
7
+ - Add specific exceptions for the most common 5xx server-side errors
8
+
9
+ ## 1.9.18
10
+
11
+ Security:
12
+
13
+ - Upgrade rest-client development dependency to a CVE-fixed version.
14
+
15
+ ## 1.9.17
16
+
17
+ Feature:
18
+
19
+ - Methods can be specified as a symbol for generating fake data (thanks to Niall Duggan for the feature request).
20
+
21
+ ## 1.9.16
22
+
23
+ Bugfix:
24
+
25
+ - Cached responses were always returning as `dirty?`/`changed?` (thanks to AKRathore for the bug report).
26
+
27
+ ## 1.9.15
28
+
29
+ Bugfix:
30
+
31
+ - Fix not marking unchanged attributes as dirty (thanks to Khairi Adnan for the bug report).
32
+
3
33
  ## 1.9.14
4
34
 
5
35
  Bugfix:
@@ -16,6 +16,17 @@ class Person < Flexirest::Base
16
16
  end
17
17
  ```
18
18
 
19
+ Alternatively, you can specify a symbol as the `fake` parameter, and Flexirest will call that method to get a string JSON as if it was the request body:
20
+
21
+ ```ruby
22
+ class Person < Flexirest::Base
23
+ get :all, '/people', fake: :get_data
24
+
25
+ def get_data
26
+ {result: true}.to_json
27
+ end
28
+ end
29
+ ```
19
30
 
20
31
  -----
21
32
 
@@ -36,9 +36,10 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency "simplecov"
37
37
  spec.add_development_dependency "simplecov-rcov"
38
38
  spec.add_development_dependency 'coveralls'
39
- spec.add_development_dependency "api-auth", ">= 1.3.1"
39
+ spec.add_development_dependency "api-auth", ">= 1.3.1", "< 2.4"
40
40
  spec.add_development_dependency 'typhoeus'
41
41
  spec.add_development_dependency 'activemodel'
42
+ spec.add_development_dependency 'rest-client', ">= 1.8.0"
42
43
 
43
44
  spec.add_runtime_dependency "mime-types"
44
45
  spec.add_runtime_dependency "multi_json"
@@ -52,12 +53,4 @@ Gem::Specification.new do |spec|
52
53
  else
53
54
  spec.add_runtime_dependency "activesupport", "< 5.0.0"
54
55
  end
55
- # JSON is an implicit dependency of something, but JSON v2+ requires Ruby 2+
56
- # Same with "tins" which is a dependency of coveralls
57
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0')
58
- spec.add_runtime_dependency "json", "< 2.0.0"
59
- spec.add_runtime_dependency "tins", "~> 1.6.0"
60
- spec.add_runtime_dependency "term-ansicolor", "~> 1.3.2"
61
- spec.add_runtime_dependency "public_suffix", "~> 1.4.6"
62
- end
63
56
  end
@@ -41,6 +41,7 @@ module Flexirest
41
41
  def _copy_from(result)
42
42
  @attributes = result._attributes
43
43
  @_status = result._status
44
+ _clean!
44
45
  end
45
46
 
46
47
  def dirty?
@@ -189,7 +190,7 @@ module Flexirest
189
190
  old_value = @dirty_attributes[key.to_sym]
190
191
  old_value = @attributes[key.to_sym] unless old_value
191
192
  old_value = old_value[0] if old_value and old_value.is_a? Array
192
- @dirty_attributes[key.to_sym] = [old_value, value]
193
+ @dirty_attributes[key.to_sym] = [old_value, value] if old_value != value
193
194
  @attributes[key.to_sym] = value
194
195
  end
195
196
 
@@ -110,9 +110,12 @@ module Flexirest
110
110
  if @result.is_a?(Array)
111
111
  ri = ResultIterator.new(self)
112
112
  ri.items = @result.map{|i| @class_name.constantize.new(i)}
113
+ ri._clean!
113
114
  ri
114
115
  else
115
- @class_name.constantize.new(@result)
116
+ obj = @class_name.constantize.new(@result)
117
+ obj._clean!
118
+ obj
116
119
  end
117
120
  end
118
121
  end
@@ -191,9 +191,18 @@ module Flexirest
191
191
  @body = nil
192
192
  prepare_params
193
193
  prepare_url
194
- if fake = @method[:options][:fake]
194
+ fake = @method[:options][:fake]
195
+ if fake.present?
195
196
  if fake.respond_to?(:call)
196
197
  fake = fake.call(self)
198
+ elsif @object.respond_to?(fake)
199
+ fake = @object.send(fake)
200
+ elsif @object.class.respond_to?(fake)
201
+ fake = @object.class.send(fake)
202
+ elsif @object.new.respond_to?(fake)
203
+ fake = @object.new.send(fake)
204
+ elsif @object.class.new.respond_to?(fake)
205
+ fake = @object.class.new.send(fake)
197
206
  end
198
207
  Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Faked response found"
199
208
  content_type = @method[:options][:fake_content_type] || "application/json"
@@ -621,6 +630,16 @@ module Flexirest
621
630
  raise HTTPConflictClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method)
622
631
  elsif status == 429
623
632
  raise HTTPTooManyRequestsClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method)
633
+ elsif status == 500
634
+ raise HTTPInternalServerException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method)
635
+ elsif status == 501
636
+ raise HTTPNotImplementedServerException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method)
637
+ elsif status == 502
638
+ raise HTTPBadGatewayServerException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method)
639
+ elsif status == 503
640
+ raise HTTPServiceUnavailableServerException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method)
641
+ elsif status == 504
642
+ raise HTTPGatewayTimeoutServerException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method)
624
643
  elsif (400..499).include? status
625
644
  raise HTTPClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method)
626
645
  elsif (500..599).include? status
@@ -892,5 +911,9 @@ module Flexirest
892
911
  class HTTPNotFoundClientException < HTTPClientException ; end
893
912
  class HTTPTooManyRequestsClientException < HTTPClientException ; end
894
913
  class HTTPServerException < HTTPException ; end
895
-
914
+ class HTTPInternalServerException < HTTPServerException ; end
915
+ class HTTPNotImplementedServerException < HTTPServerException ; end
916
+ class HTTPBadGatewayServerException < HTTPServerException ; end
917
+ class HTTPServiceUnavailableServerException < HTTPServerException ; end
918
+ class HTTPGatewayTimeoutServerException < HTTPServerException ; end
896
919
  end
@@ -15,6 +15,10 @@ module Flexirest
15
15
  @items << item
16
16
  end
17
17
 
18
+ def _clean!
19
+ @items.map(&:_clean!)
20
+ end
21
+
18
22
  def size
19
23
  @items.size
20
24
  end
@@ -62,8 +66,8 @@ module Flexirest
62
66
  self
63
67
  end
64
68
 
65
- def delete_if
66
- @items = @items.delete_if &Proc.new
69
+ def delete_if(&block)
70
+ @items = @items.delete_if &block
67
71
  self
68
72
  end
69
73
 
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.9.14"
2
+ VERSION = "1.10.0"
3
3
  end
@@ -126,6 +126,13 @@ describe Flexirest::BaseWithoutValidation do
126
126
  expect(client).to be_dirty
127
127
  end
128
128
 
129
+ it "should not mark attributes that don't change as dirty" do
130
+ client = EmptyExample.new(test: "Foo")
131
+ client._clean!
132
+ client.test = client.test
133
+ expect(client).to_not be_dirty
134
+ end
135
+
129
136
  it "should store attribute set using []= array notation and mark them as dirty" do
130
137
  client = EmptyExample.new()
131
138
  client["test"] = "Something"
@@ -171,6 +178,13 @@ describe Flexirest::BaseWithoutValidation do
171
178
  expect(client).to be_dirty
172
179
  end
173
180
 
181
+ it "should not mark a freshly retrieved object as changed" do
182
+ expect_any_instance_of(Flexirest::Connection).to receive(:get).with('/find/1', anything).and_return(::FaradayResponseMock.new(OpenStruct.new(status:200, response_headers:{}, body:"{\"first_name\":\"Billy\"}")))
183
+ client = AlteringClientExample.find(id: 1)
184
+ expect(client).to_not be_changed
185
+ expect(client.changes).to eq({})
186
+ end
187
+
174
188
  it 'should respond_to? attributes defined in the response' do
175
189
  client = EmptyExample.new(hello: "World")
176
190
  expect(client.respond_to?(:hello)).to be_truthy
@@ -184,6 +184,18 @@ describe Flexirest::Caching do
184
184
  expect(ret.first_name).to eq("Johnny")
185
185
  end
186
186
 
187
+ it "cache read objects shouldn't be marked as changed" do
188
+ cached_response = Flexirest::CachedResponse.new(
189
+ status:200,
190
+ result:@cached_object,
191
+ expires:Time.now + 30)
192
+ expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(Marshal.dump(cached_response))
193
+ expect_any_instance_of(Flexirest::Connection).not_to receive(:get)
194
+ ret = Person.all
195
+ expect(ret).to_not be_changed
196
+ expect(ret.changes).to eq({})
197
+ end
198
+
187
199
  it "should read from the cache store and restore to the same object" do
188
200
  cached_response = Flexirest::CachedResponse.new(
189
201
  status:200,
@@ -55,6 +55,7 @@ describe Flexirest::Request do
55
55
  get :hal, "/hal", fake:"{\"_links\":{\"child\": {\"href\": \"/child/1\"}, \"other\": {\"href\": \"/other/1\"}, \"cars\":[{\"href\": \"/car/1\", \"name\":\"car1\"}, {\"href\": \"/car/2\", \"name\":\"car2\"}, {\"href\": \"/car/not-embed\", \"name\":\"car_not_embed\"} ], \"lazy\": {\"href\": \"/lazy/load\"}, \"invalid\": [{\"href\": \"/invalid/1\"}]}, \"_embedded\":{\"other\":{\"name\":\"Jane\"},\"child\":{\"name\":\"Billy\"}, \"cars\":[{\"_links\": {\"self\": {\"href\": \"/car/1\"} }, \"make\": \"Bugatti\", \"model\": \"Veyron\"}, {\"_links\": {\"self\": {\"href\": \"/car/2\"} }, \"make\": \"Ferrari\", \"model\": \"F458 Italia\"} ], \"invalid\": [{\"present\":true, \"_links\": {} } ] } }", has_many:{other:ExampleOtherClient}
56
56
  get :fake_object, "/fake", fake:"{\"result\":true, \"list\":[1,2,3,{\"test\":true}], \"child\":{\"grandchild\":{\"test\":true}}}"
57
57
  get :fake_proc_object, "/fake", fake:->(request) { "{\"result\":#{request.get_params[:id]}}" }
58
+ get :fake_method, "/fake", fake: :generate_fake_data
58
59
  get :fake_array, "/fake", fake:"[1,2,3,{\"test\":true},null]"
59
60
  get :fake_proc_array, "/fake", fake:->(request) { "[{\"result\":#{request.get_params[:id]}},null]" }
60
61
  get :defaults, "/defaults", defaults:{overwrite:"no", persist:"yes"}
@@ -62,6 +63,10 @@ describe Flexirest::Request do
62
63
  patch :only_changed_1, "/changed1", only_changed: true
63
64
  patch :only_changed_2, "/changed2", only_changed: [:debug1, :debug2]
64
65
  patch :only_changed_3, "/changed3", only_changed: { debug1: false, debug2: true }
66
+
67
+ def generate_fake_data
68
+ "{\"result\":true, \"list\":[1,2,3,{\"test\":true}], \"child\":{\"grandchild\":{\"test\":true}}}"
69
+ end
65
70
  end
66
71
 
67
72
  class ExampleLoadBalancedClient < Flexirest::Base
@@ -576,6 +581,11 @@ describe Flexirest::Request do
576
581
  expect(object.result).to eq(1234)
577
582
  end
578
583
 
584
+ it "should parse JSON object from a fake response generated by method defined as a symbol" do
585
+ object = ExampleClient.fake_method id:1234
586
+ expect(object.result).to eq(true)
587
+ end
588
+
579
589
  it "should parse JSON array and return a nice result iterator for faked responses" do
580
590
  object = ExampleClient.fake_array debug:true
581
591
  expect(object).to be_instance_of(Flexirest::ResultIterator)
@@ -995,7 +1005,7 @@ describe Flexirest::Request do
995
1005
  rescue Flexirest::HTTPServerException => e
996
1006
  e
997
1007
  end
998
- expect(e).to be_instance_of(Flexirest::HTTPServerException)
1008
+ expect(e).to be_instance_of(Flexirest::HTTPInternalServerException)
999
1009
  expect(e.status).to eq(500)
1000
1010
  expect(e.result.first_name).to eq("John")
1001
1011
  end
@@ -1011,7 +1021,7 @@ describe Flexirest::Request do
1011
1021
  rescue Flexirest::HTTPServerException => e
1012
1022
  e
1013
1023
  end
1014
- expect(e.message).to eq(%q{The POST to '/create' returned a 500 status, which raised a Flexirest::HTTPServerException with a body of: \{"first_name":"John", "id":1234\}})
1024
+ expect(e.message).to eq(%q{The POST to '/create' returned a 500 status, which raised a Flexirest::HTTPInternalServerException with a body of: \{"first_name":"John", "id":1234\}})
1015
1025
  end
1016
1026
 
1017
1027
  it "should raise a parse exception for invalid JSON returns" do
@@ -1026,7 +1036,7 @@ describe Flexirest::Request do
1026
1036
  rescue => e
1027
1037
  e
1028
1038
  end
1029
- expect(e).to be_instance_of(Flexirest::HTTPServerException)
1039
+ expect(e).to be_instance_of(Flexirest::HTTPInternalServerException)
1030
1040
  expect(e.status).to eq(500)
1031
1041
  expect(e.result).to eq(error_content)
1032
1042
  end
@@ -66,12 +66,12 @@ class FaradayResponseMock < ::Flexirest::FaradayResponseProxy
66
66
  @finished = false
67
67
  end
68
68
 
69
- def on_complete
69
+ def on_complete(&block)
70
70
  if @auto_resolve
71
71
  @finished = true
72
72
  yield(@response)
73
73
  else
74
- @callback = Proc.new
74
+ @callback = block
75
75
  end
76
76
  end
77
77
 
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.9.14
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-02 00:00:00.000000000 Z
11
+ date: 2020-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -129,6 +129,9 @@ dependencies:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
131
  version: 1.3.1
132
+ - - "<"
133
+ - !ruby/object:Gem::Version
134
+ version: '2.4'
132
135
  type: :development
133
136
  prerelease: false
134
137
  version_requirements: !ruby/object:Gem::Requirement
@@ -136,6 +139,9 @@ dependencies:
136
139
  - - ">="
137
140
  - !ruby/object:Gem::Version
138
141
  version: 1.3.1
142
+ - - "<"
143
+ - !ruby/object:Gem::Version
144
+ version: '2.4'
139
145
  - !ruby/object:Gem::Dependency
140
146
  name: typhoeus
141
147
  requirement: !ruby/object:Gem::Requirement
@@ -164,6 +170,20 @@ dependencies:
164
170
  - - ">="
165
171
  - !ruby/object:Gem::Version
166
172
  version: '0'
173
+ - !ruby/object:Gem::Dependency
174
+ name: rest-client
175
+ requirement: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: 1.8.0
180
+ type: :development
181
+ prerelease: false
182
+ version_requirements: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: 1.8.0
167
187
  - !ruby/object:Gem::Dependency
168
188
  name: mime-types
169
189
  requirement: !ruby/object:Gem::Requirement
@@ -364,7 +384,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
364
384
  - !ruby/object:Gem::Version
365
385
  version: '0'
366
386
  requirements: []
367
- rubygems_version: 3.0.6
387
+ rubygems_version: 3.1.2
368
388
  signing_key:
369
389
  specification_version: 4
370
390
  summary: This gem is for accessing REST services in a flexible way. ActiveResource