flexirest 1.3.24 → 1.3.25

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: 14a35d637cc44c2743283d3c63c3072b5d46b43c
4
- data.tar.gz: a5b9fa6ad5756255d0f7d7033fe7f82ff85927bf
3
+ metadata.gz: 47cf1c23a48bd2918fe49fd5470a9a3b0342e877
4
+ data.tar.gz: 5e217cfef265cbe32682ae8a6d1749b4be843428
5
5
  SHA512:
6
- metadata.gz: e2e4b418eb542330bcc6223c05ed6335d0727d12a654facf6977e21c639082a066c3e48ffab1bd806a49a1d910a8752799ca351ac9df82dab0cc4331a9bed55b
7
- data.tar.gz: 174b86be52593db6abd70544225a2775029c648e03eb4c971e52973a356a31fa304accd0281e7c14f4a167056b369f75843dda5c46fcd38755204360ed11a53b
6
+ metadata.gz: fc90cf108ae54f5ee86d6f884e35eecf4fe46e5f7f0b835f2fe5dd77759392f62e3a7cbf1582a749068332e757ddaa672a77894e1858eef88cedcfb621013584
7
+ data.tar.gz: 9245bc5bc022f899fb8db5a71339fac3d5ad33d19abb0d13d1dd60d97640f1d751757fd55f1395c24fe32be81dbeac372f2c745a2e75ab817879a6037fdbd9be
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.25
4
+
5
+ Feature:
6
+
7
+ - Improvements in performance due to date parsing (thanks to guanglunw for the PR)
8
+
3
9
  ## 1.3.24
4
10
 
5
11
  Bugfix:
data/README.md CHANGED
@@ -702,7 +702,7 @@ would output the following url
702
702
 
703
703
  ### Automatic Conversion of Fields to Date/DateTime
704
704
 
705
- 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:
705
+ 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. You can disable this automatic conversion completely with:
706
706
 
707
707
  ```ruby
708
708
  Flexirest::Base.disable_automatic_date_parsing = true
@@ -716,6 +716,21 @@ class Person < Flexirest::Base
716
716
  end
717
717
  ```
718
718
 
719
+ It is also possible to whitelist fields should be parsed in your models, which is useful if you are instantiating these objects directly. The specified fields also apply automatically to request mapping.
720
+
721
+ ```ruby
722
+ class Person < Flexirest::Base
723
+ parse_date :updated_at, :created_at
724
+ end
725
+
726
+ # to disable all mapping
727
+ class Disabled < Flexirest::Base
728
+ parse_date :none
729
+ end
730
+ ```
731
+
732
+ This system respects `disable_automatic_date_parsing`, and will default to mapping everything - unless a `parse_date` whitelist is specified, or automatic parsing is globally disabled.
733
+
719
734
  ### Raw Requests
720
735
 
721
736
  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 callbacks run (say for authentication). The easiest way to do that is to call `_request` on the class:
@@ -42,7 +42,9 @@ Gem::Specification.new do |spec|
42
42
  spec.add_runtime_dependency "activesupport", "< 5.0.0"
43
43
  end
44
44
  # JSON is an implicit dependency of something, but JSON v2+ requires Ruby 2+
45
+ # Same with "tins" which is a dependency of coveralls
45
46
  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0')
46
47
  spec.add_runtime_dependency "json", "< 2.0.0"
48
+ spec.add_runtime_dependency "tins", "~> 1.6.0"
47
49
  end
48
50
  end
@@ -50,6 +50,19 @@ module Flexirest
50
50
  _attributes[key]
51
51
  end
52
52
  end
53
+
54
+ def parse_date(*keys)
55
+ keys.each { |key| @_date_fields << key }
56
+ end
57
+
58
+ def _date_fields
59
+ @_date_fields.uniq
60
+ end
61
+
62
+ def inherited(subclass)
63
+ subclass.instance_variable_set(:@_date_fields, [])
64
+ super
65
+ end
53
66
  end
54
67
 
55
68
  def self.included(base)
@@ -3,6 +3,8 @@ module Flexirest
3
3
  private
4
4
 
5
5
  def parse_attribute_value(v)
6
+ return v if v.is_a?(Date) || v.is_a?(DateTime)
7
+
6
8
  if v.to_s[(/\A(((19|20)\d\d[- \/.](0[1-9]|1[012]|[1-9])[- \/.](0[1-9]|[12][0-9]|3[01]|[1-9]))|((0[1-9]|1[012]|[1-9])[- \/.](0[1-9]|[12][0-9]|3[01]|[1-9])[- \/.](19|20)\d\d))\Z/)]
7
9
  Date.parse(v)
8
10
  elsif v.to_s[/\A([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?))\Z/]
@@ -26,7 +26,7 @@ module Flexirest
26
26
 
27
27
  attrs.each do |attribute_name, attribute_value|
28
28
  attribute_name = attribute_name.to_sym
29
- @attributes[attribute_name] = parse_attribute_value(attribute_value)
29
+ @attributes[attribute_name] = parse_date?(attribute_name) ? parse_attribute_value(attribute_value) : attribute_value
30
30
  @dirty_attributes << attribute_name
31
31
  end
32
32
  end
@@ -175,6 +175,12 @@ module Flexirest
175
175
  end
176
176
  end
177
177
 
178
+ def parse_date?(name)
179
+ return true if self.class._date_fields.include?(name)
180
+ return true if !Flexirest::Base.disable_automatic_date_parsing && self.class._date_fields.empty?
181
+ false
182
+ end
183
+
178
184
  end
179
185
 
180
186
  class NoAttributeException < StandardError ; end
@@ -479,9 +479,11 @@ module Flexirest
479
479
  end
480
480
  end
481
481
  else
482
- if @method[:options][:parse_fields] && @method[:options][:parse_fields].include?(k)
482
+ parse_fields = [ @method[:options][:parse_fields], @object._date_fields ].compact.reduce([], :|)
483
+ parse_fields = nil if parse_fields.empty?
484
+ if (parse_fields && parse_fields.include?(k))
483
485
  object._attributes[k] = parse_attribute_value(v)
484
- elsif @method[:options][:parse_fields]
486
+ elsif parse_fields
485
487
  object._attributes[k] = v
486
488
  elsif Flexirest::Base.disable_automatic_date_parsing
487
489
  object._attributes[k] = v
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.3.24"
2
+ VERSION = "1.3.25"
3
3
  end
@@ -29,6 +29,16 @@ class DeepNestedHasManyExample < Flexirest::Base
29
29
  get :find, "/iterate", fake: hash.to_json
30
30
  end
31
31
 
32
+ class WhitelistedDateExample < Flexirest::Base
33
+ parse_date :updated_at
34
+ end
35
+
36
+ class WhitelistedDateMultipleExample < Flexirest::Base
37
+ parse_date :updated_at, :created_at
38
+ parse_date :generated_at
39
+ end
40
+
41
+
32
42
  describe "Has Many Associations" do
33
43
  let(:subject) {AssociationExampleBase.new}
34
44
 
@@ -108,3 +118,29 @@ describe "Has One Associations" do
108
118
  expect(subject.child.nested_child.test).to eq("foo")
109
119
  end
110
120
  end
121
+
122
+ describe "whitelisted date fields" do
123
+ context "no whitelist specified" do
124
+ let(:subject) {AssociationExampleNested}
125
+
126
+ it "should show whitelist as empty array" do
127
+ expect(subject._date_fields).to eq([])
128
+ end
129
+ end
130
+
131
+ context "whitelist specified" do
132
+ let(:subject) {WhitelistedDateExample}
133
+
134
+ it "should contain whitelisted field" do
135
+ expect(subject._date_fields).to eq([:updated_at])
136
+ end
137
+ end
138
+
139
+ context "multiple attributes whitelisted" do
140
+ let(:subject) {WhitelistedDateMultipleExample}
141
+
142
+ it "should contain all fields" do
143
+ expect(subject._date_fields).to match_array([:updated_at, :created_at, :generated_at])
144
+ end
145
+ end
146
+ end
@@ -42,6 +42,11 @@ class InstanceMethodExample < Flexirest::Base
42
42
  get :all, "/all"
43
43
  end
44
44
 
45
+ class WhitelistedDateExample < Flexirest::Base
46
+ parse_date :updated_at
47
+ end
48
+
49
+
45
50
  describe Flexirest::Base do
46
51
  it 'should instantiate a new descendant' do
47
52
  expect{EmptyExample.new}.to_not raise_error
@@ -431,4 +436,22 @@ describe Flexirest::Base do
431
436
  end
432
437
  end
433
438
 
439
+ describe "instantiating object" do
440
+ context "no whitelist specified" do
441
+ it "should convert dates automatically" do
442
+ client = EmptyExample.new(test: Time.now.iso8601)
443
+ expect(client["test"]).to be_an_instance_of(DateTime)
444
+ end
445
+ end
446
+
447
+ context "whitelist specified" do
448
+ it "should only convert specified dates" do
449
+ time = Time.now.iso8601
450
+ client = WhitelistedDateExample.new(updated_at: time, created_at: time)
451
+ expect(client["updated_at"]).to be_an_instance_of(DateTime)
452
+ expect(client["created_at"]).to be_an_instance_of(String)
453
+ end
454
+ end
455
+ end
456
+
434
457
  end
@@ -99,6 +99,12 @@ describe Flexirest::Request do
99
99
  }
100
100
  end
101
101
 
102
+ class WhitelistedDateClient < Flexirest::Base
103
+ base_url "http://www.example.com"
104
+ put :conversion, "/put/:id"
105
+ parse_date :converted
106
+ end
107
+
102
108
  allow_any_instance_of(Flexirest::Request).to receive(:read_cached_response)
103
109
  end
104
110
 
@@ -287,6 +293,13 @@ describe Flexirest::Request do
287
293
  expect(object.not_converted).to be_an_instance_of(String)
288
294
  end
289
295
 
296
+ it "should convert date times in JSON if whitelisted" do
297
+ 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:{})))
298
+ object = WhitelistedDateClient.conversion id:1234, debug:true
299
+ expect(object.converted).to be_an_instance_of(DateTime)
300
+ expect(object.not_converted).to be_an_instance_of(String)
301
+ end
302
+
290
303
  it "should parse JSON and return a nice object for faked responses" do
291
304
  object = ExampleClient.fake id:1234, debug:true
292
305
  expect(object.result).to eq(true)
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.3.24
4
+ version: 1.3.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-14 00:00:00.000000000 Z
11
+ date: 2016-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler