active_campaign 0.0.12 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/Gemfile +26 -0
- data/active_campaign.gemspec +6 -11
- data/lib/active_campaign.rb +26 -17
- data/lib/active_campaign/arguments.rb +14 -0
- data/lib/active_campaign/authentication.rb +68 -0
- data/lib/active_campaign/client.rb +201 -14
- data/lib/active_campaign/configurable.rb +84 -0
- data/lib/active_campaign/default.rb +135 -0
- data/lib/active_campaign/error.rb +93 -27
- data/lib/active_campaign/response/debugger.rb +42 -0
- data/lib/active_campaign/response/instrumentation.rb +33 -0
- data/lib/active_campaign/response/json_normalizer.rb +49 -0
- data/lib/active_campaign/response/mashify.rb +40 -0
- data/lib/active_campaign/response/parse_json.rb +83 -0
- data/lib/active_campaign/response/raise_error.rb +24 -0
- data/lib/active_campaign/version.rb +1 -1
- data/spec/active_campaign_spec.rb +3 -3
- data/spec/helper.rb +1 -1
- metadata +32 -84
- data/lib/active_campaign/configuration.rb +0 -74
- data/lib/active_campaign/connection.rb +0 -52
- data/lib/active_campaign/param_validation.rb +0 -12
- data/lib/active_campaign/request.rb +0 -84
- data/lib/faraday/response/body_logger.rb +0 -37
- data/lib/faraday/response/json_normalizer.rb +0 -46
- data/lib/faraday/response/raise_active_campaign_error.rb +0 -25
@@ -0,0 +1,83 @@
|
|
1
|
+
module ActiveCampaign
|
2
|
+
module Response
|
3
|
+
# Public: Parse response bodies as Oj.
|
4
|
+
class ParseJson < Faraday::Response::Middleware
|
5
|
+
CONTENT_TYPE = 'Content-Type'.freeze
|
6
|
+
MIME_TYPE = 'application/json'.freeze
|
7
|
+
BRACKETS = %w- [ { -
|
8
|
+
WHITESPACE = [ " ", "\n", "\r", "\t" ]
|
9
|
+
|
10
|
+
dependency do
|
11
|
+
require 'oj' unless defined?(::Oj)
|
12
|
+
end
|
13
|
+
|
14
|
+
def parser
|
15
|
+
lambda { |body|
|
16
|
+
::Oj.load body unless body.strip.empty?
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(app = nil, options = {})
|
21
|
+
super(app)
|
22
|
+
@options = options
|
23
|
+
@content_types = Array(options[:content_type])
|
24
|
+
end
|
25
|
+
|
26
|
+
def call(environment)
|
27
|
+
|
28
|
+
|
29
|
+
@app.call(environment).on_complete do |env|
|
30
|
+
process_response(env)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def process_response(env)
|
35
|
+
old_type = env[:response_headers][CONTENT_TYPE].to_s
|
36
|
+
new_type = MIME_TYPE.dup
|
37
|
+
new_type << ';' << old_type.split(';', 2).last if old_type.index(';')
|
38
|
+
env[:response_headers][CONTENT_TYPE] = new_type
|
39
|
+
|
40
|
+
env[:raw_body] = env[:body] if preserve_raw?(env)
|
41
|
+
env[:body] = parse(env[:body])
|
42
|
+
end
|
43
|
+
|
44
|
+
def parse(body)
|
45
|
+
parser.call(body)
|
46
|
+
end
|
47
|
+
|
48
|
+
def response_type(env)
|
49
|
+
type = env[:response_headers][CONTENT_TYPE].to_s
|
50
|
+
type = type.split(';', 2).first if type.index(';')
|
51
|
+
type
|
52
|
+
end
|
53
|
+
|
54
|
+
def process_response_type?(type)
|
55
|
+
@content_types.empty? or @content_types.any? { |pattern|
|
56
|
+
pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
def parse_response?(env)
|
61
|
+
env[:body].respond_to?(:to_str) &&
|
62
|
+
BRACKETS.include?(first_char(env[:body]))
|
63
|
+
end
|
64
|
+
|
65
|
+
def preserve_raw?(env)
|
66
|
+
env[:request].fetch(:preserve_raw, @options[:preserve_raw])
|
67
|
+
end
|
68
|
+
|
69
|
+
def first_char(body)
|
70
|
+
idx = -1
|
71
|
+
begin
|
72
|
+
char = body[idx += 1]
|
73
|
+
char = char.chr if char
|
74
|
+
end while char and WHITESPACE.include? char
|
75
|
+
char
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
Faraday.register_middleware :response, parse_json: lambda {
|
82
|
+
ActiveCampaign::Response::ParseJson
|
83
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'active_campaign/error'
|
3
|
+
|
4
|
+
module ActiveCampaign
|
5
|
+
# Faraday response middleware
|
6
|
+
module Response
|
7
|
+
|
8
|
+
# This class raises an ActiveCampaign-flavored exception based
|
9
|
+
# HTTP status codes returned by the API
|
10
|
+
class RaiseError < Faraday::Response::Middleware
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def on_complete(response)
|
15
|
+
if error = ActiveCampaign::Error.from_response(response)
|
16
|
+
raise error
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
Faraday.register_middleware :response, raise_error: lambda {
|
23
|
+
ActiveCampaign::Response::RaiseError
|
24
|
+
}
|
@@ -3,18 +3,18 @@ require 'helper'
|
|
3
3
|
describe ActiveCampaign do
|
4
4
|
|
5
5
|
after do
|
6
|
-
ActiveCampaign.
|
6
|
+
ActiveCampaign.setup
|
7
7
|
end
|
8
8
|
|
9
9
|
describe ".respond_to?" do
|
10
10
|
it "is true if method exists" do
|
11
|
-
expect(ActiveCampaign.respond_to?(:
|
11
|
+
expect(ActiveCampaign.respond_to?(:client, true)).to eq(true)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
describe ".new" do
|
16
16
|
it "is a ActiveCampaign::Client" do
|
17
|
-
expect(ActiveCampaign.
|
17
|
+
expect(ActiveCampaign.client).to be_a ActiveCampaign::Client
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
data/spec/helper.rb
CHANGED
metadata
CHANGED
@@ -1,43 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_campaign
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikael Henriksson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ~>
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '2.3'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ~>
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '2.3'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: hashie
|
14
|
+
name: faraday
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
30
16
|
requirements:
|
31
|
-
- -
|
17
|
+
- - '>='
|
32
18
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
19
|
+
version: 0.8.8
|
34
20
|
type: :runtime
|
35
21
|
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
|
-
- -
|
24
|
+
- - '>='
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
26
|
+
version: 0.8.8
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: activesupport
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,49 +39,35 @@ dependencies:
|
|
53
39
|
- !ruby/object:Gem::Version
|
54
40
|
version: 4.0.0
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0.8'
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ~>
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0.8'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: faraday_middleware
|
42
|
+
name: uri_template
|
71
43
|
requirement: !ruby/object:Gem::Requirement
|
72
44
|
requirements:
|
73
|
-
- -
|
45
|
+
- - '>='
|
74
46
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0
|
47
|
+
version: '0'
|
76
48
|
type: :runtime
|
77
49
|
prerelease: false
|
78
50
|
version_requirements: !ruby/object:Gem::Requirement
|
79
51
|
requirements:
|
80
|
-
- -
|
52
|
+
- - '>='
|
81
53
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0
|
54
|
+
version: '0'
|
83
55
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
56
|
+
name: hashie
|
85
57
|
requirement: !ruby/object:Gem::Requirement
|
86
58
|
requirements:
|
87
|
-
- -
|
59
|
+
- - '>='
|
88
60
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
61
|
+
version: '0'
|
90
62
|
type: :runtime
|
91
63
|
prerelease: false
|
92
64
|
version_requirements: !ruby/object:Gem::Requirement
|
93
65
|
requirements:
|
94
|
-
- -
|
66
|
+
- - '>='
|
95
67
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
68
|
+
version: '0'
|
97
69
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
70
|
+
name: oj
|
99
71
|
requirement: !ruby/object:Gem::Requirement
|
100
72
|
requirements:
|
101
73
|
- - '>='
|
@@ -109,47 +81,19 @@ dependencies:
|
|
109
81
|
- !ruby/object:Gem::Version
|
110
82
|
version: '0'
|
111
83
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
84
|
+
name: bundler
|
113
85
|
requirement: !ruby/object:Gem::Requirement
|
114
86
|
requirements:
|
115
87
|
- - ~>
|
116
88
|
- !ruby/object:Gem::Version
|
117
|
-
version: '1.
|
89
|
+
version: '1.0'
|
118
90
|
type: :development
|
119
91
|
prerelease: false
|
120
92
|
version_requirements: !ruby/object:Gem::Requirement
|
121
93
|
requirements:
|
122
94
|
- - ~>
|
123
95
|
- !ruby/object:Gem::Version
|
124
|
-
version: '1.
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: rspec
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
128
|
-
requirements:
|
129
|
-
- - '>='
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
version: '2.14'
|
132
|
-
type: :development
|
133
|
-
prerelease: false
|
134
|
-
version_requirements: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
136
|
-
- - '>='
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: '2.14'
|
139
|
-
- !ruby/object:Gem::Dependency
|
140
|
-
name: webmock
|
141
|
-
requirement: !ruby/object:Gem::Requirement
|
142
|
-
requirements:
|
143
|
-
- - '>='
|
144
|
-
- !ruby/object:Gem::Version
|
145
|
-
version: '0'
|
146
|
-
type: :development
|
147
|
-
prerelease: false
|
148
|
-
version_requirements: !ruby/object:Gem::Requirement
|
149
|
-
requirements:
|
150
|
-
- - '>='
|
151
|
-
- !ruby/object:Gem::Version
|
152
|
-
version: '0'
|
96
|
+
version: '1.0'
|
153
97
|
description: A simple ruby wrapper for the ActiveCampaign API
|
154
98
|
email:
|
155
99
|
- mikael@zoolutions.se
|
@@ -159,6 +103,7 @@ extra_rdoc_files: []
|
|
159
103
|
files:
|
160
104
|
- .gitignore
|
161
105
|
- .rspec
|
106
|
+
- .ruby-version
|
162
107
|
- .travis.yml
|
163
108
|
- Gemfile
|
164
109
|
- LICENSE.txt
|
@@ -167,20 +112,23 @@ files:
|
|
167
112
|
- active_campaign.gemspec
|
168
113
|
- lib/active_campaign.rb
|
169
114
|
- lib/active_campaign/.DS_Store
|
115
|
+
- lib/active_campaign/arguments.rb
|
116
|
+
- lib/active_campaign/authentication.rb
|
170
117
|
- lib/active_campaign/client.rb
|
171
118
|
- lib/active_campaign/client/campaigns.rb
|
172
119
|
- lib/active_campaign/client/contacts.rb
|
173
120
|
- lib/active_campaign/client/lists.rb
|
174
121
|
- lib/active_campaign/client/messages.rb
|
175
|
-
- lib/active_campaign/
|
176
|
-
- lib/active_campaign/
|
122
|
+
- lib/active_campaign/configurable.rb
|
123
|
+
- lib/active_campaign/default.rb
|
177
124
|
- lib/active_campaign/error.rb
|
178
|
-
- lib/active_campaign/
|
179
|
-
- lib/active_campaign/
|
125
|
+
- lib/active_campaign/response/debugger.rb
|
126
|
+
- lib/active_campaign/response/instrumentation.rb
|
127
|
+
- lib/active_campaign/response/json_normalizer.rb
|
128
|
+
- lib/active_campaign/response/mashify.rb
|
129
|
+
- lib/active_campaign/response/parse_json.rb
|
130
|
+
- lib/active_campaign/response/raise_error.rb
|
180
131
|
- lib/active_campaign/version.rb
|
181
|
-
- lib/faraday/response/body_logger.rb
|
182
|
-
- lib/faraday/response/json_normalizer.rb
|
183
|
-
- lib/faraday/response/raise_active_campaign_error.rb
|
184
132
|
- spec/active_campaign_spec.rb
|
185
133
|
- spec/client/contacts_spec.rb
|
186
134
|
- spec/client/lists_spec.rb
|
@@ -1,74 +0,0 @@
|
|
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
|
-
TEST_URL = "https://yourdomain.activehosted.com"
|
27
|
-
DEFAULT_API_ENDPOINT = ENV["ACTIVECAMPAIGN_API_ENDPOINT"] || TEST_URL
|
28
|
-
DEFAULT_API_PATH = ENV["ACTIVECAMPAIGN_API_PATH"] || "admin/api.php"
|
29
|
-
DEFAULT_API_OUTPUT = ENV["ACTIVECAMPAIGN_API_OUTPUT"] || "json"
|
30
|
-
DEFAULT_USER_AGENT = "ActiveCampaign Ruby Gem #{VERSION}".freeze
|
31
|
-
DEFAULT_AUTO_TRAVERSAL = false
|
32
|
-
|
33
|
-
attr_accessor(*VALID_OPTIONS_KEYS)
|
34
|
-
|
35
|
-
def self.extended(base)
|
36
|
-
base.reset
|
37
|
-
end
|
38
|
-
|
39
|
-
def configure
|
40
|
-
yield self
|
41
|
-
end
|
42
|
-
|
43
|
-
def options
|
44
|
-
VALID_OPTIONS_KEYS.inject({}){|o,k| o.merge!(k => send(k)) }
|
45
|
-
end
|
46
|
-
|
47
|
-
def api_endpoint=(value)
|
48
|
-
@api_endpoint = File.join(value, "")
|
49
|
-
end
|
50
|
-
|
51
|
-
def api_key=(value)
|
52
|
-
@api_key = value.chomp
|
53
|
-
end
|
54
|
-
|
55
|
-
def faraday_config(&block)
|
56
|
-
@faraday_config_block = block
|
57
|
-
end
|
58
|
-
|
59
|
-
def reset
|
60
|
-
self.adapter = DEFAULT_ADAPTER
|
61
|
-
self.api_version = DEFAULT_API_VERSION
|
62
|
-
self.api_endpoint = DEFAULT_API_ENDPOINT
|
63
|
-
self.api_path = DEFAULT_API_PATH
|
64
|
-
self.api_output = DEFAULT_API_OUTPUT
|
65
|
-
self.list_id = nil
|
66
|
-
self.proxy = nil
|
67
|
-
# self.url_params = {}
|
68
|
-
self.request_host = nil
|
69
|
-
self.user_agent = DEFAULT_USER_AGENT
|
70
|
-
self.auto_traversal = DEFAULT_AUTO_TRAVERSAL
|
71
|
-
self.log_requests = false
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'faraday_middleware'
|
2
|
-
require 'faraday/response/raise_active_campaign_error'
|
3
|
-
require 'faraday/response/body_logger'
|
4
|
-
require 'faraday/response/json_normalizer'
|
5
|
-
|
6
|
-
module ActiveCampaign
|
7
|
-
|
8
|
-
# @private
|
9
|
-
module Connection
|
10
|
-
private
|
11
|
-
|
12
|
-
def connection(options={})
|
13
|
-
options = {
|
14
|
-
force_urlencoded: true,
|
15
|
-
raw: false
|
16
|
-
}.merge(options)
|
17
|
-
|
18
|
-
if !proxy.nil?
|
19
|
-
options.merge!(proxy: proxy)
|
20
|
-
end
|
21
|
-
|
22
|
-
# TODO: Don't build on every request
|
23
|
-
connection = Faraday.new(options) do |conn|
|
24
|
-
|
25
|
-
if options[:force_urlencoded]
|
26
|
-
conn.request :url_encoded
|
27
|
-
else
|
28
|
-
conn.request :json
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
conn.use Faraday::Response::RaiseActiveCampaignError
|
33
|
-
conn.use FaradayMiddleware::FollowRedirects
|
34
|
-
if api_output == 'json'
|
35
|
-
conn.response :mashify, content_type: /\bjson$/
|
36
|
-
conn.response :json_normalizer
|
37
|
-
conn.response :json, content_type: /\bjson$/
|
38
|
-
end
|
39
|
-
faraday_config_block.call(conn) if faraday_config_block
|
40
|
-
if log_requests
|
41
|
-
conn.response :body_logger
|
42
|
-
conn.use :instrumentation
|
43
|
-
end
|
44
|
-
|
45
|
-
conn.adapter *adapter
|
46
|
-
end
|
47
|
-
|
48
|
-
connection.headers[:user_agent] = user_agent
|
49
|
-
connection
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|