yandex-api-direct 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "/test/"
5
+ add_filter "/config/"
6
+ add_filter "database"
7
+
8
+ add_group 'Lib', 'lib/'
9
+ end
10
+
11
+ require 'colorize'
12
+ require 'rubygems'
13
+ require 'bundler'
14
+ begin
15
+ Bundler.setup(:default, :development)
16
+ rescue Bundler::BundlerError => e
17
+ $stderr.puts e.message
18
+ $stderr.puts "Run `bundle install` to install missing gems"
19
+ exit e.status_code
20
+ end
21
+
22
+ require 'test/unit'
23
+ require 'mocha'
24
+ require 'turn'
25
+ require 'shoulda-context'
26
+
27
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
28
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
29
+ require 'yandex-api-direct'
30
+
31
+ # Set test to use YML config
32
+ require 'yaml'
33
+
34
+ ENV['RACK_ENV'] = "test"
35
+
36
+ class MissingConfigFile < Exception ; end
37
+
38
+
39
+ require "webmock/test_unit"
40
+
41
+ # test models
42
+ require 'models'
43
+
44
+ def load_fixture name
45
+ File.open(File.join(File.dirname(__FILE__), "fixtures", name), "r:UTF-8").read
46
+ end
47
+
48
+ def set_sandbox_access
49
+ YandexApiDirect.url "sandbox"
50
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class TestYandexApiDirect < Test::Unit::TestCase
5
+
6
+ context "Setup config values" do
7
+
8
+ should "have defaults" do
9
+ assert_not_nil YandexApiDirect.config
10
+ end
11
+
12
+ should "be set" do
13
+ conf = YandexApiDirect.config.merge({ access_token: "Test"})
14
+ YandexApiDirect.config = conf
15
+ assert_equal YandexApiDirect.config, conf
16
+ end
17
+
18
+ end
19
+
20
+ context "Yandex url" do
21
+ should "be set to sandbox for tests" do
22
+ assert_equal YandexApiDirect.url, "https://api-sandbox.direct.yandex.ru/json-api/v4/"
23
+ end
24
+
25
+ should "be set to sandbox by param" do
26
+ YandexApiDirect.url "sandbox"
27
+ assert_equal YandexApiDirect.url, "https://api-sandbox.direct.yandex.ru/json-api/v4/"
28
+ end
29
+
30
+ should "be set to production by setting it to production" do
31
+ YandexApiDirect.url "production"
32
+ assert_equal YandexApiDirect.url, "https://soap.direct.yandex.ru/json-api/v4/"
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class TestCampaign < Test::Unit::TestCase
5
+
6
+ context "Yandex Campaign" do
7
+ setup do
8
+ set_sandbox_access
9
+
10
+ # webmock get campaign list
11
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
12
+ with( :body => "{\"method\":\"GetCampaignsList\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":{}}",
13
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
14
+ to_return(:status => 200, :body => load_fixture("yandex_get_campaigns_list.json"))
15
+
16
+ # webmock campaign params
17
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
18
+ with( :body => "{\"method\":\"GetCampaignParams\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":{\"CampaignIDS\":[123451]}}",
19
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
20
+ to_return(:status => 200, :body => load_fixture("yandex_campaign_params.json"))
21
+
22
+ # webmock campaign stats
23
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
24
+ with(:body => "{\"method\":\"GetSummaryStat\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":{\"StartDate\":\"2008-11-13\",\"EndDate\":\"2008-11-15\",\"CampaignIDS\":[123451]}}",
25
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
26
+ to_return(:status => 200, :body => load_fixture("yandex_campaign_stats.json"))
27
+
28
+ end
29
+
30
+ context "find" do
31
+ setup do
32
+ @campaigns = YandexApiDirect::Campaign.find
33
+ end
34
+
35
+ should "be array" do
36
+ assert @campaigns.is_a?(Array)
37
+ end
38
+
39
+ should "be array of Campaigns" do
40
+ assert @campaigns.first.is_a?(YandexApiDirect::Campaign)
41
+ end
42
+
43
+ should "have right count of clients" do
44
+ assert_equal @campaigns.size, 3
45
+ end
46
+
47
+ context "campaign" do
48
+ setup do
49
+ @campaign = @campaigns.first
50
+ end
51
+
52
+ should "have right attributes" do
53
+ assert_equal @campaign.name, "Campaign #1"
54
+ end
55
+
56
+ should "have campaign params" do
57
+ assert @campaign.campaign_params.is_a?(YandexApiDirect::CampaignParams)
58
+ end
59
+
60
+ should "have campaign stats" do
61
+ assert_equal @campaign.campaign_stats(start_date: Date.new(2008,11,13), end_date: Date.new(2008,11,15)).size, 3
62
+ end
63
+
64
+ end
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ #require "webmock/test_unit"
5
+
6
+ class TestCampaignParams < Test::Unit::TestCase
7
+
8
+ context "Yandex Campaign Params" do
9
+ setup do
10
+ set_sandbox_access
11
+
12
+ # webmock campaign params
13
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
14
+ with( :body => "{\"method\":\"GetCampaignParams\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":{\"CampaignID\":123451}}",
15
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
16
+ to_return(:status => 200, :body => load_fixture("yandex_campaign_params.json"))
17
+
18
+ end
19
+
20
+ context "find" do
21
+ setup do
22
+ @campaign_param = YandexApiDirect::CampaignParams.find campaign_id: 123451
23
+ end
24
+
25
+ should "have right attributes" do
26
+ assert_equal @campaign_param.name, "Campaign #1"
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ #require "webmock/test_unit"
5
+
6
+ class TestCampaignStats < Test::Unit::TestCase
7
+
8
+ context "Yandex Campaign Stats" do
9
+ setup do
10
+ set_sandbox_access
11
+
12
+ # webmock campaign stats
13
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
14
+ with( :body => "{\"method\":\"GetSummaryStat\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":{\"CampaignIDS\":[123451],\"StartDate\":\"2008-11-13\",\"EndDate\":\"2008-11-15\"}}",
15
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
16
+ to_return(:status => 200, :body => load_fixture("yandex_campaign_stats.json"))
17
+
18
+ end
19
+
20
+ context "find" do
21
+ setup do
22
+ @campaign_stats = YandexApiDirect::CampaignStats.find campaign_ids: [123451], start_date: Date.new(2008,11,13), end_date: Date.new(2008,11,15)
23
+ end
24
+
25
+ should "have right attributes" do
26
+ assert_equal @campaign_stats.first.campaign_id, 123451
27
+ end
28
+
29
+ should "be for 3 days" do
30
+ assert_equal @campaign_stats.size, 3
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class TestClient < Test::Unit::TestCase
5
+
6
+ context "Yandex Client" do
7
+ setup do
8
+ set_sandbox_access
9
+
10
+ # webmock get client list
11
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
12
+ with(:body => "{\"method\":\"GetClientsList\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":{}}").
13
+ to_return(:status => 200, :body => load_fixture("yandex_get_client_list.json"))
14
+
15
+ # webmock get campaign list
16
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
17
+ with(:body => "{\"method\":\"GetCampaignsList\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":[\"test\"]}",
18
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
19
+ to_return(:status => 200, :body => load_fixture("yandex_get_campaigns_list.json"))
20
+
21
+ # webmock campaign params
22
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
23
+ with(:body => "{\"method\":\"GetCampaignsParams\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":[\"yand-10\"]}").
24
+ to_return(:status => 200, :body => load_fixture("yandex_campaign_params.json"))
25
+
26
+ # webmock campaign stats
27
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
28
+ with(:body => "{\"method\":\"GetSummaryStat\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":{\"StartDate\":\"2008-11-13\",\"EndDate\":\"2008-11-15\",\"CampaignIDS\":[123451]}}",
29
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
30
+ to_return(:status => 200, :body => load_fixture("yandex_campaign_stats.json"))
31
+
32
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
33
+ with(:body => "{\"method\":\"GetSummaryStat\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":{\"StartDate\":\"2008-11-13\",\"EndDate\":\"2008-11-15\",\"CampaignIDS\":[123452]}}",
34
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
35
+ to_return(:status => 200, :body => "{\"data\":[]}")
36
+
37
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
38
+ with(:body => "{\"method\":\"GetSummaryStat\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\",\"param\":{\"StartDate\":\"2008-11-13\",\"EndDate\":\"2008-11-15\",\"CampaignIDS\":[123453]}}",
39
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
40
+ to_return(:status => 200, :body => "{\"data\":[]}")
41
+ end
42
+
43
+ context "initialization" do
44
+ should "have methods defined by params" do
45
+ client = YandexApiDirect::Client.new( foo: "bar")
46
+ assert_equal client.foo, "bar"
47
+ end
48
+ end
49
+
50
+ context "find" do
51
+ setup do
52
+ @clients = YandexApiDirect::Client.find
53
+ end
54
+
55
+ should "be array" do
56
+ assert @clients.is_a?(Array)
57
+ end
58
+
59
+ should "be array of Clients" do
60
+ assert @clients.first.is_a?(YandexApiDirect::Client)
61
+ end
62
+
63
+ should "have right count of clients" do
64
+ assert_equal @clients.size, 2
65
+ end
66
+
67
+ context "client" do
68
+ setup do
69
+ @client = @clients.first
70
+ end
71
+
72
+ should "have right attributes" do
73
+ assert_equal @client.login, "test", "Same login"
74
+ end
75
+
76
+ should "have underscored method names" do
77
+ assert_equal @client.status_arch, "no"
78
+ end
79
+ end
80
+ end
81
+
82
+ context "campaigns find" do
83
+ setup do
84
+ @client = YandexApiDirect::Client.find.first
85
+ end
86
+
87
+ should "be array" do
88
+ assert @client.campaigns.is_a?(Array)
89
+ end
90
+
91
+ should "be array of campaigns" do
92
+ assert @client.campaigns.first.is_a?(YandexApiDirect::Campaign)
93
+ end
94
+
95
+ should "be array of campaigns with valid informations" do
96
+ assert_same_elements @client.campaigns.collect{|c| c.name}, ["Campaign #1","Campaign #2","Campaign #3"]
97
+ end
98
+ end
99
+
100
+ context "campaigns stats" do
101
+ setup do
102
+ @client = YandexApiDirect::Client.find.first
103
+ end
104
+
105
+ should "return campaigns and inside of them stats" do
106
+ campaigns = @client.campaigns_stats start_date: Date.new(2008,11,13), end_date: Date.new(2008,11,15)
107
+ assert_equal campaigns.size, 3, "Should have 3 campaigns"
108
+ assert_equal campaigns.first.stats.size, 3, "First campaign should have 3 date statistics"
109
+ assert_equal campaigns.last.stats.size, 0, "Last campaign has no stats for this dates"
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ #require "webmock/test_unit"
5
+
6
+ class TestGeneric < Test::Unit::TestCase
7
+
8
+ context "Yandex Generic" do
9
+ setup do
10
+ set_sandbox_access
11
+
12
+ # webmock get client list
13
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
14
+ with(:body => "{\"method\":\"GetClientsList\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\"}",
15
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
16
+ to_return(:status => 200, :body => load_fixture("yandex_get_client_list.json"))
17
+
18
+ # webmock get client info
19
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").
20
+ with(:body => "{\"method\":\"GetClientInfo\",\"locale\":\"uk\",\"login\":\"\",\"application_id\":\"\",\"token\":\"\"}",
21
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
22
+ to_return(:status => 200, :body => load_fixture("yandex_get_client_info.json"))
23
+ end
24
+
25
+ context "get only one item" do
26
+ setup do
27
+ @generic = YandexApiDirect::Generic.get "get_client_info"
28
+ end
29
+
30
+ should "have right attributes" do
31
+ assert_equal @generic.login, "test"
32
+ end
33
+
34
+ end
35
+
36
+ context "get more one items" do
37
+ setup do
38
+ @generics = YandexApiDirect::Generic.get "get_clients_list"
39
+ end
40
+
41
+ should "be array" do
42
+ assert @generics.is_a?(Array)
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,131 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+
5
+ class TestYandexObject < Test::Unit::TestCase
6
+
7
+ context "Yandex object" do
8
+ setup do
9
+ set_sandbox_access
10
+ end
11
+
12
+ context "initialization" do
13
+ setup do
14
+ @args_camelized = {
15
+ :'Foo'=> "bar",
16
+ :'BarFoo' => "foo bar",
17
+ :'Deep' => {
18
+ :'DarkSide' => "bar"
19
+ }
20
+ }
21
+
22
+ @object = TestingYandexObject.new @args_camelized
23
+ end
24
+
25
+ should "have added methods for params" do
26
+ assert_equal @object.foo, "bar"
27
+ assert_equal @object.bar_foo, "foo bar"
28
+ end
29
+
30
+ context "change keys" do
31
+ setup do
32
+ @args_underscored = {
33
+ foo: "bar",
34
+ bar_foo: "foo bar",
35
+ deep: {
36
+ dark_side: "bar"
37
+ }
38
+ }
39
+ end
40
+
41
+ should "take care about more ID characters Underscore" do
42
+ assert_equal @object.underscore_keys("CampaignID" => "foo"), {campaign_id: "foo" }
43
+ assert_equal @object.underscore_keys("FIO" => "foo"), {fio: "foo" }
44
+ end
45
+
46
+ should "take care about more ID characters Camelize" do
47
+ assert_equal @object.camelize_keys(campaign_id: "foo"), {:"CampaignID" => "foo"}
48
+ assert_equal @object.camelize_keys(fio: "foo"), {:"FIO" => "foo"}
49
+ end
50
+
51
+ should "underscore keys" do
52
+ assert_equal @object.underscore_keys(@args_camelized), @args_underscored
53
+ end
54
+
55
+ should "camelize keys" do
56
+ assert_equal @object.camelize_keys(@args_underscored), @args_camelized
57
+ end
58
+
59
+ should "camelize keys only for Hash, Array return without update" do
60
+ assert_equal @object.camelize_keys(["test"]), ["test"]
61
+ end
62
+
63
+ end
64
+ end
65
+
66
+ context "valid call" do
67
+ setup do
68
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").to_return(:status => 200, :body => load_fixture("yandex_object_valid_responce.json"))
69
+ @responce = TestingYandexObject.new.call_method "get_client_info", [YandexApiDirect.config[:login]]
70
+ end
71
+
72
+ should "return not nil" do
73
+ assert_not_nil @responce
74
+ end
75
+
76
+ should "return hash" do
77
+ assert @responce.is_a?(Hash)
78
+ end
79
+
80
+ end
81
+
82
+ context "authorization error" do
83
+ setup do
84
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").to_return(:status => 200, :body => "{\"error_detail\":\"\",\"error_str\":\"Authorization error\",\"error_code\":53}")
85
+ end
86
+
87
+ should "raise exception" do
88
+ assert_raises YandexApiDirect::YandexAuthorizationError do
89
+ TestingYandexObject.new.call_method "get_client_info", [YandexApiDirect.config[:login]]
90
+ end
91
+ end
92
+ end
93
+
94
+ context "unknown method error" do
95
+ setup do
96
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").to_return(:status => 200, :body => "{\"error_detail\":\"\",\"error_str\":\"Authorization error\",\"error_code\":55}")
97
+ end
98
+
99
+ should "raise exception" do
100
+ assert_raises YandexApiDirect::YandexMethodError do
101
+ TestingYandexObject.new.call_method "unkwnown method", [YandexApiDirect.config[:login]]
102
+ end
103
+ end
104
+ end
105
+
106
+ context "generic error" do
107
+ setup do
108
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").to_return(:status => 200, :body => "{\"error_detail\":\"\",\"error_str\":\"Authorization error\",\"error_code\":123}")
109
+ end
110
+
111
+ should "raise exception" do
112
+ assert_raises YandexApiDirect::YandexError do
113
+ TestingYandexObject.new.call_method "get_client_info", [YandexApiDirect.config[:login]]
114
+ end
115
+ end
116
+ end
117
+
118
+ context "bad connection" do
119
+ setup do
120
+ stub_request(:post, "https://api-sandbox.direct.yandex.ru/json-api/v4/").to_return(:status => 502, :body => "502 Bad Gateway")
121
+ end
122
+
123
+ should "raise exception" do
124
+ assert_raises YandexApiDirect::YandexConnectionError do
125
+ TestingYandexObject.new.call_method "get_client_info", [YandexApiDirect.config[:login]]
126
+ end
127
+ end
128
+ end
129
+
130
+ end
131
+ end