action_service 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -57
- data/lib/action_service.rb +5 -23
- data/lib/action_service/version.rb +1 -1
- data/lib/generators/application_service/application_service_generator.rb +4 -7
- data/lib/generators/service/service_generator.rb +2 -4
- data/lib/generators/service/templates/service.rb.tt +6 -8
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1380fb36f26ce37de08cac1a1234a2ba69526a93e8481975bc342f89ffef03d
|
4
|
+
data.tar.gz: 0b060a1922cf2994daa803e881180b2c7c6a55a03100c5c28599a98b16784e51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 961f51fd421624c0d486c7da5f4a73e65c38fc6b99daaf9abf8f5dfb52f15e51ac15ec70e5e95e61f4ac2fcfabe19fa7c409fcae71eb9a5bfa2ad19b09eb3a8d
|
7
|
+
data.tar.gz: e1f3e3399f218967483bc612edbe059cbe87d22979cb290f3fa6304d9f9677fe4e4da584c2380725892c006d54d15c9fa39c8246e6d99420b5dd2fc4db5e3932
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
# ActionService
|
1
|
+
# ActionService v2.x
|
2
2
|
Welcome to Action Service gem, is a ruby gem to create and interact easily with services.
|
3
3
|
|
4
4
|
## Why services
|
5
|
-
|
5
|
+
This is where you can add your code to perform a simple function instead of making complex controllers or models.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
Add this line to your application's Gemfile:
|
@@ -22,7 +22,7 @@ $ gem install action_service
|
|
22
22
|
```
|
23
23
|
|
24
24
|
## Setting up
|
25
|
-
|
25
|
+
To start to use the gem execute:
|
26
26
|
```bash
|
27
27
|
$ rails g service:application_service
|
28
28
|
```
|
@@ -42,84 +42,79 @@ Running via Spring preloader in process 30051
|
|
42
42
|
And when you open `authenticate_service.rb` will be like this
|
43
43
|
```ruby
|
44
44
|
class Admin::AuthenticateService < ApplicationService
|
45
|
+
def initialize()
|
46
|
+
super()
|
47
|
+
end
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
def call
|
51
|
-
return self
|
52
|
-
end
|
53
|
-
|
49
|
+
def call
|
50
|
+
self
|
51
|
+
end
|
54
52
|
end
|
55
53
|
```
|
56
54
|
|
57
55
|
### ApplicationService class
|
58
|
-
All generated services are
|
56
|
+
All generated services are inheriting from `ApplcaitonService`, `ApplicationService` is inherited from `ActoinService::Base, So all service will contain:
|
59
57
|
#### Three instance variables
|
60
|
-
1. `@success` is a boolean with `true` as default value
|
61
|
-
2. `@errors` is
|
58
|
+
1. `@success` is a boolean with `true` as the default value, and if you add any error this flag will be changed to `false`.
|
59
|
+
2. `@errors` is an array containing a list of errors.
|
62
60
|
3. `@response` is a hash to add service response (values, objects, etc.).
|
63
61
|
#### Six instance methods
|
64
|
-
1. `success?` to get boolean if service
|
65
|
-
2. `errors` to get list of errors.
|
66
|
-
3. `response` to get response hash.
|
62
|
+
1. `success?` to get boolean if service is executed successfully or not (based on adding errors).
|
63
|
+
2. `errors` to get a list of errors.
|
64
|
+
3. `response` to get the response hash.
|
67
65
|
4. `add_error(error_message)` to add error (will change `@success` to `false`), Example `add_error("wrong admin id")`.
|
68
66
|
5. `add_errors(*error_messages)` to add errors as parameters or array (will change `@success` to `false`), Example `add_errors("email is required","phone number is aready exist")` OR `add_errors(["email is required","phone number is aready exist"])`.
|
69
|
-
6. `add_errors_array(error_messages_array)` to add array of errors (will change `@success` to `false`), Example `add_errors_array(["email is required","phone number is aready exist"])`.
|
70
67
|
|
71
68
|
Now, let's implement `Admin::AuthenticateService`
|
72
69
|
```ruby
|
73
70
|
class Admin::AuthenticateService < ApplicationService
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
71
|
+
def initialize(email, password)
|
72
|
+
super()
|
73
|
+
@admin = Admin.find_by(email: email)
|
74
|
+
@password = password
|
75
|
+
end
|
76
|
+
|
77
|
+
def call
|
78
|
+
add_error "wrong admin email" and return self if not @admin
|
79
|
+
add_error "wrong password" and return self if not @admin.authenticate(@password)
|
80
|
+
@response[:admin] = @admin
|
81
|
+
self
|
82
|
+
end
|
87
83
|
end
|
88
84
|
```
|
89
85
|
|
90
|
-
This service is now ready to be used, For example we will call the service inside a controller
|
86
|
+
This service is now ready to be used, For example, we will call the service inside a controller
|
91
87
|
```ruby
|
92
88
|
result = Admin::AuthenticateService.new(params[:email], params[:password]).call
|
93
89
|
if result.success?
|
94
|
-
|
95
|
-
|
96
|
-
else
|
97
|
-
|
98
|
-
|
90
|
+
# {success: true, response: {admin object}, errors: []}
|
91
|
+
render json: { success: result.success?, response: result.response, errors: result.errors }
|
92
|
+
else
|
93
|
+
# {success: false, response: {}, errors: ["wrong email" OR "wrong password"]}
|
94
|
+
render json: { success: result.success?, response: result.response, errors: result.errors }, status: :unauthorized
|
99
95
|
end
|
100
96
|
```
|
101
97
|
|
102
|
-
#### Services layer will help you to write clean code, by small models, controller and DRY code (don't repeat yourself), you can use service inside another service and stop
|
98
|
+
#### Services layer will help you to write clean code, by small models, controller and DRY code (don't repeat yourself), you can use service inside another service and stop executing the first service if the second one fails, Example:
|
103
99
|
```ruby
|
104
100
|
class Cache::List::AddHashService < ApplicationService
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
end
|
101
|
+
def initialize(list_key, hash_key, hash_data, expiry_datetime=nil)
|
102
|
+
super()
|
103
|
+
@list_key = list_key
|
104
|
+
@hash_key = hash_key
|
105
|
+
@hash = hash_data
|
106
|
+
@expiry_datetime = expiry_datetime
|
107
|
+
end
|
108
|
+
|
109
|
+
def call
|
110
|
+
result = Cache::Hash::SetService.new(hash_key, hash_data, @expiry_datetime).call
|
111
|
+
# if Cache::Hash::SetService fails will stop excuting and return the errors
|
112
|
+
add_errors result.errors and return self if not result.success?
|
113
|
+
result = Cache::List::AddService.new(list_key, hash_key, @expiry_datetime).call
|
114
|
+
# if Cache::List::AddService fails will stop excuting and return the errors
|
115
|
+
add_errors result.errors and return self if not result.success?
|
116
|
+
self
|
117
|
+
end
|
123
118
|
end
|
124
119
|
```
|
125
120
|
|
@@ -127,4 +122,4 @@ end
|
|
127
122
|
Bug reports and pull requests are welcome on GitHub at [https://github.com/abdofawzi5/action_service](https://github.com/abdofawzi5/action_service). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
128
123
|
|
129
124
|
## License
|
130
|
-
The gem is available as open
|
125
|
+
The gem is available as open-source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/action_service.rb
CHANGED
@@ -4,24 +4,16 @@ require 'generators/service/service_generator'
|
|
4
4
|
require 'generators/application_service/application_service_generator'
|
5
5
|
|
6
6
|
module ActionService
|
7
|
-
|
8
|
-
|
9
7
|
# ActionService::Base is the parent class for all services that will be generated.
|
10
8
|
class Base
|
11
|
-
|
9
|
+
attr_accessor :errors, :response
|
10
|
+
|
11
|
+
def initialize
|
12
12
|
@errors = [] # contain errors
|
13
13
|
@response = {} # contain the service response data
|
14
14
|
@success = true # flag service execution without any error
|
15
15
|
end
|
16
16
|
|
17
|
-
def errors
|
18
|
-
@errors
|
19
|
-
end
|
20
|
-
|
21
|
-
def response
|
22
|
-
@response
|
23
|
-
end
|
24
|
-
|
25
17
|
def success?
|
26
18
|
@success
|
27
19
|
end
|
@@ -33,18 +25,8 @@ module ActionService
|
|
33
25
|
|
34
26
|
def add_errors(*error_messages)
|
35
27
|
@success = false
|
36
|
-
if error_messages[0].
|
37
|
-
|
38
|
-
add_errors_array(error_messages[0])
|
39
|
-
else
|
40
|
-
@errors += error_messages
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def add_errors_array(error_messages_array)
|
45
|
-
@success = false
|
46
|
-
@errors += error_messages_array
|
28
|
+
error_messages = error_messages[0] if error_messages[0].is_a?
|
29
|
+
@errors += error_messages
|
47
30
|
end
|
48
31
|
end
|
49
|
-
|
50
32
|
end
|
@@ -1,18 +1,15 @@
|
|
1
1
|
module Service
|
2
2
|
class ApplicationServiceGenerator < Rails::Generators::Base
|
3
|
-
|
4
|
-
source_root File.expand_path("templates", __dir__)
|
3
|
+
source_root File.expand_path('templates', __dir__)
|
5
4
|
|
6
5
|
def create_application_service
|
7
|
-
template
|
6
|
+
template 'application_service.rb', application_service_file_name
|
8
7
|
end
|
9
8
|
|
10
9
|
private
|
11
10
|
|
12
11
|
def application_service_file_name
|
13
|
-
@
|
12
|
+
@application_service_file_name ||= 'app/services/application_service.rb'
|
14
13
|
end
|
15
|
-
|
16
|
-
|
17
14
|
end
|
18
|
-
end
|
15
|
+
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
module Service
|
2
2
|
class ServiceGenerator < Rails::Generators::NamedBase
|
3
|
-
|
4
3
|
source_root File.expand_path('templates', __dir__)
|
5
4
|
|
6
5
|
def create_service_file
|
@@ -10,12 +9,11 @@ module Service
|
|
10
9
|
private
|
11
10
|
|
12
11
|
def file_name
|
13
|
-
@
|
12
|
+
@file_name ||= remove_possible_suffix(super)
|
14
13
|
end
|
15
14
|
|
16
15
|
def remove_possible_suffix(name)
|
17
|
-
name.sub(/_?service$/i,
|
16
|
+
name.sub(/_?service$/i, '')
|
18
17
|
end
|
19
|
-
|
20
18
|
end
|
21
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Abdo Fawzi
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Is where you can add your code to do simple functionality instead of
|
14
14
|
make complex controller or models.
|
@@ -34,7 +34,7 @@ homepage: https://github.com/abdofawzi5/action_service
|
|
34
34
|
licenses:
|
35
35
|
- MIT
|
36
36
|
metadata: {}
|
37
|
-
post_install_message:
|
37
|
+
post_install_message:
|
38
38
|
rdoc_options: []
|
39
39
|
require_paths:
|
40
40
|
- lib
|
@@ -49,9 +49,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: '0'
|
51
51
|
requirements: []
|
52
|
-
|
53
|
-
|
54
|
-
signing_key:
|
52
|
+
rubygems_version: 3.1.4
|
53
|
+
signing_key:
|
55
54
|
specification_version: 4
|
56
55
|
summary: Is a ruby gem to create and interact easily with services.
|
57
56
|
test_files: []
|