twitterdev/twitter-ruby-ads-sdk

View on GitHub
lib/twitter-ads/campaign/tracking_tags.rb

Summary

Maintainability
A
40 mins
Test Coverage
# frozen_string_literal: true
# Copyright (C) 2019 Twitter, Inc.

module TwitterAds
  class TrackingTag

    include TwitterAds::DSL
    include TwitterAds::Resource
    include TwitterAds::Persistence

    attr_reader :account

    property :id, read_only: true
    property :deleted, type: :bool, read_only: true
    property :created_at, type: :time, read_only: true
    property :updated_at, type: :time, read_only: true

    property :line_item_id
    property :tracking_tag_type
    property :tracking_tag_url

    # sdk only
    property :to_delete, type: :bool

    RESOURCE_COLLECTION  = "/#{TwitterAds::API_VERSION}/" \
                           'accounts/%{account_id}/tracking_tags' # @api private
    RESOURCE             = "/#{TwitterAds::API_VERSION}/" \
                           'accounts/%{account_id}/tracking_tags/%{id}' # @api private

    def initialize(account)
      @account = account
      self
    end

    # Creates a new Tracking Tag
    #
    # @param line_item_id [String] The line item id to create tags for.
    # @param tracking_tag_url [String] tracking tag URL.
    #
    # @return [self] Returns the instance refreshed from the API
    def create(line_item_id, tracking_tag_url)
      resource = self.class::RESOURCE_COLLECTION % { account_id: account.id }
      params = to_params.merge!(
        line_item_id: line_item_id,
        tracking_tag_url: tracking_tag_url,
        tracking_tag_type: 'IMPRESSION_TAG'
      )
      response = Request.new(account.client, :post, resource, params: params).perform
      from_response(response.body[:data])
    end

    class << self

      # Returns a Cursor instance for a given resource.
      #
      # @param account [Account] The Account object instance.
      # @param line_item_ids [String] A String or String array of Line Item IDs.
      # @param opts [Hash] An optional Hash of extended options.
      # @option opts [Boolean] :with_deleted Indicates if deleted items should be included.
      # @option opts [String] :sort_by The object param to sort the API response by.
      #
      # @return [Cursor] A Cusor object ready to iterate through the API response.
      #
      # @since 0.3.1
      # @see Cursor
      # @see https://dev.twitter.com/ads/basics/sorting Sorting
      def all(account, line_item_ids, opts = {})
        if !line_item_ids.empty?
          params = { line_item_ids: Array(line_item_ids).join(',') }.merge!(opts)
        end
        resource = RESOURCE_COLLECTION % { account_id: account.id }
        request = Request.new(account.client, :get, resource, params: params)
        Cursor.new(self, request, init_with: [account])
      end

      # Returns an object instance for a given resource.
      #
      # @param account [Account] The Account object instance.
      # @param id [String] The ID of the specific object to be loaded.
      # @param opts [Hash] An optional Hash of extended options.
      # @option opts [Boolean] :with_deleted Indicates if deleted items should be included.
      # @option opts [String] :sort_by The object param to sort the API response by.
      #
      # @return [self] The object instance for the specified resource.
      #
      # @since 0.3.1
      def load(account, id, opts = {})
        params   = { with_deleted: true }.merge!(opts)
        resource = RESOURCE % { account_id: account.id, id: id }
        response = Request.new(account.client, :get, resource, params: params).perform
        new(account).from_response(response.body[:data])
      end

    end

  end
end