lite-service 1.0.1 → 1.0.2

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: 5e6494a1b7ccefe36f2aee1a17040806bb8c4d80a737a26d0e19fedecfd79b68
4
- data.tar.gz: f03bbccc41398f75e59182e7177c580e7b54efa9e189795a2f4cf9665ab472bc
3
+ metadata.gz: fbffb01cc4f2a0cc3ed9c2581adf7f903208b1bf888a34740a9d12ee63ecf7c1
4
+ data.tar.gz: 41597b9d29fcd29af77c4ab5c10c1dd030193ef72433316a4d78fa93fb7ca348
5
5
  SHA512:
6
- metadata.gz: 911d83e318b8878da98e35808b4121baf0c2661299eed3c8cc9e7fe94ce08faf8c867835a8f8ea059c53e41ea10ae4d4ef1e11316371001774eaf1f360f1cdc1
7
- data.tar.gz: 9d292b6f38cd39b008c10f90c09bfc9aaa789851c4edc08a88bb122aa095896338e2a4aeb16bf5280e4cb4d47597844b4a82c6f891b90a5e61b417876a154d21
6
+ metadata.gz: 2549a3c693bab9643b2e917947f4c43bfb12714a35dd5947033a9b396df38c6f426d938f9b117d9db2a2b454147359f2c250ea825f481be51a8980b0ab59a8a9
7
+ data.tar.gz: b9f9720664c1cbd5188de94d02ad86324634d1477db0810d06a5de60de63615c1a7d02d1cfb9c0b651edd78a7cfedae00edc609874585248c0d7f290807a9515
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.0.1] - 2019-06-25
10
+ ### Changed
11
+ - Changed command module to class so that it's inheritable
12
+
9
13
  ## [1.0.0] - 2019-06-24
10
14
  ### Added
11
15
  - Initial project version
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-service (1.0.1)
4
+ lite-service (1.0.2)
5
5
  lite-errors
6
6
  lite-memoize
7
7
 
data/README.md CHANGED
@@ -3,7 +3,8 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/lite-service.svg)](http://badge.fury.io/rb/lite-service)
4
4
  [![Build Status](https://travis-ci.org/drexed/lite-service.svg?branch=master)](https://travis-ci.org/drexed/lite-service)
5
5
 
6
- Lite::Service provides an API for building service objects.
6
+ Lite::Service provides an API for building command based service objects.
7
+ It also has mixins for handling errors and memoization.
7
8
 
8
9
  ## Installation
9
10
 
@@ -24,45 +25,40 @@ Or install it yourself as:
24
25
  ## Table of Contents
25
26
 
26
27
  * [Setup](#setup)
27
- * [Usage](#usage)
28
+ * [Base](#base)
29
+ * [Errors](#errors)
30
+ * [Memoize](#memoize)
28
31
 
29
32
  ## Setup
30
33
 
31
34
  Setting up the service object is very easy and provides a high level API for memoizing pre-resulted
32
35
  values and for surfacing errors.
33
36
 
34
- Learn more about using memoization subclass in [Lite::Memoize](https://github.com/drexed/lite-memoize)
35
- and errors subclass [Lite::Errors](https://github.com/drexed/lite-errors).
36
-
37
37
  ```ruby
38
- class SearchMovies < Lite::Service::Command
38
+ class SearchMovies < Lite::Service::Base
39
39
 
40
40
  def initialize(name)
41
41
  @name = name
42
42
  end
43
43
 
44
- # NOTE: This method is required to call the command
45
- def run
44
+ # NOTE: This method is required
45
+ def command
46
46
  { generate_fingerprint => movies_by_name }
47
47
  end
48
48
 
49
49
  private
50
50
 
51
51
  def movies_by_name
52
- cache.memoize { HTTP.get("http://movies.com?title=#{title}") }
52
+ HTTP.get("http://movies.com?title=#{title}")
53
53
  end
54
54
 
55
55
  def generate_fingerprint
56
- Digest::MD5.hexdigest(find_by_name)
57
- rescue
58
- errors.add(:fingerprint, 'invalid movie name')
56
+ Digest::MD5.hexdigest(movies_by_name)
59
57
  end
60
58
 
61
59
  end
62
60
  ```
63
61
 
64
- ## Usage
65
-
66
62
  **Caller**
67
63
 
68
64
  ```ruby
@@ -70,48 +66,97 @@ service = SearchMovies.new('Toy Story')
70
66
  service.called? #=> false
71
67
  service.call #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
72
68
  service.called? #=> true
73
- service.run #=> Returns a fresh uncached value
74
69
 
75
70
  # or
76
71
 
77
72
  service = SearchMovies.call('Toy Story')
78
73
  service.called? #=> true
74
+ service.call #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
75
+ ```
76
+
77
+ **Result**
78
+
79
+ ```ruby
80
+ service = SearchMovies.new('Toy Story')
81
+ service.result #=> nil
82
+
83
+ service.call #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
79
84
  service.result #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
85
+
86
+ service.recall! #=> Clears the call, cache, errors, and then re-performs the call
87
+ service.result #=> { 'fingerprint_2' => [ 'Toy Story 2', ... ] }
80
88
  ```
81
89
 
82
- **Cache**
90
+ ## Errors (optional)
91
+
92
+ Learn more about using [Lite::Errors](https://github.com/drexed/lite-errors)
83
93
 
84
94
  ```ruby
85
- service = SearchMovies.call('Toy Story')
86
- service.cache #=> Lite::Memoize::Instance object
95
+ class SearchMovies < Lite::Service::Base
96
+ include Lite::Service::Errors
97
+
98
+ # ... ommited ...
99
+
100
+ private
101
+
102
+ # Add a fingerprint error to the error pool
103
+ def generate_fingerprint
104
+ Digest::MD5.hexdigest(movies_by_name)
105
+ rescue
106
+ errors.add(:fingerprint, 'invalid md5 request value')
107
+ end
108
+
109
+ end
87
110
  ```
88
111
 
89
- **Errors**
112
+ **Methods**
90
113
 
91
114
  ```ruby
92
115
  service = SearchMovies.call('Toy Story')
116
+ service.errors #=> Lite::Errors::Messages object
117
+
93
118
  service.validate! #=> Raises Lite::Service::ValidationError if it has any errors
94
119
  service.valid? #=> Alias for validate!
95
120
 
96
- service.errors #=> Lite::Errors::Messages object
97
121
  service.errored? #=> false
98
122
  service.success? #=> true
99
123
  service.failure? #=> Checks that it has been called and has errors
124
+
125
+ service.result! #=> Raises Lite::Service::ValidationError if it has any errors, if not it returns the result
100
126
  ```
101
127
 
102
- **Result**
128
+ ## Memoize (optional)
129
+
130
+ Learn more about using [Lite::Memoize](https://github.com/drexed/lite-memoize)
103
131
 
104
132
  ```ruby
105
- service = SearchMovies.new('Toy Story')
106
- service.result #=> nil
133
+ class SearchMovies < Lite::Service::Base
134
+ include Lite::Service::Memoize
107
135
 
108
- service.call #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
109
- service.result #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
136
+ # ... ommited ...
110
137
 
111
- service.recall! #=> Clears the call, cache, errors, and then re-performs the call
112
- service.result #=> { 'fingerprint_2' => [ 'Toy Story 2', ... ] }
138
+ private
113
139
 
114
- service.result! #=> Raises Lite::Service::ValidationError if it has any errors
140
+ # Sets the value in the cache
141
+ # Subsequent method calls gets the cached value
142
+ # This saves you the extra external HTTP.get call
143
+ def movies_by_name
144
+ cache.memoize { HTTP.get("http://movies.com?title=#{title}") }
145
+ end
146
+
147
+ # Gets the value in the cache
148
+ def generate_fingerprint
149
+ Digest::MD5.hexdigest(movies_by_name)
150
+ end
151
+
152
+ end
153
+ ```
154
+
155
+ **Methods**
156
+
157
+ ```ruby
158
+ service = SearchMovies.call('Toy Story')
159
+ service.cache #=> Lite::Memoize::Instance object
115
160
  ```
116
161
 
117
162
  ## Development
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- %w[version exception command].each do |name|
3
+ %w[version exception errors memoize base].each do |name|
4
4
  require "lite/service/#{name}"
5
5
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Service
5
+ class Base
6
+
7
+ class << self
8
+
9
+ def call(*args)
10
+ klass = new(*args)
11
+ klass.call
12
+ klass
13
+ end
14
+
15
+ end
16
+
17
+ attr_reader :result
18
+
19
+ def initialize(*args)
20
+ @args = args
21
+ end
22
+
23
+ def call
24
+ raise Lite::Service::NotImplementedError unless defined?(command)
25
+ return @result if called?
26
+
27
+ @called = true
28
+ @result = command
29
+ end
30
+
31
+ def called?
32
+ @called ||= false
33
+ end
34
+
35
+ def recall!
36
+ @called = false
37
+ %i[cache errors].each { |mixin| send(mixin).clear if respond_to?(mixin) }
38
+ call
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lite/errors'
4
+
5
+ module Lite
6
+ module Service
7
+ module Errors
8
+
9
+ def errors
10
+ @errors ||= Lite::Errors::Messages.new
11
+ end
12
+
13
+ def errored?
14
+ !errors.empty?
15
+ end
16
+
17
+ def fail!
18
+ raise Lite::Service::ValidationError
19
+ end
20
+
21
+ def failure?
22
+ called? && errored?
23
+ end
24
+
25
+ def result!
26
+ result if valid?
27
+ end
28
+
29
+ def success?
30
+ called? && !errored?
31
+ end
32
+
33
+ def validate!
34
+ return true if success?
35
+
36
+ fail!
37
+ end
38
+
39
+ alias valid? validate!
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lite/memoize'
4
+
5
+ module Lite
6
+ module Service
7
+ module Memoize
8
+
9
+ def cache
10
+ @cache ||= Lite::Memoize::Instance.new
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Service
5
5
 
6
- VERSION ||= '1.0.1'
6
+ VERSION ||= '1.0.2'
7
7
 
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-25 00:00:00.000000000 Z
11
+ date: 2019-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lite-errors
@@ -159,8 +159,10 @@ files:
159
159
  - bin/console
160
160
  - bin/setup
161
161
  - lib/lite/service.rb
162
- - lib/lite/service/command.rb
162
+ - lib/lite/service/base.rb
163
+ - lib/lite/service/errors.rb
163
164
  - lib/lite/service/exception.rb
165
+ - lib/lite/service/memoize.rb
164
166
  - lib/lite/service/version.rb
165
167
  - lite-service.gemspec
166
168
  homepage: http://drexed.github.io/lite-service
@@ -1,82 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'lite/errors'
4
- require 'lite/memoize'
5
-
6
- module Lite
7
- module Service
8
- class Command
9
-
10
- class << self
11
-
12
- def call(*args)
13
- klass = new(*args)
14
- klass.call
15
- klass
16
- end
17
-
18
- end
19
-
20
- attr_reader :result
21
-
22
- def initialize(*args)
23
- @args = args
24
- end
25
-
26
- def call
27
- raise Lite::Service::NotImplementedError unless defined?(run)
28
- return @result if called?
29
-
30
- @called = true
31
- @result = run
32
- end
33
-
34
- def cache
35
- @cache ||= Lite::Memoize::Instance.new
36
- end
37
-
38
- def called?
39
- @called ||= false
40
- end
41
-
42
- def errors
43
- @errors ||= Lite::Errors::Messages.new
44
- end
45
-
46
- def errored?
47
- !errors.empty?
48
- end
49
-
50
- def fail!
51
- raise Lite::Service::ValidationError
52
- end
53
-
54
- def failure?
55
- called? && errored?
56
- end
57
-
58
- def recall!
59
- @called = false
60
- [cache, errors].each(&:clear)
61
- call
62
- end
63
-
64
- def result!
65
- result if valid?
66
- end
67
-
68
- def success?
69
- called? && !errored?
70
- end
71
-
72
- def validate!
73
- return true if success?
74
-
75
- fail!
76
- end
77
-
78
- alias valid? validate!
79
-
80
- end
81
- end
82
- end