proc 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ec097a6117d6d476f35dbd2368b5e70e74b42ef134bf2df9a6f21f81f16a1a9
4
- data.tar.gz: 5930aff0b125dc144bca46a6505b68dc049b0be416726b9f4af8fcafaa5b0a04
3
+ metadata.gz: a33f3939a8156059653e7edde57a9ac0b0ee46ab04bcb1f08ce098073e884358
4
+ data.tar.gz: df8c021333f0f03b6acdaa195b2c39035a4054b026fd1f6c09d08df674aef7f9
5
5
  SHA512:
6
- metadata.gz: 05d23e88b3c71c8fe7e06d450da67bd95054d2eecd5b25c35a5e92c2f0e0892739f5fb7d7a6c7343ef6081529ff5d1fb46f7e0f9368fc421c03187c6e16f3842
7
- data.tar.gz: b50b6af0c4cc0a383aaa6043d2e9c99689951b87934bb5746fd59084c504d597555d61619e3cade3ed2d1be51d9d064536a0e1e50d4bcb499816db4e4e072d69
6
+ metadata.gz: 52107c5f8c68d623aead4dab7131fe7ea42c5abc29bf748cb81f2aea80bc0ae2b68447764c02a1d40aca4a21e24521f2bc291e9ae7794bd69d98cb867c7f0959
7
+ data.tar.gz: 8bcab50d65564adb5ce355b75dd407f82ccf93dee798c1219def178293978251b6bfff6c588a823eecd52732a6784a971226026f796601941f1866d3d0c00035
File without changes
@@ -10,7 +10,7 @@ class Proc
10
10
  end
11
11
 
12
12
  def call(input = nil, **arguments)
13
- @client.perform(@proc, input, **arguments)
13
+ @client.call(@proc, input, **arguments)
14
14
  end
15
15
 
16
16
  def >>(other)
@@ -28,6 +28,9 @@ class Proc
28
28
  class RateLimited < Error
29
29
  end
30
30
 
31
+ class Timeout < Error
32
+ end
33
+
31
34
  class Client < Http::Client
32
35
  def initialize(authorization, scheme: "https", host: "proc.dev")
33
36
  @authorization = authorization
@@ -62,21 +65,28 @@ class Proc
62
65
  end
63
66
 
64
67
  DEFAULT_HEADERS = {
68
+ "accept" => "application/json",
65
69
  "content-type" => "application/json"
66
70
  }.freeze
67
71
 
68
- def perform(proc, input, **arguments)
72
+ def call(proc = nil, input = nil, **arguments)
69
73
  Async(logger: NullLogger) { |task|
70
- body = {
71
- input: input, arguments: arguments
72
- }
74
+ body = { "<<" => input }
75
+
76
+ arguments.each_pair do |key, value|
77
+ body[key.to_s] = if value.respond_to?(:serialize)
78
+ value.serialize
79
+ else
80
+ value
81
+ end
82
+ end
73
83
 
74
84
  headers = {
75
- "authorization" => "Bearer #{@authorization}"
85
+ "authorization" => "bearer #{@authorization}"
76
86
  }.merge(DEFAULT_HEADERS)
77
87
 
78
88
  begin
79
- response = call(:post, build_uri(proc), headers: headers, body: Oj.dump(body, mode: :json), task: task)
89
+ response = super(:post, build_uri(proc), headers: headers, body: Oj.dump(body, mode: :json), task: task)
80
90
 
81
91
  @remaining = response.headers["x-rate-limit-remaining"].to_s.to_i
82
92
  @resets_at = Time.at(response.headers["x-rate-limit-reset"].to_s.to_i)
@@ -90,23 +100,27 @@ class Proc
90
100
 
91
101
  case response.status
92
102
  when 200
93
- payload["value"]
103
+ payload[">>"]
94
104
  when 400
95
105
  raise Proc::ArgumentError, payload.dig("error", "message")
96
106
  when 403
97
107
  raise Proc::Unauthorized, payload.dig("error", "message")
98
108
  when 404
99
109
  raise Proc::Undefined, payload.dig("error", "message")
110
+ when 408
111
+ raise Proc::Timeout, payload.dig("error", "message")
100
112
  when 429
101
113
  raise Proc::RateLimited, payload.dig("error", "message")
102
114
  when 500
103
115
  raise Proc::Error, payload.dig("error", "message")
116
+ else
117
+ raise Proc::Error, "unhandled"
104
118
  end
105
119
  }.wait
106
120
  end
107
121
 
108
122
  private def build_uri(proc)
109
- host_and_path = File.join(@host, proc.split(".").join("/"))
123
+ host_and_path = File.join(@host, proc.to_s.split(".").join("/"))
110
124
 
111
125
  "#{@scheme}://#{host_and_path}"
112
126
  end
@@ -15,13 +15,10 @@ class Proc
15
15
  @callables = @callables.dup
16
16
  end
17
17
 
18
- def call(input, **arguments)
19
- if @block
20
- evaluator = @block.call(Evaluator.new, **arguments)
21
- @client.perform("compose", input, procs: evaluator.serialize.concat(serialize))
22
- else
23
- @client.perform("compose", input, procs: serialize)
24
- end
18
+ def call(input = nil, **arguments)
19
+ # TODO: This will probably call `proc.run`.
20
+ #
21
+ @client.call("compose", input, proc: build_evaluator(**arguments))
25
22
  end
26
23
 
27
24
  def >>(other)
@@ -34,8 +31,28 @@ class Proc
34
31
  @callables << callable
35
32
  end
36
33
 
37
- protected def serialize
38
- @callables.map(&:proc)
34
+ def with(**arguments)
35
+ build_evaluator(**arguments)
36
+ end
37
+
38
+ def serialize
39
+ build_evaluator.serialize
40
+ rescue ::ArgumentError => error
41
+ raise ::ArgumentError, error.message + " (try using `with' to build a composition with arguments)"
42
+ end
43
+
44
+ private def build_evaluator(**arguments)
45
+ evaluator = if @block
46
+ @block.call(Evaluator.new, **arguments)
47
+ else
48
+ Evaluator.new
49
+ end
50
+
51
+ @callables.each do |callable|
52
+ evaluator << Deferable.new(callable.proc)
53
+ end
54
+
55
+ evaluator
39
56
  end
40
57
  end
41
58
  end
@@ -12,7 +12,17 @@ class Proc
12
12
  end
13
13
 
14
14
  def serialize
15
- [@proc, @arguments]
15
+ [@proc, serialized_arguments]
16
+ end
17
+
18
+ private def serialized_arguments
19
+ @arguments.each.each_with_object({}) do |(key, value), hash|
20
+ hash[key] = if value.respond_to?(:serialize)
21
+ value.serialize
22
+ else
23
+ value
24
+ end
25
+ end
16
26
  end
17
27
  end
18
28
  end
@@ -45,7 +45,7 @@ class Proc
45
45
  end
46
46
 
47
47
  def serialize
48
- @callables.map(&:serialize)
48
+ { "[]" => @callables.map(&:serialize) }
49
49
  end
50
50
  end
51
51
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Proc
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
 
6
6
  def self.version
7
7
  VERSION
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.0.3
4
+ version: 0.0.4
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-21 00:00:00.000000000 Z
11
+ date: 2020-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async-http
@@ -45,7 +45,7 @@ extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
47
  - LICENSE
48
- - lib/examples/scratch.rb
48
+ - README.md
49
49
  - lib/proc.rb
50
50
  - lib/proc/callable.rb
51
51
  - lib/proc/client.rb
@@ -1,61 +0,0 @@
1
- # Pass `token: "..."` or default to `PROC_ACCESS_TOKEN`:
2
- #
3
- client = Proc.connect
4
-
5
- # Not chainable, returns the value:
6
- #
7
- client.stdlib.echo("bar")
8
- # => bar
9
-
10
- # Create chains using `chain` (perhaps alias as `with`):
11
- #
12
- client.chain("foo") { |chain| chain.stdlib.reverse.capitalize.truncate(2) }
13
- # => Oo
14
-
15
- # Or return a chain to pass around:
16
- #
17
- chain = client.chain("foo")
18
- # => <Proc::Chain ...>
19
- chain.stdlib.echo("bar")
20
-
21
- # Call `value` or `perform` to evaluate the chain:
22
- #
23
- chain.value
24
-
25
- # Calling a non-existent proc raises a Proc::NameError,
26
- #
27
- chain.nonexistent
28
-
29
- # Pass values through different contexts:
30
- #
31
- client.chain("foo") { |chain| chain.stdlib.reverse >> chain.whatever.other }
32
-
33
- # Use from the cli (included with the ruby gem OR as a go library):
34
- #
35
- # proc echo "bar" -t access-token
36
- # proc chain "bar" "stdlib.reverse >> whatever.other" -t access-token
37
-
38
- #########################################################
39
- # alternatives so that we don't have to fetch metadata: #
40
- #########################################################
41
-
42
- proc = Proc.connect
43
-
44
- # I like this because it's obvious how to get a reference as well as just make the call.
45
- #
46
- proc["stdlib.echo"].call("foo")
47
-
48
- # This works for simple composition, where the value is simply passed through.
49
- #
50
- my_proc = proc["stdlib.reverse"] >> proc["stdlib.capitalize"]
51
-
52
- my_proc.call("foo")
53
-
54
- # Perhaps something like this, where we compose a proc with arguments. Each is implicitly called, or
55
- # call explicitly to pass an argument to the proc. I sort of like the simplicity of this.
56
- #
57
- my_proc = proc.compose { |context, length:|
58
- context["stdlib.reverse"] >> context["stdlib.capitalize"] >> context["stdlib.truncate"].call(length)
59
- }
60
-
61
- my_proc.call("foo", length: 2)