proc 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/proc/argument.rb +28 -0
- data/lib/proc/callable.rb +28 -3
- data/lib/proc/client.rb +20 -6
- data/lib/proc/composition.rb +54 -7
- data/lib/proc/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2950f8e2178a95a40c29372c902203d883bda75ac84785a3579134b9394d7a52
|
4
|
+
data.tar.gz: 6c8433c3f49bbc0a6a62187c9b992fd7c39e6eac2894f7d71a2056205bdc7900
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36828988724d7e3cad08a3a741f576bfa50f8d53a459062892bb270e01da1d8a1ddf4977255976a314a15b4e79e8266da4089c9cdbb8f723c30d681099654c7f
|
7
|
+
data.tar.gz: e224df7dcabcf284d5cf72f410be3ccf713dfbb0ff386ae5a50597e5c9a6b07634c87759f30d5e4b4a95fc0091d0c2f0aa490146a02b8806a3784033025fd123
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Proc
|
4
|
+
class Argument
|
5
|
+
def initialize(name, **options)
|
6
|
+
@name = name
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def serialize
|
11
|
+
{"::" => {"name" => @name.to_s}.merge(serialized_options)}
|
12
|
+
end
|
13
|
+
|
14
|
+
def serialized_options
|
15
|
+
@options.each_pair.each_with_object({}) { |(key, value), hash|
|
16
|
+
hash[key.to_s] = serialize_value(value)
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
private def serialize_value(value)
|
21
|
+
if value.respond_to?(:serialize)
|
22
|
+
value.serialize
|
23
|
+
else
|
24
|
+
value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/proc/callable.rb
CHANGED
@@ -17,11 +17,23 @@ class Proc
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def call(input = input_omitted = true, **arguments)
|
20
|
-
|
20
|
+
callable = self.class.new(
|
21
|
+
@proc,
|
22
|
+
client: @client,
|
23
|
+
input: input_omitted ? @input : input,
|
24
|
+
arguments: @arguments.merge(arguments)
|
25
|
+
)
|
26
|
+
|
27
|
+
@client.call(@proc, callable.serialized_input, **callable.serialized_arguments)
|
21
28
|
end
|
22
29
|
|
23
30
|
def with(input = input_omitted = true, **arguments)
|
24
|
-
self.class.new(
|
31
|
+
self.class.new(
|
32
|
+
@proc,
|
33
|
+
client: @client,
|
34
|
+
input: input_omitted ? @input : input,
|
35
|
+
arguments: @arguments.merge(arguments)
|
36
|
+
)
|
25
37
|
end
|
26
38
|
|
27
39
|
def >>(other)
|
@@ -34,18 +46,31 @@ class Proc
|
|
34
46
|
def serialize
|
35
47
|
{
|
36
48
|
"{}" => {
|
37
|
-
"<<" =>
|
49
|
+
"<<" => serialized_input,
|
38
50
|
"[]" => [[@proc, serialized_arguments]]
|
39
51
|
}
|
40
52
|
}
|
41
53
|
end
|
42
54
|
|
55
|
+
def serialized_input
|
56
|
+
serialize_value(@input)
|
57
|
+
end
|
58
|
+
|
43
59
|
def serialized_arguments
|
44
60
|
@arguments.each_pair.each_with_object({}) { |(key, value), hash|
|
45
61
|
hash[key.to_s] = serialize_value(value)
|
46
62
|
}
|
47
63
|
end
|
48
64
|
|
65
|
+
def [](proc)
|
66
|
+
Callable.new(
|
67
|
+
[@proc, proc].join("."),
|
68
|
+
client: @client,
|
69
|
+
input: @input,
|
70
|
+
arguments: @arguments
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
49
74
|
IGNORE_MISSING = %i[to_hash].freeze
|
50
75
|
|
51
76
|
def method_missing(name, input = input_omitted = true, **arguments)
|
data/lib/proc/client.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require "async"
|
4
4
|
require "oj"
|
5
5
|
|
6
|
+
require_relative "argument"
|
6
7
|
require_relative "callable"
|
7
8
|
require_relative "composition"
|
8
9
|
require_relative "null_logger"
|
@@ -13,7 +14,7 @@ class Proc
|
|
13
14
|
class Error < StandardError
|
14
15
|
end
|
15
16
|
|
16
|
-
class
|
17
|
+
class Invalid < ::ArgumentError
|
17
18
|
end
|
18
19
|
|
19
20
|
class Undefined < ::NameError
|
@@ -22,10 +23,13 @@ class Proc
|
|
22
23
|
class Unauthorized < Error
|
23
24
|
end
|
24
25
|
|
26
|
+
class Forbidden < Error
|
27
|
+
end
|
28
|
+
|
25
29
|
class Unavailable < Error
|
26
30
|
end
|
27
31
|
|
28
|
-
class
|
32
|
+
class Limited < Error
|
29
33
|
end
|
30
34
|
|
31
35
|
class Timeout < Error
|
@@ -60,7 +64,6 @@ class Proc
|
|
60
64
|
@resets_at
|
61
65
|
end
|
62
66
|
|
63
|
-
|
64
67
|
DEFAULT_HEADERS = {
|
65
68
|
"accept" => "application/json",
|
66
69
|
"content-type" => "application/json"
|
@@ -86,6 +89,8 @@ class Proc
|
|
86
89
|
|
87
90
|
payload = Oj.load(response.read, mode: :compat)
|
88
91
|
rescue => error
|
92
|
+
# TODO: This should wrap `error`.
|
93
|
+
#
|
89
94
|
raise Proc::Unavailable, error.message
|
90
95
|
ensure
|
91
96
|
response&.close
|
@@ -95,17 +100,21 @@ class Proc
|
|
95
100
|
when 200
|
96
101
|
payload[">>"]
|
97
102
|
when 400
|
98
|
-
raise Proc::
|
99
|
-
when
|
103
|
+
raise Proc::Invalid, payload.dig("error", "message")
|
104
|
+
when 401
|
100
105
|
raise Proc::Unauthorized, payload.dig("error", "message")
|
106
|
+
when 403
|
107
|
+
raise Proc::Forbidden, payload.dig("error", "message")
|
101
108
|
when 404
|
102
109
|
raise Proc::Undefined, payload.dig("error", "message")
|
103
110
|
when 408
|
104
111
|
raise Proc::Timeout, payload.dig("error", "message")
|
105
112
|
when 429
|
106
|
-
raise Proc::
|
113
|
+
raise Proc::Limited, payload.dig("error", "message")
|
107
114
|
when 500
|
108
115
|
raise Proc::Error, payload.dig("error", "message")
|
116
|
+
when 508
|
117
|
+
raise Proc::Error, payload.dig("error", "message")
|
109
118
|
else
|
110
119
|
raise Proc::Error, "unhandled"
|
111
120
|
end
|
@@ -120,6 +129,11 @@ class Proc
|
|
120
129
|
true
|
121
130
|
end
|
122
131
|
|
132
|
+
def argument(name, **options)
|
133
|
+
Argument.new(name, **options)
|
134
|
+
end
|
135
|
+
alias_method :arg, :argument
|
136
|
+
|
123
137
|
private def build_uri(proc)
|
124
138
|
host_and_path = File.join(@host, proc.to_s.split(".").join("/"))
|
125
139
|
|
data/lib/proc/composition.rb
CHANGED
@@ -2,10 +2,13 @@
|
|
2
2
|
|
3
3
|
class Proc
|
4
4
|
class Composition
|
5
|
-
|
5
|
+
attr_reader :input, :callables, :arguments
|
6
|
+
|
7
|
+
def initialize(client:, input:, callables: [], arguments: {})
|
6
8
|
@client = client
|
7
9
|
@input = input
|
8
10
|
@callables = callables
|
11
|
+
@arguments = arguments
|
9
12
|
end
|
10
13
|
|
11
14
|
def initialize_copy(_)
|
@@ -13,16 +16,35 @@ class Proc
|
|
13
16
|
end
|
14
17
|
|
15
18
|
def call(input = input_omitted = true, **arguments)
|
16
|
-
|
19
|
+
callable = self.class.new(
|
20
|
+
client: @client,
|
21
|
+
input: input_omitted ? @input : input,
|
22
|
+
callables: @callables.dup,
|
23
|
+
arguments: @arguments.merge(arguments)
|
24
|
+
)
|
25
|
+
|
26
|
+
@client.call("proc.exec", nil, proc: callable.serialize)
|
17
27
|
end
|
18
28
|
|
19
|
-
def with(input = input_omitted = true)
|
20
|
-
self.class.new(
|
29
|
+
def with(input = input_omitted = true, **arguments)
|
30
|
+
self.class.new(
|
31
|
+
client: @client,
|
32
|
+
input: input_omitted ? @input : input,
|
33
|
+
callables: @callables.dup,
|
34
|
+
arguments: @arguments.merge(arguments)
|
35
|
+
)
|
21
36
|
end
|
22
37
|
|
23
38
|
def >>(other)
|
24
39
|
composed = dup
|
25
|
-
|
40
|
+
|
41
|
+
case other
|
42
|
+
when Composition
|
43
|
+
composed.merge(other)
|
44
|
+
when Callable
|
45
|
+
composed << other
|
46
|
+
end
|
47
|
+
|
26
48
|
composed
|
27
49
|
end
|
28
50
|
|
@@ -33,9 +55,9 @@ class Proc
|
|
33
55
|
def serialize
|
34
56
|
{
|
35
57
|
"{}" => {
|
36
|
-
"<<" =>
|
58
|
+
"<<" => serialized_input,
|
37
59
|
"[]" => serialized_calls
|
38
|
-
}
|
60
|
+
}.merge(serialized_arguments)
|
39
61
|
}
|
40
62
|
end
|
41
63
|
|
@@ -44,5 +66,30 @@ class Proc
|
|
44
66
|
[callable.proc, callable.serialized_arguments]
|
45
67
|
}
|
46
68
|
end
|
69
|
+
|
70
|
+
def serialized_input
|
71
|
+
serialize_value(@input)
|
72
|
+
end
|
73
|
+
|
74
|
+
def serialized_arguments
|
75
|
+
@arguments.each_pair.each_with_object({}) { |(key, value), hash|
|
76
|
+
hash[key.to_s] = serialize_value(value)
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
def merge(composition)
|
81
|
+
raise ArgumentError, "expected a composition" unless composition.is_a?(self.class)
|
82
|
+
|
83
|
+
@callables.concat(composition.callables)
|
84
|
+
@arguments.merge!(composition.arguments)
|
85
|
+
end
|
86
|
+
|
87
|
+
private def serialize_value(value)
|
88
|
+
if value.respond_to?(:serialize)
|
89
|
+
value.serialize
|
90
|
+
else
|
91
|
+
value
|
92
|
+
end
|
93
|
+
end
|
47
94
|
end
|
48
95
|
end
|
data/lib/proc/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bryan Powell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: async-http
|
@@ -47,6 +47,7 @@ files:
|
|
47
47
|
- LICENSE
|
48
48
|
- README.md
|
49
49
|
- lib/proc.rb
|
50
|
+
- lib/proc/argument.rb
|
50
51
|
- lib/proc/callable.rb
|
51
52
|
- lib/proc/client.rb
|
52
53
|
- lib/proc/composition.rb
|