|
| 1 | +# frozen_string_literal: true |
| 2 | +# Copyright (C) 2019 Twitter, Inc. |
| 3 | + |
| 4 | +module TwitterAds |
| 5 | + class TrackingTag |
| 6 | + |
| 7 | + include TwitterAds::DSL |
| 8 | + include TwitterAds::Resource |
| 9 | + include TwitterAds::Persistence |
| 10 | + |
| 11 | + attr_reader :account |
| 12 | + |
| 13 | + property :id, read_only: true |
| 14 | + property :deleted, type: :bool, read_only: true |
| 15 | + property :created_at, type: :time, read_only: true |
| 16 | + property :updated_at, type: :time, read_only: true |
| 17 | + |
| 18 | + property :line_item_id |
| 19 | + property :tracking_tag_type |
| 20 | + property :tracking_tag_url |
| 21 | + |
| 22 | + # sdk only |
| 23 | + property :to_delete, type: :bool |
| 24 | + |
| 25 | + RESOURCE_COLLECTION = "/#{TwitterAds::API_VERSION}/" \ |
| 26 | + 'accounts/%{account_id}/tracking_tags' # @api private |
| 27 | + RESOURCE = "/#{TwitterAds::API_VERSION}/" \ |
| 28 | + 'accounts/%{account_id}/tracking_tags/%{id}' # @api private |
| 29 | + |
| 30 | + def initialize(account) |
| 31 | + @account = account |
| 32 | + self |
| 33 | + end |
| 34 | + |
| 35 | + # Creates a new Tracking Tag |
| 36 | + # |
| 37 | + # @param line_item_id [String] The line item id to create tags for. |
| 38 | + # @param tracking_tag_url [String] tracking tag URL. |
| 39 | + # |
| 40 | + # @return [self] Returns the instance refreshed from the API |
| 41 | + def create(line_item_id, tracking_tag_url) |
| 42 | + resource = self.class::RESOURCE_COLLECTION % { account_id: account.id } |
| 43 | + params = to_params.merge!( |
| 44 | + line_item_id: line_item_id, |
| 45 | + tracking_tag_url: tracking_tag_url, |
| 46 | + tracking_tag_type: 'IMPRESSION_TAG' |
| 47 | + ) |
| 48 | + response = Request.new(account.client, :post, resource, params: params).perform |
| 49 | + from_response(response.body[:data]) |
| 50 | + end |
| 51 | + |
| 52 | + class << self |
| 53 | + |
| 54 | + # Returns a Cursor instance for a given resource. |
| 55 | + # |
| 56 | + # @param account [Account] The Account object instance. |
| 57 | + # @param line_item_ids [String] A String or String array of Line Item IDs. |
| 58 | + # @param opts [Hash] An optional Hash of extended options. |
| 59 | + # @option opts [Boolean] :with_deleted Indicates if deleted items should be included. |
| 60 | + # @option opts [String] :sort_by The object param to sort the API response by. |
| 61 | + # |
| 62 | + # @return [Cursor] A Cusor object ready to iterate through the API response. |
| 63 | + # |
| 64 | + # @since 0.3.1 |
| 65 | + # @see Cursor |
| 66 | + # @see https://dev.twitter.com/ads/basics/sorting Sorting |
| 67 | + def all(account, line_item_ids, opts = {}) |
| 68 | + if !line_item_ids.empty? |
| 69 | + params = { line_item_ids: Array(line_item_ids).join(',') }.merge!(opts) |
| 70 | + end |
| 71 | + resource = RESOURCE_COLLECTION % { account_id: account.id } |
| 72 | + request = Request.new(account.client, :get, resource, params: params) |
| 73 | + Cursor.new(self, request, init_with: [account]) |
| 74 | + end |
| 75 | + |
| 76 | + # Returns an object instance for a given resource. |
| 77 | + # |
| 78 | + # @param account [Account] The Account object instance. |
| 79 | + # @param id [String] The ID of the specific object to be loaded. |
| 80 | + # @param opts [Hash] An optional Hash of extended options. |
| 81 | + # @option opts [Boolean] :with_deleted Indicates if deleted items should be included. |
| 82 | + # @option opts [String] :sort_by The object param to sort the API response by. |
| 83 | + # |
| 84 | + # @return [self] The object instance for the specified resource. |
| 85 | + # |
| 86 | + # @since 0.3.1 |
| 87 | + def load(account, id, opts = {}) |
| 88 | + params = { with_deleted: true }.merge!(opts) |
| 89 | + resource = RESOURCE % { account_id: account.id, id: id } |
| 90 | + response = Request.new(account.client, :get, resource, params: params).perform |
| 91 | + new(account).from_response(response.body[:data]) |
| 92 | + end |
| 93 | + |
| 94 | + end |
| 95 | + |
| 96 | + end |
| 97 | +end |
0 commit comments