simple_spark 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -6
- data/lib/simple_spark/client.rb +18 -10
- data/lib/simple_spark/version.rb +1 -1
- data/spec/simple_spark/client_spec.rb +27 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16ba8873c00d42cecd527635b7f551c03e357080
|
4
|
+
data.tar.gz: 5c2d0465090b9d068ed1761879b05a46871722f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 639c9ad49a6277585ce70ccb4dd19b5369d2c5f98cc2872539348305c3c0e1160586c0df18c09b26bd6789a6e0f01fdbbbd34cfb8f6fd1cbf38b065ffff367c5
|
7
|
+
data.tar.gz: 9864262d473c0a460a85fe8580160060feeb2a8bb382d50b142268b7490c62b491219f2527761aca101edbed9c51588104a5c05ea444ba6dce1d6f0bce64b35e
|
data/README.md
CHANGED
@@ -35,8 +35,7 @@ The official gem currently only supports the send_message (create) method of /tr
|
|
35
35
|
|
36
36
|
### Status
|
37
37
|
|
38
|
-
|
39
|
-
|
38
|
+
Breaking change: initialising the client is now done with a hash instead of with ordered parameters, as there were getting to be too many after supporting Subaccounts and user specified headers
|
40
39
|
|
41
40
|
## Endpoints
|
42
41
|
|
@@ -51,7 +50,7 @@ require 'simple_spark'
|
|
51
50
|
The simplest version of the client is to just provide your [API key from SparkPost](https://app.sparkpost.com/account/credentials)
|
52
51
|
|
53
52
|
```ruby
|
54
|
-
simple_spark = SimpleSpark::Client.new('your_api_key')
|
53
|
+
simple_spark = SimpleSpark::Client.new(api_key: 'your_api_key')
|
55
54
|
```
|
56
55
|
|
57
56
|
You can also use ENV vars to configure the key, setting ENV['SPARKPOST_API_KEY'] will allow you to just use
|
@@ -63,15 +62,27 @@ simple_spark = SimpleSpark::Client.new
|
|
63
62
|
You can also override the other options if you need to in advanced scenarios, the full signature is (api_key, api_host, base_path, debug), i.e.
|
64
63
|
|
65
64
|
```ruby
|
66
|
-
simple_spark = SimpleSpark::Client.new('your_api_key', 'https://api.sparkpost.com', '/api/v1/', false)
|
65
|
+
simple_spark = SimpleSpark::Client.new(api_key: 'your_api_key', api_host: 'https://api.sparkpost.com', base_path: '/api/v1/', debug: false, subaccount_id: 'my_subaccount')
|
67
66
|
```
|
68
67
|
|
69
|
-
|
68
|
+
#### Debug
|
69
|
+
|
70
|
+
Setting debug to true will cause [Excon](https://github.com/excon/excon) to output full debug information to the log.
|
70
71
|
|
71
72
|
This will default to true if you are running under Rails and are in a development environment, otherwise it will default to false (setting other values to nil will cause them to use their defaults)
|
72
73
|
|
74
|
+
#### Subaccounts
|
75
|
+
|
76
|
+
By setting subaccount_id on your client you are telling Simple Spark to use that subaccount for all calls made on this instance of the client.
|
77
|
+
|
78
|
+
Not all Sparkpost calls support the Subaccount feature, and their API will throw an unauthorized error if you use a subaccount_id on an unsupported call. Depending on your code this may mean you need to instantiate two instances of the Simple Spark client in your code, one for subaccount calls, and one for other calls. This is a less than ideal solution, but due to the rapid pace of Sparkpost development on their API this is the option that causes least dependency up Simple Spark to be updated as their API is.
|
79
|
+
|
80
|
+
#### Headers
|
81
|
+
|
82
|
+
Should you have any need to override the headers that are sent by default, then you can specify headers as an option. The headers specified here will override any of the generated headers that the library creates. In normal operation there should be no reason to use this option, but it is provided for convenience and to allow for Sparkpost updating their API in any unexpected way.
|
83
|
+
|
73
84
|
```ruby
|
74
|
-
simple_spark = SimpleSpark::Client.new(
|
85
|
+
simple_spark = SimpleSpark::Client.new(api_key: 'your_api_key', headers: { 'NewSparkpostHeader' => 'hello'})
|
75
86
|
```
|
76
87
|
|
77
88
|
### Transmissions
|
data/lib/simple_spark/client.rb
CHANGED
@@ -4,26 +4,30 @@ require 'json'
|
|
4
4
|
|
5
5
|
module SimpleSpark
|
6
6
|
class Client
|
7
|
-
def initialize(
|
8
|
-
@api_key = api_key || ENV['SPARKPOST_API_KEY']
|
9
|
-
@api_host = api_host || 'https://api.sparkpost.com'
|
10
|
-
@base_path = base_path || '/api/v1/'
|
7
|
+
def initialize(options = {})
|
8
|
+
@api_key = options[:api_key] || ENV['SPARKPOST_API_KEY']
|
9
|
+
@api_host = options[:api_host] || 'https://api.sparkpost.com'
|
10
|
+
@base_path = options[:base_path] || '/api/v1/'
|
11
|
+
@subaccount_id = options[:subaccount_id]
|
12
|
+
@headers = options[:headers]
|
11
13
|
|
12
14
|
fail Exceptions::InvalidConfiguration.new, 'You must provide a SparkPost API key' unless @api_key
|
13
15
|
fail Exceptions::InvalidConfiguration.new, 'You must provide a SparkPost API host' unless @api_host # this should never occur unless the default above is changed
|
14
16
|
fail Exceptions::InvalidConfiguration.new, 'You must provide a SparkPost base path' unless @base_path # this should never occur unless the default above is changed
|
17
|
+
fail Exceptions::InvalidConfiguration.new, 'The headers options provided must be a valid Hash' if @headers && !@headers.is_a?(Hash)
|
15
18
|
|
16
19
|
rails_development = !(defined?(Rails) && Rails.env.development?).nil?
|
17
20
|
|
18
|
-
@debug = debug.nil? ? rails_development : debug
|
21
|
+
@debug = options[:debug].nil? ? rails_development : options[:debug]
|
22
|
+
|
19
23
|
@session = Excon.new(@api_host, debug: @debug)
|
20
24
|
end
|
21
25
|
|
22
26
|
def call(method, path, body_values = {}, query_params = {})
|
23
|
-
fail Exceptions::InvalidConfiguration.new(
|
27
|
+
fail Exceptions::InvalidConfiguration.new(method: method), 'Only GET, POST, PUT and DELETE are supported' unless [:get, :post, :put, :delete].include?(method)
|
24
28
|
|
25
29
|
path = "#{@base_path}#{path}"
|
26
|
-
params = { path: path, headers:
|
30
|
+
params = { path: path, headers: headers }
|
27
31
|
params[:body] = body_values.to_json unless body_values.empty?
|
28
32
|
params[:query] = query_params unless query_params.empty?
|
29
33
|
response = @session.send(method.to_s, params)
|
@@ -44,15 +48,19 @@ module SimpleSpark
|
|
44
48
|
|
45
49
|
# Copied from http://apidock.com/ruby/ERB/Util/url_encode
|
46
50
|
def url_encode(s)
|
47
|
-
s.to_s.dup.force_encoding(
|
51
|
+
s.to_s.dup.force_encoding('ASCII-8BIT').gsub(/[^a-zA-Z0-9_\-.]/) { sprintf('%%%02X', $&.unpack('C')[0]) }
|
48
52
|
end
|
49
53
|
|
50
|
-
def
|
51
|
-
{
|
54
|
+
def headers
|
55
|
+
defaults = {
|
52
56
|
'User-Agent' => 'simple_spark/' + VERSION,
|
53
57
|
'Content-Type' => 'application/json',
|
58
|
+
'Accept' => 'application/json',
|
54
59
|
'Authorization' => @api_key
|
55
60
|
}
|
61
|
+
defaults.merge!('X-MSYS-SUBACCOUNT' => @subaccount_id) if @subaccount_id
|
62
|
+
defaults.merge!(@headers) if @headers
|
63
|
+
defaults
|
56
64
|
end
|
57
65
|
|
58
66
|
def inbound_domains
|
data/lib/simple_spark/version.rb
CHANGED
@@ -7,12 +7,13 @@ describe SimpleSpark::Client do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
context 'defaults' do
|
10
|
-
let(:client) { SimpleSpark::Client.new('mykey') }
|
10
|
+
let(:client) { SimpleSpark::Client.new(api_key: 'mykey') }
|
11
11
|
specify { expect(client.instance_variable_get(:@api_key)).to eq('mykey') }
|
12
12
|
specify { expect(client.instance_variable_get(:@api_host)).to eq('https://api.sparkpost.com') }
|
13
13
|
specify { expect(client.instance_variable_get(:@base_path)).to eq('/api/v1/') }
|
14
14
|
specify { expect(client.instance_variable_get(:@session).class).to eq(Excon::Connection) }
|
15
15
|
specify { expect(client.instance_variable_get(:@debug)).to eq(false) }
|
16
|
+
specify { expect(client.instance_variable_get(:@subaccount_id)).to eq(nil) }
|
16
17
|
end
|
17
18
|
|
18
19
|
it 'will use the API key from the ENV var' do
|
@@ -22,11 +23,34 @@ describe SimpleSpark::Client do
|
|
22
23
|
end
|
23
24
|
|
24
25
|
it 'will use the base_path provided' do
|
25
|
-
expect(SimpleSpark::Client.new('mykey',
|
26
|
+
expect(SimpleSpark::Client.new(api_key: 'mykey', base_path: 'base').instance_variable_get(:@base_path)).to eq('base')
|
26
27
|
end
|
27
28
|
|
28
29
|
it 'will use the debug option provided' do
|
29
|
-
expect(SimpleSpark::Client.new('mykey',
|
30
|
+
expect(SimpleSpark::Client.new(api_key: 'mykey', debug: true).instance_variable_get(:@debug)).to eq(true)
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'using the subaccount id provided' do
|
34
|
+
let(:subaccount_id) { 'my_subaccount_id' }
|
35
|
+
let(:client) { SimpleSpark::Client.new(api_key: 'mykey', subaccount_id: subaccount_id) }
|
36
|
+
specify { expect(client.instance_variable_get(:@subaccount_id)).to eq(subaccount_id) }
|
37
|
+
specify { expect(client.headers).to include('X-MSYS-SUBACCOUNT' => subaccount_id) }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'using the headers option provided' do
|
41
|
+
let(:headers) { { x: 'y' } }
|
42
|
+
let(:client) { SimpleSpark::Client.new(api_key: 'mykey', headers: headers) }
|
43
|
+
specify { expect(client.instance_variable_get(:@headers)).to eq(headers) }
|
44
|
+
specify { expect(client.headers).to include(headers) }
|
45
|
+
|
46
|
+
it 'specified headers will override default constructed ones' do
|
47
|
+
client = SimpleSpark::Client.new(api_key: 'mykey', subaccount_id: 'old', headers: { 'X-MSYS-SUBACCOUNT' => 'new' })
|
48
|
+
expect(client.headers['X-MSYS-SUBACCOUNT']).to eq('new')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'will raise when headers is not a Hash' do
|
53
|
+
expect { SimpleSpark::Client.new(api_key: 'mykey', headers: 'wrong') }.to raise_error(SimpleSpark::Exceptions::InvalidConfiguration, 'The headers options provided must be a valid Hash')
|
30
54
|
end
|
31
55
|
end
|
32
56
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_spark
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jak Charlton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -246,3 +246,4 @@ summary: A library for accessing the SparkPost REST API http://www.sparkpost.com
|
|
246
246
|
test_files:
|
247
247
|
- spec/simple_spark/client_spec.rb
|
248
248
|
- spec/spec_helper.rb
|
249
|
+
has_rdoc:
|