web_function 0.3.2 → 0.4.1

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: 651453280b0a98140477f0546132fe12d8d3019b7d4d2d70e7ab35151e64ba7a
4
- data.tar.gz: 655a1615c48658c5e21738a8c88ea8c2a65916fd1edf54087d6fd39ec3e7402f
3
+ metadata.gz: cc10b3e4f558c7bec819e5f9029f672df3e37e5e5c66790df2e1159d2b06acec
4
+ data.tar.gz: deb73a647c14e886ef7b3be4e69a5a080fead74ef00119b56af86b57462452d5
5
5
  SHA512:
6
- metadata.gz: d9d4d809aae59eb372f2c803ae14c0bb9396b7fb9e333af8a17773dc7b211981c3169f3da2f0762998825618e198afc6b172b4edad2c097a15fbe70dd09e71b5
7
- data.tar.gz: a51eceeb6e007aec231e4ab201bd4164d6c0e618ee1c268f6f4b4c200593dd887694d534de52350af7d5fcb091bfa19f16be5922b6ec46a141357b756bdca314
6
+ metadata.gz: 37b3060aefd75b936ce0c5e9f3e1ec7e20f84ab118840f262d4631b020cebba24aaa9df251c1bb37560ff881df42412c688b6c6f519bacfe377aa4a5230676d3
7
+ data.tar.gz: 6240884af37b04edf66426653b9d8de19445be9817c93b624bccbb2ed0ca290ccce6dd98669ea2c2d7c114c8ec03c4b6c0fee1871c33e36fb7aeeb65d92517fb
@@ -2,19 +2,24 @@
2
2
 
3
3
  module WebFunction
4
4
  class Client
5
- def initialize(package, bearer_auth: nil)
5
+ def initialize(package, bearer_auth: nil, pipeline: nil)
6
6
  @package = package
7
7
  @endpoints = package.endpoints.to_h { |e| [e.name.gsub("-", "_").to_sym, e] }
8
8
  @bearer_auth = bearer_auth
9
+ @pipeline = pipeline
9
10
  end
10
11
 
11
12
  attr_reader :package
12
13
 
13
- def self.from_package_endpoint(url, bearer_auth: nil)
14
+ def self.from_package_endpoint(url, bearer_auth: nil, pipeline_url: nil, pipeline: nil)
14
15
  response = Endpoint.invoke(url, bearer_auth: bearer_auth)
15
16
  package = Package.new(response)
16
17
 
17
- new(package, bearer_auth: bearer_auth)
18
+ if pipeline.is_a?(String)
19
+ pipeline = WebFunction::Pipeline.new(pipeline)
20
+ end
21
+
22
+ new(package, bearer_auth: bearer_auth, pipeline: pipeline)
18
23
  end
19
24
 
20
25
  def methods
@@ -35,6 +40,12 @@ module WebFunction
35
40
  url = URI.join(@package.base_url, endpoint.name).to_s
36
41
  args = args.first
37
42
 
43
+ if @pipeline
44
+ step = Endpoint.invoke(url, bearer_auth: @bearer_auth, args: args, as_step: true)
45
+ promise = @pipeline.add_step(step)
46
+ return promise
47
+ end
48
+
38
49
  Endpoint.invoke(url, bearer_auth: @bearer_auth, args: args)
39
50
  end
40
51
  end
@@ -6,17 +6,29 @@ module WebFunction
6
6
  @endpoint = endpoint
7
7
  end
8
8
 
9
- def self.invoke(url, bearer_auth: nil, args: {})
9
+ def self.invoke(url, bearer_auth: nil, args: {}, as_step: false)
10
10
  headers = {
11
11
  "Content-Type": "application/json",
12
12
  "Accept": "application/json",
13
13
  "User-Agent": "webfunction/#{WebFunction::VERSION}",
14
14
  }
15
15
 
16
+ if args.nil?
17
+ args = {}
18
+ end
19
+
16
20
  if bearer_auth
17
21
  headers["Authorization"] = "Bearer #{bearer_auth}"
18
22
  end
19
23
 
24
+ if as_step
25
+ return {
26
+ url: url,
27
+ headers: headers,
28
+ body: args,
29
+ }
30
+ end
31
+
20
32
  response = Excon.post(url,
21
33
  headers: headers,
22
34
  body: JSON.generate(args),
@@ -0,0 +1,62 @@
1
+ module WebFunction
2
+ class Pipeline
3
+ def initialize(url)
4
+ @url = url
5
+ @steps = []
6
+ @promises = []
7
+ end
8
+
9
+ def add_step(step)
10
+ n = @promises.count
11
+ promise = Promise.new(self, "$[#{n}]")
12
+
13
+ @steps << step
14
+ @promises << promise
15
+
16
+ promise
17
+ end
18
+
19
+ def execute(returns: :all)
20
+ case returns
21
+ when :all
22
+ responses = Endpoint.invoke(@url, args: {
23
+ steps: @steps,
24
+ returns: "$",
25
+ })
26
+
27
+ responses.each_with_index do |response, index|
28
+ @promises[index].value = response
29
+ end
30
+
31
+ reset!
32
+
33
+ responses
34
+ when :last
35
+ response = Endpoint.invoke(@url, args: {
36
+ steps: @steps,
37
+ returns: "$[-1:]",
38
+ })
39
+
40
+ @promises.last.value = response
41
+
42
+ reset!
43
+
44
+ response
45
+ else
46
+ response = Endpoint.invoke(@url, args: {
47
+ steps: @steps,
48
+ returns: returns,
49
+ })
50
+
51
+ reset!
52
+
53
+ response
54
+ end
55
+ end
56
+
57
+ def reset!
58
+ @steps = []
59
+ @promises = []
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,82 @@
1
+ module WebFunction
2
+ class Promise
3
+ def initialize(pipeline, path)
4
+ @pipeline = pipeline
5
+ @path = Path.new(path)
6
+ @value = nil
7
+ end
8
+
9
+ class Path
10
+ def initialize(path)
11
+ @path = path
12
+ end
13
+
14
+ def to_s
15
+ @path
16
+ end
17
+
18
+ def to_json(*args)
19
+ @path.to_json(*args)
20
+ end
21
+
22
+ def [](key)
23
+ case key
24
+ when String, Symbol
25
+ mutate("#{@path}.#{key}")
26
+ when Integer
27
+ mutate("#{@path}[#{key}]")
28
+ else
29
+ raise ArgumentError
30
+ end
31
+ end
32
+
33
+ def mutate(path)
34
+ Path.new(path)
35
+ end
36
+ end
37
+
38
+ attr_writer :value
39
+
40
+ def to_s
41
+ if @value
42
+ @value.to_s
43
+ else
44
+ @path.to_s
45
+ end
46
+ end
47
+
48
+ def to_json(*args)
49
+ if @value
50
+ @value.to_json(*args)
51
+ else
52
+ @path.to_json(*args)
53
+ end
54
+ end
55
+
56
+ def [](key)
57
+ if @value
58
+ @value[key]
59
+ else
60
+ @path[key]
61
+ end
62
+ end
63
+
64
+ def value
65
+ unless @value
66
+ raise UnresolvedPromise
67
+ end
68
+
69
+ @value
70
+ end
71
+
72
+ def resolve
73
+ if @value
74
+ return @value
75
+ end
76
+
77
+ @pipeline.execute
78
+
79
+ value
80
+ end
81
+ end
82
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebFunction
4
- VERSION = "0.3.2"
4
+ VERSION = "0.4.1"
5
5
  end
data/lib/web_function.rb CHANGED
@@ -11,8 +11,12 @@ require_relative "web_function/endpoint"
11
11
  require_relative "web_function/argument"
12
12
  require_relative "web_function/attribute"
13
13
  require_relative "web_function/documented_error"
14
+ require_relative "web_function/pipeline"
15
+ require_relative "web_function/promise"
14
16
 
15
17
  module WebFunction
18
+ UnresolvedPromise = Class.new(StandardError)
19
+
16
20
  class Error < StandardError
17
21
  attr_reader :code, :details
18
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_function
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Clart
@@ -61,6 +61,8 @@ files:
61
61
  - lib/web_function/documented_error.rb
62
62
  - lib/web_function/endpoint.rb
63
63
  - lib/web_function/package.rb
64
+ - lib/web_function/pipeline.rb
65
+ - lib/web_function/promise.rb
64
66
  - lib/web_function/version.rb
65
67
  - sig/web_function.rbs
66
68
  homepage: https://github.com/robinclart/web_function
@@ -86,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
88
  - !ruby/object:Gem::Version
87
89
  version: '0'
88
90
  requirements: []
89
- rubygems_version: 3.7.1
91
+ rubygems_version: 4.0.5
90
92
  specification_version: 4
91
93
  summary: A Web Function Client for Ruby
92
94
  test_files: []