Accessing Yandex API and work with campaigns and statistics


Just in console

gem install yandex-api-direct

Or put into Gemfile

gem "yandex-api-direct"

and somewhere before use (not rails - they will require gem automaticaly)

require "yandex-api-direct"


Setup your config values by calling YandexApiDirect.config = {}

YandexApiDirect.config = {
  locale: "en"
  application_id: "YOUR APPLICATION ID"
  access_token: "FILL ACCESS TOKEN FOR USER"

Get application_id by registering your application at (full doc at:

If you need to get Oauth token you will also need application password (also listed on registration page).

Working with Gem

You can access Main classes: Client, Campaign, CampaignParam, CampaignStats

How one class works

Main classes are inherited from Hashr and includes YandexObject This combination will provide very easy acces to all returned values from API.

# Find some objects:
campaigns = YandexApiDirect::Campaign.find
# you got [ <Campaign:..>, ... ] and choose first one
campaign = campaigns.first
# then you can ask for something #=> "Campaign"
campaign.campaign_id #=> 123456
campaign.clicks #=> 123

Arguments from api is hash, this hash is saved by hashr directly into instance. Keys from API are underscored for using as normal methods not campaign.Name instead of this use: Instance is still Hash, you can do all stuf like with normal hash.


#you can pass arguments to find as hash: [GetClientsList](
#=> [ <Client:..>, ... ]

#you can pass argument as array of logins (optional for agencies): [GetCampaignsList](
#=> [ <Campaign:..>, ... ]

#you can pass arguments to find as hash: [GetCampaignParams](
#=> [ <CampaignParams:..>, ... ]

#you can pass arguments to find as hash: [GetSummaryStat](
#=> [ <CampaignStats:..>, ... ]

Arguments for methods

All arguments you will pass into params are automatically camelized, because Yandex API takes only Camelcased attribute names:

# Example: 
campaign.campaign_stats start_date:
# will transform inside of method into
{ :"StartDate" => }

If you will implement your own methods, you can use built in method in YandexObject camelize_keys(hash)


Yandex uses this hierarchy and gem follows it

  • Client
    • Campaign
    • CampaignParams
    • CampaignStats
#get first Client
client = Client.find.first

#get all client campaign
campaigns = client.campaigns

campaign = campaigns.first

#get campaign statistics
campaign.campaign_stats start_date:, end_date:
#=> [<CampaingStats:..>, ...]
#get campaign params
#=> <CampaingParams:... > - not array but directly one object (campaign can have only one params!)


There is a class YandexApiDirect::Generic which provides class method get has two parameters

  • method name - underscored
  • args (optional) - should be hash or array
YandexApiDirect::Generic.get "get_clients_info"
#=> will return hash containing client info (Only one item returned)

YandexApiDirect::Generic.get "get_clients_list"
#=> will return array containing hashes of client info (More items returned)

By this you can call all methods without writing more code.

Sandbox/Production enviroments

Yandex api allows you to use sandbox for testing purposes: Yandex Sandbox

In your test helper write down

YandexApiDirect.url "sandbox" #or if you like symbols :sandbox  :-)
#or for directly saying to use production
YandexApiDirect.url "production" #or if you like symbols :production  :-)

This will set url of Yandex to sandbox or production. If you will not set url it will use ENV['RACK_ENV'] or ENV['RAILS_ENV'] == "test" it will use "sandbox" as default, all other environments (including development) will have sa default setted "production"

Contributing to redis-model-extension

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright (c) 2012 Ondrej Bartas. See LICENSE.txt for further details.