simple_spark 0.0.8 → 0.0.10
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 +4 -4
- data/README.md +20 -3
- data/lib/simple_spark/client.rb +27 -2
- data/lib/simple_spark/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1f14be82b033bbbd05c7bfb4c3879282de53136
|
4
|
+
data.tar.gz: bd7abf8bb57ece2b2f8f0a061ddb8ff74f9042e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 511b77fb41792a9328990644edcdfe9c5ceb80b5fecd1d9b606bd42f428403d4a5d080ac8a255e242160ba8bd8b63e00848e3b3b65822b182d2eec483150d4b0
|
7
|
+
data.tar.gz: b39f9cbc6b11a8b4d1d503febaf35d3c5649550903479ed4326c351d49f6804bdc5ec53d8837a0593f4459679544d934d2aa912e6790f68a32895ea66d1f0925
|
data/README.md
CHANGED
@@ -72,6 +72,12 @@ Setting debug to true will cause [Excon](https://github.com/excon/excon) to outp
|
|
72
72
|
|
73
73
|
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)
|
74
74
|
|
75
|
+
You can also pass a Logger into the client options to have SimpleSpark log there. By default Rails.logger will be used when runnign under Rails, and STDOUT will be used otherwise
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
simple_spark = SimpleSpark::Client.new(api_key: 'your_api_key', debug: true, logger: Rails.logger)
|
79
|
+
```
|
80
|
+
|
75
81
|
#### Subaccounts
|
76
82
|
|
77
83
|
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.
|
@@ -90,7 +96,7 @@ simple_spark = SimpleSpark::Client.new(api_key: 'your_api_key', headers: { 'NewS
|
|
90
96
|
|
91
97
|
SimpleSpark wraps all the common errors from the SparkPost API
|
92
98
|
|
93
|
-
If the API
|
99
|
+
If the API takes too long to respond (times out in Excon) a GatewayTimeoutExceeded will be raised
|
94
100
|
|
95
101
|
Status 400 raises Exceptions::BadRequest
|
96
102
|
|
@@ -102,6 +108,8 @@ Status 420 raises Exceptions::ThrottleLimitExceeded
|
|
102
108
|
|
103
109
|
Other response status codes raise Exceptions::UnprocessableEntity
|
104
110
|
|
111
|
+
In some cases it is possible to send too fast for the API (apparently) to handle , in this case the SparkPost API returns a 504 status with an empty body. This is raised by SimpleSpark as Exceptions::GatewayTimeoutExceeded
|
112
|
+
|
105
113
|
### Metrics
|
106
114
|
|
107
115
|
#### Discoverability Links
|
@@ -703,13 +711,22 @@ simple_spark.templates.delete(yourtemplateid)
|
|
703
711
|
|
704
712
|
## Changelog
|
705
713
|
|
714
|
+
### 0.0.9
|
715
|
+
|
716
|
+
- Breaking change: 204 responses now return an empty hash t simplify consuming code
|
717
|
+
- Added logging, if debug is set then SimpleSpark will log its options and calls in addition to Excon.
|
718
|
+
|
719
|
+
### 0.0.8
|
720
|
+
|
721
|
+
- Improved exception handling
|
722
|
+
|
706
723
|
### 0.0.7
|
707
724
|
|
708
|
-
Added Time Series to Metrics
|
725
|
+
- Added Time Series to Metrics
|
709
726
|
|
710
727
|
### 0.0.6
|
711
728
|
|
712
|
-
Fixed accidental bug
|
729
|
+
- Fixed accidental bug
|
713
730
|
|
714
731
|
### 0.0.5
|
715
732
|
|
data/lib/simple_spark/client.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'excon'
|
3
3
|
require 'json'
|
4
|
+
require 'logger'
|
4
5
|
|
5
6
|
module SimpleSpark
|
6
7
|
class Client
|
8
|
+
attr_reader :logger
|
9
|
+
|
7
10
|
def initialize(opts = {})
|
8
11
|
@api_key = opts[:api_key] || ENV['SPARKPOST_API_KEY']
|
9
12
|
@api_host = opts[:api_host] || 'https://api.sparkpost.com'
|
@@ -11,6 +14,8 @@ module SimpleSpark
|
|
11
14
|
@subaccount_id = opts[:subaccount_id]
|
12
15
|
@headers = opts[:headers]
|
13
16
|
|
17
|
+
@logger = opts[:logger] || SimpleSpark::Client.default_logger
|
18
|
+
|
14
19
|
fail Exceptions::InvalidConfiguration.new, 'You must provide a SparkPost API key' unless @api_key
|
15
20
|
fail Exceptions::InvalidConfiguration.new, 'You must provide a SparkPost API host' unless @api_host # this should never occur unless the default above is changed
|
16
21
|
fail Exceptions::InvalidConfiguration.new, 'You must provide a SparkPost base path' unless @base_path # this should never occur unless the default above is changed
|
@@ -36,8 +41,21 @@ module SimpleSpark
|
|
36
41
|
params = { path: path, headers: headers }
|
37
42
|
params[:body] = body_values.to_json unless body_values.empty?
|
38
43
|
params[:query] = query_params unless query_params.empty?
|
44
|
+
|
45
|
+
if @debug
|
46
|
+
logger.debug("Calling #{method}")
|
47
|
+
logger.debug(params)
|
48
|
+
end
|
49
|
+
|
39
50
|
response = @session.send(method.to_s, params)
|
40
51
|
|
52
|
+
if @debug
|
53
|
+
logger.debug("Response #{response.status}")
|
54
|
+
logger.debug(response)
|
55
|
+
end
|
56
|
+
|
57
|
+
fail Exceptions::GatewayTimeoutExceeded('Received 504 from SparkPost API') if response.status == 504
|
58
|
+
|
41
59
|
process_response(response, extract_results)
|
42
60
|
|
43
61
|
rescue Excon::Errors::Timeout
|
@@ -45,14 +63,15 @@ module SimpleSpark
|
|
45
63
|
end
|
46
64
|
|
47
65
|
def process_response(response, extract_results)
|
48
|
-
|
66
|
+
logger.warn('Response had an empty body') if (response.body.nil? || response.body == '') && response.status != 204
|
67
|
+
return {} if response.status == 204 || response.body.nil? || response.body == ''
|
49
68
|
|
50
69
|
response_body = JSON.parse(response.body)
|
51
70
|
if response_body['errors']
|
52
71
|
Exceptions::Error.fail_with_exception_for_status(response.status, response_body['errors'])
|
53
72
|
else
|
54
73
|
if extract_results
|
55
|
-
response_body['results'] ? response_body['results'] :
|
74
|
+
response_body['results'] ? response_body['results'] : {}
|
56
75
|
else
|
57
76
|
response_body
|
58
77
|
end
|
@@ -76,6 +95,12 @@ module SimpleSpark
|
|
76
95
|
defaults
|
77
96
|
end
|
78
97
|
|
98
|
+
def self.default_logger
|
99
|
+
logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
100
|
+
logger.progname = 'simple_spark'
|
101
|
+
logger
|
102
|
+
end
|
103
|
+
|
79
104
|
def metrics
|
80
105
|
Endpoints::Metrics.new(self)
|
81
106
|
end
|
data/lib/simple_spark/version.rb
CHANGED