-
Notifications
You must be signed in to change notification settings - Fork 1
/
buttondown.rb
61 lines (50 loc) · 1.57 KB
/
buttondown.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'faraday'
require 'addressable'
class Buttondown
attr_reader :conn, :key
def initialize(key)
@key = key
@conn = Faraday.new(url: 'https://api.buttondown.email') do |f|
f.request :authorization, 'Token', key
f.request :json
f.response :json
f.response :raise_error
end
end
# Note: Assumes all subscribers are 'regular' and not 'premium' and other paid subscription
# statuses
def subscribed?(email)
subscriber(email)&.dig('subscriber_type') == 'regular'
end
def unsubscribe!(email)
if s = subscriber(email)
conn.delete(subscriber_url(s["id"]))
end
end
# Subscribe or edit a subscription - if the email address has changed, add :email_was to metadata
def subscribe!(email, metadata={})
email_was = metadata.delete(:email_was) || email
if s = subscriber(email_was)
update = {subscriber_type: 'regular', metadata: metadata}
if email != email_was
# Only update the email if it's changed
update[:email] = email
end
conn.patch(subscriber_url(s["id"]), update)
else
conn.post("/v1/subscribers", {email: email, subscriber_type: 'regular', metadata: metadata})
end
end
def inspect
"#<Buttondown key=#{key.inspect}>"
end
private
def subscriber(id_or_email)
conn.get(subscriber_url id_or_email).body
rescue Faraday::ResourceNotFound
nil # return nil if there is a 404 and raise all other kinds of exceptions
end
def subscriber_url(id_or_email)
Addressable::Template.new("/v1/subscribers/{id}").expand(id: id_or_email)
end
end