Class: SendLayer::Client
- Inherits:
-
Object
- Object
- SendLayer::Client
- Defined in:
- lib/sendlayer/client.rb
Constant Summary collapse
- BASE_URL =
'https://console.sendlayer.com/api/v1/'.freeze
- DEFAULT_TIMEOUT =
Seconds to wait for the API before giving up.
30- ERROR_MAP =
HTTP status => [error class, fallback message used when the API response carries no message of its own].
{ 400 => [SendLayerValidationError, 'Invalid request parameters'], 401 => [SendLayerAuthenticationError, 'Invalid API key'], 404 => [SendLayerNotFoundError, 'Resource not found'], 422 => [SendLayerValidationError, 'Unprocessable Entity'], 429 => [SendLayerRateLimitError, 'Rate limit exceeded'], 500 => [SendLayerInternalServerError, 'Internal server error'] }.freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#attachment_url_timeout ⇒ Object
readonly
Returns the value of attribute attachment_url_timeout.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#initialize(api_key, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#make_request(method, endpoint, data = nil, params = {}) ⇒ Object
Make a request to the SendLayer API.
Constructor Details
#initialize(api_key, options = {}) ⇒ Client
Returns a new instance of Client.
29 30 31 32 33 34 35 |
# File 'lib/sendlayer/client.rb', line 29 def initialize(api_key, = {}) @api_key = api_key @base_url = [:base_url] || BASE_URL @timeout = [:timeout] || DEFAULT_TIMEOUT @attachment_url_timeout = [:attachment_url_timeout] || 30_000 @headers = [:headers] || {} end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
27 28 29 |
# File 'lib/sendlayer/client.rb', line 27 def api_key @api_key end |
#attachment_url_timeout ⇒ Object (readonly)
Returns the value of attribute attachment_url_timeout.
27 28 29 |
# File 'lib/sendlayer/client.rb', line 27 def @attachment_url_timeout end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
27 28 29 |
# File 'lib/sendlayer/client.rb', line 27 def base_url @base_url end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
27 28 29 |
# File 'lib/sendlayer/client.rb', line 27 def timeout @timeout end |
Instance Method Details
#make_request(method, endpoint, data = nil, params = {}) ⇒ Object
Make a request to the SendLayer API.
Always returns a Hash/Array or raises a SendLayerError -- a Net::HTTP exception is never surfaced to the caller.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sendlayer/client.rb', line 41 def make_request(method, endpoint, data = nil, params = {}) uri = URI("#{@base_url}#{endpoint}") uri.query = URI.encode_www_form(params) unless params.empty? http = Net::HTTP.new(uri.host, uri.port) # Derived from the URI rather than forced on, so an http:// base_url # (a local test server, say) is actually usable. http.use_ssl = uri.scheme == 'https' http.read_timeout = @timeout # A connection that never completes its handshake would otherwise hang # past read_timeout, which only covers waiting for the response body. http.open_timeout = @timeout request = build_request(method, uri, data) # Only the transport call is guarded. handle_response runs outside this # block so the typed error it raises is not caught and re-wrapped as a # generic "Connection error" -- which previously flattened every API # error to the base SendLayerError. response = begin http.request(request) rescue Timeout::Error raise SendLayerError.new("Request timed out after #{@timeout}s") rescue StandardError => e raise SendLayerError.new("Connection error: #{e.}") end handle_response(response) end |