web_function 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8699012072dba58c29800e828b4fd2b5805943b174e8337b94d42d84f8429608
4
+ data.tar.gz: 8e15d51256a8ee847983cdf1c354bcddf8295698ad3a47357e251678606fe083
5
+ SHA512:
6
+ metadata.gz: ad6f0306060921364ae8f2adf8c426d211fbf9fc7daed884868c1317c35b165af9833c596e82492e76bff6d2ef6a229d29916887e51de7ea64a724495c8c54ca
7
+ data.tar.gz: 191cd221a27797e9eee811fe921ceb99d71d1dfb7ad61a60f8a0e77eed29af7061b71b2b7d8a6637da43b3f89afa3e01064ce22ffc0e855e51e8bd91da8792b2
data/.rubocop.yml ADDED
@@ -0,0 +1,42 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ SuggestExtensions: false
4
+
5
+ Metrics:
6
+ Enabled: false
7
+
8
+ Style/Documentation:
9
+ Enabled: false
10
+
11
+ Style/StringLiterals:
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ EnforcedStyle: double_quotes
16
+
17
+ Style/TrailingCommaInArguments:
18
+ EnforcedStyleForMultiline: consistent_comma
19
+
20
+ Style/TrailingCommaInArrayLiteral:
21
+ EnforcedStyleForMultiline: consistent_comma
22
+
23
+ Style/TrailingCommaInHashLiteral:
24
+ EnforcedStyleForMultiline: consistent_comma
25
+
26
+ Style/RaiseArgs:
27
+ EnforcedStyle: compact
28
+
29
+ Layout/MultilineMethodCallBraceLayout:
30
+ EnforcedStyle: new_line
31
+
32
+ Style/IfUnlessModifier:
33
+ Enabled: false
34
+
35
+ Lint/SymbolConversion:
36
+ Enabled: false
37
+
38
+ Layout/ArgumentAlignment:
39
+ EnforcedStyle: with_fixed_indentation
40
+
41
+ Style/ConditionalAssignment:
42
+ EnforcedStyle: assign_inside_condition
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-02-16
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Robin Clart
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # WebFunction
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/web_function`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/web_function.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
data/exe/wfn ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "web_function"
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebFunction
4
+ class Argument
5
+ def initialize(argument)
6
+ @argument = argument
7
+ end
8
+
9
+ def name
10
+ @argument["name"]
11
+ end
12
+
13
+ def type
14
+ @argument["type"]
15
+ end
16
+
17
+ def flags
18
+ @argument["flags"]
19
+ end
20
+
21
+ def docs
22
+ @argument["docs"]
23
+ end
24
+
25
+ def to_s
26
+ [" - #{name} (#{type}):", docs].join(" ")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebFunction
4
+ class Client
5
+ def initialize(package, bearer_auth: nil)
6
+ @package = package
7
+ @endpoints = package.endpoints.to_h { |e| [e.name.gsub("-", "_").to_sym, e] }
8
+ @bearer_auth = bearer_auth
9
+ end
10
+
11
+ attr_reader :package
12
+
13
+ def self.from_package_endpoint(url, bearer_auth: nil)
14
+ response = Endpoint.invoke(url, bearer_auth: bearer_auth)
15
+ package = Package.new(response)
16
+
17
+ new(package, bearer_auth: bearer_auth)
18
+ end
19
+
20
+ def methods
21
+ super + @endpoints.keys
22
+ end
23
+
24
+ def respond_to_missing?(method_name, include_private = false)
25
+ @endpoints[method_name] || super
26
+ end
27
+
28
+ def method_missing(method_name, *args)
29
+ endpoint = @endpoints[method_name]
30
+
31
+ unless endpoint
32
+ super
33
+ end
34
+
35
+ url = URI.join(@package.base_url, endpoint.name).to_s
36
+ args = args.first
37
+
38
+ Endpoint.invoke(url, bearer_auth: @bearer_auth, args: args)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebFunction
4
+ class Documentation
5
+ def initialize(package)
6
+ @package = package
7
+ end
8
+
9
+ def generate
10
+ buffer = ""
11
+
12
+ buffer << @package.docs.strip
13
+ buffer << "\n\n"
14
+ @package.endpoints.each do |endpoint|
15
+ arguments = endpoint.arguments
16
+ buffer << endpoint.docs.strip
17
+ buffer << "\n\n"
18
+ buffer << "Signature:"
19
+ buffer << "\n"
20
+ buffer << "```"
21
+ buffer << "\n"
22
+ buffer << endpoint.name
23
+ buffer << " { "
24
+ buffer << arguments.map { |a| "#{a.name}: #{a.type}" }.join(", ")
25
+ buffer << " } -> ("
26
+ buffer << endpoint.returns.join(" | ")
27
+ buffer << ")"
28
+ buffer << "\n"
29
+ buffer << "```"
30
+ buffer << "\n\n"
31
+ buffer << "Arguments:"
32
+ buffer << "\n"
33
+ arguments.each do |argument|
34
+ buffer << " - "
35
+ buffer << "#{argument.name} (#{argument.type}): "
36
+ if argument.flags.include?("required")
37
+ buffer << "**Required.** "
38
+ else
39
+ buffer << "**Optional.** "
40
+ end
41
+ buffer << argument.docs.strip
42
+ buffer << "\n"
43
+ end
44
+ buffer << "\n"
45
+ end
46
+
47
+ buffer
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebFunction
4
+ class Endpoint
5
+ def initialize(endpoint)
6
+ @endpoint = endpoint
7
+ end
8
+
9
+ def self.invoke(url, bearer_auth: nil, args: {})
10
+ headers = {
11
+ "Content-Type": "application/json",
12
+ "Accept": "application/json",
13
+ }
14
+
15
+ if bearer_auth
16
+ headers["Authorization"] = "Bearer #{bearer_auth}"
17
+ end
18
+
19
+ response = Excon.post(url,
20
+ headers: headers,
21
+ body: JSON.generate(args),
22
+ )
23
+
24
+ result = JSON.parse(response.body)
25
+
26
+ if response.status == 400
27
+ case result
28
+ when Array
29
+ if result.length == 3 && result[0].is_a?(String) && result[1].is_a?(String)
30
+ code = result[0]
31
+ message = result[1]
32
+ details = result[2]
33
+
34
+ raise WebFunction::Error.new(message, code: code, details: details)
35
+ else
36
+ raise WebFunction::Error.new("Bad request", details: result)
37
+ end
38
+ when String
39
+ raise WebFunction::Error.new(result)
40
+ else
41
+ raise WebFunction::Error.new("Bad request", details: result)
42
+ end
43
+ end
44
+
45
+ if response.status != 200
46
+ raise WebFunction::Error.new("Something went wrong, unexpected status code [#{response.status}]")
47
+ end
48
+
49
+ result
50
+ end
51
+
52
+ def name
53
+ @endpoint["name"]
54
+ end
55
+
56
+ def returns
57
+ @endpoint["returns"]
58
+ end
59
+
60
+ def flags
61
+ @endpoint["flags"]
62
+ end
63
+
64
+ def docs
65
+ @endpoint["docs"]
66
+ end
67
+
68
+ def arguments
69
+ @endpoint["arguments"].map { |argument| Argument.new(argument) }
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebFunction
4
+ class Package
5
+ def initialize(package)
6
+ @package = package
7
+ end
8
+
9
+ def documentation
10
+ Documentation.new(self)
11
+ end
12
+
13
+ def base_url
14
+ @package["base_url"]
15
+ end
16
+
17
+ def name
18
+ @package["name"]
19
+ end
20
+
21
+ def flags
22
+ @package["flags"]
23
+ end
24
+
25
+ def docs
26
+ @package["docs"]
27
+ end
28
+
29
+ def endpoints
30
+ @package["endpoints"].map { |endpoint| Endpoint.new(endpoint) }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebFunction
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "excon"
4
+ require "json"
5
+ require "uri"
6
+
7
+ require_relative "web_function/version"
8
+ require_relative "web_function/client"
9
+ require_relative "web_function/package"
10
+ require_relative "web_function/endpoint"
11
+ require_relative "web_function/argument"
12
+ require_relative "web_function/documentation"
13
+
14
+ module WebFunction
15
+ class Error < StandardError
16
+ attr_reader :code, :details
17
+
18
+ def initialize(message = nil, code: nil, details: nil)
19
+ super(message)
20
+ @code = code
21
+ @details = details
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ module WebFunction
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: web_function
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robin Clart
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-04-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: excon
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.2'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.2'
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.10'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.10'
40
+ description: 'A lightweight Web Function client for Ruby. Web Function is a radical
41
+ rethinking of API design: no verbs, no nested URLs, no bloat. Just function calls
42
+ over HTTP. This gem lets you invoke endpoints defined in a package, with full support
43
+ for argument validations, error triples, and bearer auth.'
44
+ email:
45
+ - robin@clart.be
46
+ executables:
47
+ - wfn
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".rubocop.yml"
52
+ - CHANGELOG.md
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - exe/wfn
57
+ - lib/web_function.rb
58
+ - lib/web_function/argument.rb
59
+ - lib/web_function/client.rb
60
+ - lib/web_function/documentation.rb
61
+ - lib/web_function/endpoint.rb
62
+ - lib/web_function/package.rb
63
+ - lib/web_function/version.rb
64
+ - sig/web_function.rbs
65
+ homepage: https://github.com/robinclart/web_function
66
+ licenses:
67
+ - MIT
68
+ metadata:
69
+ allowed_push_host: https://rubygems.org
70
+ homepage_uri: https://github.com/robinclart/web_function
71
+ source_code_uri: https://github.com/robinclart/web-functions-ruby
72
+ changelog_uri: https://github.com/robinclart/web-functions-ruby/blob/main/CHANGELOG.md
73
+ rubygems_mfa_required: 'true'
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 3.1.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.6.6
89
+ specification_version: 4
90
+ summary: A Web Function Client for Ruby
91
+ test_files: []