service_caller 1.0.5 → 1.2.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
  SHA256:
3
- metadata.gz: a82a014594816863e1bfcc5595241ad98f46088584d3491b014b1a64c70a42c2
4
- data.tar.gz: 4c436447096e5d4eda6be6e3dc445cf9857686d57eab9a471a9c352de58db1c3
3
+ metadata.gz: baf3a2b14016c3d6c5d8edaaeb685b471fe044f341b499c881ce80d255fd9793
4
+ data.tar.gz: 7b0eb03312f9b820097f4f3cd2104aa8ec9c9bd203163f037fd2b1b97ba7d9bc
5
5
  SHA512:
6
- metadata.gz: 6bfda486178406cbb5d2a3f69a9c918d1b3726f5dec1ee635fccf3e1d4229bf24a2998f210a824edfe499999cd8c30454cbbe73dc731c215894bac7fca7c9d37
7
- data.tar.gz: 14808d05d2f90a70502d4c0f2978fc5e626a04942c23ee2f8a55caaea9ab33e2aafea7a607096f0bb7dcbb98868bc27dd4747a76e99e8897c177ce4e41ffb37b
6
+ metadata.gz: f96a41886c346c180dde240521dcdbfbbfe297a71f448e608120aeb51881adb1c45cda1ce03b6fd595b0a212b887c70a330d36c2eda60d7109cba0aca5db0482
7
+ data.tar.gz: 28aa75a07f70bf226d454366d8b76f20350c9c5fa28956b68d27c28566deb109fb8de8fd9129dfb65d79358924ed6df73ad9bf4ab5778f8afc79a025d2cfa1f6
data/README.md CHANGED
@@ -1,34 +1,81 @@
1
1
  # Service Caller
2
2
 
3
- ### How to Use
4
- * install gem
3
+ ## Installation
4
+ 1. Add `service_caller` in your app's `Gemfile`.
5
+
6
+ ``` ruby
7
+ # ruby version 2.7+ or later
8
+ gem 'service_caller', '~> 1.2.0'
9
+ # ruby version 2.6+ or eariler
10
+ gem 'service_caller', '~> 1.1.0'
5
11
  ```
6
- gem install service_caller-0.1.0.gem
12
+
13
+ 2. Then, in your project directory, install the gem manually from your shell, run:
14
+
15
+ ``` bash
16
+ # Download and install
17
+ $ bundle install
7
18
  ```
8
19
 
9
- * require gem
10
- ```ruby
11
- require 'service_caller'
20
+ ## Upgrading
21
+
22
+ If your project is already using an older version of service_caller, upgrade to the latest version with:
23
+
24
+ ``` bash
25
+ $ bundle update service_caller
12
26
  ```
13
27
 
28
+ ## Support of Ruby and Rails Version
29
+
30
+ | Ruby Version | Rails Verision | Service Caller Version |
31
+ | - | - | - |
32
+ | 2.6 or eariler | 5.x | 1.1.0 |
33
+ | 2.7, 3.0 later | 6.x | 1.2.0 |
34
+
35
+ ## Usage
36
+
14
37
  * define service & inherit from `ServiceCaller`
38
+ * **For ruby 2.6 or earlier** (ruby 2.7 may show deprecated warning message)
15
39
  ```ruby
16
40
  class [Custom Service] < ServiceCaller
17
41
  def initialize(*args)
18
42
  ...
43
+ @a = a
44
+ @b = b
19
45
  end
20
46
 
21
47
  def call
22
48
  ...
49
+ @result = "Your service result which you want to return"
23
50
  end
24
51
  end
25
52
  ```
53
+ * **For ruby 2.7 or ruby 3.0**
54
+ ``` ruby
55
+ class [Custom Service] < ServiceCaller
56
+ def initialize(*args, **hsh)
57
+ ...
58
+ @a = a
59
+ @b = b
60
+ end
26
61
 
27
- * call the service
62
+ def call
63
+ ...
64
+ @result = "Your service result which you want to return"
65
+ end
66
+ end
67
+ ```
68
+
69
+ * call the service (**For ruby 2.6 or earlier**)
28
70
  ```ruby
29
71
  service = [Custom Service].call(*args)
30
72
  ```
31
73
 
74
+ * call the service (**For ruby 2.7 or ruby 3.0**)
75
+ ```ruby
76
+ service = [Custom Service].call(*args, **hsh)
77
+ ```
78
+
32
79
  * check if success
33
80
  ```ruby
34
81
  service.success?
@@ -43,4 +90,42 @@ end
43
90
  * get error if failed
44
91
  ```ruby
45
92
  service.error
93
+ ```
94
+
95
+ ### Example
96
+
97
+ ``` ruby
98
+ class CalBmi < ServiceCaller
99
+ def initialize(member_name, height: 1.55, weight: 52)
100
+ @member_name = member_name
101
+ @height = height
102
+ @weight = weight
103
+ end
104
+
105
+ def call
106
+ bmi = calculate_bmi
107
+ raise ServiceError.new(:member_name_not_found, error_msg: 'not enter the name') if @member_name.blank?
108
+ @result = "#{@member_name}'s BMI is #{bmi}"
109
+ end
110
+
111
+ private
112
+
113
+ def calculate_bmi
114
+ (@weight / @height**2).round(2)
115
+ end
116
+ end
117
+
118
+
119
+ # Call the BMI Service
120
+
121
+ body_insight = {height: 1.80, weight: 73}
122
+ bmi = CalBmi.call('william', **body_insight)
123
+
124
+ bmi.success? # it will show the caller is success or failed
125
+ bmi.result # it will show you => william's BMI is 22.53
126
+
127
+ bmi = CalBmi.call('', **body_insight)
128
+ bmi.error # if failed, the service will raise the custom error => #<ServiceError: member_name_not_found>
129
+ bmi.error.key # :member_name_not_found
130
+ bmi.error.error_obj # {:error_msg=>"not enter the name"}
46
131
  ```
data/lib/service_error.rb CHANGED
@@ -4,7 +4,7 @@ class ServiceError < StandardError
4
4
 
5
5
  class << self
6
6
  def generate_from_exception(exception, append_info={})
7
- error_obj = {error_msg: "#{exception.class.name}: #{exception.message}"}
7
+ error_obj = {error_message: "#{exception.class.name}: #{exception.message}"}
8
8
  error_obj.merge!(append_info)
9
9
  service_err = new(:internal_error, error_obj)
10
10
  service_err.set_backtrace(exception.backtrace)
@@ -14,7 +14,8 @@ class ServiceError < StandardError
14
14
 
15
15
  def initialize(key, error_obj={})
16
16
  @key = key
17
+ error_message = error_obj.delete(:error_message) || key
17
18
  @error_obj = error_obj
18
- super(error_obj[:error_msg] || key)
19
+ super(error_message)
19
20
  end
20
21
  end
@@ -3,8 +3,8 @@ module ServiceExtend
3
3
  attr_reader :error, :result
4
4
 
5
5
  module ClassMethods
6
- def call(*args)
7
- service = new(*args)
6
+ def call(*args, **hsh)
7
+ service = new(*args, **hsh)
8
8
  service.call
9
9
  service
10
10
  end
@@ -2,13 +2,13 @@ module ServiceExtend
2
2
  module Handler
3
3
 
4
4
  def call
5
- if defined?(::Rails::Engine)
5
+ if defined?(::Rails::Engine) && defined?(::ActiveRecord)
6
6
  begin
7
7
  super
8
8
  rescue ServiceError => e
9
9
  @error = e
10
10
  rescue ActiveRecord::RecordNotFound => e
11
- @error = ServiceError.new(:record_not_found, error_msg: e.message)
11
+ @error = ServiceError.new(:record_not_found, error_message: e.message)
12
12
  @error.set_backtrace(e.backtrace)
13
13
  rescue Exception => e
14
14
  @error = ServiceError.generate_from_exception(e)
@@ -25,4 +25,4 @@ module ServiceExtend
25
25
  end
26
26
 
27
27
  end
28
- end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_caller
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JiaRou Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-29 00:00:00.000000000 Z
11
+ date: 2021-10-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: basic implement of service object
14
14
  email: laura34963@kdanmobile.com
@@ -35,14 +35,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
35
35
  requirements:
36
36
  - - ">="
37
37
  - !ruby/object:Gem::Version
38
- version: 2.5.1
38
+ version: 2.7.0
39
39
  required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
43
  version: '0'
44
44
  requirements: []
45
- rubygems_version: 3.0.1
45
+ rubygems_version: 3.2.22
46
46
  signing_key:
47
47
  specification_version: 4
48
48
  summary: Ruby Service Basic Class