active_campaign 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +3 -0
- data/.travis.yml +13 -0
- data/Gemfile +19 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/active_campaign.gemspec +27 -0
- data/lib/active_campaign/.DS_Store +0 -0
- data/lib/active_campaign/client/campaigns.rb +45 -0
- data/lib/active_campaign/client/contacts.rb +39 -0
- data/lib/active_campaign/client/lists.rb +23 -0
- data/lib/active_campaign/client/messages.rb +44 -0
- data/lib/active_campaign/client.rb +25 -0
- data/lib/active_campaign/configuration.rb +73 -0
- data/lib/active_campaign/connection.rb +50 -0
- data/lib/active_campaign/error.rb +68 -0
- data/lib/active_campaign/param_validation.rb +12 -0
- data/lib/active_campaign/request.rb +85 -0
- data/lib/active_campaign/version.rb +3 -0
- data/lib/active_campaign.rb +34 -0
- data/lib/faraday/response/body_logger.rb +37 -0
- data/lib/faraday/response/raise_active_campaign_error.rb +25 -0
- data/spec/active_campaign_spec.rb +31 -0
- data/spec/client/contacts_spec.rb +41 -0
- data/spec/client/lists_spec.rb +48 -0
- data/spec/client_spec.rb +7 -0
- data/spec/fixtures/contact_sync.json +8 -0
- data/spec/fixtures/contact_view.json +78 -0
- data/spec/fixtures/list_view.json +220 -0
- data/spec/helper.rb +111 -0
- metadata +168 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6c9f4c41bd412aaef640f75d80a340c63864bd63
|
4
|
+
data.tar.gz: 44387b1eb38a407c215b4b6df50f82250e8a599d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee3763d8f076c080f6e2ae84adbc358a2578b96bbcdec0b0b4d258135ee5abd3ee357b62f2a17cf0b17d5c00a7dbd367ebdc26dff24d7f05b5d7caf146db1e9b
|
7
|
+
data.tar.gz: 55e38a282f58287e668bad9d1bcbec197680d8a630724399d7956bbac0640a72fc355c3e1e933ff3ac8fd160a44578f3a4cc5973b282570248dd69cfe2343ddc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
gem 'rake'
|
3
|
+
gem 'yard'
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
gem 'pry'
|
7
|
+
end
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'coveralls', :require => false
|
11
|
+
gem 'guard-rspec'
|
12
|
+
gem 'json', '~> 1.7', :platforms => [:ruby_18, :jruby]
|
13
|
+
gem 'rspec', '>= 2.13'
|
14
|
+
gem 'simplecov', :require => false
|
15
|
+
gem 'webmock'
|
16
|
+
gem 'test-queue'
|
17
|
+
end
|
18
|
+
|
19
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mikael Henriksson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Active::Campaign::Ruby
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'active-campaign'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install active-campaign
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_campaign/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_campaign"
|
8
|
+
spec.version = ActiveCampaign::VERSION
|
9
|
+
spec.authors = ["Mikael Henriksson"]
|
10
|
+
spec.email = ["mikael@zoolutions.se"]
|
11
|
+
spec.description = %q{A simple ruby wrapper for the ActiveCampaign API}
|
12
|
+
spec.summary = %q{See http://www.activecampaign.com/api/overview.php for more information}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "httpclient", "~> 2.3"
|
22
|
+
spec.add_dependency "hashie", "~> 2.0"
|
23
|
+
spec.add_dependency "activesupport", ">= 4.0.0"
|
24
|
+
spec.add_dependency "faraday", "~> 0.8"
|
25
|
+
spec.add_dependency "faraday_middleware", "~> 0.9"
|
26
|
+
spec.add_dependency "multi_json", "~> 1.7"
|
27
|
+
end
|
Binary file
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module ActiveCampaign
|
2
|
+
class Client
|
3
|
+
module Campaigns
|
4
|
+
CONTACT_METHODS = %w[
|
5
|
+
create delete_list delete list paginator report_bounce_list
|
6
|
+
report_bounce_totals report_forward_list report_forward_totals
|
7
|
+
report_link_list report_link_totals report_open_list report_open_totals
|
8
|
+
report_totals report_unopen_list report_unsubscription_list
|
9
|
+
report_unsubscription_totals send status
|
10
|
+
]
|
11
|
+
CONTACT_POST_METHODS = %w[create]
|
12
|
+
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
# TODO: Create proper methods with parameter validation and possible naming fixes since this is one the worst APIs I have ever worked with.
|
16
|
+
included do
|
17
|
+
CONTACT_METHODS.each do |method|
|
18
|
+
define_method "contact_#{method}" do |options|
|
19
|
+
if CONTACT_POST_METHODS.include?(method)
|
20
|
+
post __method__, options
|
21
|
+
else
|
22
|
+
get __method__, options
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# contact_sync - parameters
|
32
|
+
# email - Email of the new subscriber. Example: 'test@example.com'
|
33
|
+
# first_name - First name of the subscriber. Example: 'FirstName'
|
34
|
+
# last_name - Last name of the subscriber. Example: 'LastName'
|
35
|
+
# ip4 - IP address of the subscriber. Example: '127.0.0.1' If not supplied, it will default to '127.0.0.1'
|
36
|
+
# field[345,0] - Custom field values. Example: field[345,0] = 'value'. In this example, "345" is the field ID. Leave 0 as is.
|
37
|
+
# field[%PERS_1%,0] - 'value' (You can also use the personalization tag to specify which field you want updated)
|
38
|
+
# - Assign to lists. List ID goes in brackets, as well as the value.
|
39
|
+
# status[123] - The status for each list the subscriber is added to. Examples: 1 = active, 2 = unsubscribed
|
40
|
+
# form - Optional subscription Form ID, to inherit those redirection settings. Example: 1001. This will allow you to mimic adding the subscriber through a subscription form, where you can take advantage of the redirection settings.
|
41
|
+
# noresponders[123] - Whether or not to set "do not send any future responders." Examples: 1 = yes, 0 = no.
|
42
|
+
# sdate[123] - Subscribe date for particular list - leave out to use current date/time. Example: '2009-12-07 06:00:00'
|
43
|
+
# instantresponders[123] Use only if status = 1. Whether or not to set "send instant responders." Examples: 1 = yes, 0 = no.
|
44
|
+
# lastmessage[123] - Whether or not to set "send the last broadcast campaign." Examples: 1 = yes, 0 = no.
|
45
|
+
# "field[%BALANCE%,0]": "100"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ActiveCampaign
|
2
|
+
class Client
|
3
|
+
module Contacts
|
4
|
+
CONTACT_METHODS = %w[add delete_list delete edit list paginator sync view]
|
5
|
+
CONTACT_POST_METHODS = %w[add edit sync]
|
6
|
+
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
# TODO: Create proper methods with parameter validation and possible naming fixes since this is one the worst APIs I have ever worked with.
|
10
|
+
included do
|
11
|
+
CONTACT_METHODS.each do |method|
|
12
|
+
define_method "contact_#{method}" do |options|
|
13
|
+
if CONTACT_POST_METHODS.include?(method)
|
14
|
+
post __method__, options
|
15
|
+
else
|
16
|
+
get __method__, options
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# contact_sync - parameters
|
26
|
+
# email - Email of the new subscriber. Example: 'test@example.com'
|
27
|
+
# first_name - First name of the subscriber. Example: 'FirstName'
|
28
|
+
# last_name - Last name of the subscriber. Example: 'LastName'
|
29
|
+
# ip4 - IP address of the subscriber. Example: '127.0.0.1' If not supplied, it will default to '127.0.0.1'
|
30
|
+
# field[345,0] - Custom field values. Example: field[345,0] = 'value'. In this example, "345" is the field ID. Leave 0 as is.
|
31
|
+
# field[%PERS_1%,0] - 'value' (You can also use the personalization tag to specify which field you want updated)
|
32
|
+
# - Assign to lists. List ID goes in brackets, as well as the value.
|
33
|
+
# status[123] - The status for each list the subscriber is added to. Examples: 1 = active, 2 = unsubscribed
|
34
|
+
# form - Optional subscription Form ID, to inherit those redirection settings. Example: 1001. This will allow you to mimic adding the subscriber through a subscription form, where you can take advantage of the redirection settings.
|
35
|
+
# noresponders[123] - Whether or not to set "do not send any future responders." Examples: 1 = yes, 0 = no.
|
36
|
+
# sdate[123] - Subscribe date for particular list - leave out to use current date/time. Example: '2009-12-07 06:00:00'
|
37
|
+
# instantresponders[123] Use only if status = 1. Whether or not to set "send instant responders." Examples: 1 = yes, 0 = no.
|
38
|
+
# lastmessage[123] - Whether or not to set "send the last broadcast campaign." Examples: 1 = yes, 0 = no.
|
39
|
+
# "field[%BALANCE%,0]": "100"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ActiveCampaign
|
2
|
+
class Client
|
3
|
+
module Lists
|
4
|
+
LIST_API_METHODS = %w[add delete_list delete edit field_add field_delete field_edit field_view list paginator view]
|
5
|
+
LIST_POST_METHODS = %w[add edit]
|
6
|
+
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
# TODO: Create proper methods with parameter validation and possible naming conversion since this is one the worst APIs I have ever worked with.
|
10
|
+
included do
|
11
|
+
LIST_API_METHODS.each do |method|
|
12
|
+
define_method "list_#{method}" do |options|
|
13
|
+
if LIST_POST_METHODS.include?(method)
|
14
|
+
post __method__, options
|
15
|
+
else
|
16
|
+
get __method__, options
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ActiveCampaign
|
2
|
+
class Client
|
3
|
+
module Messages
|
4
|
+
CONTACT_METHODS = %w[
|
5
|
+
add delete_list delete edit list template_add template_delete_list
|
6
|
+
template_delete template_edit template_export template_import
|
7
|
+
template_list template_view view
|
8
|
+
]
|
9
|
+
CONTACT_POST_METHODS = %w[add edit]
|
10
|
+
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
# TODO: Create proper methods with parameter validation and possible
|
14
|
+
# naming fixes since this is one the worst APIs I have ever worked with.
|
15
|
+
included do
|
16
|
+
CONTACT_METHODS.each do |method|
|
17
|
+
define_method "contact_#{method}" do |options|
|
18
|
+
if CONTACT_POST_METHODS.include?(method)
|
19
|
+
post __method__, options
|
20
|
+
else
|
21
|
+
get __method__, options
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# contact_sync - parameters
|
31
|
+
# email - Email of the new subscriber. Example: 'test@example.com'
|
32
|
+
# first_name - First name of the subscriber. Example: 'FirstName'
|
33
|
+
# last_name - Last name of the subscriber. Example: 'LastName'
|
34
|
+
# ip4 - IP address of the subscriber. Example: '127.0.0.1' If not supplied, it will default to '127.0.0.1'
|
35
|
+
# field[345,0] - Custom field values. Example: field[345,0] = 'value'. In this example, "345" is the field ID. Leave 0 as is.
|
36
|
+
# field[%PERS_1%,0] - 'value' (You can also use the personalization tag to specify which field you want updated)
|
37
|
+
# - Assign to lists. List ID goes in brackets, as well as the value.
|
38
|
+
# status[123] - The status for each list the subscriber is added to. Examples: 1 = active, 2 = unsubscribed
|
39
|
+
# form - Optional subscription Form ID, to inherit those redirection settings. Example: 1001. This will allow you to mimic adding the subscriber through a subscription form, where you can take advantage of the redirection settings.
|
40
|
+
# noresponders[123] - Whether or not to set "do not send any future responders." Examples: 1 = yes, 0 = no.
|
41
|
+
# sdate[123] - Subscribe date for particular list - leave out to use current date/time. Example: '2009-12-07 06:00:00'
|
42
|
+
# instantresponders[123] Use only if status = 1. Whether or not to set "send instant responders." Examples: 1 = yes, 0 = no.
|
43
|
+
# lastmessage[123] - Whether or not to set "send the last broadcast campaign." Examples: 1 = yes, 0 = no.
|
44
|
+
# "field[%BALANCE%,0]": "100"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'active_campaign/connection'
|
2
|
+
require 'active_campaign/request'
|
3
|
+
|
4
|
+
|
5
|
+
require 'active_campaign/client/contacts'
|
6
|
+
require 'active_campaign/client/lists'
|
7
|
+
|
8
|
+
module ActiveCampaign
|
9
|
+
class Client
|
10
|
+
attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
|
11
|
+
|
12
|
+
def initialize(options={})
|
13
|
+
options = ActiveCampaign.options.merge(options)
|
14
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
15
|
+
send("#{key}=", options[key])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
include ActiveCampaign::Connection
|
20
|
+
include ActiveCampaign::Request
|
21
|
+
|
22
|
+
include ActiveCampaign::Client::Contacts
|
23
|
+
include ActiveCampaign::Client::Lists
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'active_campaign/version'
|
3
|
+
|
4
|
+
module ActiveCampaign
|
5
|
+
module Configuration
|
6
|
+
VALID_OPTIONS_KEYS = [
|
7
|
+
:api_url,
|
8
|
+
:api_key,
|
9
|
+
:api_output,
|
10
|
+
:api_path,
|
11
|
+
:api_action,
|
12
|
+
:list_id,
|
13
|
+
:faraday_config_block,
|
14
|
+
:api_version,
|
15
|
+
:api_endpoint,
|
16
|
+
:proxy,
|
17
|
+
:user_agent,
|
18
|
+
:request_host,
|
19
|
+
:auto_traversal,
|
20
|
+
:adapter,
|
21
|
+
:log_requests,
|
22
|
+
:per_page].freeze
|
23
|
+
|
24
|
+
DEFAULT_ADAPTER = Faraday.default_adapter
|
25
|
+
DEFAULT_API_VERSION = 3
|
26
|
+
DEFAULT_API_ENDPOINT = ENV["ACTIVECAMPAIGN_API_ENDPOINT"] || "https://yourdomain.activehosted.com"
|
27
|
+
DEFAULT_API_PATH = ENV["ACTIVECAMPAIGN_API_PATH"] || "admin/api.php"
|
28
|
+
DEFAULT_API_OUTPUT = ENV["ACTIVECAMPAIGN_API_OUTPUT"] || "json"
|
29
|
+
DEFAULT_USER_AGENT = "ActiveCampaign Ruby Gem #{ActiveCampaign::VERSION}".freeze
|
30
|
+
DEFAULT_AUTO_TRAVERSAL = false
|
31
|
+
|
32
|
+
attr_accessor(*VALID_OPTIONS_KEYS)
|
33
|
+
|
34
|
+
def self.extended(base)
|
35
|
+
base.reset
|
36
|
+
end
|
37
|
+
|
38
|
+
def configure
|
39
|
+
yield self
|
40
|
+
end
|
41
|
+
|
42
|
+
def options
|
43
|
+
VALID_OPTIONS_KEYS.inject({}){|o,k| o.merge!(k => send(k)) }
|
44
|
+
end
|
45
|
+
|
46
|
+
def api_endpoint=(value)
|
47
|
+
@api_endpoint = File.join(value, "")
|
48
|
+
end
|
49
|
+
|
50
|
+
def api_key=(value)
|
51
|
+
@api_key = value.chomp
|
52
|
+
end
|
53
|
+
|
54
|
+
def faraday_config(&block)
|
55
|
+
@faraday_config_block = block
|
56
|
+
end
|
57
|
+
|
58
|
+
def reset
|
59
|
+
self.adapter = DEFAULT_ADAPTER
|
60
|
+
self.api_version = DEFAULT_API_VERSION
|
61
|
+
self.api_endpoint = DEFAULT_API_ENDPOINT
|
62
|
+
self.api_path = DEFAULT_API_PATH
|
63
|
+
self.api_output = DEFAULT_API_OUTPUT
|
64
|
+
self.list_id = nil
|
65
|
+
self.proxy = nil
|
66
|
+
# self.url_params = {}
|
67
|
+
self.request_host = nil
|
68
|
+
self.user_agent = DEFAULT_USER_AGENT
|
69
|
+
self.auto_traversal = DEFAULT_AUTO_TRAVERSAL
|
70
|
+
self.log_requests = false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
require 'faraday/response/raise_active_campaign_error'
|
3
|
+
require 'faraday/response/body_logger'
|
4
|
+
|
5
|
+
module ActiveCampaign
|
6
|
+
|
7
|
+
# @private
|
8
|
+
module Connection
|
9
|
+
private
|
10
|
+
|
11
|
+
def connection(options={})
|
12
|
+
options = {
|
13
|
+
force_urlencoded: true,
|
14
|
+
raw: false
|
15
|
+
}.merge(options)
|
16
|
+
|
17
|
+
if !proxy.nil?
|
18
|
+
options.merge!(proxy: proxy)
|
19
|
+
end
|
20
|
+
|
21
|
+
# TODO: Don't build on every request
|
22
|
+
connection = Faraday.new(options) do |conn|
|
23
|
+
|
24
|
+
if options[:force_urlencoded]
|
25
|
+
conn.request :url_encoded
|
26
|
+
else
|
27
|
+
conn.request :json
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
conn.use Faraday::Response::RaiseActiveCampaignError
|
32
|
+
conn.use FaradayMiddleware::FollowRedirects
|
33
|
+
if api_output == 'json'
|
34
|
+
conn.response :mashify, content_type: /\bjson$/
|
35
|
+
conn.response :json, content_type: /\bjson$/
|
36
|
+
end
|
37
|
+
faraday_config_block.call(conn) if faraday_config_block
|
38
|
+
if log_requests
|
39
|
+
conn.response :body_logger
|
40
|
+
conn.use :instrumentation
|
41
|
+
end
|
42
|
+
|
43
|
+
conn.adapter *adapter
|
44
|
+
end
|
45
|
+
|
46
|
+
connection.headers[:user_agent] = user_agent
|
47
|
+
connection
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module ActiveCampaign
|
2
|
+
# Custom error class for rescuing from all ActiveCampaign errors
|
3
|
+
class Error < StandardError
|
4
|
+
def initialize(response=nil)
|
5
|
+
@response = response
|
6
|
+
super(build_error_message)
|
7
|
+
end
|
8
|
+
|
9
|
+
def response_body
|
10
|
+
@response_body ||=
|
11
|
+
if (body = @response[:body]) && !body.empty?
|
12
|
+
if body.is_a?(String)
|
13
|
+
MultiJson.load(body, :symbolize_keys => true)
|
14
|
+
else
|
15
|
+
body
|
16
|
+
end
|
17
|
+
else
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def build_error_message
|
25
|
+
return nil if @response.nil?
|
26
|
+
|
27
|
+
message = if response_body
|
28
|
+
": #{response_body[:error] || response_body[:message] || ''}"
|
29
|
+
else
|
30
|
+
''
|
31
|
+
end
|
32
|
+
errors = unless message.empty?
|
33
|
+
response_body[:errors] ? ": #{response_body[:errors].map{|e|e[:message]}.join(', ')}" : ''
|
34
|
+
end
|
35
|
+
"#{@response[:method].to_s.upcase} #{@response[:url].to_s}: #{@response[:status]}#{message}#{errors}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Raised when ActiveCampaign returns a 400 HTTP status code
|
40
|
+
class BadRequest < Error; end
|
41
|
+
|
42
|
+
# Raised when ActiveCampaign returns a 401 HTTP status code
|
43
|
+
class Unauthorized < Error; end
|
44
|
+
|
45
|
+
# Raised when ActiveCampaign returns a 403 HTTP status code
|
46
|
+
class Forbidden < Error; end
|
47
|
+
|
48
|
+
# Raised when ActiveCampaign returns a 404 HTTP status code
|
49
|
+
class NotFound < Error; end
|
50
|
+
|
51
|
+
# Raised when ActiveCampaign returns a 406 HTTP status code
|
52
|
+
class NotAcceptable < Error; end
|
53
|
+
|
54
|
+
# Raised when ActiveCampaign returns a 422 HTTP status code
|
55
|
+
class UnprocessableEntity < Error; end
|
56
|
+
|
57
|
+
# Raised when ActiveCampaign returns a 500 HTTP status code
|
58
|
+
class InternalServerError < Error; end
|
59
|
+
|
60
|
+
# Raised when ActiveCampaign returns a 501 HTTP status code
|
61
|
+
class NotImplemented < Error; end
|
62
|
+
|
63
|
+
# Raised when ActiveCampaign returns a 502 HTTP status code
|
64
|
+
class BadGateway < Error; end
|
65
|
+
|
66
|
+
# Raised when ActiveCampaign returns a 503 HTTP status code
|
67
|
+
class ServiceUnavailable < Error; end
|
68
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
3
|
+
module ActiveCampaign
|
4
|
+
module Request
|
5
|
+
|
6
|
+
def delete(api_method, options={})
|
7
|
+
request(:delete, api_method, options).body
|
8
|
+
end
|
9
|
+
|
10
|
+
def get(api_method, options={})
|
11
|
+
request(:get, api_method, options).body
|
12
|
+
end
|
13
|
+
|
14
|
+
def patch(api_method, options={})
|
15
|
+
request(:patch, api_method, options).body
|
16
|
+
end
|
17
|
+
|
18
|
+
def post(api_method, options={})
|
19
|
+
request(:post, api_method, options).body
|
20
|
+
rescue => e
|
21
|
+
puts e, e.backtrace
|
22
|
+
binding.pry
|
23
|
+
end
|
24
|
+
|
25
|
+
def put(api_method, options={})
|
26
|
+
request(:put, api_method, options).body
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Executes the request, checking if it was successful
|
32
|
+
#
|
33
|
+
# @return [Boolean] True on success, false otherwise
|
34
|
+
def boolean_from_response(method, api_method, options={})
|
35
|
+
request(method, api_method, options).status == 204
|
36
|
+
rescue ActiveCampaign::NotFound
|
37
|
+
false
|
38
|
+
end
|
39
|
+
|
40
|
+
def path(api_method)
|
41
|
+
"#{api_path}?#{url_params(api_method).map{|k,v| "#{k}=#{v}" }.join('&')}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def url_params(api_method)
|
45
|
+
{
|
46
|
+
api_key: api_key,
|
47
|
+
api_action: api_method,
|
48
|
+
api_output: api_output,
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
def request(method, api_method, options={})
|
53
|
+
|
54
|
+
force_urlencoded = options.delete(:force_urlencoded) || false
|
55
|
+
url = options.delete(:endpoint) || api_endpoint
|
56
|
+
|
57
|
+
conn_options = {
|
58
|
+
:force_urlencoded => force_urlencoded,
|
59
|
+
:url => url
|
60
|
+
}
|
61
|
+
|
62
|
+
response = connection(conn_options).send(method) do |request|
|
63
|
+
case method
|
64
|
+
when :get, :delete, :head
|
65
|
+
options.reverse_merge!(url_params(api_method))
|
66
|
+
request.url(api_path, options)
|
67
|
+
when :patch, :post, :put
|
68
|
+
request.url(api_path, url_params(api_method))
|
69
|
+
if force_urlencoded
|
70
|
+
request.body = options unless options.empty?
|
71
|
+
else
|
72
|
+
request.body = MultiJson.dump(options) unless options.empty?
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
if ActiveCampaign.request_host
|
77
|
+
request.headers['Host'] = ActiveCampaign.request_host
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
response
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require "active_campaign/version"
|
4
|
+
require 'active_campaign/configuration'
|
5
|
+
require 'active_campaign/error'
|
6
|
+
require 'active_campaign/client'
|
7
|
+
|
8
|
+
|
9
|
+
require "pry"
|
10
|
+
|
11
|
+
module ActiveCampaign
|
12
|
+
|
13
|
+
extend Configuration
|
14
|
+
|
15
|
+
class << self
|
16
|
+
|
17
|
+
# Alias for ActiveCampaign::Client.new
|
18
|
+
#
|
19
|
+
# @return [ActiveCampaign::Client]
|
20
|
+
def new(options={})
|
21
|
+
ActiveCampaign::Client.new(options)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Delegate to ActiveCampaign::Client.new
|
25
|
+
def method_missing(method, *args, &block)
|
26
|
+
return super unless new.respond_to?(method)
|
27
|
+
new.send(method, *args, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def respond_to?(method, include_private=false)
|
31
|
+
new.respond_to?(method, include_private) || super(method, include_private)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|