yandex-direct-api 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3179fac1ae6dd3d8e997c28e6f8358b6ffa5089
4
- data.tar.gz: 3473b4882754ef0f0f8b3ce350514964599bf038
3
+ metadata.gz: 2b2481f400a3b99c634d239b68aae9b09e4eda09
4
+ data.tar.gz: b824938bb314043a51667375415423a4ccfe48bd
5
5
  SHA512:
6
- metadata.gz: 236a6e20d5f018ed8d650d90892bf335f43ea18111b8bd6361967e737a6b8dc0c458a5dc9a4e467cf26c7f6743921d42ad6b99928b761b395d763e8ddc1fea36
7
- data.tar.gz: 559ed3c881110c34bd0e9c207bea562206befaafe161c2b6805fcf3cbfed532481c6b44a5d6e4b7ccc7a7fd0f7d598f27eab572a0ede12328a201561d253d1ae
6
+ metadata.gz: 37f6f9d26ea3cf521d5ecb22d3a9e47b160d919fe3852d137a4ab4c3dc7201202d7f4c4ef7c51aedf9da69ce8ca83af84afc1ea3fed235960ebe7f966eb4c2e9
7
+ data.tar.gz: d709c58aaeb932b95967767a3992f033cd7491d7c678a50619f51833da0bec25776be9cbd2dc19f67bd88f9be0e0c144355f158b854d58613f3ad02a6be37391
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # yandex-direct-api
2
+ Интеграция с API Yandex Direct v.5
3
+
4
+ gem 'yandex-direct-api'
5
+
6
+ bundle
7
+
8
+ rake yandex_direct:install
9
+
10
+ Внесите изменения в config/yandex_direct_api.yml
data/lib/live/live.rb ADDED
@@ -0,0 +1,62 @@
1
+ module YandexDirect::Live
2
+ def self.configuration
3
+ YandexDirect.configuration
4
+ end
5
+
6
+ def self.request (method, params = {})
7
+ body = {
8
+ locale: configuration['locale'],
9
+ token: configuration['token'],
10
+ method: method,
11
+ param: params
12
+ }
13
+
14
+ url = URI((configuration['sandbox'] ? 'https://api-sandbox.direct.yandex.ru/live/v4/json/' : 'https://api.direct.yandex.ru/live/v4/json/'))
15
+
16
+ if configuration['verbose']
17
+ puts "\t\033[32mYandex.Direct:\033[0m #{method}(#{body[:param]})"
18
+ end
19
+
20
+ http = Net::HTTP.new(url.host, url.port)
21
+ http.use_ssl = true
22
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
23
+ response = http.post(url.path, JSON.generate(body))
24
+
25
+ raise YandexDirect::RuntimeError.new("#{response.code} - #{response.message}") unless response.code.to_i == 200
26
+
27
+ json = YandexDirect.parse_json(response.body)
28
+
29
+ if json.has_key?('error_code') and json.has_key?('error_str')
30
+ code = json['error_code'].to_i
31
+ error = json['error_detail'].length > 0 ? json['error_detail'] : json['error_str']
32
+ raise YandexDirect::RuntimeError.new "#{code} - #{error}"
33
+ end
34
+
35
+ return json['data']
36
+ end
37
+
38
+ def self.upload_image (url, name)
39
+ result = YandexDirect::Live.request('AdImage', {'Action': 'Upload', 'AdImageURLData': [{'Login': configuration['login'], 'URL': url, 'Name': name}]})
40
+ result['ActionsResult'].first['AdImageUploadTaskID']
41
+ end
42
+
43
+ def self.get_image_hash(upload_task_ids)
44
+ result = YandexDirect::Live.request('AdImage', {'Action': 'CheckUploadStatus', 'SelectionCriteria': {'AdImageUploadTaskIDS': upload_task_ids}})
45
+ result["AdImageUploads"]
46
+ end
47
+
48
+ def self.get_uploaded_image_hashes(params)
49
+ upload_task_ids = params.map{|p| p[:image_upload_task_id]}
50
+ if upload_task_ids.any?
51
+ image_hashes = YandexDirect::Live.get_image_hash(upload_task_ids)
52
+ associations = []
53
+ image_hashes.each do |result|
54
+ unless result['Status'] == 'Pending'
55
+ add = params.find{|p| p.image_upload_task_id == result['AdImageUploadTaskID']}
56
+ associations.push({add_id: add.add_id, ad_image_hash: result['AdImageHash']}) unless result['Status'] == 'Error'
57
+ end
58
+ end
59
+ associations
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,80 @@
1
+ class YandexDirect::AdGroup
2
+ SERVICE = 'adgroups'
3
+ attr_accessor :name, :campaign_id, :id, :status, :region_ids, :negative_keywords, :tracking_params
4
+
5
+ def initialize(params)
6
+ @name = params[:name]
7
+ @id = params[:id]
8
+ @status = params[:status]
9
+ @campaign_id = params[:campaign_id]
10
+ @region_ids = params[:region_ids]
11
+ @negative_keywords = {"Items": params[:negative_keywords]} if params[:negative_keywords].present?
12
+ @tracking_params = params[:tracking_params]
13
+ end
14
+
15
+ def self.list(params)
16
+ selection_criteria = {"Types": ["TEXT_AD_GROUP"]}
17
+ selection_criteria["CampaignIds"] = params[:campaign_ids] if params[:campaign_ids].present?
18
+ selection_criteria["Ids"] = params[:ids] if params[:ids].present?
19
+ ad_groups = YandexDirect.request(SERVICE, 'get', {
20
+ "SelectionCriteria": selection_criteria,
21
+ "FieldNames": ['Id', 'Name', 'CampaignId', 'Status', 'RegionIds', 'TrackingParams', 'NegativeKeywords']
22
+ })["AdGroups"]
23
+ (ad_groups || []).map{|c| new({ name: c["Name"], id: c["Id"], status: c["Status"], campaign_id: c["CampaignId"],
24
+ region_ids: c["RegionIds"], tracking_params: c["TrackingParams"], negative_keywords: c["NegativeKeywords"]})}
25
+ end
26
+
27
+ def self.add(params)
28
+ if params.kind_of?(Array)
29
+ batch_add(params)
30
+ else
31
+ params.id = YandexDirect.request(SERVICE, 'add', {"AdGroups": [params.parameters]})["AddResults"].first["Id"]
32
+ params
33
+ end
34
+ end
35
+
36
+ def self.update(params)
37
+ if params.kind_of?(Array)
38
+ batch_update(params)
39
+ else
40
+ params.id = YandexDirect.request(SERVICE, 'update', {"AdGroups": [params.update_parameters]})["UpdateResults"].first["Id"]
41
+ params
42
+ end
43
+ end
44
+
45
+ def parameters
46
+ hash = {"Name": @name,
47
+ "CampaignId": @campaign_id,
48
+ "RegionIds": @region_ids || [0],
49
+ "TrackingParams": @tracking_params
50
+ }
51
+ hash["NegativeKeywords"] = @negative_keywords if @negative_keywords.present?
52
+ hash
53
+ end
54
+
55
+ def update_parameters
56
+ hash = {"Name": @name,
57
+ "RegionIds": @region_ids || [0],
58
+ "TrackingParams": @tracking_params,
59
+ "Id": @id
60
+ }
61
+ hash["NegativeKeywords"] = @negative_keywords if @negative_keywords.present?
62
+ hash
63
+ end
64
+
65
+ private
66
+
67
+ def self.batch_update(ad_groups)
68
+ params = ad_groups.map(&:update_parameters)
69
+ params.each_slice(100) do |add_parameters|
70
+ YandexDirect.request(SERVICE, 'update', {"AdGroups": add_parameters.compact})["UpdateResults"]
71
+ end
72
+ end
73
+
74
+ def self.batch_add(ad_groups)
75
+ params = ad_groups.map(&:parameters)
76
+ params.each_slice(100) do |add_parameters|
77
+ YandexDirect.request(SERVICE, 'add', {"AdGroups": add_parameters.compact})["AddResults"]
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,162 @@
1
+ class YandexDirect::Add
2
+ SERVICE = 'ads'
3
+ attr_accessor :ad_group_id, :campaign_id, :id, :status, :state, :type, :status_clarification, :text, :title, :href,
4
+ :display_url_path, :ad_image_hash, :extension_ids, :mobile, :v_card_id, :sitelink_set_id
5
+
6
+ def initialize(params)
7
+ @ad_group_id = params[:ad_group_id]
8
+ @id = params[:id]
9
+ @status = params[:status]
10
+ @campaign_id = params[:campaign_id]
11
+ @negative_keywords = params[:negative_keywords]
12
+ @utm_tags = params[:utm_tags]
13
+ @state = params[:state]
14
+ @type = params[:type] || "TEXT_AD"
15
+ @status_clarification = params[:status_clarification]
16
+ @text = params[:text]
17
+ @title = params[:title]
18
+ @href = params[:href]
19
+ @display_url_path = params[:display_url_path]
20
+ @ad_image_hash = params[:ad_image_hash]
21
+ @extension_ids = params[:extension_ids]
22
+ @mobile = params[:mobile]
23
+ @v_card_id = params[:v_card_id]
24
+ @extension_ids = params[:extension_ids]
25
+ @mobile = params[:mobile]
26
+ @sitelink_set_id = params[:sitelink_set_id]
27
+ end
28
+
29
+ def self.list(params)
30
+ selection_criteria = {"Types": ["TEXT_AD"]}
31
+ selection_criteria["CampaignIds"] = params[:campaign_ids] if params[:campaign_ids].present?
32
+ selection_criteria["AdGroupIds"] = params[:ad_group_ids] if params[:ad_group_ids].present?
33
+ selection_criteria["Ids"] = params[:ids] if params[:ids].present?
34
+ ads = YandexDirect.request(SERVICE, 'get', {
35
+ "SelectionCriteria": selection_criteria,
36
+ "FieldNames": ['AdGroupId', 'CampaignId', 'State', 'Status', 'StatusClarification', 'Type', 'Id'],
37
+ "TextAdFieldNames": ['SitelinkSetId', 'Text', 'Title', 'Href']
38
+ })["Ads"]
39
+ (ads || []).map{|c| new({ ad_group_id: c["AdGroupId"], id: c["Id"], status: c["Status"], campaign_id: c["CampaignId"],
40
+ state: c["State"], type: c["Type"], status_clarification: c["StatusClarification"],
41
+ sitelink_set_id: c["TextAd"]["SitelinkSetId"], text: c["TextAd"]["Text"], title: c["TextAd"]["Title"], href: c["TextAd"]["Href"]})}
42
+ end
43
+
44
+ def self.add(params)
45
+ if params.kind_of?(Array)
46
+ batch_add(params)
47
+ else
48
+ special_parameters = params.new_text_add_parameters if params.type == "TEXT_AD"
49
+ params.id = YandexDirect.request(SERVICE, 'add', {"Ads": [params.add_parameters(special_parameters)]})["AddResults"].first["Id"]
50
+ params
51
+ end
52
+ end
53
+
54
+ def self.update(params)
55
+ if params.kind_of?(Array)
56
+ batch_update(params)
57
+ else
58
+ special_parameters = params.text_add_parameters if params.type == "TEXT_AD"
59
+ params.id = YandexDirect.request(SERVICE, 'update', {"Ads": [params.update_parameters(special_parameters)]})["UpdateResults"].first["Id"]
60
+ params
61
+ end
62
+ end
63
+
64
+ def self.unarchive(ids)
65
+ ids = [ids] unless params.kind_of?(Array)
66
+ action('unarchieve', ids)
67
+ end
68
+
69
+ def self.archive(ids)
70
+ ids = [ids] unless params.kind_of?(Array)
71
+ action('archieve', ids)
72
+ end
73
+
74
+ def self.stop(ids)
75
+ ids = [ids] unless params.kind_of?(Array)
76
+ action('suspend', ids)
77
+ end
78
+
79
+ def self.resume(ids)
80
+ ids = [ids] unless params.kind_of?(Array)
81
+ action('resume', ids)
82
+ end
83
+
84
+ def self.moderate(ids)
85
+ ids = [ids] unless params.kind_of?(Array)
86
+ action('moderate', ids)
87
+ end
88
+
89
+ def self.delete(ids)
90
+ ids = [ids] unless params.kind_of?(Array)
91
+ action('delete', ids)
92
+ end
93
+
94
+ def add_parameters(add_type)
95
+ hash = {"AdGroupId": @ad_group_id}
96
+ hash[add_type.first] = add_type.last
97
+ hash
98
+ end
99
+
100
+ def update_parameters(add_type)
101
+ hash = {"Id": @id}
102
+ hash[add_type.first] = add_type.last
103
+ hash
104
+ end
105
+
106
+ def new_text_add_parameters
107
+ hash = {"Text": @text,
108
+ "Title": @title,
109
+ "Href": @href,
110
+ "Mobile": @mobile || "NO",
111
+ "AdExtensionIds": @extension_ids || []
112
+ }
113
+ hash["DisplayUrlPath"] = @display_url_path if @display_url_path.present?
114
+ hash["VCardId"] = @v_card_id if @v_card_id.present?
115
+ hash["AdImageHash"] = @ad_image_hash if @ad_image_hash.present?
116
+ hash["SitelinkSetId"] = @sitelink_set_id if @sitelink_set_id.present?
117
+ ["TextAd", hash]
118
+ end
119
+
120
+ def text_add_parameters
121
+ hash = {"Text": @text,
122
+ "Title": @title,
123
+ "Href": @href,
124
+ }
125
+ hash["DisplayUrlPath"] = @display_url_path if @display_url_path.present?
126
+ hash["AdImageHash"] = @ad_image_hash if @ad_image_hash.present?
127
+ hash["CalloutSetting"]["AdExtensions"] = @extension_ids.map{|e| {"AdExtensionId": e, "Operation": "SET"}} if @extension_ids.present?
128
+ hash["VCardId"] = @v_card_id if @v_card_id.present?
129
+ hash["SitelinkSetId"] = @sitelink_set_id if @sitelink_set_id.present?
130
+ ["TextAd", hash]
131
+ end
132
+
133
+ private
134
+
135
+ def self.batch_update(adds)
136
+ params = adds.map do |add|
137
+ special_parameters = add.text_add_parameters if add.type == "TEXT_AD"
138
+ add.update_parameters(special_parameters)
139
+ end
140
+ params.each_slice(100) do |add_parameters|
141
+ YandexDirect.request(SERVICE, 'update', {"Ads": add_parameters.compact})["UpdateResults"]
142
+ end
143
+ end
144
+
145
+ def self.batch_add(adds)
146
+ params = adds.map do |add|
147
+ special_parameters = add.new_text_add_parameters if add.type == "TEXT_AD"
148
+ add.add_parameters(special_parameters)
149
+ end
150
+ params.each_slice(100) do |add_parameters|
151
+ YandexDirect.request(SERVICE, 'add', {"Ads": add_parameters.compact})["AddResults"]
152
+ end
153
+ end
154
+
155
+ def action(action_name, params)
156
+ selection_criteria = {}
157
+ selection_criteria["CampaignIds"] = params[:campaign_ids] if params[:campaign_ids].present?
158
+ selection_criteria["AdGroupIds"] = params[:ad_group_ids] if params[:ad_group_ids].present?
159
+ selection_criteria["Ids"] = params[:ids] if params[:ids].present?
160
+ YandexDirect.request(SERVICE, 'action_name', {"SelectionCriteria": selection_criteria}) if @id.present? && @id != 0
161
+ end
162
+ end
@@ -0,0 +1,27 @@
1
+ class YandexDirect::Bid
2
+ SERVICE = 'bids'
3
+
4
+ def self.list(params)
5
+ selection_criteria = {}
6
+ selection_criteria["CampaignIds"] = params[:campaign_ids] if params[:campaign_ids].present?
7
+ selection_criteria["Ids"] = params[:ids] if params[:ids].present?
8
+ selection_criteria["AdGroupIds"] = params[:ad_group_ids] if params[:ad_group_ids].present?
9
+ selection_criteria["KeywordIds"] = params[:keyword_ids] if params[:keyword_ids].present?
10
+ YandexDirect.request(SERVICE, 'get', {
11
+ "SelectionCriteria": selection_criteria,
12
+ "FieldNames": ["KeywordId", "AdGroupId", "CampaignId", "Bid", "ContextBid", "StrategyPriority", "CompetitorsBids", "SearchPrices", "ContextCoverage", "MinSearchPrice", "CurrentSearchPrice", "AuctionBids"]
13
+ })["Bids"] || []
14
+ end
15
+
16
+ def self.set(bids)
17
+ bids.map! do |bid|
18
+ params = {"Bid": bid[:bid] * 1000000, "StrategyPriority": bid[:StrategyPriority] || "NORMAL"}
19
+ params["CampaignId"] = bid[:campaign_id] if bid[:campaign_id].present?
20
+ params["AdGroupId"] = bid[:ad_group_id] if bid[:ad_group_id].present?
21
+ params["KeywordId"] = bid[:keyword_id] if bid[:keyword_id].present?
22
+ params["ContextBid"] = bid[:context_bid] * 1000000 if bid[:context_bid].present?
23
+ params
24
+ end
25
+ YandexDirect.request(SERVICE, 'set', {"Bids": bids})["SetResults"]
26
+ end
27
+ end
@@ -0,0 +1,140 @@
1
+ class YandexDirect::Campaign
2
+ SERVICE = 'campaigns'
3
+ attr_accessor :name, :start_date, :time_zone, :id, :status, :state, :negative_keywords, :email, :owner_name,
4
+ :search_strategy, :network_strategy, :limit_percent, :bid_percent, :end_date, :daily_budget_amount,
5
+ :daily_budget_mode, :blocked_ips, :excluded_sites, :type, :target_hours
6
+
7
+ def initialize(params)
8
+ @name = params[:name]
9
+ @id = params[:id]
10
+ @status = params[:status]
11
+ @state = params[:state]
12
+ @start_date = params[:start_date]
13
+ @time_zone = params[:time_zone]
14
+ @negative_keywords = {"Items": params[:negative_keywords]} if params[:negative_keywords].present?
15
+ @email = params[:email]
16
+ @owner_name = params[:owner_name]
17
+ @limit_percent = params[:limit_percent]
18
+ @bid_percent = params[:bid_percent]
19
+ @end_date = params[:end_date]
20
+ @daily_budget_amount = params[:daily_budget_amount]
21
+ @daily_budget_mode = params[:daily_budget_mode]
22
+ @blocked_ips = params[:blocked_ips]
23
+ @excluded_sites = params[:excluded_sites]
24
+ @search_strategy = params[:search_strategy]
25
+ @network_strategy = params[:network_strategy]
26
+ @target_hours = params[:target_hours]
27
+ @type == params[:type] || "TEXT_CAMPAIGN"
28
+ end
29
+
30
+ def self.list
31
+ YandexDirect.request(SERVICE, 'get', {
32
+ "SelectionCriteria": {
33
+ "Types": ["TEXT_CAMPAIGN"],
34
+ "States": ["OFF", "ON"],
35
+ "Statuses": ["ACCEPTED", "DRAFT", "MODERATION"]
36
+ },
37
+ "FieldNames": ['Id', 'Name', 'State', 'Status', 'TimeTargeting', 'NegativeKeywords', 'ClientInfo', 'Type'],
38
+ "TextCampaignFieldNames": ["BiddingStrategy"]
39
+ })["Campaigns"].map{|c| new({ name: c["Name"], id: c["Id"], status: c["Status"], state: c["State"], type: c["Type"],
40
+ target_hours: c["TimeTargeting"]["Schedule"]["Items"], owner_name: c["ClientInfo"],
41
+ negative_keywords: (c["NegativeKeywords"].present? ? c["NegativeKeywords"]["Items"] : nil), search_strategy: c["TextCampaign"]["BiddingStrategy"]["Search"]["BiddingStrategyType"],
42
+ network_strategy: c["TextCampaign"]["BiddingStrategy"]["Network"]["BiddingStrategyType"]})}
43
+ end
44
+
45
+ def self.add(params)
46
+ if params.kind_of?(Array)
47
+ batch_add(params)
48
+ else
49
+ special_parameters = params.text_campaign_parameters if params.type.blank? || params.type == "TEXT_CAMPAIGN"
50
+ params.id = YandexDirect.request(SERVICE, 'add', {"Campaigns": [params.parameters(special_parameters)]})["AddResults"].first["Id"]
51
+ params
52
+ end
53
+ end
54
+
55
+ def self.update(params)
56
+ if params.kind_of?(Array)
57
+ batch_update(params)
58
+ else
59
+ special_parameters = params.text_campaign_parameters if params.type.blank? || params.type == "TEXT_CAMPAIGN"
60
+ params.id = YandexDirect.request(SERVICE, 'update', {"Campaigns": [params.parameters(special_parameters)]})["UpdateResults"].first["Id"]
61
+ params
62
+ end
63
+ end
64
+
65
+ def parameters(campaign_type)
66
+ hash = {"Name": @name,
67
+ "ClientInfo": @owner_name,
68
+ "Notification": {
69
+ "EmailSettings": {
70
+ "Email": @email,
71
+ "SendAccountNews": "YES",
72
+ "SendWarnings": "YES"
73
+ }
74
+ },
75
+ "TimeTargeting": {
76
+ "Schedule": {
77
+ "Items": @target_hours
78
+ },
79
+ "ConsiderWorkingWeekends": "YES",
80
+ "HolidaysSchedule": {
81
+ "SuspendOnHolidays": "NO",
82
+ "BidPercent": 100,
83
+ "StartHour": 0,
84
+ "EndHour": 24
85
+ }
86
+ },
87
+ "TimeZone": @time_zone
88
+ }
89
+ hash["NegativeKeywords"] = @negative_keywords if @negative_keywords.present?
90
+ hash["DailyBudget"] = {"Amount": @daily_budget_amount, "Mode": @daily_budget_mode} if @daily_budget_mode.present? && @daily_budget_amount.present?
91
+ hash["BlockedIps"] = {"Items": @blocked_ips} if @blocked_ips.present?
92
+ hash["ExcludedSites"] = {"Items": @excluded_sites} if @excluded_sites.present?
93
+ hash["Id"] = @id if @id.present? && @id != 0
94
+ hash["StartDate"] = @start_date if @start_date.present?
95
+ hash["EndDate"] = @end_date if @end_date.present?
96
+ hash[campaign_type.first] = campaign_type.last
97
+ hash
98
+ end
99
+
100
+ def text_campaign_parameters
101
+ ["TextCampaign", {"BiddingStrategy": {
102
+ "Search": {
103
+ "BiddingStrategyType": @search_strategy
104
+ },
105
+ "Network": {
106
+ "BiddingStrategyType": @network_strategy,
107
+ "NetworkDefault": {
108
+ "LimitPercent": @limit_percent,
109
+ "BidPercent": @bid_percent
110
+ }
111
+ }
112
+ }
113
+ }
114
+ ]
115
+ end
116
+
117
+ private
118
+
119
+ def self.batch_add(campaigns)
120
+ params = []
121
+ campaigns.each do |campaign|
122
+ special_parameters = campaign.text_campaign_parameters if campaign.type.blank? || campaign.type == "TEXT_CAMPAIGN"
123
+ params.push(campaign.parameters(special_parameters))
124
+ end
125
+ params.each_slice(10) do |add_parameters|
126
+ YandexDirect.request(SERVICE, 'add', {"Campaigns": add_parameters.compact})["AddResults"].map{|r| r["Id"]}
127
+ end
128
+ end
129
+
130
+ def self.batch_update(campaigns)
131
+ params = []
132
+ campaigns.each do |campaign|
133
+ special_parameters = campaign.text_campaign_parameters if campaign.type.blank? || campaign.type == "TEXT_CAMPAIGN"
134
+ params.push(campaign.parameters(special_parameters))
135
+ end
136
+ params.each_slice(10) do |add_parameters|
137
+ YandexDirect.request(SERVICE, 'update', {"Campaigns": add_parameters.compact})["UpdateResults"].map{|r| r["Id"]}
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,7 @@
1
+ module YandexDirect::Dictionaries
2
+ SERVICE = 'dictionaries'
3
+
4
+ def self.get_regions
5
+ YandexDirect.request(SERVICE, 'get', {"DictionaryNames": ["GeoRegions"]})["GeoRegions"]
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ class YandexDirect::Keyword
2
+ SERVICE = 'keywords'
3
+
4
+ def self.add(params)
5
+ words = params[:words]
6
+ ad_group_id = params[:ad_group_id]
7
+ keywords = words.map do |word|
8
+ { "Keyword": word[:word],
9
+ "AdGroupId": ad_group_id,
10
+ "Bid": word[:bid],
11
+ "ContextBid": word[:context_bid],
12
+ "StrategyPriority": word[:strategy_priority] || "NORMAL"
13
+ }
14
+ end
15
+ YandexDirect.request(SERVICE, 'add', {"Keywords": keywords})["AddResults"]
16
+ end
17
+
18
+ def self.list(params)
19
+ selection_criteria = {"States": ["ON"], "Statuses": ["ACCEPTED", "DRAFT"]}
20
+ selection_criteria["CampaignIds"] = params[:campaign_ids] if params[:campaign_ids].present?
21
+ selection_criteria["Ids"] = params[:ids] if params[:ids].present?
22
+ selection_criteria["AdGroupIds"] = params[:ad_group_ids] if params[:ad_group_ids].present?
23
+ YandexDirect.request(SERVICE, 'get', {
24
+ "SelectionCriteria": selection_criteria,
25
+ "FieldNames": ["Id", "Keyword", "State", "Status", "AdGroupId", "CampaignId", "Bid", "ContextBid"]
26
+ })["Keywords"] || []
27
+ end
28
+
29
+ def self.update(keywords)
30
+ keywords.map! do |word|
31
+ { "Id": word[:id],
32
+ "Keyword": word[:word]
33
+ }
34
+ end
35
+ YandexDirect.request(SERVICE, 'update', {"Keywords": keywords})["AddResults"]
36
+ end
37
+ end
@@ -0,0 +1,20 @@
1
+ class YandexDirect::Sitelink
2
+ SERVICE = 'sitelinks'
3
+
4
+ def self.add_set(params)
5
+ sitelinks = params.map do |link|
6
+ hash = {"Title": link[:title], "Href": link[:href]}
7
+ hash["Description"] = link[:description] if link[:description].present?
8
+ hash
9
+ end
10
+ YandexDirect.request(SERVICE, 'add', {"SitelinksSets": [{"Sitelinks": sitelinks}]})["AddResults"].first["Id"]
11
+ end
12
+
13
+ def self.get(ids)
14
+ YandexDirect.request(SERVICE, 'get', {"SelectionCriteria": {"Ids": ids}, "FieldNames": ["Id", "Sitelinks"]})["SitelinksSets"] || []
15
+ end
16
+
17
+ def self.delete(ids)
18
+ YandexDirect.request(SERVICE, 'delete', {"SelectionCriteria": {"Ids": ids}})
19
+ end
20
+ end
@@ -0,0 +1,33 @@
1
+ class YandexDirect::VCard
2
+ SERVICE = 'vcards'
3
+
4
+ def self.get(ids = nil)
5
+ params = {"FieldNames": ["Id", "Country", "City", "Street", "House", "Building", "Apartment", "CompanyName", "ExtraMessage", "ContactPerson", "ContactEmail", "MetroStationId", "CampaignId", "Ogrn", "WorkTime", "InstantMessenger", "Phone", "PointOnMap"]}
6
+ params["SelectionCriteria"] = {"Ids": ids} if ids.present?
7
+ YandexDirect.request(SERVICE, 'get', params)["VCards"] || []
8
+ end
9
+
10
+ def self.add(params)
11
+ vcard = { "CampaignId": params[:campaign_id],
12
+ "Country": params[:country],
13
+ "City": params[:city],
14
+ "CompanyName": params[:company_name],
15
+ "WorkTime": params[:work_time],
16
+ "Phone": {
17
+ "CountryCode": params[:country_code],
18
+ "CityCode": params[:city_code],
19
+ "PhoneNumber": params[:phone_number],
20
+ "Extension": params[:phone_extension],
21
+ }
22
+ }
23
+ %w(Street House Building Apartment ExtraMessage Ogrn ContactEmail MetroStationId ContactPerson).each do |key|
24
+ vcard[key] = params[key.underscore.to_sym] if params[key.underscore.to_sym].present?
25
+ end
26
+ vcard["InstantMessenger"] = {"MessengerClient": params[:messenger_client], "MessengerLogin": params[:messenger_login]} if params[:messenger_client].present? && params[:messenger_login].present?
27
+ YandexDirect.request(SERVICE, 'add', {"VCards": [vcard]})["AddResults"].first["Id"]
28
+ end
29
+
30
+ def self.delete(ids)
31
+ YandexDirect.request(SERVICE, 'delete', {"SelectionCriteria": {"Ids": ids}})
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ require 'yaml'
2
+
3
+ namespace :yandex_direct_api do
4
+ desc "Create yandex-direct-api configuration file"
5
+ task install: :environment do
6
+ File.open(Rails.root.join('config', 'yandex_direct_api.yml'), 'w') do |config|
7
+ settings = {'development' => {'token' => '', 'login' => '', 'locale' => 'ru', 'verbose' => true, 'sandbox' => true},
8
+ 'test' => {'token' => '', 'login' => '', 'locale' => 'ru', 'verbose' => 'false', 'sandbox' => true},
9
+ 'production' => {'token' => '', 'login' => '', 'locale' => 'ru', 'verbose' => false, 'sandbox' => false}}
10
+ config.write(settings.to_yaml)
11
+ end
12
+ File.open(Rails.root.join('config', 'initializers', 'yandex_direct_api.rb'), 'w') do |config|
13
+ config.write "require 'yandex_direct_api'\nYandexDirect.load File.join(Rails.root, 'config','yandex_direct_api.yml'), Rails.env"
14
+ end
15
+ end
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex-direct-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - AliceO
@@ -16,7 +16,18 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - README.md
19
20
  - Rakefile
21
+ - lib/live/live.rb
22
+ - lib/services/ad_group.rb
23
+ - lib/services/add.rb
24
+ - lib/services/bid.rb
25
+ - lib/services/campaign.rb
26
+ - lib/services/dictionaries.rb
27
+ - lib/services/keyword.rb
28
+ - lib/services/sitelink.rb
29
+ - lib/services/vcard.rb
30
+ - lib/tasks/install.rake
20
31
  - lib/yandex_direct_api.rb
21
32
  homepage: https://github.com/aliceojjm/yandex-direct-api
22
33
  licenses: []