class Ya::API::Direct::Client

Yandex Direct API client class @author RikkiMongoose

Attributes

cache_timestamp[R]
gateway[R]
units_data[R]
v4[R]
v5[R]

Public Class Methods

new(config = {}) { |self| ... } click to toggle source

Initializes a new Client object

@param config [Hash] @return [Ya::API::Direct::Client]

# File lib/ya/api/direct/client.rb, line 22
def initialize(config = {})
  @config = {
    token: nil,
    app_id: nil,
    login: '',
    locale: 'en',
    mode: :sandbox,
    format: :json,
    cache: true,
    api: :v5,
    ssl: true
  }.merge(config)

  @units_data = {
    just_used: nil,
    units_left: nil,
    units_limit: nil
  }

  raise "Token can't be empty" if @config[:token].nil?
  raise "Allowed Yandex Direct API versions are #{AllowedVersions}" unless AllowedAPIVersions.include? @config[:api]

  @gateway = Ya::API::Direct::Gateway.new @config

  init_v4
  init_v5
  start_cache! if @config[:cache]
  yield self if block_given?
end

Public Instance Methods

start_cache!() click to toggle source

Start caching. Executed automatically, if @congif is true.

@return [String] New timestamp value.

# File lib/ya/api/direct/client.rb, line 63
def start_cache!
  case @config[:api]
  when :v4
    result = @gateway.request("GetChanges", {}, nil, :v4live)
    timestamp = result[:data]['data']['Timestamp']
  when :v5
    result = @gateway.request("checkDictionaries", {}, "changes", :v5)
    timestamp = result[:data]['result']['Timestamp']
    update_units_data result[:units_data]
  end
  @cache_timestamp = Time.parse(timestamp)
  @cache_timestamp
end
update_units_data(units_data = {}) click to toggle source

Update units data.

@param format [Hash] New units data values @return [Hash] Updated user data values

# File lib/ya/api/direct/client.rb, line 56
def update_units_data(units_data = {})
  @units_data.merge! units_data
end

Private Instance Methods

init_v4() click to toggle source
# File lib/ya/api/direct/client.rb, line 79
def init_v4
  @v4 = DirectServiceV4.new self, (API_V4 + API_V4_LIVE)
end
init_v5() click to toggle source
# File lib/ya/api/direct/client.rb, line 83
def init_v5
  @v5 = {}
  API_V5.each do |service, methods|
    service_item = DirectServiceV5.new(self, service, methods)
    service_key = service_item.service_url
    @v5[service_key] = service_item
    self.class.send :define_method, service_key do @v5[service_key] end
  end
end