flexirest 1.7.9 → 1.8.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: b0a27d77149e8eeba7815235367970828643b622a0925425c0cd2f18a12740dc
4
- data.tar.gz: 6c12446837777d3bef7f37d6def7be613eeb98c380406a16f4e4f41e47ae0443
3
+ metadata.gz: 0646b0d77ed69a83a024542226cd74833092523296edd7c6b48e6524a376c67c
4
+ data.tar.gz: d645d44a87ff5fabf8a7b9d6306f48bd6b899a7f6c966ded3d37a7456e091edd
5
5
  SHA512:
6
- metadata.gz: 722724ba195f14caa98a856beb93d527e1e69b8b16b247dc9bf4eabffb80f262ff930ba14bab9c01517bfcaf757e8c53f322f1efa454007ad3819d87214193e5
7
- data.tar.gz: 779f448b1ed8c1f2f7244aa672b29e24496949088c3a4248c04a196336c79eeb8733691fd397521527b37ecdacc1368e6ae4345b6dfdeef374d4d56f6d9e58f3
6
+ metadata.gz: 96a26d232291398f3bc7b13927ecd80175cdf3eea0293fd3d52c317ab55257e8269362ab4b5db0ac87d0f63621777518b6ad3758385bdda84a1782546e892dc3
7
+ data.tar.gz: c3ee959ee87e8f325d8ff16f1e1a4e2d2746dc6765e159193194fd85aa414ac3c8b5b8491a32d261bca0c982d9feb6c2caafcfa0398b591b9eae5cb2e68c2ef8
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.8.0
4
+
5
+ Features:
6
+
7
+ - For the weird situations where the remote API expects some weird formatted input, you can now specify a `request_body_type` of plain and pass in to the call `body` (and optionally `content_type`) to pass it untouched to the API. The [docs](docs/body-types.md) have been updated.
8
+
3
9
  ## 1.7.9
4
10
 
5
11
  Features:
@@ -27,6 +27,16 @@ class Person < Flexirest::Base
27
27
  end
28
28
  ```
29
29
 
30
+ If your API expects some weird formatting on the requests, but you still want to use Flexirest for caching, response parsing, other models, etc you can pass `:plain` as the request body type either at the class level or method mapping level, and this will be passed through to the API. By default `plain` requests are sent with the `Content-Type: text/plain` header, but you can override this with `:content_type` when calling the mapped method.
31
+
32
+ ```ruby
33
+ class Person < Flexirest::Base
34
+ put :save, '/person/:id/logs', request_body_type: :plain
35
+ end
36
+
37
+ Person.save(id: 1, body: '["Something here"]',
38
+ content_type: "application/json")
39
+ ```
30
40
 
31
41
  -----
32
42
 
@@ -0,0 +1,23 @@
1
+ module Flexirest::ActiveModelFormSupport
2
+ extend ActiveSupport::Concern
3
+ include ActiveModel::AttributeAssignment
4
+ include ActiveModel::Validations
5
+ include ActiveModel::Conversion
6
+
7
+ included do
8
+ extend ActiveModel::Naming
9
+ extend ActiveModel::Translation
10
+ end
11
+
12
+ def persisted?
13
+ !dirty?
14
+ end
15
+
16
+ def new_record?
17
+ id.blank?
18
+ end
19
+
20
+ def errors
21
+ ActiveSupport::HashWithIndifferentAccess.new([]).merge(self[:errors] || {})
22
+ end
23
+ end
@@ -430,6 +430,10 @@ module Flexirest
430
430
  end
431
431
  end
432
432
  headers["Content-Type"] ||= "application/json; charset=utf-8"
433
+ elsif request_body_type == :plain && @post_params[:body].present?
434
+ @body = @post_params[:body]
435
+ headers["Content-Type"] ||= "text/plain"
436
+ headers["Content-Type"] = @post_params[:content_type] if @post_params[:content_type].present?
433
437
  end
434
438
  end
435
439
 
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.7.9"
2
+ VERSION = "1.8.0"
3
3
  end
@@ -27,11 +27,11 @@ describe Flexirest::Request do
27
27
  end
28
28
  end
29
29
 
30
- get :all, "/", :has_many => {:expenses => ExampleOtherClient}
31
- get :flat, "/", :params_encoder => :flat
30
+ get :all, "/", has_many: {expenses: ExampleOtherClient}
31
+ get :flat, "/", params_encoder: :flat
32
32
  get :array, "/johnny", array: [:likes, :dislikes]
33
- get :babies, "/babies", :has_many => {:children => ExampleOtherClient}
34
- get :single_association, "/single", :has_one => {:single => ExampleSingleClient}, :has_many => {:children => ExampleOtherClient}
33
+ get :babies, "/babies", has_many: {children: ExampleOtherClient}
34
+ get :single_association, "/single", has_one: {single: ExampleSingleClient}, has_many: {children: ExampleOtherClient}
35
35
  get :headers, "/headers"
36
36
  get :cancel_callback, "/cancel-callback"
37
37
  put :headers_default, "/headers_default"
@@ -54,12 +54,12 @@ describe Flexirest::Request do
54
54
  get :requires, "/requires", requires:[:name, :age]
55
55
  patch :only_changed_1, "/changed1", only_changed: true
56
56
  patch :only_changed_2, "/changed2", only_changed: [:debug1, :debug2]
57
- patch :only_changed_3, "/changed3", only_changed: { :debug1 => false, :debug2 => true }
57
+ patch :only_changed_3, "/changed3", only_changed: { debug1: false, debug2: true }
58
58
  end
59
59
 
60
60
  class ExampleLoadBalancedClient < Flexirest::Base
61
61
  base_url ["http://api1.example.com", "http://api2.example.com"]
62
- get :all, "/", :has_many => {:expenses => ExampleOtherClient}
62
+ get :all, "/", has_many: {expenses: ExampleOtherClient}
63
63
  end
64
64
 
65
65
  class AuthenticatedExampleClient < Flexirest::Base
@@ -135,7 +135,7 @@ describe Flexirest::Request do
135
135
  base_url "http://www.example.com"
136
136
  lazy_load!
137
137
  get :fake, "/fake", fake:"{\"result\":true, \"list\":[1,2,3,{\"test\":true}], \"child\":{\"grandchild\":{\"test\":true}}}"
138
- get :lazy_test, "/does-not-matter", fake:"{\"people\":[\"http://www.example.com/some/url\"]}", :lazy => [:people]
138
+ get :lazy_test, "/does-not-matter", fake:"{\"people\":[\"http://www.example.com/some/url\"]}", lazy: [:people]
139
139
  end
140
140
 
141
141
  class VerboseExampleClient < ExampleClient
@@ -169,9 +169,9 @@ describe Flexirest::Request do
169
169
  {
170
170
  "response": {
171
171
  "data": {
172
- "object": {
172
+ "object": {
173
173
  "title": "Example Multi Level Feed"
174
- }
174
+ }
175
175
  }
176
176
  }
177
177
  }
@@ -340,12 +340,12 @@ describe Flexirest::Request do
340
340
 
341
341
  it "should pass through 'array type' get parameters" do
342
342
  expect_any_instance_of(Flexirest::Connection).to receive(:get).with("/?include%5B%5D=your&include%5B%5D=friends", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
343
- ExampleClient.all :include => [:your,:friends]
343
+ ExampleClient.all include: [:your,:friends]
344
344
  end
345
345
 
346
346
  it "should pass through 'array type' get parameters using the same parameter name if a flat param_encoder is chosen" do
347
347
  expect_any_instance_of(Flexirest::Connection).to receive(:get).with("/?include=your&include=friends", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
348
- ExampleClient.flat :include => [:your,:friends]
348
+ ExampleClient.flat include: [:your,:friends]
349
349
  end
350
350
 
351
351
  it "should encode the body in a form-encoded format by default" do
@@ -359,12 +359,28 @@ describe Flexirest::Request do
359
359
  ExampleClient.update id:1234, debug:true, test:'foo'
360
360
  end
361
361
 
362
- it "should encode the body in a JSON format if specified" do
362
+ it "should encode the body wrapped in a root element in a JSON format if specified" do
363
363
  expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", %q({"example":{"debug":true,"test":"foo"}}), an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
364
364
  ExampleClient.request_body_type :json
365
365
  ExampleClient.wrapped id:1234, debug:true, test:'foo'
366
366
  end
367
367
 
368
+ it "should pass the body untouched if plain format is specified on the class" do
369
+ header_expectation = {headers: {"Accept"=>"application/hal+json, application/json;q=0.5", "Content-Type"=>"text/plain"}, api_auth: {api_auth_access_id: "id123", api_auth_secret_key: "secret123", api_auth_options: {}}}
370
+ expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", "debug:true|test:'foo'", header_expectation).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
371
+ ExampleClient.request_body_type :plain
372
+ ExampleClient.update id:1234, body: "debug:true|test:'foo'"
373
+ end
374
+
375
+ it "should use the content type specified if plain format is specified on the class" do
376
+ header_expectation = {headers: {"Accept"=>"application/hal+json, application/json;q=0.5", "Content-Type"=>"application/flexirest"}, api_auth: {api_auth_access_id: "id123", api_auth_secret_key: "secret123", api_auth_options: {}}}
377
+ expect_any_instance_of(Flexirest::Connection).to receive(:put).
378
+ with("/put/1234", "debug:true|test:'foo'", header_expectation).
379
+ and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
380
+ ExampleClient.request_body_type :plain
381
+ ExampleClient.update id:1234, body: "debug:true|test:'foo'", content_type: "application/flexirest"
382
+ end
383
+
368
384
  it "should wrap elements if specified, in form-encoded format" do
369
385
  expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", %q(example%5Bdebug%5D=true&example%5Btest%5D=foo), an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
370
386
  ExampleClient.wrapped id:1234, debug:true, test:'foo'
@@ -897,7 +913,7 @@ describe Flexirest::Request do
897
913
  end
898
914
 
899
915
  it "should raise an exception if you try to pass in an unsupport method" do
900
- method = {:method => :wiggle, url:"/"}
916
+ method = {method: :wiggle, url:"/"}
901
917
  class RequestFakeObject < Flexirest::Base
902
918
  base_url "http://www.example.com/"
903
919
 
@@ -944,7 +960,7 @@ describe Flexirest::Request do
944
960
 
945
961
  it "should retry if an after_request callback returns :retry" do
946
962
  stub_request(:get, "http://www.example.com/do_me_twice").
947
- to_return(:status => 200, :body => "", :headers => {})
963
+ to_return(status: 200, body: "", headers: {})
948
964
  RetryingExampleClient.reset_retries
949
965
  RetryingExampleClient.do_me_twice
950
966
  expect(RetryingExampleClient.retries).to eq(2)
@@ -952,9 +968,9 @@ describe Flexirest::Request do
952
968
 
953
969
  it "should allow a second call and then retry if an after_request callback returns :retry" do
954
970
  stub_request(:get, "http://www.example.com/first_call").
955
- to_return(:status => 200, :body => "", :headers => {})
971
+ to_return(status: 200, body: "", headers: {})
956
972
  stub_request(:get, "http://www.example.com/second_call").
957
- to_return(:status => 200, :body => "", :headers => {})
973
+ to_return(status: 200, body: "", headers: {})
958
974
  RetryingExampleClient.reset_retries
959
975
  RetryingExampleClient.first_call
960
976
  expect(RetryingExampleClient.retries).to eq(2)
@@ -1023,7 +1039,7 @@ describe Flexirest::Request do
1023
1039
  end
1024
1040
 
1025
1041
  it "should recognise a HAL response" do
1026
- method = {:method => :get, url:"/"}
1042
+ method = {method: :get, url:"/"}
1027
1043
  class RequestFakeObject
1028
1044
  def base_url
1029
1045
  "http://www.example.com/"
@@ -1137,7 +1153,7 @@ describe Flexirest::Request do
1137
1153
  it "should ignore a specified root element" do
1138
1154
  expect(IgnoredRootExampleClient.root.title).to eq("Example Feed")
1139
1155
  end
1140
-
1156
+
1141
1157
  it "should ignore a specified multi-level root element" do
1142
1158
  expect(IgnoredMultiLevelRootExampleClient.multi_level_root.title).to eq("Example Multi Level Feed")
1143
1159
  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.7.9
4
+ version: 1.8.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: 2019-07-23 00:00:00.000000000 Z
11
+ date: 2019-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -275,6 +275,7 @@ files:
275
275
  - docs/xml-responses.md
276
276
  - flexirest.gemspec
277
277
  - lib/flexirest.rb
278
+ - lib/flexirest/active_model_form_support.rb
278
279
  - lib/flexirest/associations.rb
279
280
  - lib/flexirest/attribute_parsing.rb
280
281
  - lib/flexirest/base.rb