Skip to content

Commit cf807d4

Browse files
Merge pull request #64 from DripEmail/configuration_object
Extract configuration object
2 parents eb1a1d2 + b39e1b6 commit cf807d4

5 files changed

Lines changed: 174 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313
- `Drip::Client#get`, `Drip::Client#post`, `Drip::Client#put`, and `Drip::Client#delete` methods no longer auto-prepend `/v2` if the given path starts with `v2/` or `v3/`. This behavior is deprecated and will produce a warning. If you are using one of these methods and get this warning, just add `v2/` to the beginning of the path when you call it.
1414
- `Drip::Client#generate_resource` is deprecated and will be removed in a future version.
1515
- `Drip::Client#content_type` is deprecated and will be removed in a future version. It is no longer used internally, effective immediately.
16+
- When using the block form of parameter initialization, a configuration object is provided instead of the client itself.
17+
- Calling configuration setters on `Drip::Client` is deprecated.
1618

1719
### Removed
1820
- Drop support for Ruby 2.1.

lib/drip/client.rb

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "drip/client/broadcasts"
55
require "drip/client/campaigns"
66
require "drip/client/campaign_subscriptions"
7+
require "drip/client/configuration"
78
require "drip/client/conversions"
89
require "drip/client/custom_fields"
910
require "drip/client/events"
@@ -37,19 +38,24 @@ class Client # rubocop:disable Metrics/ClassLength
3738

3839
REDIRECT_LIMIT = 10
3940

40-
attr_accessor :access_token, :api_key, :account_id, :url_prefix, :http_open_timeout, :http_timeout
41+
Drip::Client::Configuration::CONFIGURATION_FIELDS.each do |config_key|
42+
define_method(config_key) do
43+
@config.public_send(config_key)
44+
end
45+
46+
setter_name = "#{config_key}=".to_sym
47+
define_method(setter_name) do |val|
48+
warn "[DEPRECATED] Setting configuration on Drip::Client after initialization will be removed in a future version"
49+
@config.public_send(setter_name, val)
50+
end
51+
end
4152

4253
JSON_API_CONTENT_TYPE = "application/vnd.api+json".freeze
4354
private_constant :JSON_API_CONTENT_TYPE
4455

4556
def initialize(options = {})
46-
@account_id = options[:account_id]
47-
@access_token = options[:access_token]
48-
@api_key = options[:api_key]
49-
@url_prefix = options[:url_prefix] || "https://api.getdrip.com/"
50-
@http_open_timeout = options[:http_open_timeout]
51-
@http_timeout = options[:http_timeout]
52-
yield(self) if block_given?
57+
@config = Drip::Client::Configuration.new(options)
58+
yield(@config) if block_given?
5359
end
5460

5561
def generate_resource(key, *args)
@@ -90,7 +96,7 @@ def make_uri(path)
9096
warn "[DEPRECATED] Automatically prepended path with 'v2/'"
9197
path = "v2/#{path}"
9298
end
93-
URI(url_prefix) + URI(path)
99+
URI(@config.url_prefix) + URI(path)
94100
end
95101

96102
def make_request(verb_klass, uri, options, step = 0)
@@ -112,10 +118,10 @@ def make_request(verb_klass, uri, options, step = 0)
112118
request['Content-Type'] = JSON_API_CONTENT_TYPE
113119
request['Accept'] = "*/*"
114120

115-
if access_token
116-
request['Authorization'] = "Bearer #{access_token}"
121+
if @config.access_token
122+
request['Authorization'] = "Bearer #{@config.access_token}"
117123
else
118-
request.basic_auth api_key, ""
124+
request.basic_auth @config.api_key, ""
119125
end
120126

121127
response = http.request request
@@ -138,12 +144,12 @@ def build_response(&block)
138144
def connection_options(uri_scheme)
139145
options = { use_ssl: uri_scheme == "https" }
140146

141-
if @http_open_timeout
142-
options[:open_timeout] = @http_open_timeout
143-
options[:ssl_timeout] = @http_open_timeout
147+
if @config.http_open_timeout
148+
options[:open_timeout] = @config.http_open_timeout
149+
options[:ssl_timeout] = @config.http_open_timeout
144150
end
145151

146-
options[:read_timeout] = @http_timeout if @http_timeout
152+
options[:read_timeout] = @config.http_timeout if @config.http_timeout
147153

148154
options
149155
end

lib/drip/client/configuration.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Drip
2+
class Client
3+
class Configuration
4+
DEFAULT_URL_PREFIX = "https://api.getdrip.com/".freeze
5+
private_constant :DEFAULT_URL_PREFIX
6+
7+
CONFIGURATION_FIELDS = %i[access_token api_key account_id url_prefix http_open_timeout http_timeout].freeze
8+
9+
attr_accessor(*CONFIGURATION_FIELDS)
10+
11+
def initialize(**options)
12+
remainder = options.keys - CONFIGURATION_FIELDS
13+
raise ArgumentError, "unknown keyword#{'s' if remainder.size > 1}: #{remainder.join(', ')}" unless remainder.empty?
14+
15+
# Initialize this variable to suppress Ruby warning.
16+
@url_prefix = nil
17+
18+
options.each do |k, v|
19+
public_send("#{k}=", v)
20+
end
21+
end
22+
23+
def url_prefix
24+
@url_prefix || DEFAULT_URL_PREFIX
25+
end
26+
end
27+
end
28+
end
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
require File.dirname(__FILE__) + '/../../test_helper.rb'
2+
require "drip/client/configuration"
3+
4+
class Drip::Client::ConfigurationTest < Drip::TestCase
5+
context "#initializer" do
6+
context "with reasonable parameters" do
7+
should "accept parameters" do
8+
config = Drip::Client::Configuration.new(access_token: "123")
9+
assert_equal "123", config.access_token
10+
end
11+
end
12+
13+
context "with one additional parameter" do
14+
should "raise singular error" do
15+
err = assert_raises(ArgumentError) { Drip::Client::Configuration.new(blahdeblah: "123") }
16+
assert_equal "unknown keyword: blahdeblah", err.message
17+
end
18+
19+
should "match core ruby behavior" do
20+
def test_method(hello: "test"); end
21+
err = assert_raises(ArgumentError) { test_method(blahdeblah: "123") }
22+
assert_equal "unknown keyword: blahdeblah", err.message
23+
end
24+
end
25+
26+
context "with multiple additional parameters" do
27+
should "raise plural error" do
28+
err = assert_raises(ArgumentError) { Drip::Client::Configuration.new(blahdeblah: "123", blahdeblah1: "123") }
29+
assert_equal "unknown keywords: blahdeblah, blahdeblah1", err.message
30+
end
31+
32+
should "match core ruby behavior" do
33+
def test_method(hello: "test"); end
34+
err = assert_raises(ArgumentError) { test_method(blahdeblah: "123", blahdeblah1: "123") }
35+
assert_equal "unknown keywords: blahdeblah, blahdeblah1", err.message
36+
end
37+
end
38+
end
39+
40+
context "#url_prefix" do
41+
should "have default url prefix" do
42+
config = Drip::Client::Configuration.new
43+
assert_equal "https://api.getdrip.com/", config.url_prefix
44+
end
45+
46+
should "accept passed parameter" do
47+
config = Drip::Client::Configuration.new(url_prefix: "https://www.example.com/")
48+
assert_equal "https://www.example.com/", config.url_prefix
49+
end
50+
51+
should "allow setter" do
52+
config = Drip::Client::Configuration.new
53+
config.url_prefix = "https://www.example.com/"
54+
assert_equal "https://www.example.com/", config.url_prefix
55+
end
56+
end
57+
58+
context "#access_token" do
59+
should "accept passed parameter" do
60+
config = Drip::Client::Configuration.new(access_token: "blah")
61+
assert_equal "blah", config.access_token
62+
end
63+
64+
should "allow setter" do
65+
config = Drip::Client::Configuration.new
66+
config.access_token = "blah"
67+
assert_equal "blah", config.access_token
68+
end
69+
end
70+
71+
context "#api_key" do
72+
should "accept passed parameter" do
73+
config = Drip::Client::Configuration.new(api_key: "blah")
74+
assert_equal "blah", config.api_key
75+
end
76+
77+
should "allow setter" do
78+
config = Drip::Client::Configuration.new
79+
config.api_key = "blah"
80+
assert_equal "blah", config.api_key
81+
end
82+
end
83+
84+
context "#account_id" do
85+
should "accept passed parameter" do
86+
config = Drip::Client::Configuration.new(account_id: "1234567")
87+
assert_equal "1234567", config.account_id
88+
end
89+
90+
should "allow setter" do
91+
config = Drip::Client::Configuration.new
92+
config.account_id = "1234567"
93+
assert_equal "1234567", config.account_id
94+
end
95+
end
96+
97+
context "#http_open_timeout" do
98+
should "accept passed parameter" do
99+
config = Drip::Client::Configuration.new(http_open_timeout: 12)
100+
assert_equal 12, config.http_open_timeout
101+
end
102+
103+
should "allow setter" do
104+
config = Drip::Client::Configuration.new
105+
config.http_open_timeout = 12
106+
assert_equal 12, config.http_open_timeout
107+
end
108+
end
109+
110+
context "#http_timeout" do
111+
should "accept passed parameter" do
112+
config = Drip::Client::Configuration.new(http_timeout: 42)
113+
assert_equal 42, config.http_timeout
114+
end
115+
116+
should "allow setter" do
117+
config = Drip::Client::Configuration.new
118+
config.http_timeout = 42
119+
assert_equal 42, config.http_timeout
120+
end
121+
end
122+
end

test/drip/client_test.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ class Drip::ClientTest < Drip::TestCase
1919
assert_equal "aaaa", client.url_prefix
2020
end
2121

22-
should "have default url prefix" do
23-
client = Drip::Client.new
24-
assert_equal "https://api.getdrip.com/", client.url_prefix
25-
end
26-
2722
should "accept access token" do
2823
client = Drip::Client.new do |config|
2924
config.access_token = "aaaa"

0 commit comments

Comments
 (0)