class TwitterAds::Creative::PromotedTweet

Constants

RESOURCE
RESOURCE_COLLECTION

Attributes

account[R]

Public Class Methods

new(account) click to toggle source
# File lib/twitter-ads/creative/promoted_tweet.rb, line 30
def initialize(account)
  @account = account
  self
end

Public Instance Methods

save() click to toggle source

Saves or updates the current object instance depending on the presence of `object.id`.

@example

object.save

@return [self] Returns the instance refreshed from the API.

Note: override to handle the inconsistency of the promoted tweet endpoint. (see REVAPI-5348)

@since 0.2.4

# File lib/twitter-ads/creative/promoted_tweet.rb, line 45
def save
  # manually check for missing params (due to API discrepancy)
  validate

  # convert to `tweet_ids` param
  params = to_params
  params[:tweet_ids] = params.delete(:tweet_id) if params.key?(:tweet_id)

  if @id
    raise TwitterAds::NotFound.new(nil, 'Method PUT not allowed.', 404)
  else
    resource = self.class::RESOURCE_COLLECTION % { account_id: account.id }
    response = Request.new(account.client, :post, resource, params: params).perform
    from_response(response.body[:data].first)
  end
end

Private Instance Methods

validate() click to toggle source
# File lib/twitter-ads/creative/promoted_tweet.rb, line 64
def validate
  details = []

  unless @line_item_id
    details << { code: 'MISSING_PARAMETER',
                 message: '"line_item_id" is a required parameter',
                 parameter: 'line_item_id' }
  end

  unless @tweet_id
    details << { code: 'MISSING_PARAMETER',
                 message: '"tweet_id" is a required parameter',
                 parameter: 'tweet_id' }
  end

  raise TwitterAds::ClientError.new(nil, details, 400) unless details.empty?
end