Module: Yandex::Direct

Defined in:
lib/yandex/direct.rb,
lib/yandex/direct/base.rb,
lib/yandex/direct/error.rb,
lib/yandex/direct/client.rb,
lib/yandex/direct/config.rb,
lib/yandex/direct/models.rb,
lib/yandex/direct/request.rb,
lib/yandex/direct/version.rb,
lib/yandex/direct/action_result.rb,
lib/yandex/direct/models/campaign.rb,
lib/yandex/direct/selection_criteria.rb,
lib/yandex/direct/exception_notification.rb

Defined Under Namespace

Classes: ActionResult, Base, Campaign, Error, ExceptionNotification, SelectionCriteria

Constant Summary collapse

URL_API =
'https://api.direct.yandex.com/json/v5'.freeze
URL_API_SANDBOX =
'https://api-sandbox.direct.yandex.com/json/v5'.freeze
VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.clientObject



7
8
9
10
11
# File 'lib/yandex/direct/client.rb', line 7

def self.client
  @client ||= Faraday.new(url: (configuration['sandbox'] ? URL_API_SANDBOX : URL_API)) do |faraday|
    faraday.use Faraday::Adapter::NetHttp
  end
end

.config(file, env = nil) ⇒ Object



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

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

.configurationObject



5
6
7
8
9
10
11
12
# File 'lib/yandex/direct/config.rb', line 5

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

.parse(hash, key, klass) ⇒ Object



5
6
7
# File 'lib/yandex/direct/request.rb', line 5

def self.parse(hash, key, klass)
  hash.fetch(key,[]).map{|attributes| klass.new(attributes)}
end

.request(method, path, params = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yandex/direct/request.rb', line 9

def self.request(method, path, params = nil)
  response = self.client.post(path, {
      'method' => method,
      'params' => (params.respond_to?(:to_param) ? params.to_param : params)
  }.to_json, {
                                      'Authorization' => "Bearer #{configuration['token']}",
                                      'Client-Login' => configuration['login'],
                                      'Accept-Language' => configuration['locale'],
                                      'Content-Type' => 'application/json; charset=utf-8'
                                  })

  raise "Yandex.Direct response with status #{response.status}" unless response.success?

  response = JSON.parse(response.body)

  if (error = response['error'])
    raise Error.new(error['request_id'], error['error_code'], error['error_string'], error['error_detail'])
  end

  response['result']
end