active_campaign 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ require 'faraday'
2
+ require 'forwardable'
3
+
4
+ module Faraday
5
+ class Response::BodyLogger < Response::Middleware
6
+ extend Forwardable
7
+
8
+ def initialize(app, logger = nil)
9
+ super(app)
10
+ @logger = logger || begin
11
+ require 'logger'
12
+ ::Logger.new(STDOUT)
13
+ end
14
+ end
15
+
16
+ def_delegators :@logger, :debug, :info, :warn, :error, :fatal
17
+
18
+ def call(env)
19
+ info("#{env[:method]} #{env[:url].to_s}")
20
+ debug('request') { dump_headers(env[:request_headers]) + "\nbody: #{env[:body]}" }
21
+ super
22
+ end
23
+
24
+ def on_complete(env)
25
+ info('Status') { env[:status].to_s }
26
+ debug('response-head') { dump_headers env[:response_headers] }
27
+ debug('response-body') { env[:body] }
28
+ end
29
+
30
+ private
31
+
32
+ def dump_headers(headers)
33
+ headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
34
+ end
35
+ end
36
+ end
37
+ Faraday.register_middleware :response, body_logger: lambda { Faraday::Response::BodyLogger }
@@ -0,0 +1,25 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ # @api private
5
+ module Faraday
6
+ class Response::RaiseActiveCampaignError < Response::Middleware
7
+ ERROR_MAP = {
8
+ 400 => ActiveCampaign::BadRequest,
9
+ 401 => ActiveCampaign::Unauthorized,
10
+ 403 => ActiveCampaign::Forbidden,
11
+ 404 => ActiveCampaign::NotFound,
12
+ 406 => ActiveCampaign::NotAcceptable,
13
+ 422 => ActiveCampaign::UnprocessableEntity,
14
+ 500 => ActiveCampaign::InternalServerError,
15
+ 501 => ActiveCampaign::NotImplemented,
16
+ 502 => ActiveCampaign::BadGateway,
17
+ 503 => ActiveCampaign::ServiceUnavailable
18
+ }
19
+
20
+ def on_complete(response)
21
+ key = response[:status].to_i
22
+ raise ERROR_MAP[key].new(response) if ERROR_MAP.has_key? key
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ describe ActiveCampaign do
4
+
5
+ after do
6
+ ActiveCampaign.reset
7
+ end
8
+
9
+ describe ".respond_to?" do
10
+ it "is true if method exists" do
11
+ expect(ActiveCampaign.respond_to?(:new, true)).to eq(true)
12
+ end
13
+ end
14
+
15
+ describe ".new" do
16
+ it "is a ActiveCampaign::Client" do
17
+ expect(ActiveCampaign.new).to be_a ActiveCampaign::Client
18
+ end
19
+ end
20
+
21
+ describe ".delegate" do
22
+ it "delegates missing methods to ActiveCampaign::Client" do
23
+ initialize_active_campaign
24
+ stub_get("contact_view", email: "mikael@zoolutions.se").
25
+ to_return json_response('contact_view.json')
26
+ contact = ActiveCampaign.contact_view(email: "mikael@zoolutions.se")
27
+ expect(contact.email).to eq('mikael@zoolutions.se')
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ # need this to filter the data
5
+ require 'json'
6
+ require 'date'
7
+ describe ActiveCampaign::Client::Contacts do
8
+
9
+ initialize_new_client
10
+
11
+ it "syncs a contact" do
12
+ params = {
13
+ id: 1,
14
+ email: 'mhenrixon@me.com',
15
+ first_name: 'Mikael',
16
+ last_name: 'Henriksson',
17
+ "p" => {"1" => 1, "2" => 2},
18
+ "status" => {"1" => 1, "2" => 2},
19
+ "instantresponders" => {"1" => 1, "2" => 2},
20
+ "field[%BALANCE%,0]" => 100,
21
+ ip4: '127.0.0.1',
22
+ }.stringify_keys
23
+
24
+ stub_post("contact_sync").
25
+ to_return json_response("contact_sync.json")
26
+
27
+ @client.contact_sync params
28
+ end
29
+
30
+ describe ".contact_view" do
31
+ it "returns the right contact" do
32
+ stub_get("contact_view", email: "mikael@zoolutions.se").
33
+ with(email: "mikael@zoolutions.se").
34
+ to_return json_response("contact_view.json")
35
+
36
+ contact = @client.contact_view(email: "mikael@zoolutions.se")
37
+ expect(contact.first_name).to eq "Mikael"
38
+ expect(contact.last_name).to eq "Henriksson"
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,48 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ # need this to filter the data
5
+ require 'json'
6
+ require 'date'
7
+ describe ActiveCampaign::Client::Lists do
8
+
9
+ initialize_new_client
10
+
11
+ pending "add a list" do
12
+ params = {
13
+ "id" => 1,
14
+ "email" => 'mhenrixon@me.com',
15
+ "first_name"=> 'Mikael',
16
+ "last_name" => 'Henriksson',
17
+ "p[1]" => 1,
18
+ "status[1]" => 1,
19
+ "instantresponders[1]" => 1,
20
+ "field[%BALANCE%,0]" => 100,
21
+ "ip4" => '127.0.0.1',
22
+ "status[2]" => 2,
23
+ "listid" => 1
24
+ }.stringify_keys
25
+
26
+ list = ActiveCampaign.list_add params
27
+ end
28
+
29
+ describe ".list_view" do
30
+ it "returns the right list" do
31
+ stub_get("list_view", id: 1).
32
+ to_return json_response("list_view.json")
33
+
34
+ list = @client.list_view id: 1
35
+ expect(list.name).to eq "Swedish Players"
36
+ end
37
+
38
+ it "returns the right list list", :focus do
39
+ WebMock.allow_net_connect!
40
+ # stub_get("list_view", name: "Swedish Players").
41
+ # to_return json_response("list_view.json")
42
+
43
+ list = @client.list_list "ids" => 'all'
44
+ binding.pry
45
+ expect(list["0"].name).to eq "Swedish Players"
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ module ActiveCampaign
4
+ describe Client do
5
+
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ {
2
+ "subscriber_id": 1,
3
+ "sendlast_should": 0,
4
+ "sendlast_did": 0,
5
+ "result_code": 1,
6
+ "result_message": "Contact updated",
7
+ "result_output": "json"
8
+ }
@@ -0,0 +1,78 @@
1
+ {
2
+ "id": "1",
3
+ "subscriberid": "1",
4
+ "listid": "1",
5
+ "formid": "0",
6
+ "sdate": "2013-06-11 21:38:06",
7
+ "udate": null,
8
+ "status": "1",
9
+ "responder": "1",
10
+ "sync": "0",
11
+ "unsubreason": null,
12
+ "unsubcampaignid": "0",
13
+ "unsubmessageid": "0",
14
+ "first_name": "Mikael",
15
+ "last_name": "Henriksson",
16
+ "ip4_sub": "2130706433",
17
+ "sourceid": "4",
18
+ "sourceid_autosync": "0",
19
+ "ip4_last": "2130706433",
20
+ "ip4_unsub": "0",
21
+ "cdate": "2013-06-11 21:38:05",
22
+ "email": "mikael@zoolutions.se",
23
+ "bounced_hard": "0",
24
+ "bounced_soft": "0",
25
+ "bounced_date": null,
26
+ "ip": "0.0.0.0",
27
+ "ua": null,
28
+ "hash": "efbe686f0dd17539ebdae49fc1cacc21",
29
+ "socialdata_lastcheck": null,
30
+ "email_local": "",
31
+ "email_domain": "",
32
+ "rating": "-1.00",
33
+ "gravatar": "2",
34
+ "deleted": "0",
35
+ "lid": "1",
36
+ "ip4": "127.0.0.1",
37
+ "name": "Mikael Henriksson",
38
+ "a_unsub_date": null,
39
+ "a_unsub_time": null,
40
+ "lists": {
41
+ "1": {
42
+ "id": "1",
43
+ "subscriberid": "1",
44
+ "listid": "1",
45
+ "formid": "0",
46
+ "sdate": "2013-06-11 21:38:06",
47
+ "udate": null,
48
+ "status": "1",
49
+ "responder": "1",
50
+ "sync": "0",
51
+ "unsubreason": null,
52
+ "unsubcampaignid": "0",
53
+ "unsubmessageid": "0",
54
+ "first_name": "Mikael",
55
+ "last_name": "Henriksson",
56
+ "ip4_sub": "2130706433",
57
+ "sourceid": "4",
58
+ "sourceid_autosync": "0",
59
+ "ip4_last": "2130706433",
60
+ "ip4_unsub": "0",
61
+ "listname": "Swedish Players"
62
+ }
63
+ },
64
+ "listslist": "1",
65
+ "fields": [],
66
+ "actions": [],
67
+ "campaign_history": [],
68
+ "bounces": {
69
+ "mailing": [],
70
+ "mailings": 0,
71
+ "responder": [],
72
+ "responders": 0
73
+ },
74
+ "bouncescnt": 0,
75
+ "result_code": 1,
76
+ "result_message": "Success: Something is returned",
77
+ "result_output": "json"
78
+ }
@@ -0,0 +1,220 @@
1
+ {
2
+ "id": "1",
3
+ "stringid": "swedish-players",
4
+ "userid": "1",
5
+ "name": "Swedish Players",
6
+ "cdate": "2013-05-22 10:07:36",
7
+ "p_use_tracking": "1",
8
+ "p_use_analytics_read": "0",
9
+ "p_use_analytics_link": "0",
10
+ "p_use_twitter": "0",
11
+ "p_use_facebook": "0",
12
+ "p_embed_image": "1",
13
+ "p_use_captcha": "1",
14
+ "send_last_broadcast": "0",
15
+ "private": "0",
16
+ "analytics_domains": null,
17
+ "analytics_source": "",
18
+ "analytics_ua": "",
19
+ "twitter_token": "",
20
+ "twitter_token_secret": "",
21
+ "facebook_session": null,
22
+ "carboncopy": null,
23
+ "subscription_notify": null,
24
+ "unsubscription_notify": null,
25
+ "require_name": "0",
26
+ "get_unsubscribe_reason": "0",
27
+ "to_name": "Subscriber",
28
+ "optinoptout": "1",
29
+ "sender_name": "Casinosaga",
30
+ "sender_addr1": "The Strand 101",
31
+ "sender_addr2": "",
32
+ "sender_city": "Sliema",
33
+ "sender_state": "",
34
+ "sender_zip": "12135",
35
+ "sender_country": "Malta",
36
+ "sender_phone": "",
37
+ "fulladdress": "Casinosaga, The Strand 101 , Sliema, 12135, Malta",
38
+ "optinmessageid": 0,
39
+ "optoutconf": "0",
40
+ "deletestamp": null,
41
+ "listid": "1",
42
+ "p_admin": "1",
43
+ "p_list_add": "1",
44
+ "p_list_edit": "1",
45
+ "p_list_delete": "1",
46
+ "p_list_sync": "1",
47
+ "p_list_filter": "1",
48
+ "p_message_add": "1",
49
+ "p_message_edit": "1",
50
+ "p_message_delete": "1",
51
+ "p_message_send": "1",
52
+ "p_subscriber_add": "1",
53
+ "p_subscriber_edit": "1",
54
+ "p_subscriber_delete": "1",
55
+ "p_subscriber_import": "1",
56
+ "p_subscriber_approve": "1",
57
+ "luserid": "1",
58
+ "fields": [],
59
+ "groups": {
60
+ "3": {
61
+ "id": "3",
62
+ "title": "Admin",
63
+ "descript": "This is a group for admin users (people that can manage content)",
64
+ "unsubscribelink": "0",
65
+ "optinconfirm": "0",
66
+ "p_admin": "1",
67
+ "pg_list_add": "1",
68
+ "pg_list_edit": "1",
69
+ "pg_list_delete": "1",
70
+ "pg_list_headers": "1",
71
+ "pg_list_emailaccount": "1",
72
+ "pg_list_bounce": "1",
73
+ "pg_message_add": "1",
74
+ "pg_message_edit": "1",
75
+ "pg_message_delete": "1",
76
+ "pg_message_send": "1",
77
+ "pg_subscriber_add": "1",
78
+ "pg_subscriber_edit": "1",
79
+ "pg_subscriber_delete": "1",
80
+ "pg_subscriber_import": "1",
81
+ "pg_subscriber_approve": "1",
82
+ "pg_subscriber_export": "1",
83
+ "pg_subscriber_sync": "1",
84
+ "pg_subscriber_filters": "1",
85
+ "pg_subscriber_actions": "1",
86
+ "pg_subscriber_fields": "1",
87
+ "pg_user_add": "1",
88
+ "pg_user_edit": "1",
89
+ "pg_user_delete": "1",
90
+ "pg_group_add": "1",
91
+ "pg_group_edit": "1",
92
+ "pg_group_delete": "1",
93
+ "pg_template_add": "1",
94
+ "pg_template_edit": "1",
95
+ "pg_template_delete": "1",
96
+ "pg_personalization_add": "1",
97
+ "pg_personalization_edit": "1",
98
+ "pg_personalization_delete": "1",
99
+ "pg_form_edit": "1",
100
+ "pg_reports_campaign": "1",
101
+ "pg_reports_list": "1",
102
+ "pg_reports_user": "1",
103
+ "pg_reports_trend": "1",
104
+ "pg_startup_reports": "1",
105
+ "pg_startup_gettingstarted": "2",
106
+ "sdate": "2013-05-22 09:18:00",
107
+ "req_approval": "0",
108
+ "req_approval_1st": "2",
109
+ "req_approval_notify": "",
110
+ "socialdata": "0",
111
+ "listid": "1",
112
+ "groupid": "3",
113
+ "p_list_add": "0",
114
+ "p_list_edit": "0",
115
+ "p_list_delete": "0",
116
+ "p_list_sync": "1",
117
+ "p_list_filter": "1",
118
+ "p_message_add": "1",
119
+ "p_message_edit": "1",
120
+ "p_message_delete": "1",
121
+ "p_message_send": "1",
122
+ "p_subscriber_add": "1",
123
+ "p_subscriber_edit": "1",
124
+ "p_subscriber_delete": "1",
125
+ "p_subscriber_import": "1",
126
+ "p_subscriber_approve": "1"
127
+ },
128
+ "1": {
129
+ "id": "1",
130
+ "title": "Visitor",
131
+ "descript": "This is a group for site visitors (people that are not logged in)",
132
+ "unsubscribelink": "0",
133
+ "optinconfirm": "0",
134
+ "p_admin": "0",
135
+ "pg_list_add": "0",
136
+ "pg_list_edit": "0",
137
+ "pg_list_delete": "0",
138
+ "pg_list_headers": "0",
139
+ "pg_list_emailaccount": "0",
140
+ "pg_list_bounce": "0",
141
+ "pg_message_add": "0",
142
+ "pg_message_edit": "0",
143
+ "pg_message_delete": "0",
144
+ "pg_message_send": "0",
145
+ "pg_subscriber_add": "0",
146
+ "pg_subscriber_edit": "0",
147
+ "pg_subscriber_delete": "0",
148
+ "pg_subscriber_import": "0",
149
+ "pg_subscriber_approve": "0",
150
+ "pg_subscriber_export": "0",
151
+ "pg_subscriber_sync": "0",
152
+ "pg_subscriber_filters": "0",
153
+ "pg_subscriber_actions": "0",
154
+ "pg_subscriber_fields": "0",
155
+ "pg_user_add": "0",
156
+ "pg_user_edit": "0",
157
+ "pg_user_delete": "0",
158
+ "pg_group_add": "0",
159
+ "pg_group_edit": "0",
160
+ "pg_group_delete": "0",
161
+ "pg_template_add": "0",
162
+ "pg_template_edit": "0",
163
+ "pg_template_delete": "0",
164
+ "pg_personalization_add": "0",
165
+ "pg_personalization_edit": "0",
166
+ "pg_personalization_delete": "0",
167
+ "pg_form_edit": "0",
168
+ "pg_reports_campaign": "0",
169
+ "pg_reports_list": "0",
170
+ "pg_reports_user": "0",
171
+ "pg_reports_trend": "0",
172
+ "pg_startup_reports": "0",
173
+ "pg_startup_gettingstarted": "0",
174
+ "sdate": "2013-05-22 09:18:00",
175
+ "req_approval": "0",
176
+ "req_approval_1st": "2",
177
+ "req_approval_notify": "",
178
+ "socialdata": "0",
179
+ "listid": "1",
180
+ "groupid": "1",
181
+ "p_list_add": "0",
182
+ "p_list_edit": "0",
183
+ "p_list_delete": "0",
184
+ "p_list_sync": "0",
185
+ "p_list_filter": "0",
186
+ "p_message_add": "0",
187
+ "p_message_edit": "0",
188
+ "p_message_delete": "0",
189
+ "p_message_send": "0",
190
+ "p_subscriber_add": "0",
191
+ "p_subscriber_edit": "0",
192
+ "p_subscriber_delete": "0",
193
+ "p_subscriber_import": "0",
194
+ "p_subscriber_approve": "0"
195
+ }
196
+ },
197
+ "groupsCnt": 2,
198
+ "bounces": [{
199
+ "id": "1",
200
+ "userid": "1",
201
+ "type": "pipe",
202
+ "email": "bounce-56272@s1.acems2.com",
203
+ "host": null,
204
+ "port": "110",
205
+ "user": null,
206
+ "pass": null,
207
+ "method": "",
208
+ "limit_hard": "1",
209
+ "limit_soft": "3",
210
+ "emails_per_batch": "120"
211
+ }
212
+ ],
213
+ "analytics_domains_list": [],
214
+ "facebook_oauth_logout_url": "",
215
+ "facebook_oauth_login_url": "https:\/\/www.facebook.com\/dialog\/oauth?client_id=118506481555093&redirect_uri=https%3A%2F%2Fyourdomain.activehosted.com%2Fadmin%2Fmain.php%3Faction%3Dlist%26formid%3D1&scope=publish_stream,manage_pages",
216
+ "facebook_oauth_me": null,
217
+ "result_code": 1,
218
+ "result_message": "Success: Something is returned",
219
+ "result_output": "json"
220
+ }
data/spec/helper.rb ADDED
@@ -0,0 +1,111 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
9
+
10
+ require "active_campaign"
11
+ require 'rspec'
12
+ require 'webmock/rspec'
13
+
14
+ WebMock.disable_net_connect!(:allow => 'coveralls.io')
15
+
16
+ RSpec.configure do |config|
17
+ config.treat_symbols_as_metadata_keys_with_true_values = true
18
+ config.filter_run focus: true
19
+ config.order = "random"
20
+ config.run_all_when_everything_filtered = true
21
+ config.expect_with :rspec do |c|
22
+ c.syntax = :expect
23
+ end
24
+ end
25
+
26
+ def a_delete(url, options = {})
27
+ a_request(:delete, active_campaign_url(url, options))
28
+ end
29
+
30
+ def a_get(url, options = {})
31
+ a_request(:get, active_campaign_url(url, options))
32
+ end
33
+
34
+ def a_patch(url, options = {})
35
+ a_request(:patch, active_campaign_url(url, options))
36
+ end
37
+
38
+ def a_post(url, options = {})
39
+ a_request(:post, active_campaign_url(url, options))
40
+ end
41
+
42
+ def a_put(url, options = {})
43
+ a_request(:put, active_campaign_url(url, options))
44
+ end
45
+
46
+ def stub_delete(url, options = {})
47
+ stub_request(:delete, active_campaign_url(url, options))
48
+ end
49
+
50
+ def stub_get(url, options = {})
51
+ stub_request(:get, active_campaign_url(url, options))
52
+ end
53
+
54
+ def stub_head(url, options = {})
55
+ stub_request(:head, active_campaign_url(url, options))
56
+ end
57
+
58
+ def stub_patch(url, options = {})
59
+ stub_request(:patch, active_campaign_url(url, options))
60
+ end
61
+
62
+ def stub_post(url, options = {})
63
+ stub_request(:post, active_campaign_url(url, options))
64
+ end
65
+
66
+ def stub_put(url, options = {})
67
+ stub_request(:put, active_campaign_url(url, options))
68
+ end
69
+
70
+ def fixture_path
71
+ File.expand_path("../fixtures", __FILE__)
72
+ end
73
+
74
+ def fixture(file)
75
+ File.new(fixture_path + '/' + file)
76
+ end
77
+
78
+ def json_response(file)
79
+ {
80
+ :body => fixture(file),
81
+ :headers => {
82
+ :content_type => 'application/json; charset=utf-8'
83
+ }
84
+ }
85
+ end
86
+
87
+ def active_campaign_url(url, options = {})
88
+ if url =~ /^http/
89
+ url
90
+ else
91
+ uri = "https://yourdomain.activehosted.com/admin/api.php?api_action=#{url}&api_key=YOURAPIKEY&api_output=serialize"
92
+ params = options.map{|k,v| "#{k}=#{v}" }.join("&")
93
+ "#{uri}&#{params}"
94
+ end
95
+ end
96
+
97
+ def initialize_new_client
98
+ before do
99
+ initialize_active_campaign
100
+ @client = ActiveCampaign::Client.new
101
+ end
102
+ end
103
+
104
+ def initialize_active_campaign
105
+ ActiveCampaign.configure do |config|
106
+ config.api_endpoint = "https://yourdomain.activehosted.com/"
107
+ config.api_key = "YOURAPIKEY"
108
+ config.api_output = "json"
109
+ config.log_requests = true
110
+ end
111
+ end