Class: Ya::API::Direct::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ya/api/direct/client.rb

Overview

Yandex Direct API client class

Author:

  • RikkiMongoose

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) {|_self| ... } ⇒ Ya::API::Direct::Client

Initializes a new Client object

Parameters:

  • config (Hash) (defaults to: {})

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ya/api/direct/client.rb', line 20

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

Instance Attribute Details

#cache_timestampObject (readonly)

Returns the value of attribute cache_timestamp.



13
14
15
# File 'lib/ya/api/direct/client.rb', line 13

def cache_timestamp
  @cache_timestamp
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



13
14
15
# File 'lib/ya/api/direct/client.rb', line 13

def gateway
  @gateway
end

#units_dataObject (readonly)

Returns the value of attribute units_data.



13
14
15
# File 'lib/ya/api/direct/client.rb', line 13

def units_data
  @units_data
end

#v4Object (readonly)

Returns the value of attribute v4.



13
14
15
# File 'lib/ya/api/direct/client.rb', line 13

def v4
  @v4
end

#v5Object (readonly)

Returns the value of attribute v5.



13
14
15
# File 'lib/ya/api/direct/client.rb', line 13

def v5
  @v5
end

Instance Method Details

#start_cache!String

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

Returns:

  • (String)

    New timestamp value.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ya/api/direct/client.rb', line 61

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']
  end
  @cache_timestamp = Time.parse(timestamp)
  update_units_data result[:units_data]
  @cache_timestamp
end

#update_units_data(units_data = {}) ⇒ Hash

Update units data.

Parameters:

  • format (Hash)

    New units data values

Returns:

  • (Hash)

    Updated user data values



54
55
56
# File 'lib/ya/api/direct/client.rb', line 54

def update_units_data(units_data = {})
  @units_data.merge! units_data
end