Class: MailchimpAPI::Client

Inherits:
Object
  • Object
show all
Includes:
APIMethods, BatchMethods
Defined in:
lib/mailchimp-api/client.rb,
lib/mailchimp-api/client/api_methods.rb,
lib/mailchimp-api/client/batch_methods.rb

Overview

Client class for interacting with the Mailchimp REST API

Defined Under Namespace

Modules: APIMethods, BatchMethods

Constant Summary collapse

API_VERSION =

Current API version

MailchimpAPI::API_VERSION

Instance Method Summary collapse

Methods included from BatchMethods

#batch, #batch_delete_request, #batch_get_request, #batch_patch_request, #batch_post_request, #batch_put_request

Methods included from APIMethods

#audience_interest_categories, #audience_interests, #audience_member_tags, #audience_members, #audience_merge_fields, #audience_segment_members, #audience_segments, #audience_webhooks, #audiences, #campaign_content, #campaign_folders, #campaigns

Constructor Details

#initialize(api_key:, http_opts: nil, retries: nil) ⇒ Client

Initializes a new Mailchimp API client

Examples:

client = MailchimpAPI::Client.new(api_key: "your-api-key-here")

Parameters:

  • api_key (String)

    Mailchimp API key in format <token>-<dc>

  • http_opts (Hash) (defaults to: nil)

    Optional HTTP client configuration

  • retries (Integer) (defaults to: nil)

    Number of retries for failed requests

Raises:

  • (ArgumentError)

    if api_key is invalid



20
21
22
23
24
25
26
27
28
# File 'lib/mailchimp-api/client.rb', line 20

def initialize(api_key:, http_opts: nil, retries: nil)
  raise ArgumentError, "Invalid api_key" unless /\w+-\w+/.match?(api_key) # <token>-<dc>
  dc = api_key.split("-", 2).last # initial api_key must have format

  @api_key = api_key
  @api_url = "https://#{dc}.api.mailchimp.com/#{API_VERSION}/"
  @authorization_token = "Bearer #{api_key}"
  @config = Config.new(http_opts: http_opts, retries: retries)
end

Instance Method Details

#delete(path, query: nil, body: nil, headers: nil) ⇒ Response

Sends a DELETE request to the Mailchimp API

Examples:

client.delete("/lists/123")

Parameters:

  • path (String)

    API endpoint path

  • query (Hash) (defaults to: nil)

    Optional query parameters

  • body (Hash) (defaults to: nil)

    Optional request body

  • headers (Hash) (defaults to: nil)

    Optional request headers

Returns:



86
87
88
# File 'lib/mailchimp-api/client.rb', line 86

def delete(path, query: nil, body: nil, headers: nil)
  execute(Net::HTTP::Delete, path, query: query, body: body, headers: headers)
end

#get(path, query: nil, body: nil, headers: nil) ⇒ Response

Sends a GET request to the Mailchimp API

Examples:

client.get("/lists")

Parameters:

  • path (String)

    API endpoint path

  • query (Hash) (defaults to: nil)

    Optional query parameters

  • body (Hash) (defaults to: nil)

    Optional request body

  • headers (Hash) (defaults to: nil)

    Optional request headers

Returns:



50
51
52
# File 'lib/mailchimp-api/client.rb', line 50

def get(path, query: nil, body: nil, headers: nil)
  execute(Net::HTTP::Get, path, query: query, body: body, headers: headers)
end

#patch(path, query: nil, body: nil, headers: nil) ⇒ Response

Sends a PATCH request to the Mailchimp API

Examples:

client.patch("/lists/123", body: { name: "Updated List" })

Parameters:

  • path (String)

    API endpoint path

  • query (Hash) (defaults to: nil)

    Optional query parameters

  • body (Hash) (defaults to: nil)

    Optional request body

  • headers (Hash) (defaults to: nil)

    Optional request headers

Returns:



62
63
64
# File 'lib/mailchimp-api/client.rb', line 62

def patch(path, query: nil, body: nil, headers: nil)
  execute(Net::HTTP::Patch, path, query: query, body: body, headers: headers)
end

#post(path, query: nil, body: nil, headers: nil) ⇒ Response

Sends a POST request to the Mailchimp API

Examples:

client.post("/lists", body: { name: "New List" })

Parameters:

  • path (String)

    API endpoint path

  • query (Hash) (defaults to: nil)

    Optional query parameters

  • body (Hash) (defaults to: nil)

    Optional request body

  • headers (Hash) (defaults to: nil)

    Optional request headers

Returns:



38
39
40
# File 'lib/mailchimp-api/client.rb', line 38

def post(path, query: nil, body: nil, headers: nil)
  execute(Net::HTTP::Post, path, query: query, body: body, headers: headers)
end

#put(path, query: nil, body: nil, headers: nil) ⇒ Response

Sends a PUT request to the Mailchimp API

Examples:

client.put("/lists/123", body: { name: "Updated List" })

Parameters:

  • path (String)

    API endpoint path

  • query (Hash) (defaults to: nil)

    Optional query parameters

  • body (Hash) (defaults to: nil)

    Optional request body

  • headers (Hash) (defaults to: nil)

    Optional request headers

Returns:



74
75
76
# File 'lib/mailchimp-api/client.rb', line 74

def put(path, query: nil, body: nil, headers: nil)
  execute(Net::HTTP::Put, path, query: query, body: body, headers: headers)
end