Skip to content

rikkimongoose/ya-api-direct

Repository files navigation

Ya::Api::Direct

A Ruby interface to Yandex Direct API. Supported versions 4, 4 Live, 5.

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file lib/ya/api/direct. To experiment with that code, run bin/console for an interactive prompt.

Installation

Add this line to your application's Gemfile:

gem 'ya-api-direct'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ya-api-direct

Usage

OAuth2 Yandex token is needed. Use omniauth-yandex to receive it. Install it according by following manual: Rails Authentication with OAuth 2.0 and OmniAuth.

Call methods of Yandex Direct API 5:

request = {
    "SelectionCriteria" => {
      "Types" => ["TEXT_CAMPAIGN"]
    },
    "FieldNames" => ["Id", "Name"],
    "TextCampaignFieldNames" => ["BiddingStrategy"]
}

options = { token: Token }
@direct = Ya::API::Direct::Client.new(options)
json = @direct.campaigns.get request

Call methods from versions 4 and 4 Live:

@direct = Ya::API::Direct::Client.new({ token: Token })
json = @direct.v4.GetCampaignsList

All names of controllers and methods are equal to ones from Direct API help. 4 vs 4 Live is autodetected.

Allowed options:

  • token: Yandex Direct API token. Required field.
  • app_id: Application ID. Isn't required. Used for Yandex stat.
  • login: Application login. Isn't required. Used for Yandex stat.
  • locale: One of locales, supported by Yandex API ('en' or 'ru'). By default: 'en'.
  • mode: Yandex Direct API server to send requests (:sandbox or production). By default: :sandbox.
  • cache: send caching request after creating the instance. By default: true.
  • api: used only to make caching request (:v5 or :v4), By default: :v5.
  • ssl: use SSL connection. By default: true.

Caching

By default the client class sends request to clients. You can avoid it by turning this option off, like this:

@direct = Ya::API::Direct::Client.new({ token: Token, cache: false })

Check Yandex manuals for more info on caching.

Date returned with first call of caching method is stored in cache_timestamp property.

Units data

Units data of last request is stored in units_data property. There're 3 keys in this hash:

  • just_used - units used by last request.
  • units_left - units left for the current session.
  • units_limit - units limit for a session of your account.

They were added only in Yandex Direct API 5. Calling the methods from 4 or 4 Live doesn't update them.

Useful links

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rikkimongoose/ya-api-direct. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.