proc 0.3.0 → 0.4.0

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: c3a73eafa61306bca405c0eaec0f8df0d145b22d27a98ee8dcf28db94cdadfa3
4
- data.tar.gz: b1e95096528fe529ec664a370095269ae26672994178ed62e3c78159c46ed173
3
+ metadata.gz: 63d146f97fd1dc28e5ff40ae924ac80e5f34838da4a3bb626b51023c2e23efbc
4
+ data.tar.gz: ebbab25f52bc3344c9fee6e10a00af6ed5022808d05f8516c41dc914987178d6
5
5
  SHA512:
6
- metadata.gz: 9708b1585d8c95deb6016cc5dbed10d5c7bffdfa55421d0392d3c51ec2af5ce334eb5b41283c585795874c7cdf5b035e6eb41ca145ee56d7409147546b61a6bf
7
- data.tar.gz: ec138355afed48dde883579c73d304196b3cb2b6b8ac7d5a0986b43c5bffb6591fd33c019b516807c46b2c1ef3c6d75df7fc10d5bae10ecfbc38428ea89977c9
6
+ metadata.gz: 06f06ba059619e117f86021bfeda3b5520d6842664d5fb284c77e7920cae38b66d4e82874329243ee0e456216034a07111e04f8b49ffaf6828470eb8a0e7f2fc
7
+ data.tar.gz: d6b1423b158de5320e963c48a988db2936c5c576eb3e02b2ec9d2d675ff54ca6c428e270baaf566935bf8df2fd18b9b59e67449c5ed661d5c57d7a4b75a8f41b
@@ -6,10 +6,11 @@ require "oj"
6
6
  require_relative "argument"
7
7
  require_relative "callable"
8
8
  require_relative "composition"
9
- require_relative "null_logger"
10
9
 
11
10
  require_relative "http/client"
12
11
 
12
+ Console.logger.off!
13
+
13
14
  class Proc
14
15
  class Error < StandardError
15
16
  end
@@ -35,13 +36,12 @@ class Proc
35
36
  class Timeout < Error
36
37
  end
37
38
 
38
- class Client < Http::Client
39
+ class Client
39
40
  def initialize(authorization, scheme: "https", host: "proc.dev")
40
41
  @authorization = authorization
41
42
  @scheme = scheme
42
43
  @host = host
43
-
44
- super()
44
+ @internal = Http::Client.new
45
45
  end
46
46
 
47
47
  def [](proc)
@@ -70,60 +70,47 @@ class Proc
70
70
  }.freeze
71
71
 
72
72
  def call(proc = nil, input = nil, **arguments)
73
- Async(logger: NullLogger) { |task|
74
- body = {"<>" => true}
75
-
76
- unless Proc.undefined?(input)
77
- body["<<"] = serialize_value(input)
78
- end
79
-
80
- arguments.each_pair do |key, value|
81
- body[key.to_s] = serialize_value(value)
82
- end
83
-
84
- headers = {
85
- "authorization" => "bearer #{@authorization}"
86
- }.merge(DEFAULT_HEADERS)
87
-
88
- begin
89
- response = super(:post, build_uri(proc), headers: headers, body: Oj.dump(body, mode: :custom), task: task)
90
-
91
- @remaining = response.headers["x-rate-limit-remaining"].to_s.to_i
92
- @resets_at = Time.at(response.headers["x-rate-limit-reset"].to_s.to_i)
93
-
94
- payload = Oj.load(response.read, mode: :compat)
95
- rescue
96
- raise Proc::Unavailable
97
- ensure
98
- response&.close
99
- end
100
-
101
- case response.status
102
- when 200
103
- payload[">>"]
104
- when 400
105
- raise Proc::Invalid, payload.dig("error", "message")
106
- when 401
107
- raise Proc::Unauthorized, payload.dig("error", "message")
108
- when 403
109
- raise Proc::Forbidden, payload.dig("error", "message")
110
- when 404
111
- raise Proc::Undefined, payload.dig("error", "message")
112
- when 408
113
- raise Proc::Timeout, payload.dig("error", "message")
114
- when 429
115
- raise Proc::Limited, payload.dig("error", "message")
116
- when 500
117
- raise Proc::Error, payload.dig("error", "message")
118
- when 508
119
- raise Proc::Error, payload.dig("error", "message")
120
- else
121
- raise Proc::Error, "unhandled"
122
- end
123
- }.wait
73
+ body = {"<>" => true}
74
+
75
+ unless Proc.undefined?(input)
76
+ body["<<"] = serialize_value(input)
77
+ end
78
+
79
+ arguments.each_pair do |key, value|
80
+ body[key.to_s] = serialize_value(value)
81
+ end
82
+
83
+ headers = {
84
+ "authorization" => "bearer #{@authorization}"
85
+ }.merge(DEFAULT_HEADERS)
86
+
87
+ payload = get_payload(proc: proc, headers: headers, body: body)
88
+
89
+ case payload["status"]
90
+ when 200
91
+ payload[">>"]
92
+ when 400
93
+ raise Proc::Invalid, payload.dig("error", "message")
94
+ when 401
95
+ raise Proc::Unauthorized, payload.dig("error", "message")
96
+ when 403
97
+ raise Proc::Forbidden, payload.dig("error", "message")
98
+ when 404
99
+ raise Proc::Undefined, payload.dig("error", "message")
100
+ when 408
101
+ raise Proc::Timeout, payload.dig("error", "message")
102
+ when 429
103
+ raise Proc::Limited, payload.dig("error", "message")
104
+ when 500
105
+ raise Proc::Error, payload.dig("error", "message")
106
+ when 508
107
+ raise Proc::Error, payload.dig("error", "message")
108
+ else
109
+ raise Proc::Error, "unhandled"
110
+ end
124
111
  end
125
112
 
126
- def method_missing(name, input = input_omitted = true, **arguments)
113
+ def method_missing(name, input = input_omitted = true, *, **arguments)
127
114
  if input_omitted
128
115
  Callable.new(name, client: self, arguments: arguments)
129
116
  else
@@ -140,6 +127,10 @@ class Proc
140
127
  end
141
128
  alias_method :arg, :argument
142
129
 
130
+ def close
131
+ @internal.close
132
+ end
133
+
143
134
  private def build_uri(proc)
144
135
  host_and_path = File.join(@host, proc.to_s.split(".").join("/"))
145
136
 
@@ -153,5 +144,18 @@ class Proc
153
144
  value
154
145
  end
155
146
  end
147
+
148
+ private def get_payload(proc:, headers:, body:)
149
+ @internal.call(:post, build_uri(proc), headers: headers, body: Oj.dump(body, mode: :custom)) { |response|
150
+ @remaining = response.headers["x-rate-limit-remaining"].to_s.to_i
151
+ @resets_at = Time.at(response.headers["x-rate-limit-reset"].to_s.to_i)
152
+
153
+ payload = Oj.load(response.read, mode: :compat, compat_bigdecimal: true)
154
+ payload["status"] = response.status
155
+ payload
156
+ }
157
+ rescue
158
+ raise Proc::Unavailable
159
+ end
156
160
  end
157
161
  end
@@ -5,50 +5,45 @@ require "async/http/internet"
5
5
 
6
6
  require "protocol/http/body/streamable"
7
7
 
8
+ require "core/async"
9
+
8
10
  require_relative "request"
9
11
  require_relative "response"
10
12
 
11
- require_relative "../null_logger"
12
-
13
13
  class Proc
14
14
  module Http
15
15
  class Client
16
+ include Is::Async
17
+
16
18
  def initialize
17
19
  @internet = Async::HTTP::Internet.new
18
- @responses = {}
19
20
  end
20
21
 
21
- def call(method, uri, params: {}, headers: {}, body: nil, task: nil)
22
+ def call(method, uri, params: {}, headers: {}, body: nil)
22
23
  request = Request.new(method: method, uri: uri, params: params, headers: headers, body: body)
23
24
 
24
- if task
25
- make_request(request)
26
- else
27
- Async(logger: NullLogger) {
28
- make_request(request)
29
- }.wait
30
- end
25
+ await {
26
+ begin
27
+ response = make_request(request)
28
+
29
+ yield response
30
+ ensure
31
+ response&.close
32
+ end
33
+ }
31
34
  end
32
35
 
33
36
  def close
34
- # TODO: Make sure this works. We should also close / clear after accumulating some amount.
35
- #
36
- @responses.each_value(&:close)
37
- @responses.clear
38
37
  @internet.close
39
38
  end
40
39
 
41
- def count
42
- @responses.count
43
- end
44
-
45
40
  private def make_request(request)
46
41
  async_response = @internet.call(*request.callable)
47
42
  wrap_async_response_for_request(async_response, request)
48
43
  end
49
44
 
50
45
  private def wrap_async_response_for_request(async_response, request)
51
- @responses[async_response] = Response.new(request, async_response)
46
+ Response.new(request, async_response)
52
47
  end
53
48
  end
54
49
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Proc
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
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.3.0
4
+ version: 0.4.0
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-11-23 00:00:00.000000000 Z
11
+ date: 2021-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async-http
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.52.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: core-async
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: oj
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -54,7 +68,6 @@ files:
54
68
  - lib/proc/http/client.rb
55
69
  - lib/proc/http/request.rb
56
70
  - lib/proc/http/response.rb
57
- - lib/proc/null_logger.rb
58
71
  - lib/proc/version.rb
59
72
  homepage: https://proc.dev/
60
73
  licenses:
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Proc
4
- class NullLogger
5
- class << self
6
- def method_missing(*, **)
7
- end
8
-
9
- def respond_to_missing?(*)
10
- true
11
- end
12
- end
13
- end
14
- end