buttondown 0.2.0 → 0.2.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 +4 -4
- data/CHANGELOG.md +8 -1
- data/README.md +25 -3
- data/lib/buttondown/client.rb +34 -82
- data/lib/buttondown/errors.rb +9 -13
- data/lib/buttondown/subscriber.rb +0 -1
- data/lib/buttondown/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c9a1ec9426dbe8c8a1dd311dd8c5b76ab90d4ca15a60696427a8e5fa5b34c9d9
|
|
4
|
+
data.tar.gz: 5fd7f2c6da5a2f21e7584caee96b65a8d79768d9bae89130c33afc49fef48be0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6ff3908bb43bc36f1aad1ced1e5f65e690320f4e508ff6c5bc793d6167615e87c2b9c8870fcfa12d65b5286c8d8b72d2d0b327f90d096bdbff862b30dc61b214
|
|
7
|
+
data.tar.gz: d7a19215bc5c4a48001b55c3fbe1e8066a442309023dff96b686c592f2a38b75cd164396e1ccd2cec16adfc64aa314e110c4753faa66e434da7dba6611fc09fe
|
data/CHANGELOG.md
CHANGED
|
@@ -6,7 +6,14 @@ hierarchy is listed.
|
|
|
6
6
|
|
|
7
7
|
## Unreleased
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- `Buttondown::Client#subscribe` now passes any further attributes to the API:
|
|
10
|
+
`ip_address`, `tags`, `notes`, `metadata`, `referrer_url`,
|
|
11
|
+
`referring_subscriber_id` and the `utm_*` fields. `ip_address` and the
|
|
12
|
+
`utm_*` fields can only be set at creation, so they were previously
|
|
13
|
+
unreachable
|
|
14
|
+
- `collision_behavior:` is now one of those attributes rather than its own
|
|
15
|
+
keyword. Existing calls are unaffected: it still goes out as the
|
|
16
|
+
`X-Buttondown-Collision-Behavior` header, and never as a field
|
|
10
17
|
|
|
11
18
|
## 0.2.0 - 2026-11-23
|
|
12
19
|
|
data/README.md
CHANGED
|
@@ -36,6 +36,26 @@ Two keywords have defaults, so the call above is the same as writing:
|
|
|
36
36
|
client.subscribe(email, type: "regular", collision_behavior: "add")
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
+
Anything else Buttondown accepts goes along as given — `ip_address`, `tags`,
|
|
40
|
+
`notes`, `metadata`, `referrer_url`, `referring_subscriber_id` and the `utm_*`
|
|
41
|
+
fields:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
client.subscribe(email,
|
|
45
|
+
ip_address: request.remote_ip,
|
|
46
|
+
referrer_url: request.referer,
|
|
47
|
+
tags: ["signup-form"],
|
|
48
|
+
utm_source: params[:utm_source])
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Attributes aren't checked against that list, so the API rejects an unknown one
|
|
52
|
+
with a 422 and a new field needs no gem release.
|
|
53
|
+
|
|
54
|
+
`ip_address` is worth sending from a web form: Buttondown uses it to locate the
|
|
55
|
+
subscriber and to judge whether the signup is legitimate. It and the `utm_*`
|
|
56
|
+
fields can **only** be set at creation — the update endpoint doesn't accept
|
|
57
|
+
them, so a value missed here is gone.
|
|
58
|
+
|
|
39
59
|
`type:` is the state the subscriber starts in.
|
|
40
60
|
|
|
41
61
|
| Value | Effect |
|
|
@@ -44,7 +64,8 @@ client.subscribe(email, type: "regular", collision_behavior: "add")
|
|
|
44
64
|
| `nil` | Sends no type, so Buttondown's own default applies: `"unactivated"` until they confirm by email |
|
|
45
65
|
|
|
46
66
|
`collision_behavior:` decides what happens when the address is already a
|
|
47
|
-
subscriber.
|
|
67
|
+
subscriber. Pass it like any other attribute; the gem sends it as a header
|
|
68
|
+
rather than a field, which is Buttondown's business and not yours.
|
|
48
69
|
|
|
49
70
|
| Value | Effect |
|
|
50
71
|
|---|---|
|
|
@@ -247,8 +268,9 @@ Update `CHANGELOG.md`, then from a clean `main`:
|
|
|
247
268
|
bin/release 0.1.1
|
|
248
269
|
```
|
|
249
270
|
|
|
250
|
-
That runs `bin/ci`, bumps `lib/buttondown/version.rb`, tags,
|
|
251
|
-
|
|
271
|
+
That runs `bin/ci`, bumps `lib/buttondown/version.rb`, tags, pushes the gem,
|
|
272
|
+
and creates the GitHub release with generated notes. One-time setup: `gem
|
|
273
|
+
signin`, and `gh auth login` for the release step.
|
|
252
274
|
|
|
253
275
|
## Contributing
|
|
254
276
|
|
data/lib/buttondown/client.rb
CHANGED
|
@@ -4,16 +4,14 @@ module Buttondown
|
|
|
4
4
|
class Client
|
|
5
5
|
BASE_URL = "https://api.buttondown.com/v1"
|
|
6
6
|
|
|
7
|
-
#
|
|
8
|
-
#
|
|
7
|
+
# Unpinned requests use whatever the account's settings page says, which can
|
|
8
|
+
# change under you.
|
|
9
9
|
API_VERSION = "2026-04-01"
|
|
10
10
|
|
|
11
|
-
#
|
|
12
|
-
# opt-in for this subscriber.
|
|
11
|
+
# Skips Buttondown's double opt-in for this subscriber.
|
|
13
12
|
DEFAULT_TYPE = "regular"
|
|
14
13
|
|
|
15
|
-
#
|
|
16
|
-
# returning a 400.
|
|
14
|
+
# Merges a repeat subscribe into the existing subscriber instead of failing.
|
|
17
15
|
DEFAULT_COLLISION_BEHAVIOR = "add"
|
|
18
16
|
|
|
19
17
|
OPEN_TIMEOUT = 5
|
|
@@ -68,74 +66,44 @@ module Buttondown
|
|
|
68
66
|
@write_timeout = write_timeout
|
|
69
67
|
end
|
|
70
68
|
|
|
71
|
-
# Creates a subscriber.
|
|
69
|
+
# Creates a subscriber. Further attributes -- ip_address, tags, metadata and
|
|
70
|
+
# the rest -- are passed through for the API to validate. See the README.
|
|
72
71
|
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
75
|
-
#
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
# record, a successful call does NOT mean this person will receive email.
|
|
84
|
-
# Check Subscriber#regular?.
|
|
85
|
-
def subscribe(email, type: DEFAULT_TYPE, collision_behavior: DEFAULT_COLLISION_BEHAVIOR)
|
|
86
|
-
payload = {email_address: normalize_email(email)}
|
|
72
|
+
# With the default collision_behavior "add" a duplicate is a success that
|
|
73
|
+
# returns the existing record, so this returning does not mean the person
|
|
74
|
+
# will receive email. Check Subscriber#regular?.
|
|
75
|
+
def subscribe(email, type: DEFAULT_TYPE, **attributes)
|
|
76
|
+
# Routed to a header rather than the body, so the caller need not know
|
|
77
|
+
# which is which. fetch, not delete-or-default: an explicit nil means
|
|
78
|
+
# "omit the header" and has to survive.
|
|
79
|
+
collision_behavior = attributes.fetch(:collision_behavior, DEFAULT_COLLISION_BEHAVIOR)
|
|
80
|
+
|
|
81
|
+
payload = attributes.except(:collision_behavior).merge(email_address: normalize_email(email))
|
|
87
82
|
payload[:type] = type if type
|
|
88
83
|
|
|
89
84
|
Subscriber.new(request(:post, "/subscribers", payload: payload, collision_behavior: collision_behavior))
|
|
90
85
|
end
|
|
91
86
|
|
|
92
|
-
# Retrieves a subscriber
|
|
93
|
-
#
|
|
94
|
-
#
|
|
95
|
-
# Buttondown's path is /subscribers/{id_or_email}, so either identifies a
|
|
96
|
-
# subscriber and there is no need to store the id:
|
|
97
|
-
#
|
|
98
|
-
# client.subscriber("sub_0dys87p9a08v1ttfy6ascstnpx")
|
|
99
|
-
# client.subscriber("reader@example.com")
|
|
87
|
+
# Retrieves a subscriber by id or email address -- Buttondown's path is
|
|
88
|
+
# /subscribers/{id_or_email}, so either works.
|
|
100
89
|
def subscriber(id_or_email)
|
|
101
90
|
Subscriber.new(request(:get, "/subscribers/#{path_segment(id_or_email)}"))
|
|
102
91
|
end
|
|
103
92
|
|
|
104
|
-
# Updates a subscriber
|
|
105
|
-
#
|
|
93
|
+
# Updates a subscriber by id or email address. Only the attributes given are
|
|
94
|
+
# touched, and they are passed through for the API to validate.
|
|
106
95
|
#
|
|
107
|
-
#
|
|
108
|
-
#
|
|
109
|
-
# email_which_prompted_unsubscription_id. Attributes are passed through
|
|
110
|
-
# rather than checked against that list, so the API rejects an unknown one
|
|
111
|
-
# with a 422 and a new field needs no gem release.
|
|
112
|
-
#
|
|
113
|
-
# Only the attributes given are touched; the rest of the record is left
|
|
114
|
-
# alone. Note that this bypasses the suppression rules: setting type back
|
|
115
|
-
# to "regular" reactivates an unsubscribed subscriber without the
|
|
116
|
-
# SubscriberSuppressedError that a subscribe with collision_behavior
|
|
117
|
-
# "overwrite" would raise. The subscribe defaults, "regular" plus "add",
|
|
118
|
-
# reactivate them without raising either -- no call here protects an
|
|
119
|
-
# existing opt-out on its own.
|
|
96
|
+
# This bypasses the suppression rules: setting type back to "regular"
|
|
97
|
+
# reactivates an unsubscribed subscriber with no error at all.
|
|
120
98
|
def update_subscriber(id_or_email, **attributes)
|
|
121
99
|
raise InvalidArgumentError, "No attributes to update" if attributes.empty?
|
|
122
100
|
|
|
123
101
|
Subscriber.new(request(:patch, "/subscribers/#{path_segment(id_or_email)}", payload: attributes))
|
|
124
102
|
end
|
|
125
103
|
|
|
126
|
-
# Unsubscribes a subscriber.
|
|
127
|
-
#
|
|
128
|
-
#
|
|
129
|
-
# Buttondown has no unsubscribe endpoint -- this is an update setting the
|
|
130
|
-
# type -- but it is the action worth naming, and the one to reach for when
|
|
131
|
-
# someone asks to stop receiving email. The record survives, so Buttondown
|
|
132
|
-
# remembers the opt-out: a later subscribe with collision_behavior
|
|
133
|
-
# "overwrite" raises SubscriberSuppressedError.
|
|
134
|
-
#
|
|
135
|
-
# Note that the subscribe defaults -- type "regular", collision "add" --
|
|
136
|
-
# DO reactivate an unsubscribed subscriber, and Subscriber#regular? then
|
|
137
|
-
# reports true because of it. Check terminal? before subscribing, or pass
|
|
138
|
-
# collision_behavior: "overwrite".
|
|
104
|
+
# Unsubscribes a subscriber. Buttondown has no unsubscribe endpoint: this is
|
|
105
|
+
# an update setting the type, named for the action worth having. Keeping the
|
|
106
|
+
# record is what makes the opt-out stick -- see the README.
|
|
139
107
|
def unsubscribe(id_or_email, reason: nil)
|
|
140
108
|
attributes = {type: "unsubscribed"}
|
|
141
109
|
attributes[:unsubscription_reason] = reason if reason
|
|
@@ -150,12 +118,8 @@ module Buttondown
|
|
|
150
118
|
|
|
151
119
|
private
|
|
152
120
|
|
|
153
|
-
#
|
|
154
|
-
#
|
|
155
|
-
# arrives here as an empty Hash.
|
|
156
|
-
#
|
|
157
|
-
# The URL is interpolated rather than joined -- URI.join would replace
|
|
158
|
-
# "/v1" and silently drop the version prefix.
|
|
121
|
+
# Interpolated rather than joined: URI.join would replace "/v1" and silently
|
|
122
|
+
# drop the version prefix.
|
|
159
123
|
def request(verb, path, payload: nil, collision_behavior: nil)
|
|
160
124
|
uri = URI("#{BASE_URL}#{path}")
|
|
161
125
|
headers = request_headers(collision_behavior: collision_behavior)
|
|
@@ -185,12 +149,10 @@ module Buttondown
|
|
|
185
149
|
http
|
|
186
150
|
end
|
|
187
151
|
|
|
188
|
-
#
|
|
189
|
-
#
|
|
190
|
-
#
|
|
191
|
-
#
|
|
192
|
-
# Classifying the response is inside the rescues but raises only APIError
|
|
193
|
-
# and InvalidResponseError, neither of which they catch.
|
|
152
|
+
# An explicit rescue list rather than bare `rescue`, so a bug in this gem
|
|
153
|
+
# surfaces as itself instead of as a connection failure. Classifying the
|
|
154
|
+
# response sits inside the rescues but raises only APIError and
|
|
155
|
+
# InvalidResponseError, neither of which they catch.
|
|
194
156
|
def perform(http, http_request)
|
|
195
157
|
response = http.request(http_request)
|
|
196
158
|
|
|
@@ -250,8 +212,6 @@ module Buttondown
|
|
|
250
212
|
)
|
|
251
213
|
end
|
|
252
214
|
|
|
253
|
-
# Buttondown's own wording wherever it gives any, falling back to the
|
|
254
|
-
# status line.
|
|
255
215
|
def error_message(parsed, response)
|
|
256
216
|
parsed["detail"] || parsed["error"] || parsed["message"] || response.message
|
|
257
217
|
end
|
|
@@ -267,17 +227,9 @@ module Buttondown
|
|
|
267
227
|
nil
|
|
268
228
|
end
|
|
269
229
|
|
|
270
|
-
#
|
|
271
|
-
#
|
|
272
|
-
#
|
|
273
|
-
# This message omits the address, but that is a local courtesy rather than a
|
|
274
|
-
# gem-wide guarantee: Buttondown quotes the address back in its own error
|
|
275
|
-
# `detail` (see `email_already_exists`) and APIError#to_s passes that
|
|
276
|
-
# through unchanged. Scrub at the error-tracker boundary if messages must
|
|
277
|
-
# be address-free.
|
|
278
|
-
# An id or an email address goes in a path segment. "@" and "+" are legal
|
|
279
|
-
# there and Buttondown accepts them raw, but encoding is equally accepted
|
|
280
|
-
# and stops a stray "/" or "?" from rewriting the request path.
|
|
230
|
+
# Guards a 100/day rate limit against addresses no mail server would accept.
|
|
231
|
+
# "@" and "+" are legal in a path segment and Buttondown accepts them raw,
|
|
232
|
+
# but encoding is accepted too and stops a stray "/" rewriting the path.
|
|
281
233
|
def path_segment(value)
|
|
282
234
|
segment = value.to_s.strip
|
|
283
235
|
|
data/lib/buttondown/errors.rb
CHANGED
|
@@ -30,10 +30,8 @@ module Buttondown
|
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
# Exposes the response as plain data (`headers`, `raw_body`) rather than the
|
|
36
|
-
# Net::HTTPResponse itself, so swapping the HTTP backend stays a patch release.
|
|
33
|
+
# Exposes the response as plain data rather than the Net::HTTPResponse itself,
|
|
34
|
+
# so swapping the HTTP backend stays a patch release.
|
|
37
35
|
class APIError < Error
|
|
38
36
|
attr_reader :status, :code, :body, :raw_body, :headers
|
|
39
37
|
|
|
@@ -55,14 +53,13 @@ module Buttondown
|
|
|
55
53
|
|
|
56
54
|
class SubscriberSuppressedError < BadRequestError; end
|
|
57
55
|
|
|
58
|
-
#
|
|
59
|
-
#
|
|
60
|
-
# failing, so they never raise this.
|
|
56
|
+
# Only reachable with `collision_behavior: nil`: "add" and "overwrite" merge
|
|
57
|
+
# instead of failing.
|
|
61
58
|
class EmailAlreadyExistsError < BadRequestError; end
|
|
62
59
|
|
|
63
|
-
# Buttondown's firewall rejected the address
|
|
64
|
-
#
|
|
65
|
-
#
|
|
60
|
+
# Buttondown's firewall rejected the address -- a reserved domain such as
|
|
61
|
+
# example.com, a disposable provider, or too high a risk score. Retrying the
|
|
62
|
+
# same address fails the same way.
|
|
66
63
|
class SubscriberBlockedError < BadRequestError; end
|
|
67
64
|
|
|
68
65
|
class AuthenticationError < APIError; end
|
|
@@ -73,9 +70,8 @@ module Buttondown
|
|
|
73
70
|
|
|
74
71
|
class NotFoundError < APIError; end
|
|
75
72
|
|
|
76
|
-
#
|
|
77
|
-
#
|
|
78
|
-
# here the same call may succeed on a retry.
|
|
73
|
+
# The request collided with concurrent work on the same record, so unlike the
|
|
74
|
+
# other 4xx errors here it may succeed on a retry.
|
|
79
75
|
class ConflictError < APIError; end
|
|
80
76
|
|
|
81
77
|
class UnprocessableEntityError < APIError; end
|
data/lib/buttondown/version.rb
CHANGED