|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Mailtrap |
| 4 | + class EmailTemplates |
| 5 | + API_HOST = 'mailtrap.io' |
| 6 | + API_PORT = 443 |
| 7 | + |
| 8 | + attr_reader :api_key, :api_host, :api_port |
| 9 | + |
| 10 | + def initialize(api_key:, api_host: API_HOST, api_port: API_PORT) |
| 11 | + raise ArgumentError, 'api_key is required' if api_key.nil? |
| 12 | + raise ArgumentError, 'api_port is required' if api_port.nil? |
| 13 | + |
| 14 | + @api_key = api_key |
| 15 | + @api_host = api_host |
| 16 | + @api_port = api_port |
| 17 | + end |
| 18 | + |
| 19 | + def all(account_id:) |
| 20 | + request(:get, "/api/accounts/#{account_id}/email_templates") |
| 21 | + end |
| 22 | + |
| 23 | + def create(account_id:, **params) |
| 24 | + request(:post, "/api/accounts/#{account_id}/email_templates", params) |
| 25 | + end |
| 26 | + |
| 27 | + def update(account_id:, email_template_id:, **params) |
| 28 | + request( |
| 29 | + :patch, |
| 30 | + "/api/accounts/#{account_id}/email_templates/#{email_template_id}", |
| 31 | + params |
| 32 | + ) |
| 33 | + end |
| 34 | + |
| 35 | + def delete(account_id:, email_template_id:) |
| 36 | + request(:delete, "/api/accounts/#{account_id}/email_templates/#{email_template_id}") |
| 37 | + true |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + def http_client |
| 43 | + @http_client ||= Net::HTTP.new(api_host, api_port).tap { |client| client.use_ssl = true } |
| 44 | + end |
| 45 | + |
| 46 | + def request(http_method, path, body = nil) # rubocop:disable Metrics/MethodLength |
| 47 | + request_class = { |
| 48 | + get: Net::HTTP::Get, |
| 49 | + post: Net::HTTP::Post, |
| 50 | + patch: Net::HTTP::Patch, |
| 51 | + delete: Net::HTTP::Delete |
| 52 | + }.fetch(http_method) |
| 53 | + |
| 54 | + request = request_class.new(path) |
| 55 | + request['Authorization'] = "Bearer #{api_key}" |
| 56 | + request['User-Agent'] = 'mailtrap-ruby (https://github.com/railsware/mailtrap-ruby)' |
| 57 | + if body |
| 58 | + request['Content-Type'] = 'application/json' |
| 59 | + request.body = JSON.generate(body) |
| 60 | + end |
| 61 | + |
| 62 | + response = http_client.request(request) |
| 63 | + handle_response(response) |
| 64 | + end # rubocop:enable Metrics/MethodLength |
| 65 | + |
| 66 | + def handle_response(response) # rubocop:disable Metrics/MethodLength |
| 67 | + case response |
| 68 | + when Net::HTTPNoContent |
| 69 | + true |
| 70 | + when Net::HTTPSuccess |
| 71 | + json_response(response.body) |
| 72 | + when Net::HTTPUnauthorized |
| 73 | + raise Mailtrap::AuthorizationError, json_response(response.body)[:errors] |
| 74 | + when Net::HTTPForbidden |
| 75 | + raise Mailtrap::RejectionError, json_response(response.body)[:errors] |
| 76 | + when Net::HTTPClientError, Net::HTTPServerError |
| 77 | + raise Mailtrap::Error, json_response(response.body)[:errors] |
| 78 | + else |
| 79 | + raise Mailtrap::Error, ["unexpected status code=#{response.code}"] |
| 80 | + end |
| 81 | + end # rubocop:enable Metrics/MethodLength |
| 82 | + |
| 83 | + def json_response(body) |
| 84 | + return {} if body.nil? || body.empty? |
| 85 | + |
| 86 | + JSON.parse(body, symbolize_names: true) |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments