Class: Mailclerk::Client
- Inherits:
-
Object
- Object
- Mailclerk::Client
- Defined in:
- lib/client.rb
Instance Method Summary collapse
- #deliver(template, recipient, data = {}, options = {}) ⇒ Object
-
#initialize(api_key, api_url = nil) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(api_key, api_url = nil) ⇒ Client
Returns a new instance of Client.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/client.rb', line 3 def initialize(api_key, api_url=nil) @api_key = api_key @api_url = api_url || ENV['MAILCLERK_API_URL'] || DEFAULT_API_URL if @api_key.nil? raise MailclerkError.new( "No Mailclerk API Key provided. Set `Mailclerk.api_key`" ) end if @api_url.nil? || @api_url.empty? raise MailclerkError.new("Mailclerk API URL empty") end end |
Instance Method Details
#deliver(template, recipient, data = {}, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/client.rb', line 18 def deliver(template, recipient, data={}, ={}) conn = Faraday.new(url: @api_url) conn.basic_auth(@api_key, '') if Mailclerk.outbox_enabled? = .merge( "local_outbox" => true ) .delete(:local_outbox) end params = { 'template' => template, 'recipient' => recipient, 'data' => data, 'options' => } response = conn.post('deliver', params.to_json, { 'Content-Type' => 'application/json', 'X-Client-Version' => Identity.version_label }) if response.status >= 400 begin = JSON.parse(response.body)["message"] || "Unknown" description = "Mailclerk API Error: #{ }" rescue JSON::ParserError description = "Mailclerk API Unknown Error" end raise MailclerkAPIError.new( description, response.status, response ) end if Mailclerk.outbox_enabled? params["options"].delete("local_outbox") Mailclerk.outbox.add_send(params, JSON.parse(response.body)["delivery"]) end return response end |