Simple API Wrappers - Active Campaign Simple
Description
I like helper libraries that are useful in accessing APIs. However, sometimes those libraries can hide and obfuscate the calls and methods of the source API. This can lead to difficult troubleshooting when you have an issue. Sometimes, these types of libraries can be good if the source API is poorly written and implemented. I’ve written API libraries to poorly written and documented APIs before, and it’s always best to try to implement standards and best practice with your library. I think there is a time and place for those.
However, sometimes you want to follow the source documentation as closely as possible, while also having some conveniences like logging, error handling, authentication, etc.. Well, here is my attempt at a very simple wrapper API, where I add in some of those conveniences, but also allow for the original source API to be used. Hopefully, this will make it easier to troubleshoot and implement.
Code: https://github.com/nateleavitt/active-campaign-simple
Installation
gem install active-campaign-simple
Config
- add
gem 'active-campaign-simple'
to yourGemfile
- Get your API URL and Key from within your application (Settings > Developer)
- Then create an initializer in
config\initializers
called active_campaign.rb and the following
1
2
3
4
5
6
# Added to your config\initializers file
ActiveCampaign.configure do |config|
config.api_url = 'YOUR_API_URL'
config.api_key = 'YOUR_API_KEY'
config.api_logger = Logger.new("#{Rails.root}/log/active_campaign_api.log") # optional logger file
end
Examples
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
# Get a list of contacts
ActiveCampaign.get('/contacts')
# Get a contact
ActiveCampaign.get('/contacts/' + id)
# Create (post) a new contact
# https://developers.activecampaign.com/reference#create-a-contact-new
ActiveCampaign.post('/contacts', payload: {
contact: {
email: '[email protected]',
firstName: 'Nate',
lastName: 'Test',
phone: '1231231234'
},
fieldValues: [
{
field: '1',
value: 'The Value for First Field'
}
]
})
# Update (put) a contact
ActiveCampaign.put('/contacts/' + id, payload: {
contact: {
email: '[email protected]'
}
})
# Delete a contact
ActiveCampaign.delete('/contacts/' + id)
# Search for a contact
ActiveCampaign.get('/contacts', query: { email: '[email protected]' })
# Event Tracking
# See: https://developers.activecampaign.com/reference#track-event
# NOTE - The tracking API is different from all other calls as it changes the arguments a little to simplify.
ActiveCampaign.track_event('event-key', 'actid', 'event-name', 'email')
# or with optional eventdata
ActiveCampaign.track_event('event-key', 'actid', 'event-name', 'email', 'eventdata')