Module: YandexDirect::Live

Defined in:
lib/live/live.rb

Class Method Summary collapse

Class Method Details

.configurationObject



2
3
4
# File 'lib/live/live.rb', line 2

def self.configuration
  YandexDirect.configuration
end

.get_image_hash(upload_task_ids) ⇒ Object



43
44
45
46
# File 'lib/live/live.rb', line 43

def self.get_image_hash(upload_task_ids)
  result = YandexDirect::Live.request('AdImage', {'Action': 'CheckUploadStatus', 'SelectionCriteria': {'AdImageUploadTaskIDS': upload_task_ids}})
  result["AdImageUploads"]
end

.get_uploaded_image_hashes(params) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/live/live.rb', line 48

def self.get_uploaded_image_hashes(params)
  upload_task_ids = params.map{|p| p[:image_upload_task_id]}
  if upload_task_ids.any?
    image_hashes = YandexDirect::Live.get_image_hash(upload_task_ids)
    associations = []
    image_hashes.each do |result|
      unless result['Status'] == 'Pending'
        add = params.find{|p| p.image_upload_task_id == result['AdImageUploadTaskID']}
        associations.push({add_id: add.add_id, ad_image_hash: result['AdImageHash']}) unless result['Status'] == 'Error'
      end
    end
    associations
  end
end

.request(method, params = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/live/live.rb', line 6

def self.request (method, params = {})
  body = {
    locale: configuration['locale'],
    token: configuration['token'],
    method: method,
    param: params
  }

  url = URI((configuration['sandbox'] ? 'https://api-sandbox.direct.yandex.ru/live/v4/json/' : 'https://api.direct.yandex.ru/live/v4/json/'))

  if configuration['verbose']
    puts "\t\033[32mYandex.Direct:\033[0m #{method}(#{body[:param]})"
  end

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.post(url.path, JSON.generate(body))

  raise YandexDirect::RuntimeError.new("#{response.code} - #{response.message}") unless response.code.to_i == 200

  json = YandexDirect.parse_json(response.body)
  
  if json.has_key?('error_code') and json.has_key?('error_str')
    code = json['error_code'].to_i
    error = json['error_detail'].length > 0 ? json['error_detail'] : json['error_str']
    raise YandexDirect::RuntimeError.new "#{code} - #{error}"
  end
  
  return json['data']
end

.upload_image(url, name) ⇒ Object



38
39
40
41
# File 'lib/live/live.rb', line 38

def self.upload_image (url, name)
  result = YandexDirect::Live.request('AdImage', {'Action': 'Upload', 'AdImageURLData': [{'Login': configuration['login'], 'URL': url, 'Name': name}]})
  result['ActionsResult'].first['AdImageUploadTaskID']
end