Class: Pinterest::AdvertiserApiClient

Inherits:
BaseService show all
Defined in:
app/services/pinterest/advertiser_api_client.rb

Overview

Service object: advertiser API client.

Constant Summary collapse

PAGE_LIMIT =

Page size for the campaigns list endpoint. Pinterest caps at 250,
well above the realistic active-campaign count for one ad account
(ChatGPT and Google parallels run ~50 campaigns) so we expect a
single page in the common case.

250

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #process, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#list_campaigns(ad_account_id:, token:) ⇒ Array<Hash>

List all campaigns on the ad account. Walks the bookmark cursor
pagination until the API stops returning a bookmark and returns
the concatenated list.

Parameters:

  • ad_account_id (String)

    Pinterest ad account ID

  • token (String)

    Pinterest OAuth access token (bearer) with
    ads:read scope.

Returns:

  • (Array<Hash>)

    campaign objects per Pinterest's spec —
    id, name, status ("ACTIVE"|"PAUSED"|"ARCHIVED"),
    objective_type, daily_spend_cap, lifetime_spend_cap,
    created_time/updated_time (Unix timestamps), etc.

Raises:

  • (RuntimeError)

    on non-200 responses (Sidekiq retry surface).



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/services/pinterest/advertiser_api_client.rb', line 40

def list_campaigns(ad_account_id:, token:)
  campaigns = []
  bookmark  = nil

  loop do
    response = connection(token).get("ad_accounts/#{}/campaigns") do |req|
      req.params['page_size'] = PAGE_LIMIT
      req.params['bookmark']  = bookmark if bookmark
    end

    unless response.status == 200
      body = safe_parse(response.body)
      msg  = body['message'] || body['error'] || "HTTP #{response.status}"
      raise "Pinterest::AdvertiserApiClient: list_campaigns failed (HTTP #{response.status}): #{msg}"
    end

    body = safe_parse(response.body)
    page = Array(body['items'])
    campaigns.concat(page)

    next_bookmark = body['bookmark']
    break if next_bookmark.blank? || next_bookmark == bookmark # defensive — same cursor twice would loop forever

    bookmark = next_bookmark
  end

  campaigns
end