/
manage_subscriber.rb
98 lines (87 loc) · 2.42 KB
/
manage_subscriber.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true
require 'faraday'
require 'json'
require 'time'
BASE_URL = 'https://api.aweber.com/1.0/'
credentials = JSON.parse(File.read('./credentials.json'))
puts credentials
conn = Faraday.new(
headers: {
Authorization: "Bearer #{credentials['access_token']}"
}
) do |f|
# f.response :raise_error
end
def get_collection(conn, url)
puts "getting #{url}"
collection = []
while url
res = conn.get(url)
page = JSON.parse(res.body)
collection.push(*page['entries'])
url = page['next_collection_link']
end
collection
end
accounts = get_collection(conn, "#{BASE_URL}accounts")
lists_url = accounts[0]['lists_collection_link']
lists = get_collection(conn, lists_url)
email = 'example@example.com'
params = {
'ws.op' => 'find',
'email' => email
}
subs_url = lists[0]['subscribers_collection_link']
find_url = "#{subs_url}?#{URI.encode_www_form(params)}"
found_subscribers = get_collection(conn, find_url)
puts 'Found subscribers: '
puts found_subscribers
if found_subscribers.dig(0,'subscription_collection_link')
data = {
'custom_fields' => {
'awesomeness' => 'really awesome'
},
'tags' => {
'add' => ['prospect']
}
}
subscriber_url = found_subscribers.first['self_link']
subscriber_response = conn.patch(subscriber_url,JSON.generate(data)) do |req|
req.headers['Content-Type'] = 'application/json'
end
subscriber = JSON.parse(subscriber_response.body)
puts "Updated subscriber"
else
data = {
'email' => email,
'custom_fields' => {
'awesomeness' => 'somewhat'
},
'tags' => ['prospect']
}
puts "subs_url #{subs_url}"
puts "json data #{data.to_json}"
body = conn.post(subs_url, JSON.generate(data)) do |req|
req.headers['Access-Control-Expose-Headers'] = 'Location'
req.headers['Content-Type'] = 'application/json'
end
subscriber_url = body.headers['Location']
puts "#{body.headers}"
puts "#{body.body}"
puts "subscriber url #{subscriber_url}"
subscriber_response = conn.get(subscriber_url)
subscriber = JSON.parse(subscriber_response.body)
puts 'Created Subscriber:'
end
puts subscriber
params = {
'ws.op' => 'getActivity'
}
activity_url = "#{subscriber_url}?#{URI.encode_www_form(params)}"
activity = conn.get(activity_url)
puts "Subscriber Activity:"
puts JSON.parse(activity.body)
if subscriber['status'] == 'subscribed'
conn.delete(subscriber_url)
puts "Deleted subscriber with email: #{email}"
end