open-wire 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: 5b4a78b6f4bcdacc5ec15ad927dda0bcb677f6ae4eb0d64e51a43579dc4a6f0a
4
+ data.tar.gz: 7bca3016d72a127a1ed9ea26b228b797d1e2aa6b024f8c6b40f8f694f93cd78a
5
+ SHA512:
6
+ metadata.gz: 79296d68c11e232cc4aa22054b6687cd8b9b8f85e0793c49c98f2d5c9ece0b4ce31c1c9bf25b3baa473e5726d4646452ed25f06ee8026813b7feafebcdd941e6
7
+ data.tar.gz: 2dd4177c8c722ab33e5f759d55d74577d97cfb70b362bb2033c005b6cb27c547c7265d5ff81b351d0ecb4ea1d5f3715a21f53c74285616684669383f4206eeff
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tiny Bubble Company
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Open-Wire Ruby gem (`open-wire`)
2
+
3
+ Protocol client and webhook helpers for [open-wire/1](https://openwire.rails-agent.com/docs/protocol).
4
+
5
+ ## Install
6
+
7
+ ```ruby
8
+ # Gemfile
9
+ gem "open-wire", "~> 0.1"
10
+ ```
11
+
12
+ ```bash
13
+ gem install open-wire
14
+ ```
15
+
16
+ ## Send
17
+
18
+ ```ruby
19
+ client = OpenWire::Client.new(
20
+ base_url: "https://openwire.rails-agent.com",
21
+ api_key: ENV["OPEN_WIRE_API_KEY"]
22
+ )
23
+
24
+ client.send_message(
25
+ installation_id: "inst_…",
26
+ to: { id: "C123", kind: "channel" },
27
+ text: "Hello from Rails",
28
+ thread_id: "C123:1710000000.000100"
29
+ )
30
+ ```
31
+
32
+ ## Inbound webhook
33
+
34
+ ```ruby
35
+ message = OpenWire::Webhook.verify_and_parse!(
36
+ secret: ENV["OPEN_WIRE_WEBHOOK_SECRET"],
37
+ body: request.raw_post,
38
+ headers: request.headers
39
+ )
40
+ ```
41
+
42
+ See [protocol docs](https://openwire.rails-agent.com/docs/protocol) for the full envelope shape.
data/lib/open-wire.rb ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # open-wire Ruby SDK — protocol client + webhook helpers.
4
+ # Used by rails-agent-stack as a channel adapter; publishable on RubyGems later.
5
+
6
+ require "open_wire"
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenWire
4
+ # HTTP client for the Open-Wire gateway (outbound message.send).
5
+ class Client
6
+ def initialize(base_url: nil, api_key: nil)
7
+ @base_url = (base_url || OpenWire.configuration.base_url).to_s.sub(%r{/+\z}, "")
8
+ @api_key = api_key || OpenWire.configuration.api_key
9
+ raise ConfigurationError, "OPEN_WIRE_API_KEY / api_key is required" if @api_key.to_s.empty?
10
+ end
11
+
12
+ # Send a message through an installation.
13
+ #
14
+ # client.send_message(
15
+ # installation_id: "inst_…",
16
+ # to: { id: "C123", kind: "channel" },
17
+ # text: "Hello",
18
+ # thread_id: "C123:1710000000.000100"
19
+ # )
20
+ def send_message(installation_id:, to:, text: nil, body: nil, thread_id: nil, blocks: nil)
21
+ payload_body = body || {}
22
+ payload_body = payload_body.merge("text" => text) if text
23
+ payload_body = payload_body.merge("blocks" => blocks) if blocks
24
+
25
+ envelope = {
26
+ "protocol" => OpenWire::PROTOCOL,
27
+ "type" => "message.send",
28
+ "installation_id" => installation_id,
29
+ "to" => stringify_keys(to),
30
+ "body" => stringify_keys(payload_body)
31
+ }
32
+ if thread_id
33
+ envelope["thread"] = { "id" => thread_id }
34
+ end
35
+
36
+ request_json(:post, "/api/v1/messages", envelope)
37
+ end
38
+
39
+ def health
40
+ uri = URI("#{@base_url}/api/v1/health")
41
+ res = Net::HTTP.get_response(uri)
42
+ JSON.parse(res.body)
43
+ end
44
+
45
+ private
46
+
47
+ def request_json(method, path, body = nil)
48
+ uri = URI("#{@base_url}#{path}")
49
+ http = Net::HTTP.new(uri.host, uri.port)
50
+ http.use_ssl = uri.scheme == "https"
51
+ http.open_timeout = OpenWire.configuration.open_timeout
52
+ http.read_timeout = OpenWire.configuration.read_timeout
53
+
54
+ req =
55
+ case method
56
+ when :post then Net::HTTP::Post.new(uri)
57
+ when :get then Net::HTTP::Get.new(uri)
58
+ else
59
+ raise ArgumentError, "unsupported method #{method}"
60
+ end
61
+ req["Authorization"] = "Bearer #{@api_key}"
62
+ req["Content-Type"] = "application/json"
63
+ req["Accept"] = "application/json"
64
+ req.body = JSON.generate(body) if body
65
+
66
+ res = http.request(req)
67
+ parsed = JSON.parse(res.body) rescue {}
68
+ unless res.is_a?(Net::HTTPSuccess)
69
+ message = parsed.dig("error", "message") || "Open-Wire error #{res.code}"
70
+ raise ApiError, message
71
+ end
72
+ parsed
73
+ end
74
+
75
+ def stringify_keys(obj)
76
+ case obj
77
+ when Hash
78
+ obj.each_with_object({}) { |(k, v), h| h[k.to_s] = stringify_keys(v) }
79
+ when Array
80
+ obj.map { |v| stringify_keys(v) }
81
+ else
82
+ obj
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenWire
4
+ class Configuration
5
+ attr_accessor :base_url, :api_key, :open_timeout, :read_timeout
6
+
7
+ def initialize
8
+ @base_url = ENV.fetch("OPEN_WIRE_URL", "https://openwire.rails-agent.com")
9
+ @api_key = ENV["OPEN_WIRE_API_KEY"]
10
+ @open_timeout = 3
11
+ @read_timeout = 15
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenWire
4
+ # Parsed open-wire/1 inbound envelope.
5
+ class InboundMessage
6
+ attr_reader :raw, :id, :channel, :installation_id, :created_at,
7
+ :thread_id, :from_id, :from_kind, :to_id, :to_kind, :text, :body
8
+
9
+ def initialize(payload)
10
+ @raw = payload.is_a?(Hash) ? payload : {}
11
+ @id = @raw["id"]
12
+ @channel = @raw["channel"]
13
+ @installation_id = @raw["installation_id"]
14
+ @created_at = @raw["created_at"]
15
+ thread = @raw["thread"] || {}
16
+ @thread_id = thread["id"]
17
+ from = @raw["from"] || {}
18
+ @from_id = from["id"]
19
+ @from_kind = from["kind"]
20
+ to = @raw["to"] || {}
21
+ @to_id = to["id"]
22
+ @to_kind = to["kind"]
23
+ @body = @raw["body"] || {}
24
+ @text = (@body["text"] || "").to_s
25
+ end
26
+
27
+ def dm?
28
+ to_kind.to_s == "dm"
29
+ end
30
+
31
+ def protocol
32
+ @raw["protocol"]
33
+ end
34
+
35
+ def type
36
+ @raw["type"]
37
+ end
38
+
39
+ def to_h
40
+ @raw
41
+ end
42
+ end
43
+
44
+ module Envelope
45
+ module_function
46
+
47
+ def inbound?(payload)
48
+ payload.is_a?(Hash) &&
49
+ payload["protocol"].to_s == OpenWire::PROTOCOL &&
50
+ payload["type"].to_s == "message.inbound"
51
+ end
52
+
53
+ def parse_inbound(payload)
54
+ data =
55
+ case payload
56
+ when String
57
+ JSON.parse(payload)
58
+ when Hash
59
+ payload
60
+ else
61
+ raise WebhookError, "Inbound payload must be JSON object or string"
62
+ end
63
+
64
+ raise WebhookError, "Not an open-wire/1 message.inbound envelope" unless inbound?(data)
65
+
66
+ InboundMessage.new(data)
67
+ rescue JSON::ParserError => e
68
+ raise WebhookError, "Invalid JSON: #{e.message}"
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenWire
4
+ class Error < StandardError; end
5
+ class ConfigurationError < Error; end
6
+ class AuthError < Error; end
7
+ class ApiError < Error; end
8
+ class WebhookError < Error; end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenWire
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenWire
4
+ # Verify and parse inbound POSTs from the Open-Wire gateway.
5
+ module Webhook
6
+ module_function
7
+
8
+ HEADER_PROTOCOL = "X-Open-Wire-Protocol"
9
+ HEADER_INSTALLATION = "X-Open-Wire-Installation"
10
+ HEADER_SECRET = "X-Open-Wire-Secret"
11
+
12
+ # opts:
13
+ # secret: expected webhook secret (required unless allow_missing_secret)
14
+ # body: raw request body string OR already-parsed Hash
15
+ # headers: Hash-like (Rack env or ActionDispatch headers)
16
+ def verify_and_parse!(opts)
17
+ secret = opts.fetch(:secret)
18
+ body = opts.fetch(:body)
19
+ headers = normalize_headers(opts[:headers] || {})
20
+
21
+ provided = headers[HEADER_SECRET.downcase] || headers[HEADER_SECRET] || ""
22
+ raise AuthError, "Missing X-Open-Wire-Secret" if provided.to_s.empty?
23
+ raise AuthError, "Invalid Open-Wire webhook secret" unless secure_compare(provided.to_s, secret.to_s)
24
+
25
+ protocol = headers[HEADER_PROTOCOL.downcase] || headers[HEADER_PROTOCOL]
26
+ if protocol && protocol.to_s != OpenWire::PROTOCOL
27
+ raise WebhookError, "Unsupported protocol #{protocol}"
28
+ end
29
+
30
+ Envelope.parse_inbound(body)
31
+ end
32
+
33
+ def normalize_headers(headers)
34
+ out = {}
35
+ headers.each do |key, value|
36
+ name = key.to_s
37
+ name = name.sub(/\AHTTP_/, "").tr("_", "-") if name.start_with?("HTTP_")
38
+ out[name.downcase] = value.to_s
39
+ out[name] = value.to_s
40
+ end
41
+ # ActionDispatch::Http::Headers style
42
+ if headers.respond_to?(:[])
43
+ [HEADER_SECRET, HEADER_PROTOCOL, HEADER_INSTALLATION].each do |h|
44
+ val = headers[h] || headers[h.downcase]
45
+ out[h.downcase] = val.to_s if val
46
+ out[h] = val.to_s if val
47
+ end
48
+ end
49
+ out
50
+ end
51
+
52
+ def secure_compare(a, b)
53
+ return false if a.bytesize != b.bytesize
54
+
55
+ l = a.unpack("C*")
56
+ r = 0
57
+ b.each_byte.with_index { |byte, i| r |= byte ^ l[i] }
58
+ r.zero?
59
+ end
60
+ end
61
+ end
data/lib/open_wire.rb ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "uri"
6
+ require "securerandom"
7
+ require "open_wire/version"
8
+ require "open_wire/errors"
9
+ require "open_wire/envelope"
10
+ require "open_wire/webhook"
11
+ require "open_wire/client"
12
+ require "open_wire/configuration"
13
+
14
+ module OpenWire
15
+ PROTOCOL = "open-wire/1"
16
+
17
+ class << self
18
+ def configure
19
+ yield configuration
20
+ end
21
+
22
+ def configuration
23
+ @configuration ||= Configuration.new
24
+ end
25
+
26
+ def reset!
27
+ @configuration = Configuration.new
28
+ end
29
+ end
30
+ end
data/open-wire.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/open_wire/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "open-wire"
7
+ spec.version = OpenWire::VERSION
8
+ spec.authors = ["Kannan Reghu", "Tiny Bubble Company"]
9
+ spec.email = ["kannan@rails-agent.com"]
10
+
11
+ spec.summary = "open-wire/1 client and webhook helpers for agent↔channel transport"
12
+ spec.description = "Ruby SDK for the Open-Wire protocol: send messages via the gateway " \
13
+ "and verify/parse inbound message.inbound webhooks from Slack and other channels."
14
+ spec.homepage = "https://openwire.rails-agent.com"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 3.2.0"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/Tiny-Bubble-Company/open-wire"
20
+ spec.metadata["documentation_uri"] = "https://openwire.rails-agent.com/docs/protocol"
21
+
22
+ spec.files = Dir.chdir(__dir__) do
23
+ Dir["{lib}/**/*", "*.gemspec", "README.md", "MIT-LICENSE"].select { |f| File.file?(f) }
24
+ end
25
+
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "json", "~> 2.0"
29
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open-wire
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kannan Reghu
8
+ - Tiny Bubble Company
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2026-08-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.0'
28
+ description: 'Ruby SDK for the Open-Wire protocol: send messages via the gateway and
29
+ verify/parse inbound message.inbound webhooks from Slack and other channels.'
30
+ email:
31
+ - kannan@rails-agent.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - MIT-LICENSE
37
+ - README.md
38
+ - lib/open-wire.rb
39
+ - lib/open_wire.rb
40
+ - lib/open_wire/client.rb
41
+ - lib/open_wire/configuration.rb
42
+ - lib/open_wire/envelope.rb
43
+ - lib/open_wire/errors.rb
44
+ - lib/open_wire/version.rb
45
+ - lib/open_wire/webhook.rb
46
+ - open-wire.gemspec
47
+ homepage: https://openwire.rails-agent.com
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://openwire.rails-agent.com
52
+ source_code_uri: https://github.com/Tiny-Bubble-Company/open-wire
53
+ documentation_uri: https://openwire.rails-agent.com/docs/protocol
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.2.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.4.1
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: open-wire/1 client and webhook helpers for agent↔channel transport
73
+ test_files: []