ringwillow 1.0.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: a6b462c3ae2e392baf8918cf81f4cf2554f9b2e8b742c91c07e4dd2442cc6d33
4
+ data.tar.gz: 47957b1466b57b167ffdd69493b26c61200569108a26569bd25945d102ad7b4a
5
+ SHA512:
6
+ metadata.gz: 82dcdc13057be33e257c7cf63e460f8a420fef6e2fe79b91835b01bcb5b46bde98b4784d9c565e8ea2f6fc0a09423d0334f1a11d6eef868a2ad31d340173a33e
7
+ data.tar.gz: 62d8c1da6c08aa090083c4e5b3f9ea3a05655f3583b96588e8958673f5f881b0a0582cb535855f333b6e72c3d8e8dc22e173645e424032edc38ee1335e0b977e
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Ringwillow SDK for Ruby
2
+
3
+ Official SDK for [Ringwillow](https://ringwillow.com): phone numbers, call flows, AI voice, texting (A2P 10DLC) and signed webhooks. Ruby 3.0+, standard library only.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ gem install ringwillow --source https://ringwillow.com/gems
9
+ ```
10
+
11
+ Gemfile:
12
+
13
+ ```ruby
14
+ source "https://ringwillow.com/gems" do
15
+ gem "ringwillow"
16
+ end
17
+ ```
18
+
19
+ ## Use
20
+
21
+ Create a project key (`rw_live_…`) in the console under API & MCP and put it in `RINGWILLOW_KEY`. Never commit it.
22
+
23
+ ```ruby
24
+ require "ringwillow"
25
+
26
+ rw = Ringwillow::Client.new # reads RINGWILLOW_KEY
27
+ calls = rw.calls.list(limit: 20)
28
+ rw.messages.send_message(to: "+15555550123", body: "Your car is ready.")
29
+
30
+ # Webhook route (raw body!)
31
+ ok = Ringwillow::Webhook.verify(ENV["RINGWILLOW_WEBHOOK_SECRET"], request.headers["Ringwillow-Signature"], request.raw_post)
32
+ ```
33
+
34
+ Every group: `business`, `numbers` (list, ready), `flows`, `calls` (list, get, place), `messages` (list, send), `webhooks` (list, create, delete), `registration`. Anything else: the generic request method with a path from the [API reference](https://ringwillow.com/docs/api).
35
+
36
+ - Errors carry the HTTP status and the API's message. A 429 is retried after `Retry-After` (2 tries).
37
+ - A 402 means the account needs credit; a 409 on send means the contact replied STOP.
38
+ - Texts need approved 10DLC texting. Calls-only projects can still read calls and place calls.
39
+
40
+ Docs: https://ringwillow.com/docs/sdks · For AI assistants: https://ringwillow.com/llms.txt
41
+
42
+ © O2A2 LLC. MIT licence.
@@ -0,0 +1,3 @@
1
+ module Ringwillow
2
+ VERSION = "1.0.0"
3
+ end
data/lib/ringwillow.rb ADDED
@@ -0,0 +1,163 @@
1
+ # Ringwillow SDK for Ruby. Docs: https://ringwillow.com/docs/sdks
2
+ #
3
+ # require "ringwillow"
4
+ # rw = Ringwillow::Client.new # reads RINGWILLOW_KEY
5
+ # rw.messages.send_message(to: "+15555550123", body: "Your car is ready.")
6
+ #
7
+ # Standard library only.
8
+ require "json"
9
+ require "net/http"
10
+ require "openssl"
11
+ require "uri"
12
+ require_relative "ringwillow/version"
13
+
14
+ module Ringwillow
15
+ DEFAULT_BASE_URL = "https://api.ringwillow.com/api/v1".freeze
16
+ WEBHOOK_EVENTS = %w[
17
+ call.completed call.step voicemail.created message.received message.status message.taken
18
+ registration.updated registration.approved registration.rejected registration.needs_info
19
+ contact.opted_out contact.opted_in
20
+ ].freeze
21
+
22
+ class Error < StandardError
23
+ attr_reader :status, :retry_after
24
+
25
+ def initialize(message, status, retry_after = nil)
26
+ super(message)
27
+ @status = status
28
+ @retry_after = retry_after
29
+ end
30
+ end
31
+
32
+ class Client
33
+ attr_reader :base_url, :business, :numbers, :flows, :calls, :messages, :webhooks, :registration, :agents, :actions
34
+ attr_accessor :max_retries, :timeout
35
+
36
+ def initialize(api_key: nil, base_url: nil, max_retries: 2, timeout: 30)
37
+ @key = api_key || ENV["RINGWILLOW_KEY"]
38
+ raise ArgumentError, "Ringwillow: pass api_key or set RINGWILLOW_KEY" if @key.nil? || @key.empty?
39
+ @base_url = (base_url || ENV["RINGWILLOW_BASE_URL"] || DEFAULT_BASE_URL).sub(%r{/+\z}, "")
40
+ @max_retries = max_retries
41
+ @timeout = timeout
42
+ @business = Group.new(self) { |c| { get: -> { c.request(:get, "/business") } } }
43
+ @numbers = Numbers.new(self)
44
+ @flows = Flows.new(self)
45
+ @calls = Calls.new(self)
46
+ @messages = Messages.new(self)
47
+ @webhooks = Webhooks.new(self)
48
+ @registration = Group.new(self) { |c| { get: -> { c.request(:get, "/registration") } } }
49
+ @agents = Agents.new(self)
50
+ @actions = Group.new(self) { |c| { list: -> { c.request(:get, "/actions") } } }
51
+ end
52
+
53
+ # Any endpoint: rw.request(:get, "/agents")
54
+ def request(method, path, body = nil, query = nil)
55
+ q = (query || {}).reject { |_, v| v.nil? || v == "" }
56
+ uri = URI(@base_url + (path.start_with?("/") ? path : "/#{path}"))
57
+ uri.query = URI.encode_www_form(q) unless q.empty?
58
+ attempt = 0
59
+ loop do
60
+ req = Net::HTTP.const_get(method.to_s.capitalize).new(uri)
61
+ req["Authorization"] = "Bearer #{@key}"
62
+ req["Accept"] = "application/json"
63
+ req["User-Agent"] = "ringwillow-ruby/#{VERSION}"
64
+ unless body.nil?
65
+ req["Content-Type"] = "application/json"
66
+ req.body = JSON.generate(body)
67
+ end
68
+ res = transport(uri, req)
69
+ code = res.code.to_i
70
+ ra = res["Retry-After"]
71
+ ra = ra && ra =~ /\A\d+\z/ ? ra.to_i : nil
72
+ if code == 429 && attempt < @max_retries
73
+ attempt += 1
74
+ sleep([ra || 1, 30].min)
75
+ next
76
+ end
77
+ raw = res.body.to_s
78
+ data = begin
79
+ raw.empty? ? nil : JSON.parse(raw)
80
+ rescue JSON::ParserError
81
+ nil
82
+ end
83
+ raise Error.new((data.is_a?(Hash) && data["error"]) || raw, code, ra) if code >= 300
84
+ return data
85
+ end
86
+ end
87
+
88
+ # Seam for tests.
89
+ def transport(uri, req)
90
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: @timeout, read_timeout: @timeout) { |h| h.request(req) }
91
+ end
92
+ end
93
+
94
+ class Group
95
+ def initialize(client, &blk)
96
+ @c = client
97
+ (blk ? blk.call(client) : {}).each { |name, fn| define_singleton_method(name) { fn.call } }
98
+ end
99
+
100
+ private
101
+
102
+ def esc(s) = URI.encode_www_form_component(s).gsub("+", "%20")
103
+ end
104
+
105
+ class Numbers < Group
106
+ def list = @c.request(:get, "/numbers")
107
+ def ready = @c.request(:get, "/numbers/ready")
108
+ end
109
+
110
+ class Flows < Group
111
+ def list = @c.request(:get, "/flows")
112
+ end
113
+
114
+ class Calls < Group
115
+ def list(q: nil, limit: nil, before: nil) = @c.request(:get, "/calls", nil, { q: q, limit: limit, before: before })
116
+ def get(id) = @c.request(:get, "/calls/#{esc(id)}")
117
+
118
+ # Rings `to` and runs a published flow.
119
+ def place(to:, flow_id:, from: nil) = @c.request(:post, "/calls", { to: to, flowId: flow_id, from: from }.compact)
120
+ end
121
+
122
+ class Messages < Group
123
+ def list(q: nil, limit: nil, before: nil) = @c.request(:get, "/messages", nil, { q: q, limit: limit, before: before })
124
+
125
+ # Needs approved texting (10DLC) and credit; 409 if the contact replied STOP.
126
+ def send_message(to:, body:, from: nil) = @c.request(:post, "/messages", { to: to, body: body, from: from }.compact)
127
+ alias create send_message
128
+ end
129
+
130
+ class Webhooks < Group
131
+ def list = @c.request(:get, "/webhooks")
132
+
133
+ # Returns {"endpoint"=>{...}, "secret"=>"whsec_..."}; the secret is shown once.
134
+ def create(url:, events: nil) = @c.request(:post, "/webhooks", { url: url, events: events }.compact)
135
+ def delete(id) = @c.request(:delete, "/webhooks/#{esc(id)}")
136
+ end
137
+
138
+ class Agents < Group
139
+ def list = @c.request(:get, "/agents")
140
+ def get(id) = @c.request(:get, "/agents/#{esc(id)}")
141
+ end
142
+
143
+ module Webhook
144
+ module_function
145
+
146
+ # Checks Ringwillow-Signature ("t=<unix>,v1=<hex>") against the raw body. tolerance 0 skips the age check.
147
+ def verify(secret, header, raw_body, tolerance: 300)
148
+ return false if secret.to_s.empty? || header.to_s.empty?
149
+ parts = header.split(",").map { |p| p.strip.split("=", 2) }.select { |p| p.size == 2 }.to_h
150
+ return false unless parts["t"] =~ /\A\d+\z/ && parts["v1"]
151
+ t = parts["t"].to_i
152
+ return false if tolerance.positive? && (Time.now.to_i - t).abs > tolerance
153
+ want = OpenSSL::HMAC.hexdigest("SHA256", secret, "#{t}.#{raw_body}")
154
+ OpenSSL.fixed_length_secure_compare(want, parts["v1"]) rescue false
155
+ end
156
+
157
+ # Verifies, then parses the delivery. Raises Ringwillow::Error (400) if the signature is wrong.
158
+ def parse(secret, header, raw_body, tolerance: 300)
159
+ raise Error.new("Bad Ringwillow-Signature", 400) unless verify(secret, header, raw_body, tolerance: tolerance)
160
+ JSON.parse(raw_body)
161
+ end
162
+ end
163
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ringwillow
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - O2A2 LLC
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-12-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Phone numbers, call flows, AI voice, texting (A2P 10DLC) and signed webhooks.
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/ringwillow.rb
21
+ - lib/ringwillow/version.rb
22
+ homepage: https://ringwillow.com/docs/sdks
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '3.0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.5.22
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Official Ringwillow SDK for Ruby
45
+ test_files: []