-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
29 lines (25 loc) · 998 Bytes
/
app.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
# Default ENV to dev if not present
ENV["APP_ENV"] ||= "development"
# Autoload common standard library features and Gemfile gems
require "json"
require "digest"
require "bundler"
Bundler.require :default, ENV["APP_ENV"].to_sym
# Load dev environment variables
Dotenv.load if %w[development test].include? ENV["APP_ENV"]
class App < Sinatra::Base
get "/consent" do
email = String(params["email"]).gsub(/\s+/, "+")
hexmail = Digest::MD5.hexdigest(email)
list_id = ENV.fetch("MAILCHIMP_LIST_ID")
api_key = ENV.fetch("MAILCHIMP_API_KEY")
gibbon = Gibbon::Request.new(api_key: api_key)
timestamp = Time.now.strftime("%m/%d/%Y")
puts "#{email} consenting #{timestamp}"
gibbon.lists(list_id).members(hexmail).upsert(body: {email_address: email, status: "subscribed", merge_fields: {WEB_GDPR: timestamp}})
rescue Gibbon::MailChimpError => e
puts "Houston, we have a problem: #{e.message} - #{e.raw_body}"
ensure
redirect ENV.fetch("THANKS_URL")
end
end