proc 0.9.1 → 0.10.0

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: cbe8ec8f147808e160d44b09da719dafb03e329b3b1fb78f345300af4c9ab798
4
- data.tar.gz: f7d81cecd2d9c6cf88e3b4f59bae7681e962bd2dd7d95fd6a1c1593f8245639c
3
+ metadata.gz: cc5ea46072b5407ec2b71822b8406163aeecd2aa6a57d10773e440bb08721ac5
4
+ data.tar.gz: 4640295398b9266d57361f62aadf461b9d23e69f9fa0d21d60c5d1cd8b00715f
5
5
  SHA512:
6
- metadata.gz: e31c9f0c03adc5b2b82f0a2205cfb990f7be7bf529cf287580f2253b70425b63be00f19583838b93e15642f5ea2d3a1289d88065d6658f247f25b875d5bd8e3f
7
- data.tar.gz: 9f44c4c22a636faa3f3032420a48e8aa8e7b92b18d2023115736612ef1b03736d967b770a43cf28e3ef426057b2ffa31a0398e627b51e271b6612cfadd2d0fea
6
+ metadata.gz: 45bb39ab2f194fe673f22a09d7d681c8514e639c71829a5673ac437991652212260fa6031c46fbdf92c48397865e1552ca7fdda72064a0ce6011c29dbee5e018
7
+ data.tar.gz: 8d1ae22aa4ec5f39cd4c10e2fdf1fa24ad38355e511d5d12cd8935563cb7f6a835a166683922e56bf7db9cc30a5dda7dc649c4c39f3c47b060c5f528f4077331
data/LICENSE CHANGED
@@ -1,22 +1,353 @@
1
- This software is licensed under the MIT License.
2
-
3
- Copyright 2020 Metabahn.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a
6
- copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to permit
10
- persons to whom the Software is furnished to do so, subject to the
11
- following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included
14
- in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
19
- NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20
- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21
- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22
- USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Mozilla Public License, version 2.0
2
+
3
+ 1. Definitions
4
+
5
+ 1.1. “Contributor”
6
+
7
+ means each individual or legal entity that creates, contributes to the
8
+ creation of, or owns Covered Software.
9
+
10
+ 1.2. “Contributor Version”
11
+
12
+ means the combination of the Contributions of others (if any) used by a
13
+ Contributor and that particular Contributor’s Contribution.
14
+
15
+ 1.3. “Contribution”
16
+
17
+ means Covered Software of a particular Contributor.
18
+
19
+ 1.4. “Covered Software”
20
+
21
+ means Source Code Form to which the initial Contributor has attached the
22
+ notice in Exhibit A, the Executable Form of such Source Code Form, and
23
+ Modifications of such Source Code Form, in each case including portions
24
+ thereof.
25
+
26
+ 1.5. “Incompatible With Secondary Licenses”
27
+ means
28
+
29
+ a. that the initial Contributor has attached the notice described in
30
+ Exhibit B to the Covered Software; or
31
+
32
+ b. that the Covered Software was made available under the terms of version
33
+ 1.1 or earlier of the License, but not also under the terms of a
34
+ Secondary License.
35
+
36
+ 1.6. “Executable Form”
37
+
38
+ means any form of the work other than Source Code Form.
39
+
40
+ 1.7. “Larger Work”
41
+
42
+ means a work that combines Covered Software with other material, in a separate
43
+ file or files, that is not Covered Software.
44
+
45
+ 1.8. “License”
46
+
47
+ means this document.
48
+
49
+ 1.9. “Licensable”
50
+
51
+ means having the right to grant, to the maximum extent possible, whether at the
52
+ time of the initial grant or subsequently, any and all of the rights conveyed by
53
+ this License.
54
+
55
+ 1.10. “Modifications”
56
+
57
+ means any of the following:
58
+
59
+ a. any file in Source Code Form that results from an addition to, deletion
60
+ from, or modification of the contents of Covered Software; or
61
+
62
+ b. any new file in Source Code Form that contains any Covered Software.
63
+
64
+ 1.11. “Patent Claims” of a Contributor
65
+
66
+ means any patent claim(s), including without limitation, method, process,
67
+ and apparatus claims, in any patent Licensable by such Contributor that
68
+ would be infringed, but for the grant of the License, by the making,
69
+ using, selling, offering for sale, having made, import, or transfer of
70
+ either its Contributions or its Contributor Version.
71
+
72
+ 1.12. “Secondary License”
73
+
74
+ means either the GNU General Public License, Version 2.0, the GNU Lesser
75
+ General Public License, Version 2.1, the GNU Affero General Public
76
+ License, Version 3.0, or any later versions of those licenses.
77
+
78
+ 1.13. “Source Code Form”
79
+
80
+ means the form of the work preferred for making modifications.
81
+
82
+ 1.14. “You” (or “Your”)
83
+
84
+ means an individual or a legal entity exercising rights under this
85
+ License. For legal entities, “You” includes any entity that controls, is
86
+ controlled by, or is under common control with You. For purposes of this
87
+ definition, “control” means (a) the power, direct or indirect, to cause
88
+ the direction or management of such entity, whether by contract or
89
+ otherwise, or (b) ownership of more than fifty percent (50%) of the
90
+ outstanding shares or beneficial ownership of such entity.
91
+
92
+
93
+ 2. License Grants and Conditions
94
+
95
+ 2.1. Grants
96
+
97
+ Each Contributor hereby grants You a world-wide, royalty-free,
98
+ non-exclusive license:
99
+
100
+ a. under intellectual property rights (other than patent or trademark)
101
+ Licensable by such Contributor to use, reproduce, make available,
102
+ modify, display, perform, distribute, and otherwise exploit its
103
+ Contributions, either on an unmodified basis, with Modifications, or as
104
+ part of a Larger Work; and
105
+
106
+ b. under Patent Claims of such Contributor to make, use, sell, offer for
107
+ sale, have made, import, and otherwise transfer either its Contributions
108
+ or its Contributor Version.
109
+
110
+ 2.2. Effective Date
111
+
112
+ The licenses granted in Section 2.1 with respect to any Contribution become
113
+ effective for each Contribution on the date the Contributor first distributes
114
+ such Contribution.
115
+
116
+ 2.3. Limitations on Grant Scope
117
+
118
+ The licenses granted in this Section 2 are the only rights granted under this
119
+ License. No additional rights or licenses will be implied from the distribution
120
+ or licensing of Covered Software under this License. Notwithstanding Section
121
+ 2.1(b) above, no patent license is granted by a Contributor:
122
+
123
+ a. for any code that a Contributor has removed from Covered Software; or
124
+
125
+ b. for infringements caused by: (i) Your and any other third party’s
126
+ modifications of Covered Software, or (ii) the combination of its
127
+ Contributions with other software (except as part of its Contributor
128
+ Version); or
129
+
130
+ c. under Patent Claims infringed by Covered Software in the absence of its
131
+ Contributions.
132
+
133
+ This License does not grant any rights in the trademarks, service marks, or
134
+ logos of any Contributor (except as may be necessary to comply with the
135
+ notice requirements in Section 3.4).
136
+
137
+ 2.4. Subsequent Licenses
138
+
139
+ No Contributor makes additional grants as a result of Your choice to
140
+ distribute the Covered Software under a subsequent version of this License
141
+ (see Section 10.2) or under the terms of a Secondary License (if permitted
142
+ under the terms of Section 3.3).
143
+
144
+ 2.5. Representation
145
+
146
+ Each Contributor represents that the Contributor believes its Contributions
147
+ are its original creation(s) or it has sufficient rights to grant the
148
+ rights to its Contributions conveyed by this License.
149
+
150
+ 2.6. Fair Use
151
+
152
+ This License is not intended to limit any rights You have under applicable
153
+ copyright doctrines of fair use, fair dealing, or other equivalents.
154
+
155
+ 2.7. Conditions
156
+
157
+ Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
158
+ Section 2.1.
159
+
160
+
161
+ 3. Responsibilities
162
+
163
+ 3.1. Distribution of Source Form
164
+
165
+ All distribution of Covered Software in Source Code Form, including any
166
+ Modifications that You create or to which You contribute, must be under the
167
+ terms of this License. You must inform recipients that the Source Code Form
168
+ of the Covered Software is governed by the terms of this License, and how
169
+ they can obtain a copy of this License. You may not attempt to alter or
170
+ restrict the recipients’ rights in the Source Code Form.
171
+
172
+ 3.2. Distribution of Executable Form
173
+
174
+ If You distribute Covered Software in Executable Form then:
175
+
176
+ a. such Covered Software must also be made available in Source Code Form,
177
+ as described in Section 3.1, and You must inform recipients of the
178
+ Executable Form how they can obtain a copy of such Source Code Form by
179
+ reasonable means in a timely manner, at a charge no more than the cost
180
+ of distribution to the recipient; and
181
+
182
+ b. You may distribute such Executable Form under the terms of this License,
183
+ or sublicense it under different terms, provided that the license for
184
+ the Executable Form does not attempt to limit or alter the recipients’
185
+ rights in the Source Code Form under this License.
186
+
187
+ 3.3. Distribution of a Larger Work
188
+
189
+ You may create and distribute a Larger Work under terms of Your choice,
190
+ provided that You also comply with the requirements of this License for the
191
+ Covered Software. If the Larger Work is a combination of Covered Software
192
+ with a work governed by one or more Secondary Licenses, and the Covered
193
+ Software is not Incompatible With Secondary Licenses, this License permits
194
+ You to additionally distribute such Covered Software under the terms of
195
+ such Secondary License(s), so that the recipient of the Larger Work may, at
196
+ their option, further distribute the Covered Software under the terms of
197
+ either this License or such Secondary License(s).
198
+
199
+ 3.4. Notices
200
+
201
+ You may not remove or alter the substance of any license notices (including
202
+ copyright notices, patent notices, disclaimers of warranty, or limitations
203
+ of liability) contained within the Source Code Form of the Covered
204
+ Software, except that You may alter any license notices to the extent
205
+ required to remedy known factual inaccuracies.
206
+
207
+ 3.5. Application of Additional Terms
208
+
209
+ You may choose to offer, and to charge a fee for, warranty, support,
210
+ indemnity or liability obligations to one or more recipients of Covered
211
+ Software. However, You may do so only on Your own behalf, and not on behalf
212
+ of any Contributor. You must make it absolutely clear that any such
213
+ warranty, support, indemnity, or liability obligation is offered by You
214
+ alone, and You hereby agree to indemnify every Contributor for any
215
+ liability incurred by such Contributor as a result of warranty, support,
216
+ indemnity or liability terms You offer. You may include additional
217
+ disclaimers of warranty and limitations of liability specific to any
218
+ jurisdiction.
219
+
220
+ 4. Inability to Comply Due to Statute or Regulation
221
+
222
+ If it is impossible for You to comply with any of the terms of this License
223
+ with respect to some or all of the Covered Software due to statute, judicial
224
+ order, or regulation then You must: (a) comply with the terms of this License
225
+ to the maximum extent possible; and (b) describe the limitations and the code
226
+ they affect. Such description must be placed in a text file included with all
227
+ distributions of the Covered Software under this License. Except to the
228
+ extent prohibited by statute or regulation, such description must be
229
+ sufficiently detailed for a recipient of ordinary skill to be able to
230
+ understand it.
231
+
232
+ 5. Termination
233
+
234
+ 5.1. The rights granted under this License will terminate automatically if You
235
+ fail to comply with any of its terms. However, if You become compliant,
236
+ then the rights granted under this License from a particular Contributor
237
+ are reinstated (a) provisionally, unless and until such Contributor
238
+ explicitly and finally terminates Your grants, and (b) on an ongoing basis,
239
+ if such Contributor fails to notify You of the non-compliance by some
240
+ reasonable means prior to 60 days after You have come back into compliance.
241
+ Moreover, Your grants from a particular Contributor are reinstated on an
242
+ ongoing basis if such Contributor notifies You of the non-compliance by
243
+ some reasonable means, this is the first time You have received notice of
244
+ non-compliance with this License from such Contributor, and You become
245
+ compliant prior to 30 days after Your receipt of the notice.
246
+
247
+ 5.2. If You initiate litigation against any entity by asserting a patent
248
+ infringement claim (excluding declaratory judgment actions, counter-claims,
249
+ and cross-claims) alleging that a Contributor Version directly or
250
+ indirectly infringes any patent, then the rights granted to You by any and
251
+ all Contributors for the Covered Software under Section 2.1 of this License
252
+ shall terminate.
253
+
254
+ 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
255
+ license agreements (excluding distributors and resellers) which have been
256
+ validly granted by You or Your distributors under this License prior to
257
+ termination shall survive termination.
258
+
259
+ 6. Disclaimer of Warranty
260
+
261
+ Covered Software is provided under this License on an “as is” basis, without
262
+ warranty of any kind, either expressed, implied, or statutory, including,
263
+ without limitation, warranties that the Covered Software is free of defects,
264
+ merchantable, fit for a particular purpose or non-infringing. The entire
265
+ risk as to the quality and performance of the Covered Software is with You.
266
+ Should any Covered Software prove defective in any respect, You (not any
267
+ Contributor) assume the cost of any necessary servicing, repair, or
268
+ correction. This disclaimer of warranty constitutes an essential part of this
269
+ License. No use of any Covered Software is authorized under this License
270
+ except under this disclaimer.
271
+
272
+ 7. Limitation of Liability
273
+
274
+ Under no circumstances and under no legal theory, whether tort (including
275
+ negligence), contract, or otherwise, shall any Contributor, or anyone who
276
+ distributes Covered Software as permitted above, be liable to You for any
277
+ direct, indirect, special, incidental, or consequential damages of any
278
+ character including, without limitation, damages for lost profits, loss of
279
+ goodwill, work stoppage, computer failure or malfunction, or any and all
280
+ other commercial damages or losses, even if such party shall have been
281
+ informed of the possibility of such damages. This limitation of liability
282
+ shall not apply to liability for death or personal injury resulting from such
283
+ party’s negligence to the extent applicable law prohibits such limitation.
284
+ Some jurisdictions do not allow the exclusion or limitation of incidental or
285
+ consequential damages, so this exclusion and limitation may not apply to You.
286
+
287
+ 8. Litigation
288
+
289
+ Any litigation relating to this License may be brought only in the courts of
290
+ a jurisdiction where the defendant maintains its principal place of business
291
+ and such litigation shall be governed by laws of that jurisdiction, without
292
+ reference to its conflict-of-law provisions. Nothing in this Section shall
293
+ prevent a party’s ability to bring cross-claims or counter-claims.
294
+
295
+ 9. Miscellaneous
296
+
297
+ This License represents the complete agreement concerning the subject matter
298
+ hereof. If any provision of this License is held to be unenforceable, such
299
+ provision shall be reformed only to the extent necessary to make it
300
+ enforceable. Any law or regulation which provides that the language of a
301
+ contract shall be construed against the drafter shall not be used to construe
302
+ this License against a Contributor.
303
+
304
+
305
+ 10. Versions of the License
306
+
307
+ 10.1. New Versions
308
+
309
+ Mozilla Foundation is the license steward. Except as provided in Section
310
+ 10.3, no one other than the license steward has the right to modify or
311
+ publish new versions of this License. Each version will be given a
312
+ distinguishing version number.
313
+
314
+ 10.2. Effect of New Versions
315
+
316
+ You may distribute the Covered Software under the terms of the version of
317
+ the License under which You originally received the Covered Software, or
318
+ under the terms of any subsequent version published by the license
319
+ steward.
320
+
321
+ 10.3. Modified Versions
322
+
323
+ If you create software not governed by this License, and you want to
324
+ create a new license for such software, you may create and use a modified
325
+ version of this License if you rename the license and remove any
326
+ references to the name of the license steward (except to note that such
327
+ modified license differs from this License).
328
+
329
+ 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
330
+ If You choose to distribute Source Code Form that is Incompatible With
331
+ Secondary Licenses under the terms of this version of the License, the
332
+ notice described in Exhibit B of this License must be attached.
333
+
334
+ Exhibit A - Source Code Form License Notice
335
+
336
+ This Source Code Form is subject to the
337
+ terms of the Mozilla Public License, v.
338
+ 2.0. If a copy of the MPL was not
339
+ distributed with this file, You can
340
+ obtain one at
341
+ http://mozilla.org/MPL/2.0/.
342
+
343
+ If it is not possible or desirable to put the notice in a particular file, then
344
+ You may include the notice in a location (such as a LICENSE file in a relevant
345
+ directory) where a recipient would be likely to look for such a notice.
346
+
347
+ You may add additional accurate notices of copyright ownership.
348
+
349
+ Exhibit B - “Incompatible With Secondary Licenses” Notice
350
+
351
+ This Source Code Form is “Incompatible
352
+ With Secondary Licenses”, as defined by
353
+ the Mozilla Public License, v. 2.0.
data/lib/proc.rb CHANGED
@@ -4,15 +4,19 @@ require_relative "proc/client"
4
4
  require_relative "proc/version"
5
5
 
6
6
  class Proc
7
- def self.connect(authorization, **options)
8
- Client.new(authorization, **options)
9
- end
7
+ class << self
8
+ # [public] Connect a client with an authorization.
9
+ #
10
+ def connect(authorization, **options)
11
+ Client.new(authorization, **options)
12
+ end
10
13
 
11
- def self.undefined
12
- @_undefined ||= ::Object.new
13
- end
14
+ def undefined
15
+ @_undefined ||= ::Object.new
16
+ end
14
17
 
15
- def self.undefined?(value)
16
- value == undefined
18
+ def undefined?(value)
19
+ value == undefined
20
+ end
17
21
  end
18
22
  end
data/lib/proc/callable.rb CHANGED
@@ -16,7 +16,28 @@ class Proc
16
16
  @arguments = arguments.dup
17
17
  end
18
18
 
19
- def call(input = input_omitted = true, **arguments, &block)
19
+ # [public] Dispatches this callable context to proc using the client.
20
+ #
21
+ # If a block is passed, it will be called to prior to dispatch and its result passed as a nested context.
22
+ #
23
+ def call(input = input_omitted = true, **arguments)
24
+ if block_given?
25
+ arguments[:proc] = yield
26
+ end
27
+
28
+ callable = self.class.new(
29
+ @proc,
30
+ client: @client,
31
+ input: input_omitted ? @input : input,
32
+ arguments: @arguments.merge(arguments)
33
+ )
34
+
35
+ @client.call(@proc, callable.input, **callable.arguments)
36
+ end
37
+
38
+ # [public] Dispatches this callable context to proc using the client, calling the given block once for each value.
39
+ #
40
+ def each(input = input_omitted = true, **arguments, &block)
20
41
  callable = self.class.new(
21
42
  @proc,
22
43
  client: @client,
@@ -27,7 +48,13 @@ class Proc
27
48
  @client.call(@proc, callable.input, **callable.arguments, &block)
28
49
  end
29
50
 
51
+ # [public] Creates a new callable context based on this one, with a new input and/or arguments.
52
+ #
30
53
  def with(input = input_omitted = true, **arguments)
54
+ if block_given?
55
+ arguments[:proc] = yield
56
+ end
57
+
31
58
  self.class.new(
32
59
  @proc,
33
60
  client: @client,
@@ -36,6 +63,17 @@ class Proc
36
63
  )
37
64
  end
38
65
 
66
+ # [public] Returns a composition built from this callable context and one or more other callables.
67
+ #
68
+ def compose(*others)
69
+ composed = Composition.new(client: @client, input: @input)
70
+ composed << self
71
+ others.each { |other| composed << other }
72
+ composed
73
+ end
74
+
75
+ # [public] Returns a composition built from this callable context and another callable.
76
+ #
39
77
  def >>(other)
40
78
  composed = Composition.new(client: @client, input: @input)
41
79
  composed << self
@@ -69,21 +107,37 @@ class Proc
69
107
  }
70
108
  end
71
109
 
110
+ # [public] Returns a callable context for `proc`, nested within this callable context.
111
+ #
72
112
  def [](proc)
113
+ arguments = if block_given?
114
+ duped = @arguments.dup
115
+ duped[:proc] = yield
116
+ duped
117
+ else
118
+ @arguments
119
+ end
120
+
73
121
  Callable.new(
74
122
  [@proc, proc].join("."),
75
123
  client: @client,
76
124
  input: @input,
77
- arguments: @arguments
125
+ arguments: arguments
78
126
  )
79
127
  end
80
128
 
81
129
  IGNORE_MISSING = %i[to_hash].freeze
82
130
 
131
+ # [public] Allows nested callable contexts to be built through method lookups.
132
+ #
83
133
  def method_missing(name, input = input_omitted = true, **arguments)
84
134
  if IGNORE_MISSING.include?(name)
85
135
  super
86
136
  else
137
+ if block_given?
138
+ arguments[:proc] = yield
139
+ end
140
+
87
141
  Callable.new(
88
142
  [@proc, name].join("."),
89
143
  client: @client,
@@ -102,7 +156,9 @@ class Proc
102
156
  end
103
157
 
104
158
  private def serialize_value(value)
105
- if value.respond_to?(:serialize)
159
+ if value.is_a?(Symbol)
160
+ ["@@", value.to_s, {}]
161
+ elsif value.respond_to?(:serialize)
106
162
  value.serialize
107
163
  else
108
164
  ["%%", value]
data/lib/proc/client.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "async"
3
+ require "core/async"
4
+ require "http"
4
5
  require "msgpack"
5
6
 
6
7
  require_relative "msgpack/types/decimal"
@@ -12,59 +13,111 @@ require_relative "callable"
12
13
  require_relative "composition"
13
14
  require_relative "enumerator"
14
15
 
15
- require_relative "http/client"
16
-
17
- Console.logger.off!
18
-
19
16
  class Proc
17
+ # [public]
18
+ #
20
19
  class Error < StandardError
21
20
  end
22
21
 
22
+ # [public] Raised when input or arguments are invalid.
23
+ #
23
24
  class Invalid < ::ArgumentError
24
25
  end
25
26
 
27
+ # [public] Raised when a proc endpoint does not exist.
28
+ #
26
29
  class Undefined < ::NameError
27
30
  end
28
31
 
32
+ # [public] Raised when the client is unauthorized.
33
+ #
29
34
  class Unauthorized < Error
30
35
  end
31
36
 
37
+ # [public] Raised when a call is authorized but is blocked.
38
+ #
32
39
  class Forbidden < Error
33
40
  end
34
41
 
42
+ # [public] Raised when proc is unavailable due to an unexpected error.
43
+ #
35
44
  class Unavailable < Error
36
45
  end
37
46
 
47
+ # [public] Raised when the client is being rate limited.
48
+ #
38
49
  class Limited < Error
39
50
  end
40
51
 
52
+ # [public] Raised when a proc call surpasses the defined limit.
53
+ #
41
54
  class Timeout < Error
42
55
  end
43
56
 
57
+ # [public] Connection to proc, configured with an authorization.
58
+ #
44
59
  class Client
45
- attr_reader :authorization, :scheme, :host
60
+ include Is::Async
61
+
62
+ # [public] The configured authorization.
63
+ #
64
+ attr_reader :authorization
65
+
66
+ # [public] The configured scheme.
67
+ #
68
+ attr_reader :scheme
69
+
70
+ # [public] The configured host.
71
+ #
72
+ attr_reader :host
73
+
74
+ # [public] The number of requests this client has performed.
75
+ #
76
+ attr_reader :request_count
77
+
78
+ DEFAULT_HEADERS = {
79
+ "accept" => "application/vnd.proc+msgpack",
80
+ "content-type" => "application/vnd.proc+msgpack"
81
+ }.freeze
46
82
 
47
83
  def initialize(authorization, scheme: "https", host: "proc.dev")
48
84
  @authorization = authorization
49
85
  @scheme = scheme
50
86
  @host = host
51
- @internal = Http::Client.new
87
+ @request_count = 0
88
+
89
+ @__base_url = "#{@scheme}://#{host}"
90
+ @__headers = {
91
+ "authorization" => "bearer #{@authorization}"
92
+ }.merge(DEFAULT_HEADERS)
52
93
  end
53
94
 
95
+ # [public] Returns a callable context for `proc`.
96
+ #
54
97
  def [](proc)
55
- Callable.new(proc, client: self)
98
+ if block_given?
99
+ Callable.new(proc, client: self, arguments: {proc: yield})
100
+ else
101
+ Callable.new(proc, client: self)
102
+ end
56
103
  end
57
104
 
105
+ # [public] Returns the current rate limit.
106
+ #
58
107
  def rate_limit
59
108
  refresh_rate_limit
60
109
  @rate_limit
61
110
  end
62
111
 
112
+ # [public] Returns the current rate limit window.
113
+ #
63
114
  def rate_limit_window
64
115
  refresh_rate_limit
65
116
  @rate_limit_window
66
117
  end
67
118
 
119
+ # [public] Returns the time at which the current rate limit will reset.
120
+ #
68
121
  def rate_limit_reset
69
122
  refresh_rate_limit
70
123
  @rate_limit_reset
@@ -76,11 +129,10 @@ class Proc
76
129
  end
77
130
  end
78
131
 
79
- DEFAULT_HEADERS = {
80
- "accept" => "application/vnd.proc+msgpack",
81
- "content-type" => "application/vnd.proc+msgpack"
82
- }.freeze
83
-
132
+ # [public] Calls a proc with the given input and arguments.
133
+ #
134
+ # If a block is passed and the proc returns an enumerable, the block will be called with each value.
135
+ #
84
136
  def call(proc = nil, input = Proc.undefined, **arguments, &block)
85
137
  body = []
86
138
 
@@ -92,11 +144,7 @@ class Proc
92
144
  body << ["$$", key.to_s, serialize_value(value)]
93
145
  end
94
146
 
95
- headers = {
96
- "authorization" => "bearer #{@authorization}"
97
- }.merge(DEFAULT_HEADERS)
98
-
99
- status, headers, payload = get_payload(proc: proc, headers: headers, body: body)
147
+ status, headers, payload = get_payload(proc: proc, body: body)
100
148
 
101
149
  case status
102
150
  when 200
@@ -143,6 +191,8 @@ class Proc
143
191
  end
144
192
  end
145
193
 
194
+ # [public] Allows callable contexts to be built through method lookups.
195
+ #
146
196
  def method_missing(name, input = input_omitted = true, *, **arguments)
147
197
  if input_omitted
148
198
  Callable.new(name, client: self, arguments: arguments)
@@ -155,52 +205,58 @@ class Proc
155
205
  true
156
206
  end
157
207
 
208
+ # [public] Builds a named argument with options.
209
+ #
158
210
  def argument(name, **options)
159
211
  Argument.new(name, **options)
160
212
  end
161
213
  alias_method :arg, :argument
162
214
 
163
- def close
164
- @internal.close
165
- end
166
-
167
215
  private def build_uri(proc)
168
- host_and_path = File.join(@host, proc.to_s.split(".").join("/"))
169
-
170
- "#{@scheme}://#{host_and_path}"
216
+ File.join(@__base_url, proc.to_s.split(".").join("/"))
171
217
  end
172
218
 
173
219
  private def serialize_value(value)
174
- if value.respond_to?(:serialize)
220
+ if value.is_a?(Symbol)
221
+ ["@@", value.to_s, {}]
222
+ elsif value.respond_to?(:serialize)
175
223
  value.serialize
176
224
  else
177
225
  ["%%", value]
178
226
  end
179
227
  end
180
228
 
181
- private def get_payload(proc:, headers:, body:)
182
- @internal.call(:post, build_uri(proc), headers: headers, body: MessagePack.pack(body)) do |response|
183
- split_limit = response.headers["x-rate-limit"].to_s.split(";window=")
229
+ private def get_payload(proc:, body:)
230
+ await {
231
+ @request_count += 1
184
232
 
185
- @rate_limit = split_limit[0].to_i
233
+ response = HTTP.headers(@__headers).post(build_uri(proc), body: MessagePack.pack(body))
186
234
 
187
- @rate_limit_window = case split_limit[1]
188
- when "60"
189
- :minute
190
- when "1"
191
- :second
192
- end
235
+ update_rate_limit(response)
193
236
 
194
- @rate_limit_reset = if (reset = response.headers["x-rate-limit-reset"])
195
- Time.at(reset.to_s.to_i)
196
- end
197
-
198
- [response.status, response.headers, MessagePack.unpack(response.read)]
199
- end
237
+ [response.status, response.headers, MessagePack.unpack(response.to_s)]
238
+ }
200
239
  rescue
201
240
  raise Proc::Unavailable
202
241
  end
203
242
 
243
+ private def update_rate_limit(response)
244
+ split_limit = response.headers["x-rate-limit"].to_s.split(";window=")
245
+
246
+ @rate_limit = split_limit[0].to_i
247
+
248
+ @rate_limit_window = case split_limit[1]
249
+ when "60"
250
+ :minute
251
+ when "1"
252
+ :second
253
+ end
254
+
255
+ @rate_limit_reset = if (reset = response.headers["x-rate-limit-reset"])
256
+ Time.at(reset.to_s.to_i)
257
+ end
258
+ end
259
+
204
260
  private def extract_output(payload)
205
261
  payload.each do |tuple|
206
262
  case tuple[0]
@@ -15,7 +15,26 @@ class Proc
15
15
  @callables = @callables.dup
16
16
  end
17
17
 
18
- def call(input = input_omitted = true, **arguments, &block)
18
+ # [public] Dispatches this composition to proc using the client.
19
+ #
20
+ def call(input = input_omitted = true, **arguments)
21
+ if block_given?
22
+ arguments[:proc] = yield
23
+ end
24
+
25
+ callable = self.class.new(
26
+ client: @client,
27
+ input: input_omitted ? @input : input,
28
+ callables: @callables.dup,
29
+ arguments: @arguments.merge(arguments)
30
+ )
31
+
32
+ @client.call("exec", Proc.undefined, proc: callable)
33
+ end
34
+
35
+ # [public] Dispatches this composition to proc using the client, calling the given block once for each value.
36
+ #
37
+ def each(input = input_omitted = true, **arguments, &block)
19
38
  callable = self.class.new(
20
39
  client: @client,
21
40
  input: input_omitted ? @input : input,
@@ -26,7 +45,13 @@ class Proc
26
45
  @client.call("exec", Proc.undefined, proc: callable, &block)
27
46
  end
28
47
 
48
+ # [public] Creates a new composition based on this one, with a new input and/or arguments.
49
+ #
29
50
  def with(input = input_omitted = true, **arguments)
51
+ if block_given?
52
+ arguments[:proc] = yield
53
+ end
54
+
30
55
  self.class.new(
31
56
  client: @client,
32
57
  input: input_omitted ? @input : input,
@@ -35,6 +60,16 @@ class Proc
35
60
  )
36
61
  end
37
62
 
63
+ # [public] Returns a composition from this composition and one or more other callables.
64
+ #
65
+ def compose(*others)
66
+ composed = dup
67
+ others.each { |other| composed << other }
68
+ composed
69
+ end
70
+
71
+ # [public] Returns a composition built from this composition and another callable.
72
+ #
38
73
  def >>(other)
39
74
  composed = dup
40
75
  composed << other
@@ -78,7 +113,9 @@ class Proc
78
113
  end
79
114
 
80
115
  private def serialize_value(value)
81
- if value.respond_to?(:serialize)
116
+ if value.is_a?(Symbol)
117
+ ["@@", value.to_s, {}]
118
+ elsif value.respond_to?(:serialize)
82
119
  value.serialize
83
120
  else
84
121
  ["%%", value]
@@ -4,18 +4,21 @@ class Proc
4
4
  class Enumerator
5
5
  include Enumerable
6
6
 
7
+ attr_reader :values, :next_block
8
+
7
9
  def initialize(values, &next_block)
8
10
  @values = values
9
11
  @next_block = next_block
10
12
  end
11
13
 
12
- def each(&block)
14
+ # [public] Calls the given block once for each value.
15
+ #
16
+ def each(enumerable = self, &block)
13
17
  return to_enum(:each) unless block
14
18
 
15
- @values.each(&block)
16
-
17
- if @next_block
18
- @next_block.call.each(&block)
19
+ while enumerable
20
+ enumerable.values.each(&block)
21
+ enumerable = enumerable.next_block&.call
19
22
  end
20
23
  end
21
24
  end
data/lib/proc/version.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Proc
4
- VERSION = "0.9.1"
4
+ VERSION = "0.10.0"
5
5
 
6
+ # [public]
7
+ #
6
8
  def self.version
7
9
  VERSION
8
10
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.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: 2021-03-17 00:00:00.000000000 Z
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: async-http
14
+ name: core-async
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.54.1
19
+ version: 0.5.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.54.1
26
+ version: 0.5.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: core-async
28
+ name: http
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.2.1
33
+ version: '4.4'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.2.1
40
+ version: '4.4'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: msgpack
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,14 +66,11 @@ files:
66
66
  - lib/proc/client.rb
67
67
  - lib/proc/composition.rb
68
68
  - lib/proc/enumerator.rb
69
- - lib/proc/http/client.rb
70
- - lib/proc/http/request.rb
71
- - lib/proc/http/response.rb
72
69
  - lib/proc/msgpack/types/decimal.rb
73
70
  - lib/proc/version.rb
74
71
  homepage: https://proc.dev/
75
72
  licenses:
76
- - MIT
73
+ - MPL-2.0
77
74
  metadata: {}
78
75
  post_install_message:
79
76
  rdoc_options: []
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "async"
4
- require "async/http/internet"
5
-
6
- require "core/async"
7
-
8
- require_relative "request"
9
- require_relative "response"
10
-
11
- class Proc
12
- module Http
13
- class Client
14
- include Is::Async
15
-
16
- def call(method, uri, params: {}, headers: {}, body: nil)
17
- internet = Async::HTTP::Internet.new
18
- request = Request.new(method: method, uri: uri, params: params, headers: headers, body: body)
19
-
20
- await {
21
- begin
22
- response = make_request(internet, request)
23
-
24
- yield response
25
- ensure
26
- response&.close
27
- internet&.close
28
- end
29
- }
30
- end
31
-
32
- private def make_request(internet, request)
33
- async_response = internet.call(*request.callable)
34
- wrap_async_response_for_request(async_response, request)
35
- end
36
-
37
- private def wrap_async_response_for_request(async_response, request)
38
- Response.new(request, async_response)
39
- end
40
- end
41
- end
42
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Proc
4
- module Http
5
- class Request
6
- attr_reader :method, :uri, :params, :headers, :body
7
-
8
- def initialize(method:, uri:, params:, headers:, body:)
9
- @method = method
10
- @uri = uri
11
- @params = params
12
- @headers = headers
13
- @body = body
14
- end
15
-
16
- def callable
17
- [@method.to_s.upcase, finalize_uri(@uri, @params), @headers, @body]
18
- end
19
-
20
- private def finalize_uri(uri, params)
21
- "#{uri}#{query_string(params)}"
22
- end
23
-
24
- private def query_string(params)
25
- params = params.compact
26
-
27
- if params.any?
28
- "?" + params.compact.map { |key, value|
29
- "#{key}=#{value}"
30
- }.join("&")
31
- else
32
- ""
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "forwardable"
4
-
5
- class Proc
6
- module Http
7
- class Response
8
- extend Forwardable
9
- def_delegators :@response, :status, :version, :headers, :read, :close
10
-
11
- attr_reader :request
12
-
13
- def initialize(request, response)
14
- @request = request
15
- @response = response
16
- end
17
- end
18
- end
19
- end