Yandex-API-Direct
Accessing Yandex API and work with campaigns and statistics
Instalation
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"
Initialization
Setup your config values by calling YandexApiDirect.config = {}
YandexApiDirect.config = {
locale: "en"
application_id: "YOUR APPLICATION ID"
login: "FILL LOGIN OF USER"
access_token: "FILL ACCESS TOKEN FOR USER"
}
Get application_id by registering your application at https://oauth.yandex.com/client/new
(full doc at: http://api.yandex.com/oauth/doc/dg/tasks/register-client.xml)
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.
campaigns = YandexApiDirect::Campaign.find
campaign = campaigns.first
campaign.name
campaign.campaign_id
campaign.clicks
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: campaign.name
Instance is still Hash, you can do all stuf like with normal hash.
Find
YandexApiDirect::Client.find
YandexApiDirect::Campaign.find
YandexApiDirect::CampaignParams.find
YandexApiDirect::CampaignStats.find
Arguments for methods
All arguments you will pass into params are automatically camelized, because Yandex API takes only Camelcased attribute names:
campaign.campaign_stats start_date: Date.today
{ :"StartDate" => Date.today }
If you will implement your own methods, you can use built in method in YandexObject camelize_keys(hash)
Hierarchy
Yandex uses this hierarchy and gem follows it
- Client
- Campaign
- CampaignParams
- CampaignStats
client = Client.find.first
campaigns = client.campaigns
campaign = campaigns.first
campaign.campaign_stats start_date: Date.today-8, end_date: Date.today-1
campaign.campaign_params
Flexibility
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"
YandexApiDirect::Generic.get "get_clients_list"
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"
YandexApiDirect.url "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
Copyright (c) 2012 Ondrej Bartas. See LICENSE.txt for
further details.