flexirest 1.5.2 → 1.5.3

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
  SHA1:
3
- metadata.gz: b2a62022220324552e709d4f353392307b3e1cad
4
- data.tar.gz: 0a31df3353a51c1f3308ffa7b2cbe3ebcb3b85a1
3
+ metadata.gz: 499dd67a747959e769fab4e11aa7ee9eed6599b8
4
+ data.tar.gz: 632be3df00eae719a6238d312d29c1dae4bef214
5
5
  SHA512:
6
- metadata.gz: 5a7d7779afeb7ed595f065fc3464fbb4df653651aa4c26d73fc87c0aeed98d248a9c6aa0882f22c7c4419c0d09f0ebbcaa589712a99d40d121dd5bc8564eff0e
7
- data.tar.gz: daa5bb17168731c3014f7f253c0def715101714a20ca817ea1a162d895922df6f980313b4e4266228386f3c7f246f577fc0d0ed1c42e2981775376704a081c60
6
+ metadata.gz: 63f8eaaa04a862b48151effef441782ac3917cb71417834cb4cfcfbe84586907c7f45c47b33846a7a22a5bdf1584d68124a6ac6a5c2fc5becfc9a00da37f622b
7
+ data.tar.gz: 38c540be4278f892a9bc2461c2c7b0e12b9f3074c8b90202be880aed496056c6ad258475b713ae8a86877787814f45e739739d659deed3fcb899adb00d00de9b
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.5.3
4
+
5
+ Feature:
6
+
7
+ - Allow passing custom headers as an option to raw requests.
8
+
3
9
  ## 1.5.2
4
10
 
5
11
  Bugfix:
data/README.md CHANGED
@@ -793,6 +793,12 @@ people = Person._request('http://api.example.com/v1/people') # Defaults to get w
793
793
  Person._request('http://api.example.com/v1/people', :post, {id:1234,name:"John"}) # Post with parameters
794
794
  ```
795
795
 
796
+ When you need to specify custom headers (for example for authentication) you can do this with a fourth option to the `_request` method. If you are using the default paramaters you'll need to specify them. For example:
797
+
798
+ ```ruby
799
+ Person._request("http://api.example.com/v1/people", :get, {}, {"X-Something": "foo/bar"})
800
+ ```
801
+
796
802
  If you want to use a lazy loaded request instead (so it will create an object that will only call the API if you use it), you can use `_lazy_request` instead of `_request`. If you want you can create a construct that creates and object that lazy loads itself from a given method (rather than a URL):
797
803
 
798
804
  ```ruby
@@ -66,16 +66,16 @@ module Flexirest
66
66
  @attributes[:errors] || (_errors != {} ? _errors : nil)
67
67
  end
68
68
 
69
- def self._request(request, method = :get, params = nil)
70
- prepare_direct_request(request, method).call(params)
69
+ def self._request(request, method = :get, params = nil, headers = {})
70
+ prepare_direct_request(request, method, headers: headers).call(params)
71
71
  end
72
72
 
73
- def self._plain_request(request, method = :get, params = nil)
74
- prepare_direct_request(request, method, plain:true).call(params)
73
+ def self._plain_request(request, method = :get, params = nil, headers = {})
74
+ prepare_direct_request(request, method, plain:true, headers: headers).call(params)
75
75
  end
76
76
 
77
- def self._lazy_request(request, method = :get, params = nil)
78
- Flexirest::LazyLoader.new(prepare_direct_request(request, method), params)
77
+ def self._lazy_request(request, method = :get, params = nil, headers = {})
78
+ Flexirest::LazyLoader.new(prepare_direct_request(request, method, headers: headers), params)
79
79
  end
80
80
 
81
81
  def self.prepare_direct_request(request, method = :get, options={})
@@ -21,6 +21,9 @@ module Flexirest
21
21
  @response_delegate = Flexirest::RequestDelegator.new(nil)
22
22
  @params = params
23
23
  @headers = HeadersList.new
24
+ (@method[:options][:headers] || {}).each do |k,v|
25
+ @headers[k] = v
26
+ end
24
27
  @forced_url = nil
25
28
  end
26
29
 
@@ -463,6 +466,7 @@ module Flexirest
463
466
  def handle_response(response, cached = nil)
464
467
  @response = response
465
468
  status = @response.status || 200
469
+ @response.body = "{}" if @response.body.blank?
466
470
 
467
471
  if cached && response.status == 304
468
472
  Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name}" +
@@ -471,12 +475,8 @@ module Flexirest
471
475
  end
472
476
 
473
477
  if (200..399).include?(status)
474
- if @response.body.blank?
475
- @response.body = "{}"
476
- end
477
-
478
478
  if @method[:options][:plain]
479
- return @response = Flexirest::PlainResponse.from_response(response)
479
+ return @response = Flexirest::PlainResponse.from_response(@response)
480
480
  elsif is_json_response? || is_xml_response?
481
481
  if @response.respond_to?(:proxied) && @response.proxied
482
482
  Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Response was proxied, unable to determine size"
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.5.2"
2
+ VERSION = "1.5.3"
3
3
  end
@@ -57,17 +57,17 @@ describe Flexirest::Base do
57
57
  end
58
58
 
59
59
  it "should save attributes passed in constructor" do
60
- client = EmptyExample.new(:test => "Something")
60
+ client = EmptyExample.new(test: "Something")
61
61
  expect(client._attributes[:test]).to be_a(String)
62
62
  end
63
63
 
64
64
  it "should allow attribute reading using missing method names" do
65
- client = EmptyExample.new(:test => "Something")
65
+ client = EmptyExample.new(test: "Something")
66
66
  expect(client.test).to eq("Something")
67
67
  end
68
68
 
69
69
  it "should allow attribute reading using [] array notation" do
70
- client = EmptyExample.new(:test => "Something")
70
+ client = EmptyExample.new(test: "Something")
71
71
  expect(client["test"]).to eq("Something")
72
72
  end
73
73
 
@@ -85,37 +85,37 @@ describe Flexirest::Base do
85
85
 
86
86
  it "should automatically parse ISO 8601 format date and time" do
87
87
  t = Time.now
88
- client = EmptyExample.new(:test => t.iso8601)
88
+ client = EmptyExample.new(test: t.iso8601)
89
89
  expect(client["test"]).to be_an_instance_of(DateTime)
90
90
  expect(client["test"].to_s).to eq(t.to_datetime.to_s)
91
91
  end
92
92
 
93
93
  it "should automatically parse ISO 8601 format date and time with milliseconds" do
94
94
  t = Time.now
95
- client = EmptyExample.new(:test => t.iso8601(3))
95
+ client = EmptyExample.new(test: t.iso8601(3))
96
96
  expect(client["test"]).to be_an_instance_of(DateTime)
97
97
  expect(client["test"].to_s).to eq(t.to_datetime.to_s)
98
98
  end
99
99
 
100
100
  it "should automatically parse ISO 8601 format dates" do
101
101
  d = Date.today
102
- client = EmptyExample.new(:test => d.iso8601)
102
+ client = EmptyExample.new(test: d.iso8601)
103
103
  expect(client["test"]).to be_an_instance_of(Date)
104
104
  expect(client["test"]).to eq(d)
105
105
  end
106
106
 
107
107
  it "should automatically parse date/time strings regardless if the date portion has no delimiters" do
108
- client = EmptyExample.new(:test => "20151230T09:48:50-05:00")
108
+ client = EmptyExample.new(test: "20151230T09:48:50-05:00")
109
109
  expect(client["test"]).to be_an_instance_of(DateTime)
110
110
  end
111
111
 
112
112
  it "should allow strings of 4 digits and not intepret them as dates" do
113
- client = EmptyExample.new(:test => "2015")
113
+ client = EmptyExample.new(test: "2015")
114
114
  expect(client["test"]).to be_an_instance_of(String)
115
115
  end
116
116
 
117
117
  it "should allow strings of 8 digits and not intepret them as dates" do
118
- client = EmptyExample.new(:test => "1266129")
118
+ client = EmptyExample.new(test: "1266129")
119
119
  expect(client["test"]).to be_an_instance_of(String)
120
120
  end
121
121
 
@@ -163,7 +163,7 @@ describe Flexirest::Base do
163
163
  end
164
164
 
165
165
  it "should overwrite attributes already set and mark them as dirty" do
166
- client = EmptyExample.new(:hello => "World")
166
+ client = EmptyExample.new(hello: "World")
167
167
  client._clean!
168
168
  expect(client).to_not be_dirty
169
169
 
@@ -172,7 +172,7 @@ describe Flexirest::Base do
172
172
  end
173
173
 
174
174
  it 'should respond_to? attributes defined in the response' do
175
- client = EmptyExample.new(:hello => "World")
175
+ client = EmptyExample.new(hello: "World")
176
176
  expect(client.respond_to?(:hello)).to be_truthy
177
177
  expect(client.respond_to?(:world)).to be_falsey
178
178
  end
@@ -311,6 +311,13 @@ describe Flexirest::Base do
311
311
  EmptyExample._request("http://api.example.com/")
312
312
  end
313
313
 
314
+ it "passes headers" do
315
+ stub_request(:get, "http://api.example.com/v1").
316
+ with(headers: {'Accept'=>'application/hal+json, application/json;q=0.5', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Connection'=>'Keep-Alive', 'X-Something'=>'foo/bar', 'User-Agent'=>'Flexirest/1.5.2'}).
317
+ to_return(status: 200, body: "", headers: {})
318
+ EmptyExample._request("http://api.example.com/v1", :get, {}, {"X-Something": "foo/bar"})
319
+ end
320
+
314
321
  it "runs callbacks as usual" do
315
322
  expect_any_instance_of(Flexirest::Request).to receive(:do_request).with(any_args).and_return(::FaradayResponseMock.new(OpenStruct.new(status:200, response_headers:{}, body:"{\"first_name\":\"Billy\"}")))
316
323
  expect(EmptyExample).to receive(:_callback_request).with(any_args).exactly(2).times
@@ -418,7 +425,7 @@ describe Flexirest::Base do
418
425
 
419
426
  it "should be able to use mapped methods to create a request to pass in to _lazy_request" do
420
427
  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\"}")))
421
- request = AlteringClientExample._request_for(:find, :id => 1)
428
+ request = AlteringClientExample._request_for(:find, id: 1)
422
429
  example = AlteringClientExample._lazy_request(request)
423
430
  expect(example.first_name).to eq("Billy")
424
431
  end
@@ -361,6 +361,16 @@ describe Flexirest::Request do
361
361
  expect(ExampleClient.all).to be_truthy
362
362
  end
363
363
 
364
+ it "should return true from 202 with empty bodies" do
365
+ expect_any_instance_of(Flexirest::Connection).to receive(:get).with(any_args).and_return(::FaradayResponseMock.new(OpenStruct.new(status:202, response_headers:{}, body: nil)))
366
+ expect(ExampleClient.all).to be_truthy
367
+ end
368
+
369
+ it "should return true from 200 with empty bodies" do
370
+ expect_any_instance_of(Flexirest::Connection).to receive(:get).with(any_args).and_return(::FaradayResponseMock.new(OpenStruct.new(status:200, response_headers:{}, body: nil)))
371
+ expect(ExampleClient.all).to be_truthy
372
+ end
373
+
364
374
  it "should return a lazy loader object if lazy loading is enabled" do
365
375
  object = LazyLoadedExampleClient.fake id:1234, debug:true
366
376
  expect(object).to be_an_instance_of(Flexirest::LazyLoader)
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.5.2
4
+ version: 1.5.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: 2017-10-13 00:00:00.000000000 Z
11
+ date: 2017-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler