services_base 0.3.0 → 0.4.0

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: 76f9b2dbd6def46e2fd9d04e111922114aa491b6
4
- data.tar.gz: a30277e540d0f3cc81059462a66c0b350a0142f6
3
+ metadata.gz: a0ccf1501a60361ff4746fe5e597a45bcd99f851
4
+ data.tar.gz: c89233b63b68af9f60d2819fede2f631e7a225af
5
5
  SHA512:
6
- metadata.gz: 56e85f8247cde3512526a1b4b4ba5dc66e237b0649c1629bf988f6cd3bc1ea32ac7af1d0d56767c8744805cf2260b3e8502305ccd9b744b6cbae61875dcc3530
7
- data.tar.gz: 0f2672dddac5b2b98b715532a6eacfe0e68385c1c9179a35476fa079112e65e5a4fcd8541c01e596af005f8a9ffe9eb354565932c5c71bcf53b3012b263338c0
6
+ metadata.gz: f84ea0cbb0088f1fc89a23a277d5f0237b96f142d45b39852a444dabd48a3d9ce4bad25f34f8ea0f208c20c898f917a30def7ea24e72fb704e08a2b2771a2da9
7
+ data.tar.gz: 36112023f6dfd8f4ed2ebee787e10abf4b61b88ebc83a983fbbcada06b29982c37e62f7b6e411500f4c91f842bc5adbcaed2aee95b62ed5ca3738ce82ea91faf
data/.gitignore CHANGED
@@ -8,4 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.gem
11
-
11
+ .byebug_history
data/README.md CHANGED
@@ -38,6 +38,36 @@ class Services::MyService
38
38
  return Services::Responses::Error.new(result_object, error_message: 'Something went wrong', error_code: 123)
39
39
  end
40
40
  end
41
+
42
+ response = Services::MyService.call(123)
43
+ response.is_a?(Services::Responses::Success) # true when it's a success
44
+ response.is_a?(Services::Responses::Error) # true when it's an error
45
+ response.is_a?(Services::Responses::Base) # always true
46
+
47
+ response.success? # true/false
48
+ response.result # the resulting object
49
+ response.error_code # available when there's an error
50
+ response.error_message # available when there's an error
51
+ ```
52
+
53
+ ## Configuration
54
+
55
+ When running a service asynchronously (ie: in Sidekiq or Delayed Job), it may be desirable to raise an exception instead of returning a `Services::Responses::Error` in order to retry the job at a later time. It is easy to do so:
56
+
57
+ ```ruby
58
+ Services::Responses.configure do |config|
59
+ config.raise_exception_on_async_error = true
60
+ end
61
+ ```
62
+
63
+ When `raise_exception_on_async_error` is set to `true`, an exception will be thrown whenever a new
64
+ `Services::Responses::Error` is initialized during an asynchronous job.
65
+
66
+ Alternatively, you can raise an exception only for a specific `Error` object:
67
+
68
+ ```ruby
69
+ # will raise an exception ONLY if it's called from an async job.
70
+ Services::Responses::Error.new(my_exception, raise_exception_on_async_error: true)
41
71
  ```
42
72
 
43
73
  ## License
@@ -15,6 +15,9 @@ module Services
15
15
  !success?
16
16
  end
17
17
 
18
+ def self.async_environment?
19
+ (defined?(Sidekiq) && !!Sidekiq.server?) || (ENV["_"] || "").include?("delayed_job") || (ENV["_"] || "").include?("resque")
20
+ end
18
21
  end
19
22
  end
20
23
  end
@@ -0,0 +1,19 @@
1
+ module Services::Responses
2
+ class << self
3
+ attr_accessor :configuration
4
+ end
5
+
6
+ def self.configure
7
+ yield(configuration)
8
+ end
9
+
10
+ class Configuration
11
+ attr_accessor :raise_exception_on_async_error
12
+
13
+ def initialize
14
+ @raise_exception_on_async_error = false
15
+ end
16
+ end
17
+ end
18
+
19
+ Services::Responses.configuration ||= Services::Responses::Configuration.new
@@ -3,13 +3,33 @@ module Services
3
3
  class Error < Services::Responses::Base
4
4
  attr_accessor :error_message, :error_code
5
5
 
6
- def initialize(result, error_message: nil, error_code: nil)
7
- @result, @error_message, @error_code = result, error_message, error_code
6
+ def initialize(result, error_message: nil, error_code: nil, raise_exception_on_async_error: nil)
7
+ @result, @error_message, @error_code, @raise_exception_on_async_error =
8
+ result, error_message, error_code, raise_exception_on_async_error
9
+
10
+ raise_if_async!
8
11
  end
9
12
 
10
13
  def success?
11
14
  false
12
15
  end
16
+
17
+ protected
18
+ def raise_if_async!
19
+ return unless should_raise?
20
+
21
+ if @result.is_a?(Exception)
22
+ raise @result
23
+ else
24
+ err = ("#{@error_message} " + "[#{@error_code}]".gsub("[]", "")).strip
25
+ raise StandardError.new(err)
26
+ end
27
+ end
28
+
29
+ def should_raise?
30
+ (Services::Responses.configuration.raise_exception_on_async_error || @raise_exception_on_async_error) \
31
+ && Services::Responses::Base.async_environment?
32
+ end
13
33
  end
14
34
  end
15
35
  end
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/services_base.rb CHANGED
@@ -3,3 +3,4 @@ require "services/base"
3
3
  require "services/responses/base"
4
4
  require "services/responses/error"
5
5
  require "services/responses/success"
6
+ require "services/responses/configuration"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: services_base
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Mercier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-15 00:00:00.000000000 Z
11
+ date: 2018-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,6 +100,7 @@ files:
100
100
  - bin/setup
101
101
  - lib/services/base.rb
102
102
  - lib/services/responses/base.rb
103
+ - lib/services/responses/configuration.rb
103
104
  - lib/services/responses/error.rb
104
105
  - lib/services/responses/success.rb
105
106
  - lib/services/version.rb
@@ -125,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
126
  version: '0'
126
127
  requirements: []
127
128
  rubyforge_project:
128
- rubygems_version: 2.5.1
129
+ rubygems_version: 2.6.13
129
130
  signing_key:
130
131
  specification_version: 4
131
132
  summary: Simple skeleton for implementing consistent Service Objects and Micro-Services