patiently_try 0.2.1 → 0.2.2

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: 78e0754b4d341a4aa9b2774d0f21537aff3f6b25
4
- data.tar.gz: 049c95a74e26aa87acab6017a19052a8f9403432
3
+ metadata.gz: a67075f35f07ddcedf9b7bd67747901250c6bae7
4
+ data.tar.gz: b4adc86463ad4338aa75c974520d84be69a6d4d7
5
5
  SHA512:
6
- metadata.gz: 0cc1c84c04f16a5931539092e67f7c948c949b4868fc32873e685049f019d6878fcf876849dc9c6612e2bd3cc2e6b2cbcdaca5ebacf82154b560fe71a8763557
7
- data.tar.gz: f8cad3534ecffd68a9bbfba948ae61b43e096a44288bcd985ed64dea3be9825cbff9b68a0f6e25e68fda8b77bd28228d16bd0a298d42632efbc661533abe4136
6
+ metadata.gz: e1e0f7d03d5aa818e02a5594a93eef480c60a326d4ccbbbd3bdd19814603ae6fbcaa9e821b789921db76246f5f36d30e5d3498a07815c4748bddea10b2e5f188
7
+ data.tar.gz: 53a35d48517d0a9882783d4f6febfec314f00a57cea2fc76a205ffa69b70f885917cd91a20287bf418a89578b4c704f96ff05e49999a5454b1e842fe581434f8
data/.travis.yml CHANGED
@@ -1,5 +1,16 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=c64783cbfb92f2f65f706d60f15269d7113dcc39a4824bbfb1be0569c26ea266
1
4
  sudo: false
2
5
  language: ruby
3
6
  rvm:
4
7
  - 2.3.1
5
8
  before_install: gem install bundler -v 1.12.2
9
+ before_script:
10
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
11
+ - chmod +x ./cc-test-reporter
12
+ - ./cc-test-reporter before-build
13
+ script:
14
+ - bundle exec rspec
15
+ after_script:
16
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/patiently_try.svg)](https://badge.fury.io/rb/patiently_try)
2
2
  [![Build Status](https://travis-ci.org/Ninigi/patiently_try.svg?branch=master)](https://travis-ci.org/Ninigi/patiently_try)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/f9046959d31212330821/maintainability)](https://codeclimate.com/github/Ninigi/patiently_try/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/f9046959d31212330821/test_coverage)](https://codeclimate.com/github/Ninigi/patiently_try/test_coverage)
3
5
 
4
6
  # PatientlyTry
5
7
 
data/lib/patiently_try.rb CHANGED
@@ -2,36 +2,69 @@ require "patiently_try/version"
2
2
 
3
3
  module PatientlyTry
4
4
  def patiently_try(opts = {})
5
- retries = opts[:retries] || 100
6
- wait = opts[:wait] || 0
7
- catch_errors = Array(opts[:catch] || StandardError)
8
- excluded_errors = Array(opts[:raise_if_caught])
9
- logging = opts[:logging].nil? ? true : opts[:logging]
10
- try = 0
5
+ opts = _parse_opts(opts)
6
+ try = 0
11
7
 
12
8
  begin
13
9
  yield
14
- rescue *(catch_errors) => e
10
+ rescue *(opts[:catch]) => e
15
11
  try += 1
16
12
 
17
- _log_error(e) if logging
13
+ _rescue_or_raise(e, try, opts)
18
14
 
19
- if try >= retries || _rescued_but_excluded?(e, excluded_errors)
20
- _log_backtrace(e) if logging
21
- raise e
22
- end
15
+ _wait(opts[:wait])
16
+ retry
17
+ end
18
+ end
19
+
20
+ private
23
21
 
24
- _log_retry(e) if logging
22
+ def _rescue_or_raise(e, try, opts)
23
+ if opts[:logging]
24
+ _rescue_or_raise_with_logging(e, try, opts)
25
+ else
26
+ _rescue_or_raise_without_logging(e, try, opts)
27
+ end
28
+ end
25
29
 
26
- sleep wait if wait && wait > 0
30
+ def _rescue_or_raise_with_logging(e, try, opts)
31
+ _log_error(e) if opts[:logging]
27
32
 
28
- retry
33
+ if _should_raise?(e, try, opts)
34
+ _log_backtrace(e) if opts[:logging]
35
+ raise e
29
36
  end
37
+
38
+ _log_retry(try, opts[:retries]) if opts[:logging]
30
39
  end
31
40
 
32
- private
41
+ def _rescue_or_raise_without_logging(e, try, opts)
42
+ raise e if _should_raise?(e, try, opts)
43
+ end
44
+
45
+ def _should_raise?(e, try, opts)
46
+ _exceeded_retries?(try, opts[:retries]) || _rescued_but_excluded?(e, opts[:raise_if_caught])
47
+ end
48
+
49
+ def _parse_opts(opts)
50
+ {
51
+ catch: Array(opts[:catch] || StandardError),
52
+ raise_if_caught: Array(opts[:raise_if_caught]),
53
+ wait: opts[:wait].to_i,
54
+ logging: opts[:logging].nil? ? true : opts[:logging],
55
+ retries: opts[:retries] || 100
56
+ }
57
+ end
58
+
59
+ def _wait(wait_time)
60
+ sleep wait_time if wait_time > 0
61
+ end
62
+
63
+ def _exceeded_retries?(try, retries)
64
+ try >= retries
65
+ end
33
66
 
34
- def _log_retry
67
+ def _log_retry(try, retries)
35
68
  puts "Retrying (#{try}/#{retries})"
36
69
  end
37
70
 
@@ -1,3 +1,3 @@
1
1
  module PatientlyTry
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patiently_try
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabian Zitter
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-18 00:00:00.000000000 Z
11
+ date: 2018-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler