Module: Yandex::API::Direct

Defined in:
lib/yandex-api/direct.rb,
lib/yandex-api/direct/base.rb,
lib/yandex-api/direct/banner_info.rb,
lib/yandex-api/direct/campaign_info.rb

Defined Under Namespace

Classes: BannerInfo, BannerPhraseInfo, Base, CampaignInfo, CampaignStrategy, ContactInfo, EmailNotification, MapPoint, PhraseUserParams, Sitelink, SmsNotification, TimeTarget, TimeTargetItem

Constant Summary collapse

URL_API =
'https://api.direct.yandex.ru/v4/json/'
URL_API_SANDBOX =
'https://api-sandbox.direct.yandex.ru/v4/json/'

Class Method Summary collapse

Class Method Details

.configurationObject



13
14
15
16
17
18
19
20
# File 'lib/yandex-api/direct.rb', line 13

def self.configuration
  if defined? @environment
    raise RuntimeError.new("not configured Yandex.Direct for #{@environment} enviroment") unless @configuration
  else
    raise RuntimeError.new('not configured Yandex.Direct') unless @configuration
  end
  @configuration
end

.load(file, env = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/yandex-api/direct.rb', line 30

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

.parse_json(json) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/yandex-api/direct.rb', line 22

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

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



37
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
# File 'lib/yandex-api/direct.rb', line 37

def self.request method, params = {}

  body = {
    :locale => configuration['locale'],
    :token => configuration['token'],
    :method => method
  }

  if body[:method] == 'GetCampaignsList'
    body.merge!({:param => [configuration['login']]})
  else
    body.merge!({:param => params})
  end

  url = URI((configuration['sandbox'] ?  URL_API_SANDBOX : URL_API))

  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 Yandex::API::RuntimeError.new("#{response.code} - #{response.message}") unless response.code.to_i == 200

  json = Direct.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 RuntimeError.new "#{code} - #{error}"
  end
  
  return json['data']
end