Module: YandexDirect

Defined in:
lib/yandex_direct_api.rb

Defined Under Namespace

Modules: Dictionaries, Live Classes: AdGroup, Add, Bid, Campaign, Keyword, RuntimeError, Sitelink, VCard

Class Method Summary collapse

Class Method Details

.configurationObject



15
16
17
18
19
20
21
22
# File 'lib/yandex_direct_api.rb', line 15

def self.configuration
  if defined? @environment
    raise RuntimeError.new("Configure yandex-direct-api for #{@environment} environment.") unless @configuration
  else
    raise RuntimeError.new('Configure yandex-direct-api for current environment.') unless @configuration
  end
  @configuration
end

.load(file, env = nil) ⇒ Object



32
33
34
35
36
# File 'lib/yandex_direct_api.rb', line 32

def self.load file, env = nil
  @environment = env.to_s if env
  config = YAML.load_file(file)
  @configuration = defined?(@environment) ? config[@environment] : config
end

.parse_json(json) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/yandex_direct_api.rb', line 24

def self.parse_json json
  begin
    return JSON.parse(json)
  rescue => e
    raise RuntimeError.new "#{e.message} in response."
  end
end

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

Raises:

  • (Yandex::API::RuntimeError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/yandex_direct_api.rb', line 38

def self.request(service, method, params = {})
  uri = URI(url + service)
  body = {
    method: method,
    params: params
  }
  if configuration['verbose']
    puts "\t\033[32mYandexDirect:\033[0m #{service}.#{method}(#{body[:params]})"
  end
  
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(uri.path)
  request['Authorization'] = "Bearer #{configuration['token']}"
  request['Accept-Language'] = configuration['locale']
  request['Client-Login'] = configuration['login'].downcase.gsub('.', '-')
  request.body = JSON.generate(body)

  response = http.start do |http| 
    http.request(request)
  end

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

  json = parse_json(response.body)
  messages = []
  if json.has_key?('error')
    messages.push("#{json['error']['error_code'].to_i} - #{json['error']['error_string']}: #{json['error']['error_detail']}") 
  else
    value = json['result'].present? ? (json['result'].values.first || json['result']) : json
    value = value.values if value.kind_of?(Hash)
    value.each do |item|
      item['Errors'].each do |error|
        messages.push("#{error['Code'].to_i} - #{error['Message']}: #{error['Details']}")
      end if item.has_key?('Errors')
    end if value.present?
  end
  raise RuntimeError.new(messages.join("\n")) if messages.any?
  return json['result']
end

.urlObject



11
12
13
# File 'lib/yandex_direct_api.rb', line 11

def self.url
  configuration['sandbox'] ? 'https://api-sandbox.direct.yandex.com/json/v5/' : 'https://api.direct.yandex.com/json/v5/'
end