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://soap.direct.yandex.ru/json-api/v4/"

Class Method Summary collapse

Class Method Details

.configurationObject



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

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

.load(file, env = nil) ⇒ Object



20
21
22
23
24
# File 'lib/yandex-api/direct.rb', line 20

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

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



26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/yandex-api/direct.rb', line 26

def self.request method, params = {}
  body = {
    :locale => configuration["locale"],
    :login => configuration["login"],
    :application_id => configuration["application_id"],
    :token => configuration["token"],
    :method => method
  }
  
  body.merge!({:param => params}) unless params.empty?
  url = URI.parse(URL_API)

  if configuration["debug"]
    puts "\t\033[32mURL: \033[0m#{URL_API}"
    puts "\t\033[32mMethod: \033[0m#{method}"
    puts "\t\033[32mParams: \033[0m#{params.inspect}"  
  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 = {}
  
  begin
    json = JSON.parse(response.body)
  rescue => e
    raise RuntimeError.new "#{e.message} in response"
  end

  if json.has_key?("error_code")
    code = json["error_code"].to_i
    error = json.has_key?("error_detail") ? json["error_detail"] : json["error_str"]
    raise RuntimeError.new "#{code} - #{error}"
  end
  
  return json["data"]
end