|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Mailtrap |
| 4 | + # Data Transfer Object for Contact Create Request |
| 5 | + # @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/284bcc8fd846f-contact-create-request |
| 6 | + # @attr_reader email [String] The contact's email address (required) |
| 7 | + # @attr_reader fields [Hash] Object of fields with merge tags |
| 8 | + # @attr_reader list_ids [Array<Integer>] Array of list IDs |
| 9 | + ContactCreateRequest = Struct.new(:email, :fields, :list_ids, keyword_init: true) do |
| 10 | + # @return [Hash] The contact request attributes as a hash |
| 11 | + def to_h |
| 12 | + super.compact |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + # Data Transfer Object for Contact Update Request |
| 17 | + # @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/d3efb09dbeda8-contact-update-request |
| 18 | + # @attr_reader email [String] The contact's email address (required) |
| 19 | + # @attr_reader fields [Hash] Object of fields with merge tags |
| 20 | + # @attr_reader list_ids_included [Array<Integer>] Array of list IDs to include |
| 21 | + # @attr_reader list_ids_excluded [Array<Integer>] Array of list IDs to exclude |
| 22 | + # @attr_reader unsubscribed [Boolean] Whether to unsubscribe the contact |
| 23 | + ContactUpdateRequest = Struct.new(:email, :fields, :list_ids_included, :list_ids_excluded, :unsubscribed, |
| 24 | + keyword_init: true) do |
| 25 | + # @return [Hash] The contact request attributes as a hash |
| 26 | + def to_h |
| 27 | + super.compact |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + # Data Transfer Object for Contact |
| 32 | + # @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/220a54e31e5ca-contact |
| 33 | + # @attr_reader id [String] The contact ID |
| 34 | + # @attr_reader email [String] The contact's email address |
| 35 | + # @attr_reader fields [Hash] Object of fields with merge tags |
| 36 | + # @attr_reader list_ids [Array<Integer>] Array of list IDs |
| 37 | + # @attr_reader status [String] The contact status (subscribed/unsubscribed) |
| 38 | + # @attr_reader created_at [Integer] The creation timestamp |
| 39 | + # @attr_reader updated_at [Integer] The last update timestamp |
| 40 | + Contact = Struct.new( |
| 41 | + :id, |
| 42 | + :email, |
| 43 | + :fields, |
| 44 | + :list_ids, |
| 45 | + :status, |
| 46 | + :created_at, |
| 47 | + :updated_at, |
| 48 | + keyword_init: true |
| 49 | + ) do |
| 50 | + # @return [Hash] The contact attributes as a hash |
| 51 | + def to_h |
| 52 | + super.compact |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + # Data Transfer Object for Contact Update Response |
| 57 | + # @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/16eab4fff9740-contact-update-response |
| 58 | + # @attr_reader action [String] The performed action (created/updated) |
| 59 | + # @attr_reader data [Contact] The contact data |
| 60 | + ContactUpdateResponse = Struct.new(:action, :data, keyword_init: true) do |
| 61 | + def initialize(*) |
| 62 | + super |
| 63 | + self.data = Contact.new(data) if data.is_a?(Hash) |
| 64 | + end |
| 65 | + |
| 66 | + # @return [Hash] The response attributes as a hash |
| 67 | + def to_h |
| 68 | + super.compact |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + class ContactsAPI |
| 73 | + include Mailtrap::API |
| 74 | + |
| 75 | + def initialize(account_id, client = Mailtrap::Client.new) |
| 76 | + @account_id = account_id |
| 77 | + @client = client |
| 78 | + end |
| 79 | + |
| 80 | + # Retrieves a specific contact |
| 81 | + # @param contact_id [String] The contact identifier, which can be either a UUID or an email address |
| 82 | + # @return [Contact] Contact object |
| 83 | + # @raise [Mailtrap::Error] If the API request fails with a client or server error |
| 84 | + # @raise [Mailtrap::AuthorizationError] If the API key is invalid |
| 85 | + # @raise [Mailtrap::RejectionError] If the server refuses to process the request |
| 86 | + # @raise [Mailtrap::RateLimitError] If too many requests are made |
| 87 | + def get(contact_id) |
| 88 | + response = @client.get("#{base_path}/#{contact_id}") |
| 89 | + build_entity(response[:data], Contact) |
| 90 | + end |
| 91 | + |
| 92 | + # Creates a new contact |
| 93 | + # @param request [ContactCreateRequest, Hash] The contact create request object or a hash with the same attributes |
| 94 | + # @return [Contact] Created contact object |
| 95 | + # @raise [Mailtrap::Error] If the API request fails with a client or server error |
| 96 | + # @raise [Mailtrap::AuthorizationError] If the API key is invalid |
| 97 | + # @raise [Mailtrap::RejectionError] If the server refuses to process the request |
| 98 | + # @raise [Mailtrap::RateLimitError] If too many requests are made |
| 99 | + def create(request) |
| 100 | + response = @client.post(base_path, { contact: prepare_request(request, ContactCreateRequest) }) |
| 101 | + build_entity(response[:data], Contact) |
| 102 | + end |
| 103 | + |
| 104 | + # Updates an existing contact |
| 105 | + # @param contact_id [String] The contact ID |
| 106 | + # @param request [ContactUpdateRequest, Hash] The contact update request object or a hash with the same attributes |
| 107 | + # @return [ContactUpdateResponse] Response containing the action performed and contact data |
| 108 | + # @raise [Mailtrap::Error] If the API request fails with a client or server error |
| 109 | + # @raise [Mailtrap::AuthorizationError] If the API key is invalid |
| 110 | + # @raise [Mailtrap::RejectionError] If the server refuses to process the request |
| 111 | + # @raise [Mailtrap::RateLimitError] If too many requests are made |
| 112 | + def update(contact_id, request) |
| 113 | + response = @client.patch( |
| 114 | + "#{base_path}/#{contact_id}", |
| 115 | + { contact: prepare_request(request, ContactUpdateRequest) } |
| 116 | + ) |
| 117 | + build_entity(response, ContactUpdateResponse) |
| 118 | + end |
| 119 | + |
| 120 | + # Deletes a contact |
| 121 | + # @param contact_id [String] The contact ID |
| 122 | + # @return nil |
| 123 | + # @raise [Mailtrap::Error] If the API request fails with a client or server error |
| 124 | + # @raise [Mailtrap::AuthorizationError] If the API key is invalid |
| 125 | + # @raise [Mailtrap::RejectionError] If the server refuses to process the request |
| 126 | + # @raise [Mailtrap::RateLimitError] If too many requests are made |
| 127 | + def delete(contact_id) |
| 128 | + @client.delete("#{base_path}/#{contact_id}") |
| 129 | + end |
| 130 | + |
| 131 | + private |
| 132 | + |
| 133 | + def base_path |
| 134 | + "/api/accounts/#{@account_id}/contacts" |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments