Class: YandexDirect::AdGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/services/ad_group.rb

Constant Summary collapse

SERVICE =
'adgroups'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ AdGroup

Returns a new instance of AdGroup.



5
6
7
8
9
10
11
12
13
# File 'lib/services/ad_group.rb', line 5

def initialize(params)
  @name = params[:name]
  @id = params[:id]
  @status = params[:status]
  @campaign_id = params[:campaign_id]
  @region_ids = params[:region_ids]
  @negative_keywords = {"Items": params[:negative_keywords]} if params[:negative_keywords].present?
  @tracking_params = params[:tracking_params]
end

Instance Attribute Details

#campaign_idObject

Returns the value of attribute campaign_id.



3
4
5
# File 'lib/services/ad_group.rb', line 3

def campaign_id
  @campaign_id
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/services/ad_group.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/services/ad_group.rb', line 3

def name
  @name
end

#negative_keywordsObject

Returns the value of attribute negative_keywords.



3
4
5
# File 'lib/services/ad_group.rb', line 3

def negative_keywords
  @negative_keywords
end

#region_idsObject

Returns the value of attribute region_ids.



3
4
5
# File 'lib/services/ad_group.rb', line 3

def region_ids
  @region_ids
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/services/ad_group.rb', line 3

def status
  @status
end

#tracking_paramsObject

Returns the value of attribute tracking_params.



3
4
5
# File 'lib/services/ad_group.rb', line 3

def tracking_params
  @tracking_params
end

Class Method Details

.add(params) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/services/ad_group.rb', line 27

def self.add(params)
  if params.kind_of?(Array)
    batch_add(params)
  else
    params.id = YandexDirect.request(SERVICE, 'add', {"AdGroups": [params.parameters]})["AddResults"].first["Id"]
    params
  end
end

.list(params) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/services/ad_group.rb', line 15

def self.list(params)
  selection_criteria = {"Types":  ["TEXT_AD_GROUP"]}
  selection_criteria["CampaignIds"] = params[:campaign_ids] if params[:campaign_ids].present?
  selection_criteria["Ids"] = params[:ids] if params[:ids].present?
  ad_groups = YandexDirect.request(SERVICE, 'get', { 
    "SelectionCriteria": selection_criteria,
    "FieldNames": ['Id', 'Name', 'CampaignId', 'Status', 'RegionIds', 'TrackingParams', 'NegativeKeywords']
  })["AdGroups"]
  (ad_groups || []).map{|c| new({ name: c["Name"], id: c["Id"], status: c["Status"], campaign_id: c["CampaignId"], 
                                  region_ids: c["RegionIds"], tracking_params: c["TrackingParams"], negative_keywords: c["NegativeKeywords"]})}
end

.update(params) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/services/ad_group.rb', line 36

def self.update(params)
  if params.kind_of?(Array)
    batch_update(params)
  else
    params.id = YandexDirect.request(SERVICE, 'update', {"AdGroups": [params.update_parameters]})["UpdateResults"].first["Id"]
    params
  end
end

Instance Method Details

#parametersObject



45
46
47
48
49
50
51
52
53
# File 'lib/services/ad_group.rb', line 45

def parameters
  hash = {"Name": @name,
          "CampaignId": @campaign_id,
          "RegionIds": @region_ids || [0],
          "TrackingParams": @tracking_params
        }
  hash["NegativeKeywords"] = @negative_keywords if @negative_keywords.present?
  hash
end

#update_parametersObject



55
56
57
58
59
60
61
62
63
# File 'lib/services/ad_group.rb', line 55

def update_parameters
  hash = {"Name": @name,
          "RegionIds": @region_ids || [0],
          "TrackingParams": @tracking_params,
          "Id": @id
        }
  hash["NegativeKeywords"] = @negative_keywords if @negative_keywords.present?
  hash
end