neuron-client 0.2.6 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/README.md +11 -8
  2. data/lib/neuron-client/api.rb +5 -0
  3. data/lib/neuron-client/model/ad.rb +142 -0
  4. data/lib/neuron-client/model/ad_calculations.rb +325 -0
  5. data/lib/neuron-client/model/ad_zone.rb +45 -0
  6. data/lib/neuron-client/model/base.rb +233 -18
  7. data/lib/neuron-client/model/blocked_referer.rb +20 -0
  8. data/lib/neuron-client/model/blocked_user_agent.rb +21 -0
  9. data/lib/neuron-client/model/geo_target.rb +36 -0
  10. data/lib/neuron-client/model/pixel.rb +22 -0
  11. data/lib/neuron-client/model/report.rb +35 -0
  12. data/lib/neuron-client/model/s3_file.rb +30 -0
  13. data/lib/neuron-client/model/zone.rb +64 -0
  14. data/lib/neuron-client/model/zone_calculations.rb +37 -0
  15. data/lib/neuron-client/schema/ad.rb +406 -0
  16. data/lib/neuron-client/schema/ad_zone.rb +49 -0
  17. data/lib/neuron-client/schema/blocked_referer.rb +52 -0
  18. data/lib/neuron-client/schema/blocked_user_agent.rb +64 -0
  19. data/lib/neuron-client/schema/common.rb +220 -0
  20. data/lib/neuron-client/schema/event.rb +17 -0
  21. data/lib/neuron-client/schema/geo_target.rb +33 -0
  22. data/lib/neuron-client/schema/pixel.rb +39 -0
  23. data/lib/neuron-client/schema/report.rb +59 -0
  24. data/lib/neuron-client/schema/s3_file.rb +87 -0
  25. data/lib/neuron-client/schema/zone.rb +214 -0
  26. data/lib/neuron-client/version.rb +1 -1
  27. data/lib/neuron-client.rb +24 -59
  28. data/neuron-client.gemspec +3 -0
  29. data/spec/lib/admin_connection_spec.rb +234 -0
  30. data/spec/lib/api_spec.rb +41 -63
  31. data/spec/lib/model/ad_calculations_spec.rb +1146 -0
  32. data/spec/lib/model/ad_spec.rb +253 -0
  33. data/spec/lib/model/ad_zone_spec.rb +15 -0
  34. data/spec/lib/model/base_spec.rb +5 -83
  35. data/spec/lib/model/blocked_referer_spec.rb +36 -0
  36. data/spec/lib/model/blocked_user_agent_spec.rb +36 -0
  37. data/spec/lib/model/geo_target_spec.rb +28 -0
  38. data/spec/lib/model/pixel_spec.rb +36 -0
  39. data/spec/lib/model/report_spec.rb +17 -0
  40. data/spec/lib/{s3_file_spec.rb → model/s3_file_spec.rb} +6 -5
  41. data/spec/lib/model/zone_calculations_spec.rb +49 -0
  42. data/spec/lib/model/zone_spec.rb +155 -0
  43. data/spec/lib/schema/ad_spec.rb +515 -0
  44. data/spec/lib/schema/ad_zone_spec.rb +149 -0
  45. data/spec/lib/schema/blocked_referer_spec.rb +136 -0
  46. data/spec/lib/schema/blocked_user_agent_spec.rb +147 -0
  47. data/spec/lib/schema/geo_target_spec.rb +92 -0
  48. data/spec/lib/schema/pixel_spec.rb +125 -0
  49. data/spec/lib/schema/report_spec.rb +129 -0
  50. data/spec/lib/schema/s3_file_spec.rb +164 -0
  51. data/spec/lib/schema/zone_spec.rb +243 -0
  52. data/spec/spec_helper.rb +2 -1
  53. metadata +141 -121
  54. data/lib/neuron-client/model/admin/ad.rb +0 -22
  55. data/lib/neuron-client/model/admin/ad_zone.rb +0 -15
  56. data/lib/neuron-client/model/admin/base.rb +0 -91
  57. data/lib/neuron-client/model/admin/blocked_referer.rb +0 -12
  58. data/lib/neuron-client/model/admin/blocked_user_agent.rb +0 -12
  59. data/lib/neuron-client/model/admin/geo_target.rb +0 -16
  60. data/lib/neuron-client/model/admin/pixel.rb +0 -12
  61. data/lib/neuron-client/model/admin/report.rb +0 -15
  62. data/lib/neuron-client/model/admin/s3_file.rb +0 -12
  63. data/lib/neuron-client/model/admin/zone.rb +0 -15
  64. data/lib/neuron-client/model/common/ad.rb +0 -42
  65. data/lib/neuron-client/model/common/ad_calculations.rb +0 -329
  66. data/lib/neuron-client/model/common/ad_zone.rb +0 -17
  67. data/lib/neuron-client/model/common/base.rb +0 -67
  68. data/lib/neuron-client/model/common/blocked_referer.rb +0 -16
  69. data/lib/neuron-client/model/common/blocked_user_agent.rb +0 -16
  70. data/lib/neuron-client/model/common/geo_target.rb +0 -16
  71. data/lib/neuron-client/model/common/pixel.rb +0 -18
  72. data/lib/neuron-client/model/common/report.rb +0 -21
  73. data/lib/neuron-client/model/common/s3_file.rb +0 -16
  74. data/lib/neuron-client/model/common/zone.rb +0 -22
  75. data/lib/neuron-client/model/common/zone_calculations.rb +0 -41
  76. data/lib/neuron-client/model/membase/ad.rb +0 -49
  77. data/lib/neuron-client/model/membase/ad_zone.rb +0 -11
  78. data/lib/neuron-client/model/membase/blocked_referer.rb +0 -20
  79. data/lib/neuron-client/model/membase/blocked_user_agent.rb +0 -20
  80. data/lib/neuron-client/model/membase/geo_target.rb +0 -11
  81. data/lib/neuron-client/model/membase/pixel.rb +0 -22
  82. data/lib/neuron-client/model/membase/report.rb +0 -11
  83. data/lib/neuron-client/model/membase/s3_file.rb +0 -11
  84. data/lib/neuron-client/model/membase/zone.rb +0 -30
  85. data/lib/neuron-client/model/models.rb +0 -15
  86. data/spec/lib/model/admin/ad_spec.rb +0 -34
  87. data/spec/lib/model/admin/ad_zone_spec.rb +0 -19
  88. data/spec/lib/model/admin/base_spec.rb +0 -11
  89. data/spec/lib/model/admin/blocked_referer_spec.rb +0 -11
  90. data/spec/lib/model/admin/blocked_user_agent_spec.rb +0 -11
  91. data/spec/lib/model/admin/geo_target_spec.rb +0 -30
  92. data/spec/lib/model/admin/report_spec.rb +0 -21
  93. data/spec/lib/model/admin/s3_spec.rb +0 -11
  94. data/spec/lib/model/admin/zone_spec.rb +0 -21
  95. data/spec/lib/model/common/ad_calculations_spec.rb +0 -1151
  96. data/spec/lib/model/common/ad_spec.rb +0 -11
  97. data/spec/lib/model/common/ad_zone_spec.rb +0 -11
  98. data/spec/lib/model/common/base_spec.rb +0 -11
  99. data/spec/lib/model/common/blocked_referer_spec.rb +0 -11
  100. data/spec/lib/model/common/blocked_user_agent_spec.rb +0 -11
  101. data/spec/lib/model/common/geo_target_spec.rb +0 -11
  102. data/spec/lib/model/common/report_spec.rb +0 -11
  103. data/spec/lib/model/common/s3_spec.rb +0 -11
  104. data/spec/lib/model/common/zone_calculations_spec.rb +0 -54
  105. data/spec/lib/model/common/zone_spec.rb +0 -11
  106. data/spec/lib/model/membase/ad_spec.rb +0 -54
  107. data/spec/lib/model/membase/ad_zone_spec.rb +0 -11
  108. data/spec/lib/model/membase/base_spec.rb +0 -11
  109. data/spec/lib/model/membase/blocked_referer_spec.rb +0 -34
  110. data/spec/lib/model/membase/blocked_user_agent_spec.rb +0 -34
  111. data/spec/lib/model/membase/geo_target_spec.rb +0 -11
  112. data/spec/lib/model/membase/pixel_spec.rb +0 -34
  113. data/spec/lib/model/membase/report_spec.rb +0 -11
  114. data/spec/lib/model/membase/s3_spec.rb +0 -11
  115. data/spec/lib/model/membase/zone_spec.rb +0 -32
  116. data/spec/lib/old_spec.rb +0 -437
data/spec/lib/old_spec.rb DELETED
@@ -1,437 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
- require 'yajl'
3
-
4
- module Neuron
5
- module Client
6
-
7
- describe API do
8
- describe "configure" do
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
14
- config.admin_url = "https://example.com"
15
- config.admin_key = "secret"
16
- end
17
- API.default_api.connection.should be_a(AdminConnection)
18
- end
19
- end
20
- end
21
-
22
- module Model
23
- describe AdZone do
24
- before(:each) do
25
- @connection = stub(:connection)
26
- API.stub(:default_api).and_return(stub(:default_api, :connection => @connection, :connection_type => :admin))
27
- end
28
- describe "create" do
29
- it "makes a post call"
30
- end
31
- end
32
- end
33
-
34
- module Model
35
- describe Ad do
36
- before(:each) do
37
- @connection = stub(:connection)
38
- API.stub(:default_api).and_return(stub(:default_api, :connection => @connection, :connection_type => :admin))
39
- end
40
-
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
52
-
53
- it "makes a get call" do
54
- @connection.should_receive(:get).with("ads").once.and_return(@response)
55
- Ad.all
56
- end
57
- end
58
-
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
69
-
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
74
- end
75
-
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
103
- end
104
-
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
119
- end
120
- end
121
- end
122
-
123
- module Model
124
- describe Zone do
125
- before(:each) do
126
- @connection = stub(:connection)
127
- API.stub(:default_api).and_return(stub(:default_api, :connection => @connection, :connection_type => :admin))
128
- end
129
- describe "all" do
130
- before(:each) do
131
- @response = [{}]
132
- end
133
-
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
141
-
142
- it "makes a get call" do
143
- @connection.should_receive(:get).with("zones").once.and_return(@response)
144
- Zone.all
145
- end
146
- end
147
-
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
163
- end
164
-
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
182
- end
183
-
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
191
- end
192
- end
193
- end
194
-
195
-
196
- module Model
197
- module Common
198
- class TestModel
199
- include Base
200
-
201
- resource_name 'test'
202
- resources_name 'tests'
203
- end
204
- end
205
- end
206
-
207
- module Model
208
- module Admin
209
- class TestModel < Common::TestModel
210
- include Base
211
-
212
- end
213
- end
214
- end
215
-
216
- module Model
217
- class TestModel < Base; end
218
- end
219
-
220
- describe AdminConnection do
221
- before(:each) do
222
- @connection = stub(:connection)
223
- API.stub(:default_api).and_return(stub(:default_api, :connection => @connection, :connection_type => :admin))
224
- end
225
-
226
- describe "update_attributes" do
227
- it "should return false when errors occur for updated objects" do
228
- @connection.should_receive(:put).with("tests/1", {'test' => {}}) do
229
- throw :errors, {:error => "is required"}
230
- end
231
-
232
- Model::TestModel.new(:id => 1).update_attributes({}).should be_false
233
- end
234
-
235
- it "should provide access to errors when validation fails" do
236
- @connection.should_receive(:put).with("tests/1", {'test' => {}}) do
237
- throw :errors, {:error => "is required"}
238
- end
239
-
240
- c = Model::TestModel.new(:id => 1)
241
- c.update_attributes({}).should be_false
242
- c.errors.should == {:error => "is required"}
243
- end
244
-
245
- it "should return true when errors do not occure for updated objects" do
246
- @connection.should_receive(:put).with("tests/1", {'test' => {}})
247
-
248
- Model::TestModel.new(:id => 1).update_attributes({}).should be_true
249
- end
250
- end
251
-
252
- describe "save" do
253
- it "should return false when errors occur for new objects" do
254
- @connection.should_receive(:post).with("tests", {'test' => {}}) do
255
- throw :errors, {:error => "is required"}
256
- end
257
-
258
- Model::TestModel.new.save.should be_false
259
- end
260
-
261
- it "should provide access to errors when validation fails" do
262
- @connection.should_receive(:post).with("tests", {'test' => {}}) do
263
- throw :errors, {:error => "is required"}
264
- end
265
-
266
- c = Model::TestModel.new
267
- c.save.should be_false
268
- c.errors.should == {:error => "is required"}
269
- end
270
-
271
- it "should return true when errors do not occur for new objects" do
272
- @connection.should_receive(:post).with("tests", {'test' => {}}).and_return({'test' => {:id => 1}})
273
-
274
- Model::TestModel.new.save.should be_true
275
- end
276
-
277
- it "should return false when errors occur for existing objects" do
278
- @connection.should_receive(:put).with('tests/1', {'test' => {}}) do
279
- throw :errors, {:error => "is required"}
280
- end
281
-
282
- Model::TestModel.new(:id => 1).save.should be_false
283
- end
284
-
285
- it "should provide access to errors when validation fails" do
286
- @connection.should_receive(:put).with("tests/1", {'test' => {}}) do
287
- throw :errors, {:error => "is required"}
288
- end
289
-
290
- c = Model::TestModel.new(:id => 1)
291
- c.save.should be_false
292
- c.errors.should == {:error => "is required"}
293
- end
294
-
295
- it "should return true when errors do not occur for existing objects" do
296
- @connection.should_receive(:put).with("tests/1", {'test' => {}})
297
-
298
- Model::TestModel.new(:id => 1).save.should be_true
299
- end
300
- end
301
-
302
- describe "create" do
303
- it "should return nil when errors occur" do
304
- @connection.should_receive(:post).with("tests", {'test' => {}}) do
305
- throw :errors, {:error => "is_required"}
306
- end
307
-
308
- Model::TestModel.create({}).should be_nil
309
- end
310
-
311
- it "should return the created object when no errors occur" do
312
- @connection.should_receive(:post).with("tests", {'test' => {}}).and_return({'test' => {:id => 1}})
313
-
314
- Model::TestModel.create({}).should be_a Model::Admin::TestModel
315
- end
316
- end
317
-
318
- describe "create!" do
319
- it "should return nil when errors occur" do
320
- @connection.should_receive(:post).with("tests", {'test' => {}}) do
321
- throw :errors, {:error => "is_required"}
322
- end
323
-
324
- errors = catch(:errors) do
325
- Model::TestModel.create!({})
326
- nil
327
- end
328
- errors.should_not be_nil
329
- errors.should == {:error => 'is_required'}
330
- end
331
-
332
- it "should return the created object when no errors occur" do
333
- @connection.should_receive(:post).with("tests", {'test' => {}}).and_return({'test' => {:id => 1}})
334
-
335
- Model::TestModel.create!({}).should be_a Model::Admin::TestModel
336
- end
337
- end
338
- end
339
-
340
- describe AdminConnection do
341
- before(:each) do
342
- @connection = AdminConnection.new('http://neuron.admin', "my_api_key")
343
- end
344
-
345
- it "should escape the passed api_key" do
346
- connection = AdminConnection.new("http://neuron.admin", "an unescaped string")
347
- FakeWeb.register_uri(:get, "http://neuron.admin/test.json?api_key=an+unescaped+string", :body => Yajl.dump({"escaped" => true}))
348
- connection.get("test").should == {"escaped" => true}
349
- end
350
-
351
- describe "get" do
352
- it "should make a GET request to the specified url passing an API key" do
353
- FakeWeb.register_uri(:get, "http://neuron.admin/test.json", :body => "ERROR", :status => ["403", "Unauthorized"])
354
- FakeWeb.register_uri(:get, "http://neuron.admin/test.json?api_key=my_api_key", :body => "{}")
355
- @connection.get("test").should == {}
356
- end
357
-
358
- it "should GET an error if the wrong api_key is passed" do
359
- FakeWeb.register_uri(:get, "http://neuron.admin/test.json?api_key=new_api_key", :body => "{}")
360
- FakeWeb.register_uri(:get, "http://neuron.admin/test.json?api_key=my_api_key", :body => "ERROR", :status => ["403", "Unauthorized"])
361
- lambda do
362
- @connection.get("test")
363
- end.should raise_error
364
- end
365
- end
366
-
367
- describe "post" do
368
- it "should make a POST request to the specified url passing an API key" do
369
- FakeWeb.register_uri(:post, "http://neuron.admin/test.json", :body => "ERROR", :status => ["403", "Unauthorized"])
370
- FakeWeb.register_uri(:post, "http://neuron.admin/test.json?api_key=my_api_key", :body => "{}", :status => ["201", "Created"])
371
- @connection.post("test", {:data => 1}).should == {}
372
- end
373
-
374
- it "should POST an error if the wrong api_key is passed" do
375
- FakeWeb.register_uri(:post, "http://neuron.admin/test.json?api_key=new_api_key", :body => "{}", :status => ["201", "Created"])
376
- FakeWeb.register_uri(:post, "http://neuron.admin/test.json?api_key=my_api_key", :body => "ERROR", :status => ["403", "Unauthorized"])
377
- lambda do
378
- @connection.post("test", {:data => 1})
379
- end.should raise_error
380
- end
381
-
382
- it "should throw :errors if validation fails" do
383
- FakeWeb.register_uri(:post, "http://neuron.admin/test.json?api_key=my_api_key", :body => Yajl.dump({:my_field => 'is_required'}), :status => ["422", "Errors"])
384
- errors = catch(:errors) do
385
- value = @connection.post("test", {:data => 1})
386
- nil
387
- end
388
- errors.should_not be_nil
389
- errors.should == {'my_field' => 'is_required'}
390
- end
391
- end
392
-
393
- describe "put" do
394
- it "should make a PUT request to the specified url passing an API key" do
395
- FakeWeb.register_uri(:put, "http://neuron.admin/test.json", :body => "ERROR", :status => ["403", "Unauthorized"])
396
- FakeWeb.register_uri(:put, "http://neuron.admin/test.json?api_key=my_api_key", :body => "{}")
397
- @connection.put("test", {:data => 1}).should == {}
398
- end
399
-
400
- it "should PUT an error if the wrong api_key is passed" do
401
- FakeWeb.register_uri(:put, "http://neuron.admin/test.json?api_key=new_api_key", :body => "{}")
402
- FakeWeb.register_uri(:put, "http://neuron.admin/test.json?api_key=my_api_key", :body => "ERROR", :status => ["403", "Unauthorized"])
403
- lambda do
404
- @connection.put("test", {:data => 1})
405
- end.should raise_error
406
- end
407
-
408
- it "should throw :errors if validation fails" do
409
- FakeWeb.register_uri(:put, "http://neuron.admin/test.json?api_key=my_api_key", :body => Yajl.dump({:my_field => 'is_required'}), :status => ["422", "Errors"])
410
- errors = catch(:errors) do
411
- value = @connection.put("test", {:data => 1})
412
- nil
413
- end
414
- errors.should_not be_nil
415
- errors.should == {'my_field' => 'is_required'}
416
- end
417
- end
418
-
419
- describe "delete" do
420
- it "should make a DELETE request to the specified url passing an API key" do
421
- FakeWeb.register_uri(:delete, "http://neuron.admin/test.json", :body => "ERROR", :status => ["403", "Unauthorized"])
422
- FakeWeb.register_uri(:delete, "http://neuron.admin/test.json?api_key=my_api_key", :body => "{}")
423
- @connection.delete("test").should == {}
424
- end
425
-
426
- it "should DELETE an error if the wrong api_key is passed" do
427
- FakeWeb.register_uri(:delete, "http://neuron.admin/test.json?api_key=new_api_key", :body => "{}")
428
- FakeWeb.register_uri(:delete, "http://neuron.admin/test.json?api_key=my_api_key", :body => "ERROR", :status => ["403", "Unauthorized"])
429
- lambda do
430
- @connection.delete("test")
431
- end.should raise_error
432
- end
433
- end
434
- end
435
-
436
- end
437
- end