polist 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/polist/service.rb +62 -60
- data/lib/polist/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 553f882315c30d976b80ff25c0b94b3375cf09b2
|
4
|
+
data.tar.gz: 93d2e330621e78e12d6c3e9f795ed4f96f974ce7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68ae4882f299838b62632e67f4b66048689c99c65e871fa77ae74dacb4c5690c079ac8e15a01d836393435d91a43e20cf53c918158b9de313861874a8ebb0f93
|
7
|
+
data.tar.gz: 6128535be8447deb5c5d1cb36deac5a9b3731bd1f1649a513050b5579c65a0c8a33362d372abb09946da5f525ffa99a6d4d94bfaeffe80bed310a15dc6b3e2fa
|
data/lib/polist/service.rb
CHANGED
@@ -4,85 +4,87 @@ require "active_model/validations"
|
|
4
4
|
require "plissken"
|
5
5
|
require "tainbox"
|
6
6
|
|
7
|
-
|
8
|
-
class
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
module Polist
|
8
|
+
class Service
|
9
|
+
class Failure < StandardError
|
10
|
+
attr_accessor :response
|
11
|
+
|
12
|
+
def initialize(response)
|
13
|
+
self.response = response
|
14
|
+
super
|
15
|
+
end
|
14
16
|
end
|
15
|
-
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
class Form
|
19
|
+
include Tainbox
|
20
|
+
include ActiveModel::Validations
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
+
attr_accessor :params
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
def self.call(*args)
|
26
|
+
build(*args).tap(&:call)
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
def self.run(*args)
|
30
|
+
build(*args).tap(&:run)
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
def self.param(*names)
|
34
|
+
names.each do |name|
|
35
|
+
define_method(name) { params.fetch(name) }
|
36
|
+
end
|
35
37
|
end
|
36
|
-
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
39
|
+
def initialize(params)
|
40
|
+
self.params = params
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
+
def call; end # Should be implemented in subclasses
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
def run
|
46
|
+
call
|
47
|
+
rescue Failure => error
|
48
|
+
@response = error.response
|
49
|
+
@failure = true
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
52
|
+
def response
|
53
|
+
@response ||= {}
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
56
|
+
def failure?
|
57
|
+
!!@failure
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
def success?
|
61
|
+
!failure?
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
64
|
+
def validate!
|
65
|
+
error!(form.errors.to_h.values.first) unless form.valid?
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
+
private
|
68
69
|
|
69
|
-
|
70
|
-
|
71
|
-
|
70
|
+
def form
|
71
|
+
@form ||= self.class::Form.new(form_attributes.to_snake_keys)
|
72
|
+
end
|
72
73
|
|
73
|
-
|
74
|
-
|
75
|
-
|
74
|
+
def form_attributes
|
75
|
+
params
|
76
|
+
end
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
78
|
+
def fail!(response = {})
|
79
|
+
raise self.class::Failure.new(response)
|
80
|
+
end
|
80
81
|
|
81
|
-
|
82
|
-
|
83
|
-
|
82
|
+
def error!(message = "")
|
83
|
+
fail!(error: message)
|
84
|
+
end
|
84
85
|
|
85
|
-
|
86
|
-
|
86
|
+
def success!(response = {})
|
87
|
+
@response = response
|
88
|
+
end
|
87
89
|
end
|
88
90
|
end
|
data/lib/polist/version.rb
CHANGED