Class: Mailtrap::StatsAPI

Inherits:
Object
  • Object
show all
Includes:
BaseAPI
Defined in:
lib/mailtrap/stats_api.rb

Constant Summary collapse

ARRAY_FILTERS =
i[sending_domain_ids sending_streams categories email_service_providers].freeze
GROUP_KEYS =
{
  'domains' => :sending_domain_id,
  'categories' => :category,
  'email_service_providers' => :email_service_provider,
  'date' => :date
}.freeze

Instance Attribute Summary

Attributes included from BaseAPI

#account_id, #client

Instance Method Summary collapse

Methods included from BaseAPI

included, #initialize

Instance Method Details

#by_category(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, categories: nil, email_service_providers: nil) ⇒ Array<SendingStatGroup>

Get sending stats grouped by category

Parameters:

  • Start date for the stats period (required)

  • End date for the stats period (required)

  • (defaults to: nil)

    Filter by sending domain IDs

  • (defaults to: nil)

    Filter by sending streams

  • (defaults to: nil)

    Filter by categories

  • (defaults to: nil)

    Filter by email service providers

Returns:

  • Array of SendingStatGroup structs with category and stats

Raises:

  • If the API request fails with a client or server error

  • If the API key is invalid

  • If the server refuses to process the request

  • If too many requests are made



63
64
65
66
67
# File 'lib/mailtrap/stats_api.rb', line 63

def by_category(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, categories: nil, # rubocop:disable Metrics/ParameterLists
                email_service_providers: nil)
  grouped_stats('categories', start_date, end_date,
                { sending_domain_ids:, sending_streams:, categories:, email_service_providers: })
end

#by_date(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, categories: nil, email_service_providers: nil) ⇒ Array<SendingStatGroup>

Get sending stats grouped by date

Parameters:

  • Start date for the stats period (required)

  • End date for the stats period (required)

  • (defaults to: nil)

    Filter by sending domain IDs

  • (defaults to: nil)

    Filter by sending streams

  • (defaults to: nil)

    Filter by categories

  • (defaults to: nil)

    Filter by email service providers

Returns:

  • Array of SendingStatGroup structs with date and stats

Raises:

  • If the API request fails with a client or server error

  • If the API key is invalid

  • If the server refuses to process the request

  • If too many requests are made



93
94
95
96
97
# File 'lib/mailtrap/stats_api.rb', line 93

def by_date(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, categories: nil, # rubocop:disable Metrics/ParameterLists
            email_service_providers: nil)
  grouped_stats('date', start_date, end_date,
                { sending_domain_ids:, sending_streams:, categories:, email_service_providers: })
end

#by_domain(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, categories: nil, email_service_providers: nil) ⇒ Array<SendingStatGroup>

Get sending stats grouped by domain

Parameters:

  • Start date for the stats period (required)

  • End date for the stats period (required)

  • (defaults to: nil)

    Filter by sending domain IDs

  • (defaults to: nil)

    Filter by sending streams

  • (defaults to: nil)

    Filter by categories

  • (defaults to: nil)

    Filter by email service providers

Returns:

  • Array of SendingStatGroup structs with sending_domain_id and stats

Raises:

  • If the API request fails with a client or server error

  • If the API key is invalid

  • If the server refuses to process the request

  • If too many requests are made



48
49
50
51
52
# File 'lib/mailtrap/stats_api.rb', line 48

def by_domain(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, categories: nil, # rubocop:disable Metrics/ParameterLists
              email_service_providers: nil)
  grouped_stats('domains', start_date, end_date,
                { sending_domain_ids:, sending_streams:, categories:, email_service_providers: })
end

#by_email_service_provider(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, categories: nil, email_service_providers: nil) ⇒ Array<SendingStatGroup>

Get sending stats grouped by email service provider

Parameters:

  • Start date for the stats period (required)

  • End date for the stats period (required)

  • (defaults to: nil)

    Filter by sending domain IDs

  • (defaults to: nil)

    Filter by sending streams

  • (defaults to: nil)

    Filter by categories

  • (defaults to: nil)

    Filter by email service providers

Returns:

  • Array of SendingStatGroup structs with email_service_provider and stats

Raises:

  • If the API request fails with a client or server error

  • If the API key is invalid

  • If the server refuses to process the request

  • If too many requests are made



78
79
80
81
82
# File 'lib/mailtrap/stats_api.rb', line 78

def by_email_service_provider(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, # rubocop:disable Metrics/ParameterLists
                              categories: nil, email_service_providers: nil)
  grouped_stats('email_service_providers', start_date, end_date,
                { sending_domain_ids:, sending_streams:, categories:, email_service_providers: })
end

#get(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, categories: nil, email_service_providers: nil) ⇒ SendingStats

Get aggregated sending stats

Parameters:

  • Start date for the stats period (required)

  • End date for the stats period (required)

  • (defaults to: nil)

    Filter by sending domain IDs

  • (defaults to: nil)

    Filter by sending streams

  • (defaults to: nil)

    Filter by categories

  • (defaults to: nil)

    Filter by email service providers

Returns:

  • Aggregated sending stats

Raises:

  • If the API request fails with a client or server error

  • If the API key is invalid

  • If the server refuses to process the request

  • If too many requests are made



29
30
31
32
33
34
35
36
37
# File 'lib/mailtrap/stats_api.rb', line 29

def get(start_date:, end_date:, sending_domain_ids: nil, sending_streams: nil, categories: nil, # rubocop:disable Metrics/ParameterLists
        email_service_providers: nil)
  query_params = build_query_params(
    start_date, end_date,
    { sending_domain_ids:, sending_streams:, categories:, email_service_providers: }
  )
  response = client.get(base_path, query_params)
  build_entity(response, SendingStats)
end