neuron-client 0.1.0 → 0.2.0
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.
- data/README.md +34 -8
- data/lib/neuron-client.rb +61 -12
- data/lib/neuron-client/{connection.rb → admin_connection.rb} +7 -7
- data/lib/neuron-client/api.rb +48 -31
- data/lib/neuron-client/membase_connection.rb +18 -0
- data/lib/neuron-client/model/admin/ad.rb +22 -0
- data/lib/neuron-client/model/admin/ad_zone.rb +15 -0
- data/lib/neuron-client/model/admin/base.rb +91 -0
- data/lib/neuron-client/model/admin/blocked_referer.rb +12 -0
- data/lib/neuron-client/model/admin/blocked_user_agent.rb +12 -0
- data/lib/neuron-client/model/admin/geo_target.rb +16 -0
- data/lib/neuron-client/model/admin/report.rb +15 -0
- data/lib/neuron-client/model/admin/s3_file.rb +12 -0
- data/lib/neuron-client/model/admin/zone.rb +15 -0
- data/lib/neuron-client/model/base.rb +38 -0
- data/lib/neuron-client/model/common/ad.rb +40 -0
- data/lib/neuron-client/model/common/ad_calculations.rb +329 -0
- data/lib/neuron-client/model/common/ad_zone.rb +17 -0
- data/lib/neuron-client/model/common/base.rb +67 -0
- data/lib/neuron-client/model/common/blocked_referer.rb +16 -0
- data/lib/neuron-client/model/common/blocked_user_agent.rb +16 -0
- data/lib/neuron-client/model/common/geo_target.rb +16 -0
- data/lib/neuron-client/model/common/report.rb +21 -0
- data/lib/neuron-client/model/common/s3_file.rb +16 -0
- data/lib/neuron-client/model/common/zone.rb +22 -0
- data/lib/neuron-client/model/common/zone_calculations.rb +41 -0
- data/lib/neuron-client/model/membase/ad.rb +31 -0
- data/lib/neuron-client/model/membase/ad_zone.rb +11 -0
- data/lib/neuron-client/model/membase/blocked_referer.rb +18 -0
- data/lib/neuron-client/model/membase/blocked_user_agent.rb +18 -0
- data/lib/neuron-client/model/membase/geo_target.rb +11 -0
- data/lib/neuron-client/model/membase/report.rb +11 -0
- data/lib/neuron-client/model/membase/s3_file.rb +11 -0
- data/lib/neuron-client/model/membase/zone.rb +19 -0
- data/lib/neuron-client/model/models.rb +14 -0
- data/lib/neuron-client/version.rb +1 -1
- data/neuron-client.gemspec +18 -11
- data/spec/fixtures/vcr_cassettes/s3_file.yml +186 -4
- data/spec/lib/admin_connection_spec.rb +82 -0
- data/spec/lib/api_spec.rb +80 -0
- data/spec/lib/membase_connection_spec.rb +27 -0
- data/spec/lib/model/admin/ad_spec.rb +34 -0
- data/spec/lib/model/admin/ad_zone_spec.rb +19 -0
- data/spec/lib/model/admin/base_spec.rb +11 -0
- data/spec/lib/model/admin/blocked_referer_spec.rb +11 -0
- data/spec/lib/model/admin/blocked_user_agent_spec.rb +11 -0
- data/spec/lib/model/admin/geo_target_spec.rb +30 -0
- data/spec/lib/model/admin/report_spec.rb +21 -0
- data/spec/lib/model/admin/s3_spec.rb +11 -0
- data/spec/lib/model/admin/zone_spec.rb +21 -0
- data/spec/lib/model/base_spec.rb +89 -0
- data/spec/lib/model/common/ad_calculations_spec.rb +1148 -0
- data/spec/lib/model/common/ad_spec.rb +11 -0
- data/spec/lib/model/common/ad_zone_spec.rb +11 -0
- data/spec/lib/model/common/base_spec.rb +11 -0
- data/spec/lib/model/common/blocked_referer_spec.rb +11 -0
- data/spec/lib/model/common/blocked_user_agent_spec.rb +11 -0
- data/spec/lib/model/common/geo_target_spec.rb +11 -0
- data/spec/lib/model/common/report_spec.rb +11 -0
- data/spec/lib/model/common/s3_spec.rb +11 -0
- data/spec/lib/model/common/zone_calculations_spec.rb +54 -0
- data/spec/lib/model/common/zone_spec.rb +11 -0
- data/spec/lib/model/membase/ad_spec.rb +50 -0
- data/spec/lib/model/membase/ad_zone_spec.rb +11 -0
- data/spec/lib/model/membase/base_spec.rb +11 -0
- data/spec/lib/model/membase/blocked_referer_spec.rb +30 -0
- data/spec/lib/model/membase/blocked_user_agent_spec.rb +30 -0
- data/spec/lib/model/membase/geo_target_spec.rb +11 -0
- data/spec/lib/model/membase/report_spec.rb +11 -0
- data/spec/lib/model/membase/s3_spec.rb +11 -0
- data/spec/lib/model/membase/zone_spec.rb +28 -0
- data/spec/lib/old_spec.rb +192 -149
- data/spec/lib/s3_file_spec.rb +45 -42
- data/spec/spec_helper.rb +2 -1
- metadata +296 -57
- data/lib/neuron-client/ad.rb +0 -39
- data/lib/neuron-client/ad_zone.rb +0 -16
- data/lib/neuron-client/blocked_referer.rb +0 -12
- data/lib/neuron-client/blocked_user_agent.rb +0 -12
- data/lib/neuron-client/connected.rb +0 -138
- data/lib/neuron-client/geo_target.rb +0 -16
- data/lib/neuron-client/real_time_stats.rb +0 -0
- data/lib/neuron-client/report.rb +0 -20
- data/lib/neuron-client/s3_file.rb +0 -10
- data/lib/neuron-client/zone.rb +0 -16
@@ -0,0 +1,54 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
class FakeZone
|
6
|
+
include ZoneCalculations
|
7
|
+
def initialize(ads, ad_links)
|
8
|
+
@ads = ads
|
9
|
+
@ad_links = ad_links
|
10
|
+
end
|
11
|
+
attr_reader :ad_links
|
12
|
+
def find_ad(ad_id)
|
13
|
+
@ads[ad_id]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class ZoneCalculationSpecAd
|
18
|
+
def initialize(active, pressure)
|
19
|
+
@active = active
|
20
|
+
@pressure = pressure
|
21
|
+
end
|
22
|
+
attr_reader :pressure, :active
|
23
|
+
alias_method :active?, :active
|
24
|
+
end
|
25
|
+
|
26
|
+
describe ZoneCalculations do
|
27
|
+
describe ".calculate_ads_by_priority" do
|
28
|
+
it "should generate the expected result" do
|
29
|
+
ads = {
|
30
|
+
"0" => ZoneCalculationSpecAd.new(false, 42.0),
|
31
|
+
"1" => ZoneCalculationSpecAd.new(true, 11.1),
|
32
|
+
"2" => ZoneCalculationSpecAd.new(true, 11.1),
|
33
|
+
"3" => ZoneCalculationSpecAd.new(true, 22.2),
|
34
|
+
"4" => ZoneCalculationSpecAd.new(true, 1000),
|
35
|
+
"5" => ZoneCalculationSpecAd.new(true, 828282)
|
36
|
+
}
|
37
|
+
ad_links = {
|
38
|
+
"0" => {"priority" => 1, "weight" => 50},
|
39
|
+
"1" => {"priority" => 1, "weight" => 4},
|
40
|
+
"2" => {"priority" => 1, "weight" => 2},
|
41
|
+
"3" => {"priority" => 1, "weight" => 2},
|
42
|
+
"4" => {"priority" => 2, "weight" => 99.9}
|
43
|
+
}
|
44
|
+
zone = FakeZone.new(ads, ad_links)
|
45
|
+
|
46
|
+
zone.calculate_ads_by_priority.should == [[["1", 44.4], ["2", 22.2], ["3", 44.4]], [["4", 99900.0]]]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Membase
|
5
|
+
describe Ad do
|
6
|
+
describe "total_impressed" do
|
7
|
+
it "should call the expected methods and return the expected value" do
|
8
|
+
a = Ad.allocate
|
9
|
+
a.should_receive(:id).and_return(7)
|
10
|
+
Ad.stub_chain(:connection, :get).with('count_delivery_ad_7').and_return('999.999')
|
11
|
+
|
12
|
+
a.total_impressed.should == 999.999
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "today_impressed" do
|
17
|
+
it "should call the expected methods and return the expected value" do
|
18
|
+
a = Ad.allocate
|
19
|
+
a.should_receive(:time_zone).and_return('time_zone_value')
|
20
|
+
Time.stub_chain(:now, :in_time_zone).with('time_zone_value').and_return(Time.new(2011, 5, 6, 7, 8, 9))
|
21
|
+
a.should_receive(:id).and_return(7)
|
22
|
+
Ad.stub_chain(:connection, :get).with('count_delivery_20110506_ad_7').and_return('99.99')
|
23
|
+
|
24
|
+
a.today_impressed.should == 99.99
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "Ad.find(id)" do
|
29
|
+
context "when the connection returns a value" do
|
30
|
+
it "should call the expected methods and return the expected value" do
|
31
|
+
Ad.stub_chain(:connection, :get).with('Ad:7').and_return('{"ad":{"attr":"value","attr2":"value2"}}')
|
32
|
+
a = stub(:ad)
|
33
|
+
Ad.should_receive(:new).with({'attr' => 'value', 'attr2' => 'value2'}).and_return(a)
|
34
|
+
|
35
|
+
Ad.find(7).should == a
|
36
|
+
end
|
37
|
+
end
|
38
|
+
context "when the connection returns nil" do
|
39
|
+
it "should call the expected methods and return nil" do
|
40
|
+
Ad.stub_chain(:connection, :get).with('Ad:7').and_return(nil)
|
41
|
+
|
42
|
+
Ad.find(7).should be_nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Membase
|
5
|
+
describe BlockedReferer do
|
6
|
+
describe "BlockedReferer.all" do
|
7
|
+
context "when connection returns a value" do
|
8
|
+
it "should call the expected methods and return the expected value" do
|
9
|
+
BlockedReferer.stub_chain(:connection, :get).with('blocked_referers').and_return('[{"blocked_referer":{"attr":"value"}},{"blocked_referer":{"attr2":"value2"}}]')
|
10
|
+
br = stub(:blocked_referer)
|
11
|
+
br2 = stub(:blocked_referer2)
|
12
|
+
BlockedReferer.should_receive(:new).with({'attr' => 'value'}).and_return(br)
|
13
|
+
BlockedReferer.should_receive(:new).with({'attr2' => 'value2'}).and_return(br2)
|
14
|
+
|
15
|
+
BlockedReferer.all.should == [br, br2]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
context "when connection returns nil" do
|
19
|
+
it "should call the expected methods and return the expected value" do
|
20
|
+
BlockedReferer.stub_chain(:connection, :get).with('blocked_referers').and_return(nil)
|
21
|
+
|
22
|
+
BlockedReferer.all.should == []
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Membase
|
5
|
+
describe BlockedUserAgent do
|
6
|
+
describe "BlockedUserAgent.all" do
|
7
|
+
context "when connection returns a value" do
|
8
|
+
it "should call the expected methods and return the expected value" do
|
9
|
+
BlockedUserAgent.stub_chain(:connection, :get).with('blocked_user_agents').and_return('[{"blocked_user_agent":{"attr":"value"}},{"blocked_user_agent":{"attr2":"value2"}}]')
|
10
|
+
bua = stub(:blocked_user_agent)
|
11
|
+
bua2 = stub(:blocked_user_agent2)
|
12
|
+
BlockedUserAgent.should_receive(:new).with({'attr' => 'value'}).and_return(bua)
|
13
|
+
BlockedUserAgent.should_receive(:new).with({'attr2' => 'value2'}).and_return(bua2)
|
14
|
+
|
15
|
+
BlockedUserAgent.all.should == [bua, bua2]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
context "when connection returns nil" do
|
19
|
+
it "should call the expected methods and return the expected value" do
|
20
|
+
BlockedUserAgent.stub_chain(:connection, :get).with('blocked_user_agents').and_return(nil)
|
21
|
+
|
22
|
+
BlockedUserAgent.all.should == []
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Membase
|
5
|
+
describe Zone do
|
6
|
+
describe "Zone.all" do
|
7
|
+
context "when connection.get returns a value" do
|
8
|
+
it "should call the expected methods and return the expected result" do
|
9
|
+
Zone.stub_chain(:connection, :get).with('Zone:7').and_return('{"zone":{"attr":"value","attr2":"value2"}}')
|
10
|
+
z = stub(:zone)
|
11
|
+
Zone.should_receive(:new).with({'attr' => 'value', 'attr2' => 'value2'}).and_return(z)
|
12
|
+
|
13
|
+
Zone.find(7).should == z
|
14
|
+
end
|
15
|
+
end
|
16
|
+
context "when connection.get returns nil" do
|
17
|
+
it "should call the expected methods and return nil" do
|
18
|
+
Zone.stub_chain(:connection, :get).with('Zone:7').and_return(nil)
|
19
|
+
|
20
|
+
Zone.find(7).should be_nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/lib/old_spec.rb
CHANGED
@@ -6,280 +6,323 @@ module Neuron
|
|
6
6
|
|
7
7
|
describe API do
|
8
8
|
describe "configure" do
|
9
|
-
it "creates a valid
|
10
|
-
API.
|
11
|
-
API.connection.should be_nil
|
12
|
-
API.configure do |config|
|
9
|
+
it "creates a valid AdminConnection object" do
|
10
|
+
API.default_api = API.new
|
11
|
+
API.default_api.connection.should be_nil
|
12
|
+
API.default_api.configure do |config|
|
13
|
+
config.connection_type = :admin
|
13
14
|
config.admin_url = "https://example.com"
|
14
15
|
config.admin_key = "secret"
|
15
16
|
end
|
16
|
-
API.connection.should be_a(
|
17
|
+
API.default_api.connection.should be_a(AdminConnection)
|
17
18
|
end
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
@connection = stub(:connection)
|
24
|
-
API.stub(:connection).and_return(@connection)
|
25
|
-
end
|
26
|
-
describe "create" do
|
27
|
-
it "makes a post call"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe Ad do
|
32
|
-
before(:each) do
|
33
|
-
@connection = stub(:connection)
|
34
|
-
API.stub(:connection).and_return(@connection)
|
35
|
-
end
|
36
|
-
|
37
|
-
describe "all" do
|
22
|
+
module Model
|
23
|
+
describe AdZone do
|
38
24
|
before(:each) do
|
39
|
-
@
|
25
|
+
@connection = stub(:connection)
|
26
|
+
API.stub(:default_api).and_return(stub(:default_api, :connection => @connection, :connection_type => :admin))
|
40
27
|
end
|
41
|
-
|
42
|
-
|
43
|
-
ads = Ad.all
|
44
|
-
ads.should be_a(Array)
|
45
|
-
ads.length.should be > 0
|
46
|
-
ads.first.should be_a(Ad)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "makes a get call" do
|
50
|
-
@connection.should_receive(:get).with("ads").once.and_return(@response)
|
51
|
-
Ad.all
|
28
|
+
describe "create" do
|
29
|
+
it "makes a post call"
|
52
30
|
end
|
53
31
|
end
|
32
|
+
end
|
54
33
|
|
55
|
-
|
34
|
+
module Model
|
35
|
+
describe Ad do
|
56
36
|
before(:each) do
|
57
|
-
@
|
37
|
+
@connection = stub(:connection)
|
38
|
+
API.stub(:default_api).and_return(stub(:default_api, :connection => @connection, :connection_type => :admin))
|
58
39
|
end
|
59
40
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
41
|
+
describe "all" do
|
42
|
+
before(:each) do
|
43
|
+
@response = [{}]
|
44
|
+
end
|
45
|
+
it "returns a list of Ads" do
|
46
|
+
@connection.stub(:get).and_return(@response)
|
47
|
+
ads = Ad.all
|
48
|
+
ads.should be_a(Array)
|
49
|
+
ads.length.should be > 0
|
50
|
+
ads.first.should be_a(Admin::Ad)
|
51
|
+
end
|
65
52
|
|
66
|
-
|
67
|
-
|
68
|
-
|
53
|
+
it "makes a get call" do
|
54
|
+
@connection.should_receive(:get).with("ads").once.and_return(@response)
|
55
|
+
Ad.all
|
56
|
+
end
|
69
57
|
end
|
70
|
-
end
|
71
58
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
59
|
+
describe "find" do
|
60
|
+
before(:each) do
|
61
|
+
@response = {:ad => {:id => 1, :name => "Ad 1"}}
|
62
|
+
end
|
63
|
+
|
64
|
+
it "returns a Ad" do
|
65
|
+
@connection.stub(:get).and_return(@response)
|
66
|
+
ad = Ad.find(1)
|
67
|
+
ad.should be_a(Admin::Ad)
|
68
|
+
end
|
77
69
|
|
78
|
-
|
79
|
-
|
80
|
-
|
70
|
+
it "makes a get call" do
|
71
|
+
@connection.should_receive(:get).with("ads/1").once.and_return(@response)
|
72
|
+
Ad.find(1)
|
73
|
+
end
|
81
74
|
end
|
82
75
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
76
|
+
describe "create" do
|
77
|
+
before(:each) do
|
78
|
+
@attrs = {:name => "Ad 1"}
|
79
|
+
@response = {'ad' => @attrs.merge({:id => 1})}
|
80
|
+
end
|
81
|
+
|
82
|
+
it "posts json" do
|
83
|
+
@connection.should_receive(:post).with("ads", {'ad' => @attrs}).once.and_return(@response)
|
84
|
+
Ad.create(@attrs)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "returns the created Ad" do
|
88
|
+
@connection.should_receive(:post).and_return(@response)
|
89
|
+
ad = Ad.create(@attrs)
|
90
|
+
ad.id.should == 1
|
91
|
+
ad.name.should == "Ad 1"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "returns the created Ad With AdTrackers" do
|
95
|
+
@attrs = {:name => "Ad 1", :ad_trackers => [{:url => "http://www.google.com", :event => "video.impression"}]}
|
96
|
+
@response = {'ad' => @attrs.merge({:id => 1})}
|
97
|
+
@connection.should_receive(:post).and_return(@response)
|
98
|
+
ad = Ad.create(@attrs)
|
99
|
+
ad.id.should == 1
|
100
|
+
ad.name.should == "Ad 1"
|
101
|
+
ad.ad_trackers.should have(1).things
|
102
|
+
end
|
88
103
|
end
|
89
|
-
end
|
90
104
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
105
|
+
describe "update_attributes" do
|
106
|
+
it "makes a put call" do
|
107
|
+
attrs = {:id => 1, :name => "Ad 1"}
|
108
|
+
ad = Ad.new(attrs)
|
109
|
+
@connection.should_receive(:put).with("ads/1", {'ad' => {:name => "Ad 2"}})
|
110
|
+
ad.update_attributes(:name => "Ad 2")
|
111
|
+
end
|
112
|
+
|
113
|
+
it "makes a put call with AdTrackers" do
|
114
|
+
attrs = {:id => 1, :name => "Ad 1", :ad_trackers => [{:id => 1, :advertiser_id => 1, :url => "http://testurl.com", :event => "video.impression"}]}
|
115
|
+
ad = Ad.new(attrs)
|
116
|
+
@connection.should_receive(:put).with("ads/1", {'ad' => {:ad => {:ad_trackers => [{:url => "http://testurl.com/new"}]}}})
|
117
|
+
ad.update_attributes(:ad => {:ad_trackers => [{:url => "http://testurl.com/new"}]})
|
118
|
+
end
|
97
119
|
end
|
98
120
|
end
|
99
121
|
end
|
100
122
|
|
101
|
-
|
102
|
-
|
103
|
-
@connection = stub(:connection)
|
104
|
-
API.stub(:connection).and_return(@connection)
|
105
|
-
end
|
106
|
-
describe "all" do
|
123
|
+
module Model
|
124
|
+
describe Zone do
|
107
125
|
before(:each) do
|
108
|
-
@
|
126
|
+
@connection = stub(:connection)
|
127
|
+
API.stub(:default_api).and_return(stub(:default_api, :connection => @connection, :connection_type => :admin))
|
109
128
|
end
|
129
|
+
describe "all" do
|
130
|
+
before(:each) do
|
131
|
+
@response = [{}]
|
132
|
+
end
|
110
133
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
134
|
+
it "returns a list of Zones" do
|
135
|
+
@connection.stub(:get).and_return(@response)
|
136
|
+
zones = Zone.all
|
137
|
+
zones.should be_a(Array)
|
138
|
+
zones.length.should be > 0
|
139
|
+
zones.first.should be_a(Admin::Zone)
|
140
|
+
end
|
118
141
|
|
119
|
-
|
120
|
-
|
121
|
-
|
142
|
+
it "makes a get call" do
|
143
|
+
@connection.should_receive(:get).with("zones").once.and_return(@response)
|
144
|
+
Zone.all
|
145
|
+
end
|
122
146
|
end
|
123
|
-
end
|
124
147
|
|
125
|
-
|
126
|
-
|
127
|
-
|
148
|
+
describe "find" do
|
149
|
+
before(:each) do
|
150
|
+
@response = {:zone => {:id => 1, :slug => "zone1"}}
|
151
|
+
end
|
152
|
+
|
153
|
+
it "returns a Zone" do
|
154
|
+
@connection.stub(:get).and_return(@response)
|
155
|
+
zone = Zone.find(1)
|
156
|
+
zone.should be_a(Admin::Zone)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "makes a get call" do
|
160
|
+
@connection.should_receive(:get).with("zones/1").once.and_return(@response)
|
161
|
+
Zone.find(1)
|
162
|
+
end
|
128
163
|
end
|
129
164
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
165
|
+
describe "create" do
|
166
|
+
before(:each) do
|
167
|
+
@attrs = {:slug => "zone1"}
|
168
|
+
@response = {'zone' => @attrs.merge({:id => 1})}
|
169
|
+
end
|
170
|
+
|
171
|
+
it "posts json" do
|
172
|
+
@connection.should_receive(:post).with("zones", {'zone' => @attrs}).once.and_return(@response)
|
173
|
+
Zone.create(@attrs)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "returns the created Ad" do
|
177
|
+
@connection.should_receive(:post).and_return(@response)
|
178
|
+
zone = Zone.create(@attrs)
|
179
|
+
zone.id.should == 1
|
180
|
+
zone.slug.should == "zone1"
|
181
|
+
end
|
134
182
|
end
|
135
183
|
|
136
|
-
|
137
|
-
|
138
|
-
|
184
|
+
describe "update_attributes" do
|
185
|
+
it "makes a put call" do
|
186
|
+
attrs = {:id => 1, :slug => "zone1"}
|
187
|
+
zone = Zone.new(attrs)
|
188
|
+
@connection.should_receive(:put).with("zones/1", {'zone' => {:slug => "zone2"}})
|
189
|
+
zone.update_attributes(:slug => "zone2")
|
190
|
+
end
|
139
191
|
end
|
140
192
|
end
|
193
|
+
end
|
141
194
|
|
142
|
-
describe "create" do
|
143
|
-
before(:each) do
|
144
|
-
@attrs = {:slug => "zone1"}
|
145
|
-
@response = {'zone' => @attrs.merge({:id => 1})}
|
146
|
-
end
|
147
195
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
196
|
+
module Model
|
197
|
+
module Common
|
198
|
+
class TestModel
|
199
|
+
include Base
|
152
200
|
|
153
|
-
|
154
|
-
|
155
|
-
zone = Zone.create(@attrs)
|
156
|
-
zone.id.should == 1
|
157
|
-
zone.slug.should == "zone1"
|
201
|
+
resource_name 'test'
|
202
|
+
resources_name 'tests'
|
158
203
|
end
|
159
204
|
end
|
205
|
+
end
|
206
|
+
|
207
|
+
module Model
|
208
|
+
module Admin
|
209
|
+
class TestModel < Common::TestModel
|
210
|
+
include Base
|
160
211
|
|
161
|
-
describe "update_attributes" do
|
162
|
-
it "makes a put call" do
|
163
|
-
attrs = {:id => 1, :slug => "zone1"}
|
164
|
-
zone = Zone.new(attrs)
|
165
|
-
@connection.should_receive(:put).with("zones/1", {'zone' => {:slug => "zone2"}})
|
166
|
-
zone.update_attributes(:slug => "zone2")
|
167
212
|
end
|
168
213
|
end
|
169
214
|
end
|
170
215
|
|
171
|
-
|
172
|
-
class
|
173
|
-
|
174
|
-
resource_name 'connection'
|
175
|
-
resources_name 'connection'
|
176
|
-
end
|
216
|
+
module Model
|
217
|
+
class TestModel < Base; end
|
218
|
+
end
|
177
219
|
|
220
|
+
describe AdminConnection do
|
178
221
|
before(:each) do
|
179
222
|
@connection = stub(:connection)
|
180
|
-
API.stub(:
|
223
|
+
API.stub(:default_api).and_return(stub(:default_api, :connection => @connection, :connection_type => :admin))
|
181
224
|
end
|
182
225
|
|
183
226
|
describe "update_attributes" do
|
184
227
|
it "should return false when errors occur for updated objects" do
|
185
|
-
@connection.should_receive(:put).with("
|
228
|
+
@connection.should_receive(:put).with("tests/1", {'test' => {}}) do
|
186
229
|
throw :errors, {:error => "is required"}
|
187
230
|
end
|
188
231
|
|
189
|
-
|
232
|
+
Model::TestModel.new(:id => 1).update_attributes({}).should be_false
|
190
233
|
end
|
191
234
|
|
192
235
|
it "should provide access to errors when validation fails" do
|
193
|
-
@connection.should_receive(:put).with("
|
236
|
+
@connection.should_receive(:put).with("tests/1", {'test' => {}}) do
|
194
237
|
throw :errors, {:error => "is required"}
|
195
238
|
end
|
196
239
|
|
197
|
-
c =
|
240
|
+
c = Model::TestModel.new(:id => 1)
|
198
241
|
c.update_attributes({}).should be_false
|
199
242
|
c.errors.should == {:error => "is required"}
|
200
243
|
end
|
201
244
|
|
202
245
|
it "should return true when errors do not occure for updated objects" do
|
203
|
-
@connection.should_receive(:put).with("
|
246
|
+
@connection.should_receive(:put).with("tests/1", {'test' => {}})
|
204
247
|
|
205
|
-
|
248
|
+
Model::TestModel.new(:id => 1).update_attributes({}).should be_true
|
206
249
|
end
|
207
250
|
end
|
208
251
|
|
209
252
|
describe "save" do
|
210
253
|
it "should return false when errors occur for new objects" do
|
211
|
-
@connection.should_receive(:post).with("
|
254
|
+
@connection.should_receive(:post).with("tests", {'test' => {}}) do
|
212
255
|
throw :errors, {:error => "is required"}
|
213
256
|
end
|
214
257
|
|
215
|
-
|
258
|
+
Model::TestModel.new.save.should be_false
|
216
259
|
end
|
217
260
|
|
218
261
|
it "should provide access to errors when validation fails" do
|
219
|
-
@connection.should_receive(:post).with("
|
262
|
+
@connection.should_receive(:post).with("tests", {'test' => {}}) do
|
220
263
|
throw :errors, {:error => "is required"}
|
221
264
|
end
|
222
265
|
|
223
|
-
c =
|
266
|
+
c = Model::TestModel.new
|
224
267
|
c.save.should be_false
|
225
268
|
c.errors.should == {:error => "is required"}
|
226
269
|
end
|
227
270
|
|
228
271
|
it "should return true when errors do not occur for new objects" do
|
229
|
-
@connection.should_receive(:post).with("
|
272
|
+
@connection.should_receive(:post).with("tests", {'test' => {}}).and_return({'test' => {:id => 1}})
|
230
273
|
|
231
|
-
|
274
|
+
Model::TestModel.new.save.should be_true
|
232
275
|
end
|
233
276
|
|
234
277
|
it "should return false when errors occur for existing objects" do
|
235
|
-
@connection.should_receive(:put).with('
|
278
|
+
@connection.should_receive(:put).with('tests/1', {'test' => {}}) do
|
236
279
|
throw :errors, {:error => "is required"}
|
237
280
|
end
|
238
281
|
|
239
|
-
|
282
|
+
Model::TestModel.new(:id => 1).save.should be_false
|
240
283
|
end
|
241
284
|
|
242
285
|
it "should provide access to errors when validation fails" do
|
243
|
-
@connection.should_receive(:put).with("
|
286
|
+
@connection.should_receive(:put).with("tests/1", {'test' => {}}) do
|
244
287
|
throw :errors, {:error => "is required"}
|
245
288
|
end
|
246
289
|
|
247
|
-
c =
|
290
|
+
c = Model::TestModel.new(:id => 1)
|
248
291
|
c.save.should be_false
|
249
292
|
c.errors.should == {:error => "is required"}
|
250
293
|
end
|
251
294
|
|
252
295
|
it "should return true when errors do not occur for existing objects" do
|
253
|
-
@connection.should_receive(:put).with("
|
296
|
+
@connection.should_receive(:put).with("tests/1", {'test' => {}})
|
254
297
|
|
255
|
-
|
298
|
+
Model::TestModel.new(:id => 1).save.should be_true
|
256
299
|
end
|
257
300
|
end
|
258
301
|
|
259
302
|
describe "create" do
|
260
303
|
it "should return nil when errors occur" do
|
261
|
-
@connection.should_receive(:post).with("
|
304
|
+
@connection.should_receive(:post).with("tests", {'test' => {}}) do
|
262
305
|
throw :errors, {:error => "is_required"}
|
263
306
|
end
|
264
307
|
|
265
|
-
|
308
|
+
Model::TestModel.create({}).should be_nil
|
266
309
|
end
|
267
310
|
|
268
311
|
it "should return the created object when no errors occur" do
|
269
|
-
@connection.should_receive(:post).with("
|
312
|
+
@connection.should_receive(:post).with("tests", {'test' => {}}).and_return({'test' => {:id => 1}})
|
270
313
|
|
271
|
-
|
314
|
+
Model::TestModel.create({}).should be_a Model::Admin::TestModel
|
272
315
|
end
|
273
316
|
end
|
274
317
|
|
275
318
|
describe "create!" do
|
276
319
|
it "should return nil when errors occur" do
|
277
|
-
@connection.should_receive(:post).with("
|
320
|
+
@connection.should_receive(:post).with("tests", {'test' => {}}) do
|
278
321
|
throw :errors, {:error => "is_required"}
|
279
322
|
end
|
280
323
|
|
281
324
|
errors = catch(:errors) do
|
282
|
-
|
325
|
+
Model::TestModel.create!({})
|
283
326
|
nil
|
284
327
|
end
|
285
328
|
errors.should_not be_nil
|
@@ -287,20 +330,20 @@ module Neuron
|
|
287
330
|
end
|
288
331
|
|
289
332
|
it "should return the created object when no errors occur" do
|
290
|
-
@connection.should_receive(:post).with("
|
333
|
+
@connection.should_receive(:post).with("tests", {'test' => {}}).and_return({'test' => {:id => 1}})
|
291
334
|
|
292
|
-
|
335
|
+
Model::TestModel.create!({}).should be_a Model::Admin::TestModel
|
293
336
|
end
|
294
337
|
end
|
295
338
|
end
|
296
339
|
|
297
|
-
describe
|
340
|
+
describe AdminConnection do
|
298
341
|
before(:each) do
|
299
|
-
@connection =
|
342
|
+
@connection = AdminConnection.new('http://neuron.admin', "my_api_key")
|
300
343
|
end
|
301
344
|
|
302
345
|
it "should escape the passed api_key" do
|
303
|
-
connection =
|
346
|
+
connection = AdminConnection.new("http://neuron.admin", "an unescaped string")
|
304
347
|
FakeWeb.register_uri(:get, "http://neuron.admin/test.json?api_key=an+unescaped+string", :body => Yajl.dump({"escaped" => true}))
|
305
348
|
connection.get("test").should == {"escaped" => true}
|
306
349
|
end
|