flexirest 1.3.15 → 1.3.16

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
  SHA1:
3
- metadata.gz: 0704471f0511508955b73ff6c1a9f4490d6d2953
4
- data.tar.gz: 2edada3ba600208f0df0f13aa06d2d91a4eb1a38
3
+ metadata.gz: a05916ea9c0a9dff55d6d260df70ab263b4cb51f
4
+ data.tar.gz: b8f74642b6c7ee86a8819f6ca2077cfc153be34c
5
5
  SHA512:
6
- metadata.gz: 4cd56fe8131e1dd4e835a6ca77c90896283f0441cadaaf7f4e844d51907dc8ddcbcdd39d260c12e9d95d18a12adcd3688bdf1e880c3f719b81068ecc4a4f52f7
7
- data.tar.gz: e0901b4f1796580a9eb229609e8f43c5f6500bdef5819857a22d1a3279fc19669cbd49d6069fbd65627d3352c420026e98b272aa980d9f22b209561e71d833cf
6
+ metadata.gz: 9e92347d9e7654e6296827a40ace68a536f39204b6fec78ece3955efba329768c5522e65715dc8ea3f7fb6bff739f639892fec26e06627a2b67a356375cf3bf9
7
+ data.tar.gz: d9582c21066db41a975ffe5f4c76ffcff27c85ea11af012f38acc267d71a7791ebc83dfc6b6951c6857527f7fc2b5c9e3d2e1ec163f577225c89bdb31e10d94a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.16
4
+
5
+ Feature:
6
+
7
+ - Allows disabling of the automatic date parsing with the `Flexirest::Base.disable_automatic_date_parsing` setting and/or specifying it per mapped method with the `:parse_fields` option (thanks to Michael Mealling for the request).
8
+
3
9
  ## 1.3.15
4
10
 
5
11
  Feature:
data/README.md CHANGED
@@ -646,12 +646,29 @@ class Person < Flexirest::Base
646
646
  get :all, '/people', params_encoder: :flat
647
647
  end
648
648
  ```
649
+
649
650
  would output the following url
650
651
 
651
652
  ```
652
653
  ?param=1&param=2&param=3
653
654
  ```
654
655
 
656
+ ### Automatic Conversion of Fields to Date/DateTime
657
+
658
+ By default Flexirest will attempt to convert all fields to a Date or DateTime object if it's a string and the value matches a pair of regular expressions. However, on large responses this can be computationally expensive. So, you can either disable this automatic conversion completely with:
659
+
660
+ ```ruby
661
+ Flexirest::Base.disable_automatic_date_parsing = true
662
+ ```
663
+
664
+ Additionally, you can specify when mapping the methods which fields should be parsed (so you can disable it in general, then apply it to particular known fields):
665
+
666
+ ```ruby
667
+ class Person < Flexirest::Base
668
+ get :all, '/people', parse_fields: [:created_at, :updated_at]
669
+ end
670
+ ```
671
+
655
672
  ### Raw Requests
656
673
 
657
674
  Sometimes you have have a URL that you just want to force through, but have the response handled in the same way as normal objects or you want to have the filters run (say for authentication). The easiest way to do that is to call `_request` on the class:
@@ -7,6 +7,7 @@ module Flexirest
7
7
  @@username = nil
8
8
  @@password = nil
9
9
  @@request_body_type = :form_encoded
10
+ @@disable_automatic_date_parsing = nil
10
11
  @lazy_load = false
11
12
  @api_auth_access_id = nil
12
13
  @api_auth_secret_key = nil
@@ -107,6 +108,28 @@ module Flexirest
107
108
  @@request_body_type = value
108
109
  end
109
110
 
111
+ def disable_automatic_date_parsing(value = nil)
112
+ @disable_automatic_date_parsing ||= nil
113
+ if value.nil?
114
+ if @disable_automatic_date_parsing.nil?
115
+ if value.nil? && superclass.respond_to?(:disable_automatic_date_parsing)
116
+ superclass.disable_automatic_date_parsing
117
+ else
118
+ @@disable_automatic_date_parsing || false
119
+ end
120
+ else
121
+ @disable_automatic_date_parsing
122
+ end
123
+ else
124
+ @disable_automatic_date_parsing = value
125
+ end
126
+ end
127
+
128
+ def disable_automatic_date_parsing=(value)
129
+ Flexirest::Logger.info "\033[1;4;32m#{name}\033[0m Request Body Type set to be #{value}"
130
+ @@disable_automatic_date_parsing = value
131
+ end
132
+
110
133
  def adapter=(adapter)
111
134
  Flexirest::Logger.info "\033[1;4;32m#{name}\033[0m Adapter set to be #{adapter}"
112
135
  @adapter = adapter
@@ -473,7 +473,15 @@ module Flexirest
473
473
  end
474
474
  end
475
475
  else
476
- object._attributes[k] = parse_attribute_value(v)
476
+ if @method[:options][:parse_fields] && @method[:options][:parse_fields].include?(k)
477
+ object._attributes[k] = parse_attribute_value(v)
478
+ elsif @method[:options][:parse_fields]
479
+ object._attributes[k] = v
480
+ elsif Flexirest::Base.disable_automatic_date_parsing
481
+ object._attributes[k] = v
482
+ else
483
+ object._attributes[k] = parse_attribute_value(v)
484
+ end
477
485
  end
478
486
  end
479
487
  object.clean! unless object_is_class?
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.3.15"
2
+ VERSION = "1.3.16"
3
3
  end
@@ -35,6 +35,7 @@ describe Flexirest::Request do
35
35
  post :create, "/create"
36
36
  post :test_encoding, "/encoding", request_body_type: :json
37
37
  put :update, "/put/:id"
38
+ put :conversion, "/put/:id", parse_fields: [:converted]
38
39
  delete :remove, "/remove/:id"
39
40
  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}
40
41
  get :fake, "/fake", fake:"{\"result\":true, \"list\":[1,2,3,{\"test\":true}], \"child\":{\"grandchild\":{\"test\":true}}}"
@@ -248,6 +249,24 @@ describe Flexirest::Request do
248
249
  expect(object.child.grandchild.test).to eq(true)
249
250
  end
250
251
 
252
+ it "should not convert date times in JSON if automatic parsing is disabled" do
253
+ begin
254
+ Flexirest::Base.disable_automatic_date_parsing = true
255
+ expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", "debug=true", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"created_at\":\"2012-03-04T01:02:03Z\"}", response_headers:{})))
256
+ object = ExampleClient.update id:1234, debug:true
257
+ expect(object.created_at).to be_an_instance_of(String)
258
+ ensure
259
+ Flexirest::Base.disable_automatic_date_parsing = false
260
+ end
261
+ end
262
+
263
+ it "should only convert date times in JSON if specified" do
264
+ expect_any_instance_of(Flexirest::Connection).to receive(:put).with("/put/1234", "debug=true", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"converted\":\"2012-03-04T01:02:03Z\", \"not_converted\":\"2012-03-04T01:02:03Z\"}", response_headers:{})))
265
+ object = ExampleClient.conversion id:1234, debug:true
266
+ expect(object.converted).to be_an_instance_of(DateTime)
267
+ expect(object.not_converted).to be_an_instance_of(String)
268
+ end
269
+
251
270
  it "should parse JSON and return a nice object for faked responses" do
252
271
  object = ExampleClient.fake id:1234, debug:true
253
272
  expect(object.result).to eq(true)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexirest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.15
4
+ version: 1.3.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries