Class: AzureCommunicationEmail::DeliveryMethod
- Inherits:
-
Object
- Object
- AzureCommunicationEmail::DeliveryMethod
- Defined in:
- lib/azure_communication_email/delivery_method.rb
Constant Summary collapse
- DEFAULTS =
{ api_version: "2025-01-15-preview" }
Instance Attribute Summary collapse
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
Instance Method Summary collapse
- #deliver!(mail) ⇒ Object
-
#initialize(values) ⇒ DeliveryMethod
constructor
A new instance of DeliveryMethod.
Constructor Details
#initialize(values) ⇒ DeliveryMethod
Returns a new instance of DeliveryMethod.
17 18 19 20 21 |
# File 'lib/azure_communication_email/delivery_method.rb', line 17 def initialize(values) @endpoint = values.fetch(:endpoint) @api_version = values.fetch(:api_version, DEFAULTS[:api_version]) @access_key = values.fetch(:access_key) end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key.
15 16 17 |
# File 'lib/azure_communication_email/delivery_method.rb', line 15 def access_key @access_key end |
#api_version ⇒ Object
Returns the value of attribute api_version.
15 16 17 |
# File 'lib/azure_communication_email/delivery_method.rb', line 15 def api_version @api_version end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
15 16 17 |
# File 'lib/azure_communication_email/delivery_method.rb', line 15 def endpoint @endpoint end |
Instance Method Details
#deliver!(mail) ⇒ Object
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/azure_communication_email/delivery_method.rb', line 23 def deliver!(mail) raise ArgumentError, "Missing :endpoint configuration (https://my-resource.communication.azure.com)" if @endpoint.blank? raise ArgumentError, "Missing :access_key configuration" if @access_key.blank? path_and_query = "/emails:send?api-version=#{@api_version}" uri = URI.join(@endpoint, path_and_query) # Prepare email payload payload = Payload.new(mail) body_json = payload.to_json # Sign request hmac_auth = HmacAuth.new(endpoint: @endpoint, access_key: @access_key) headers = hmac_auth.sign_request( http_method: "POST", path_and_query: path_and_query, body: body_json ) # Azure Communication Services Email http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == "https") http.open_timeout = 5 http.read_timeout = 15 request = Net::HTTP::Post.new(uri.request_uri, headers) request.body = body_json response = http.request(request) unless response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPAccepted) raise Error, "Failed to send email: #{response.code} #{response.} - #{response.body}" end response rescue Error raise rescue StandardError => e raise Error, "Error sending email: #{e.}" end |