Skip to content

Commit 1f7c59b

Browse files
committed
Add Contacts API functionality
1 parent 4471f3f commit 1f7c59b

File tree

10 files changed

+178
-198
lines changed

10 files changed

+178
-198
lines changed
File renamed without changes.

lib/mailtrap.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
require_relative 'mailtrap/action_mailer' if defined? ActionMailer
44
require_relative 'mailtrap/mail'
55
require_relative 'mailtrap/errors'
6-
require_relative 'mailtrap/api'
6+
require_relative 'mailtrap/base_api'
77
require_relative 'mailtrap/version'
88
require_relative 'mailtrap/email_templates_api'
9-
require_relative 'mailtrap/contact'
10-
require_relative 'mailtrap/contact_list'
9+
require_relative 'mailtrap/contacts_api'
10+
require_relative 'mailtrap/contact_lists_api'
1111

1212
module Mailtrap
1313
# @!macro api_errors

lib/mailtrap/api.rb

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/mailtrap/base_api.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
module Mailtrap
4+
class BaseAPI
5+
attr_reader :account_id, :client
6+
7+
# @param account_id [Integer] The account ID
8+
# @param client [Mailtrap::Client] The client instance
9+
# @raise [ArgumentError] If account_id is nil
10+
def initialize(account_id = ENV.fetch('MAILTRAP_ACCOUNT_ID'), client = Mailtrap::Client.new)
11+
raise ArgumentError, 'account_id is required' if account_id.nil?
12+
13+
@account_id = account_id
14+
@client = client
15+
end
16+
17+
private
18+
19+
def validate_options!(options, supported_options)
20+
invalid_options = options.keys - supported_options
21+
return if invalid_options.empty?
22+
23+
raise ArgumentError, "invalid options are given: #{invalid_options}, supported_options: #{supported_options}"
24+
end
25+
26+
def build_entity(options, response_class)
27+
response_class.new(options.slice(*response_class.members))
28+
end
29+
end
30+
end

lib/mailtrap/contact.rb

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,6 @@
11
# frozen_string_literal: true
22

33
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-
314
# Data Transfer Object for Contact
325
# @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/220a54e31e5ca-contact
336
# @attr_reader id [String] The contact ID
@@ -56,7 +29,7 @@ def to_h
5629
# Data Transfer Object for Contact Update Response
5730
# @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/16eab4fff9740-contact-update-response
5831
# @attr_reader action [String] The performed action (created/updated)
59-
# @attr_reader data [Contact] The contact data
32+
# @attr_reader data [Contact, Hash] The contact data
6033
ContactUpdateResponse = Struct.new(:action, :data, keyword_init: true) do
6134
def initialize(*)
6235
super
@@ -68,70 +41,4 @@ def to_h
6841
super.compact
6942
end
7043
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
13744
end

lib/mailtrap/contact_list.rb

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
# frozen_string_literal: true
22

33
module Mailtrap
4-
# Data Transfer Object for Contact List Create Request
5-
# @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/e3bba0bfa185e-create-contact-list
6-
# @attr_reader name [String] The name of the contact list (required)
7-
ContactListRequest = Struct.new(:name, keyword_init: true) do
8-
# @return [Hash] The contact list request attributes as a hash
9-
def to_h
10-
super.compact
11-
end
12-
end
13-
144
# Data Transfer Object for Contact List
155
# @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/6ec7a37234af2-contact-list
166
# @attr_reader id [Integer] The contact list ID
@@ -21,76 +11,4 @@ def to_h
2111
super.compact
2212
end
2313
end
24-
25-
class ContactListsAPI
26-
include Mailtrap::API
27-
28-
def initialize(account_id, client = Mailtrap::Client.new)
29-
@account_id = account_id
30-
@client = client
31-
end
32-
33-
# Retrieves a specific contact list
34-
# @param list_id [Integer] The contact list identifier
35-
# @return [ContactList] Contact list object
36-
# @raise [Mailtrap::Error] If the API request fails with a client or server error
37-
# @raise [Mailtrap::AuthorizationError] If the API key is invalid
38-
# @raise [Mailtrap::RejectionError] If the server refuses to process the request
39-
# @raise [Mailtrap::RateLimitError] If too many requests are made
40-
def get(list_id)
41-
response = @client.get("#{base_path}/#{list_id}")
42-
build_entity(response, ContactList)
43-
end
44-
45-
# Creates a new contact list
46-
# @param request [ContactListRequest, Hash] The contact list create request object or a hash
47-
# @return [ContactList] Created contact list object
48-
# @raise [Mailtrap::Error] If the API request fails with a client or server error
49-
# @raise [Mailtrap::AuthorizationError] If the API key is invalid
50-
# @raise [Mailtrap::RejectionError] If the server refuses to process the request
51-
# @raise [Mailtrap::RateLimitError] If too many requests are made
52-
def create(request)
53-
response = @client.post(base_path, prepare_request(request, ContactListRequest))
54-
build_entity(response, ContactList)
55-
end
56-
57-
# Updates an existing contact list
58-
# @param list_id [Integer] The contact list ID
59-
# @param request [ContactListRequest, Hash] The contact list update request object or a hash
60-
# @return [ContactList] Updated contact list object
61-
# @raise [Mailtrap::Error] If the API request fails with a client or server error
62-
# @raise [Mailtrap::AuthorizationError] If the API key is invalid
63-
# @raise [Mailtrap::RejectionError] If the server refuses to process the request
64-
# @raise [Mailtrap::RateLimitError] If too many requests are made
65-
def update(list_id, request)
66-
response = @client.patch(
67-
"#{base_path}/#{list_id}", prepare_request(request, ContactListRequest)
68-
)
69-
build_entity(response, ContactList)
70-
end
71-
72-
# Deletes a contact list
73-
# @param list_id [Integer] The contact list ID
74-
# @return nil
75-
# @raise [Mailtrap::Error] If the API request fails with a client or server error
76-
# @raise [Mailtrap::AuthorizationError] If the API key is invalid
77-
# @raise [Mailtrap::RejectionError] If the server refuses to process the request
78-
# @raise [Mailtrap::RateLimitError] If too many requests are made
79-
def delete(list_id)
80-
@client.delete("#{base_path}/#{list_id}")
81-
end
82-
83-
# Lists all contact lists for the account
84-
# @return [Array<ContactList>] Array of contact list objects
85-
def list
86-
response = @client.get(base_path)
87-
response.map { |list| build_entity(list, ContactList) }
88-
end
89-
90-
private
91-
92-
def base_path
93-
"/api/accounts/#{@account_id}/contacts/lists"
94-
end
95-
end
9614
end

lib/mailtrap/contact_lists_api.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'contact_list'
4+
5+
module Mailtrap
6+
class ContactListsAPI < BaseAPI
7+
SUPPORTED_OPTIONS = %i[name].freeze
8+
private_constant :SUPPORTED_OPTIONS
9+
10+
# Retrieves a specific contact list
11+
# @param list_id [Integer] The contact list identifier
12+
# @return [ContactList] Contact list object
13+
# @!macro api_errors
14+
def get(list_id)
15+
response = client.get("#{base_path}/#{list_id}")
16+
build_entity(response, ContactList)
17+
end
18+
19+
# Creates a new contact list
20+
# @param [Hash] options The parameters to create
21+
# @option options [String] :name The contact list name
22+
# @return [ContactList] Created contact list object
23+
# @!macro api_errors
24+
# @raise [ArgumentError] If invalid options are provided
25+
def create(options)
26+
validate_options!(options, SUPPORTED_OPTIONS)
27+
28+
response = client.post(base_path, options)
29+
build_entity(response, ContactList)
30+
end
31+
32+
# Updates an existing contact list
33+
# @param list_id [Integer] The contact list ID
34+
# @param [Hash] options The parameters to update
35+
# @option options [String] :name The contact list name
36+
# @return [ContactList] Updated contact list object
37+
# @!macro api_errors
38+
# @raise [ArgumentError] If invalid options are provided
39+
def update(list_id, options)
40+
validate_options!(options, SUPPORTED_OPTIONS)
41+
42+
response = client.patch(
43+
"#{base_path}/#{list_id}", options
44+
)
45+
build_entity(response, ContactList)
46+
end
47+
48+
# Deletes a contact list
49+
# @param list_id [Integer] The contact list ID
50+
# @return nil
51+
# @!macro api_errors
52+
def delete(list_id)
53+
client.delete("#{base_path}/#{list_id}")
54+
end
55+
56+
# Lists all contact lists for the account
57+
# @return [Array<ContactList>] Array of contact list objects
58+
# @!macro api_errors
59+
def list
60+
response = client.get(base_path)
61+
response.map { |list| build_entity(list, ContactList) }
62+
end
63+
64+
private
65+
66+
def base_path
67+
"/api/accounts/#{account_id}/contacts/lists"
68+
end
69+
end
70+
end

0 commit comments

Comments
 (0)