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



28
29
30
31
32
# File 'lib/yandex-api/direct.rb', line 28

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

.parse_json(json) ⇒ Object



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

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



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

def self.request method, params = {}

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

  if configuration["verbose"]
    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 = 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