-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsendy_service.rb
More file actions
55 lines (45 loc) · 1.29 KB
/
sendy_service.rb
File metadata and controls
55 lines (45 loc) · 1.29 KB
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
require 'httparty'
class SendyService
attr_accessor :api_key, :email, :name, :signup_at, :last_checkin_at
# Default API endpoints
BASE_URL = ENV['SENDI_INSTALLATION']
LIST_ID = ENV['SENDI_LIST_ID']
def initialize(options)
@api_key = ENV['SENDI_API_KEY']
@email = options[:email]
@name = options[:name]
@signup_at = options[:signup_at]
@last_checkin_at = options[:last_checkin_at]
end
def send_data
post('subscribe', name: name,
email: email,
signup_at: signup_at,
last_checkin_at: last_checkin_at,
boolean: true, api_key: api_key, list: LIST_ID)
end
def get(path)
execute :get, path, nil
end
def post(path, data = nil)
execute :post, path, data
end
def execute(method, path, data = nil)
response = request(method, path, data)
case response.code
when 200..299
response
when 401
raise AuthenticationFailed, response["message"]
when 404
raise NotFoundError, response
else
raise RequestError, response
end
end
def request(method, path, data = nil)
headers = {}
headers['content-type'] = "application/x-www-form-urlencoded"
HTTParty.send(method, BASE_URL + path, headers: headers, body: data)
end
end