diff --git a/examples/contacts_api.rb b/examples/contacts_api.rb index b50f496..2753263 100644 --- a/examples/contacts_api.rb +++ b/examples/contacts_api.rb @@ -24,6 +24,10 @@ contact_lists.list # => [#] +# Filter contact lists by name (case-insensitive prefix match) +contact_lists.list(search: 'news') +# => [#] + # Update contact list contact_lists.update(list.id, name: 'Test List Updated') # => # diff --git a/lib/mailtrap/contact_lists_api.rb b/lib/mailtrap/contact_lists_api.rb index 90c48da..783ef89 100644 --- a/lib/mailtrap/contact_lists_api.rb +++ b/lib/mailtrap/contact_lists_api.rb @@ -49,10 +49,14 @@ def delete(list_id) end # Lists all contact lists for the account + # @param search [String] Filters contact lists by name (case-insensitive prefix match) # @return [Array] Array of contact list objects # @!macro api_errors - def list - base_list + def list(search: nil) + query_params = {} + query_params[:search] = search unless search.nil? + + base_list(query_params) end private diff --git a/spec/mailtrap/contact_lists_api_spec.rb b/spec/mailtrap/contact_lists_api_spec.rb index 3d5d44d..447acfb 100644 --- a/spec/mailtrap/contact_lists_api_spec.rb +++ b/spec/mailtrap/contact_lists_api_spec.rb @@ -25,6 +25,21 @@ expect(response.length).to eq(2) expect(response.first).to have_attributes(id: 1, name: 'List 1') end + + it 'filters contact lists by name' do + stub = stub_request(:get, "#{base_url}/contacts/lists") + .with(query: { search: 'news' }) + .to_return( + status: 200, + body: [{ 'id' => 2, 'name' => 'Newsletter' }].to_json, + headers: { 'Content-Type' => 'application/json' } + ) + + response = client.list(search: 'news') + expect(stub).to have_been_requested + expect(response.length).to eq(1) + expect(response.first).to have_attributes(id: 2, name: 'Newsletter') + end end describe '#get' do