panda 0.6.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,232 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Panda::Encoding do
4
+ before(:each) do
5
+
6
+ Panda.configure do |c|
7
+ c.access_key = "my_access_key"
8
+ c.secret_key = "my_secret_key"
9
+ c.api_host = "api.example.com"
10
+ c.cloud_id = 'my_cloud_id'
11
+ c.api_port = 85
12
+ end
13
+
14
+ end
15
+
16
+ it "should find by video_id" do
17
+ encoding_json = "[{\"abc\":\"efg\",\"id\":456}]"
18
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).
19
+ to_return(:body => encoding_json)
20
+ Panda::Encoding.find_all_by_video_id("123").first.id.should == 456
21
+ end
22
+
23
+ it "should create an encoding using instance method" do
24
+ encoding_json = "{\"source_url\":\"my_source_url\",\"id\":\"456\"}"
25
+ stub_http_request(:post, /api.example.com:85\/v2\/encodings.json/).
26
+ with(:body => /source_url=my_source_url/).
27
+ to_return(:body => encoding_json)
28
+
29
+ encoding = Panda::Encoding.new(:source_url => "my_source_url", :video_id => "123")
30
+ encoding.create.should == true
31
+ encoding.id.should == "456"
32
+ end
33
+
34
+ it "should find by encoding_id" do
35
+ encoding_json = "{\"abc\":\"efg\",\"id\":\"456\"}"
36
+ stub_http_request(:get, /api.example.com:85\/v2\/encodings\/456.json/).
37
+ to_return(:body => encoding_json)
38
+ encoding = Panda::Encoding.find("456")
39
+ encoding.id.should == "456"
40
+ end
41
+
42
+ it "should find by the video through the association" do
43
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
44
+ encoding_json = "{\"abc\":\"efg\",\"id\":\"456\", \"video_id\":\"123\"}"
45
+ stub_http_request(:get, /api.example.com:85\/v2\/encodings\/456.json/).
46
+ to_return(:body => encoding_json)
47
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).
48
+ to_return(:body => video_json)
49
+ encoding = Panda::Encoding.find("456")
50
+ encoding.video.id.should == "123"
51
+ encoding.id.should == "456"
52
+ end
53
+
54
+ it "should filter on find" do
55
+ encoding_json = "[{\"source_url\":\"my_source_url\",\"id\":\"456\"}]"
56
+
57
+ stub_http_request(:get, /api.example.com:85\/v2\/encodings.json/).
58
+ with{|r| r.uri.query =~ /profile_name=my_profile/ && r.uri.query =~ /video_id=123/ }.
59
+ to_return(:body => encoding_json)
60
+
61
+ encodings = Panda::Encoding.all(:video_id => "123", :profile_name => "my_profile")
62
+ encodings.first.id.should == "456"
63
+ end
64
+
65
+ it "should return the video_url" do
66
+ cloud_json = "{\"s3_videos_bucket\":\"my_bucket\",\"id\":\"my_cloud_id\"}"
67
+ stub_http_request(:get, /api.example.com:85\/v2\/clouds\/my_cloud_id.json/).
68
+ to_return(:body => cloud_json)
69
+
70
+ encoding = Panda::Encoding.new({:id => "456", :extname => ".ext"})
71
+ encoding.url.should == "http://s3.amazonaws.com/my_bucket/456.ext"
72
+ end
73
+
74
+ it "should generate a screenhost array" do
75
+ cloud_json = "{\"s3_videos_bucket\":\"my_bucket\",\"id\":\"my_cloud_id\"}"
76
+ stub_http_request(:get, /api.example.com:85\/v2\/clouds\/my_cloud_id.json/).
77
+ to_return(:body => cloud_json)
78
+
79
+ encoding = Panda::Encoding.new({:id => "456", :extname => ".ext", :status => "success"})
80
+ encoding.screenshots[0].should == "http://s3.amazonaws.com/my_bucket/456_1.jpg"
81
+ end
82
+
83
+ it "should generate a screenhost array" do
84
+ encoding = Panda::Encoding.new({:id => "456", :extname => ".ext", :status => "fail"})
85
+ encoding.screenshots.should == []
86
+ end
87
+
88
+
89
+ it "should create an encoding through the association" do
90
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
91
+ encoding_json = "{\"abc\":\"efg\",\"id\":\"456\", \"video_id\":\"123\", \"profile_id\":\"901\"}"
92
+
93
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).
94
+ to_return(:body => video_json)
95
+
96
+ stub_http_request(:post, /api.example.com:85\/v2\/encodings.json/).
97
+ with{|r| r.body =~ /video_id=123/ && r.body =~ /profile_id=901/}.
98
+ to_return(:body => encoding_json)
99
+
100
+ video = Panda::Video.find("123")
101
+
102
+ encoding = video.encodings.create(:profile_id => "901")
103
+ encoding.id.should == "456"
104
+ encoding.profile_id.should == "901"
105
+ end
106
+
107
+ it "should filter the profile name after triggering the request" do
108
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
109
+ encodings_1_json = "[{\"id\":\"456\", \"video_id\":\"123\", \"profile_name\":\"h264\"}]"
110
+ encodings_2_json = "[{\"id\":\"789\", \"video_id\":\"123\", \"profile_name\":\"ogg\"}]"
111
+
112
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).
113
+ to_return(:body => video_json)
114
+
115
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).
116
+ with{|r| r.uri.query =~ /profile_name=h264/ }.
117
+ to_return(:body => encodings_1_json)
118
+
119
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).
120
+ with{|r| r.uri.query =~ /profile_name=ogg/ }.
121
+ to_return(:body => encodings_2_json)
122
+
123
+ video = Panda::Video.find("123")
124
+ video.encodings.find_by_profile_name("h264").id.should == "456"
125
+ video.encodings.find_by_profile_name("ogg").id.should == "789"
126
+ end
127
+
128
+ it "should create an encoding through the association" do
129
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
130
+ encodings_json = "[{\"abc\":\"efg\",\"id\":\"456\", \"video_id\":\"123\", \"profile_id\":\"901\"}]"
131
+
132
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).
133
+ to_return(:body => video_json)
134
+
135
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).
136
+ with{|r| r.uri.query =~ /profile_id=901/}.
137
+ to_return(:body => encodings_json)
138
+
139
+ video = Panda::Video.find("123")
140
+ encodings = video.encodings.all(:profile_id => "901")
141
+ encodings.first.id = "456"
142
+ end
143
+
144
+ it "should create an encoding through the association" do
145
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
146
+ encodings_json = "[{\"abc\":\"efg\",\"id\":\"456\", \"video_id\":\"123\", \"profile_id\":\"901\"}]"
147
+
148
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).
149
+ to_return(:body => video_json)
150
+
151
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).
152
+ with{|r| r.uri.query =~ /profile_id=901/}.
153
+ to_return(:body => encodings_json)
154
+
155
+ video = Panda::Video.find("123")
156
+ encodings = video.encodings.profile("901")
157
+ encodings.first.id = "456"
158
+ end
159
+
160
+ it "should filter encodings specifying video and status as a method" do
161
+ encoding_json = "[{\"source_url\":\"my_source_url\",\"id\":\"456\"}]"
162
+
163
+ stub_http_request(:get, /api.example.com:85\/v2\/encodings.json/).
164
+ with{|r| r.uri.query =~ /status=success/ && r.uri.query =~ /video_id=123/ }.
165
+ to_return(:body => encoding_json)
166
+
167
+ encodings = Panda::Encoding.video(123).status("success").all
168
+ encodings.first.id.should == "456"
169
+ end
170
+
171
+ it "should filter encodings specifying video and status as a method" do
172
+ encoding_json = "[{\"source_url\":\"my_source_url\",\"id\":\"456\"}]"
173
+
174
+ stub_http_request(:get, /api.example.com:85\/v2\/encodings.json/).
175
+ with{|r| r.uri.query =~ /profile_id=prof_1/ && r.uri.query =~ /video_id=123/ }.
176
+ to_return(:body => encoding_json)
177
+
178
+ encodings = Panda::Encoding.video(123).profile("prof_1").all
179
+ encodings.first.id.should == "456"
180
+ end
181
+
182
+ it "should filter encodings specifying video and profile id as a method" do
183
+ encoding_json = "[{\"source_url\":\"my_source_url\",\"id\":\"456\"}]"
184
+
185
+ stub_http_request(:get, /api.example.com:85\/v2\/encodings.json/).
186
+ with{|r| r.uri.query =~ /profile_name=prof_name/ && r.uri.query =~ /video_id=123/ }.
187
+ to_return(:body => encoding_json)
188
+
189
+ encodings = Panda::Encoding.video(123).profile_name("prof_name").all
190
+ encodings.first.id.should == "456"
191
+ end
192
+
193
+ it "should find an encoding" do
194
+ encoding_json = "[{\"source_url\":\"my_source_url\",\"id\":\"456\"}]"
195
+ stub_http_request(:get, /api.example.com:85\/v2\/encodings\/456.json/).
196
+ to_return(:body => encoding_json)
197
+
198
+ Panda::Encoding.id("456")
199
+ end
200
+
201
+ it "should tell if the encoding is success" do
202
+ encoding = Panda::Encoding.new({:status => "success"})
203
+ encoding.success?.should == true
204
+ encoding.processing?.should == false
205
+ end
206
+
207
+ it "should tell if the encoding is success" do
208
+ encoding = Panda::Encoding.new({:status => "processing"})
209
+ encoding.success?.should == false
210
+ encoding.processing?.should == true
211
+ end
212
+
213
+ it "should tell if the encoding is success" do
214
+ encoding = Panda::Encoding.new({:status => "fail"})
215
+ encoding.success?.should == false
216
+ encoding.fail?.should == true
217
+ end
218
+
219
+ it "should return the most recent updated encoding" do
220
+ video_json = "[{\"source_url\":\"url_panda.mp4\",\"id\":\"123\"}]"
221
+ stub_http_request(:get, /api.example.com:85\/v2\/encodings.json/).
222
+ with{|r| r.uri.query =~ /per_page=1/ }.
223
+ to_return(:body => video_json)
224
+ Panda::Encoding.first
225
+ end
226
+
227
+ it "should not delegate scope if the method do not really exist in the scope" do
228
+ lambda {Panda::Encoding.reload}.should raise_error(NoMethodError)
229
+ lambda {Panda::Encoding.each}.should raise_error(NoMethodError)
230
+ lambda {Panda::Encoding.size}.should raise_error(NoMethodError)
231
+ end
232
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Panda::Video do
4
+ before(:each) do
5
+ cloud_json = "{\"s3_videos_bucket\":\"my_bucket\",\"id\":\"my_cloud_id\"}"
6
+ stub_http_request(:get, /api.example.com:85\/v2\/clouds\/my_cloud_id.json/).
7
+ to_return(:body => cloud_json)
8
+
9
+ my_heroku_url = "http://access_key:secret_key@api.example.com:85/my_cloud_id"
10
+ Panda.configure do |c|
11
+ c.heroku = my_heroku_url
12
+ end
13
+ end
14
+
15
+ it "should get all videos" do
16
+ videos_json = "[]"
17
+ stub_http_request(:get, /api.example.com:85\/v2\/videos.json/).to_return(:body => videos_json)
18
+
19
+ Panda::Video.all.should be_empty
20
+ end
21
+
22
+ end
data/spec/panda_spec.rb CHANGED
@@ -2,7 +2,6 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe Panda do
4
4
  before(:each) do
5
- Time.stub!(:now).and_return(mock("time", :iso8601 => "2009-11-04T17:54:11+00:00"))
6
5
  end
7
6
 
8
7
  describe "when not connected" do
@@ -120,14 +119,14 @@ describe Panda do
120
119
  before(:each) do
121
120
  @panda = Panda.connect!({:access_key => "my_access_key", :secret_key => "my_secret_key", :api_host => "myapihost", :api_port => 85, :cloud_id => 'my_cloud_id', :format => "json" })
122
121
  end
122
+
123
123
  it_should_behave_like "Connected"
124
124
  end
125
125
 
126
126
 
127
127
  describe "Panda.connect with PANDASTREAM_URL" do
128
128
  before(:each) do
129
- Panda.connect!('http://my_access_key:my_secret_key@myapihost:85/my_cloud_id', "format" => "json")
130
- @panda = Panda
129
+ @panda = Panda.connect!('http://my_access_key:my_secret_key@myapihost:85/my_cloud_id', "format" => "json")
131
130
  end
132
131
  it_should_behave_like "Connected"
133
132
  end
@@ -165,8 +164,22 @@ describe Panda do
165
164
 
166
165
  ActiveSupport::JSON.should_receive(:decode).with("abc").and_return("blah")
167
166
  @panda.get("/videos").should == "blah"
167
+
168
+ Object.send :remove_const, :ActiveSupport
168
169
  end
169
170
  end
170
171
 
171
-
172
+ describe "parsing" do
173
+ it "should raise an error if the response is not JSON parsable" do
174
+ @connection = Panda::Connection.new({"access_key" => "my_access_key", "secret_key" => "my_secret_key", "api_host" => "myapihost", "api_port" => 85, "cloud_id" => 'my_cloud_id' })
175
+ @connection.raise_error=true
176
+
177
+ stub_http_request(:get, //).to_return(:body => "blahblah")
178
+
179
+ lambda {
180
+ @connection.get("/fake")
181
+ }.should raise_error(Panda::ServiceNotAvailable)
182
+ end
183
+ end
184
+
172
185
  end
@@ -0,0 +1,117 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Panda::Profile do
4
+ before(:each) do
5
+ cloud_json = "{\"s3_videos_bucket\":\"my_bucket\",\"id\":\"my_cloud_id\"}"
6
+ stub_http_request(:get, /api.example.com:85\/v2\/clouds\/my_cloud_id.json/).
7
+ to_return(:body => cloud_json)
8
+
9
+ Panda.configure do |c|
10
+ c.access_key = "my_access_key"
11
+ c.secret_key = "my_secret_key"
12
+ c.api_host = "api.example.com"
13
+ c.cloud_id = 'my_cloud_id'
14
+ c.api_port = 85
15
+ end
16
+
17
+ end
18
+
19
+ it "should create a profile" do
20
+ profile_json = "{\"title\":\"my_profile\",\"id\":\"123\"}"
21
+ stub_http_request(:post, /api.example.com:85\/v2\/profiles.json/).
22
+ with(:body => /title=my_profile/).
23
+ to_return(:body => profile_json)
24
+
25
+ profile = Panda::Profile.new(:title => "my_profile")
26
+
27
+ profile.new?.should == true
28
+ profile.changed?.should == true
29
+ profile.save.should == true
30
+ profile.changed?.should == false
31
+ profile.id.should == "123"
32
+ profile.new?.should == false
33
+
34
+ profile.changed?.should == false
35
+
36
+ profile.title = "new_last_title"
37
+ profile.changed?.should == true
38
+ end
39
+
40
+
41
+ it "should update a profile and sending the changed attributes" do
42
+ profile_json = "{\"title\":\"my_profile\",\"id\":\"123\"}"
43
+ stub_http_request(:put, /api.example.com:85\/v2\/profiles\/999.json/).
44
+ with{|r| !(r.body =~ /title=my_new_profile_title/) && r.body =~ /width=80/}.
45
+ to_return(:body => profile_json)
46
+
47
+ profile = Panda::Profile.new(:id => "999", :title => "my_profile_title")
48
+
49
+ profile.width=80
50
+ profile.new?.should == false
51
+ profile.save.should == true
52
+ end
53
+
54
+
55
+ it "should not call update a profile" do
56
+ profile_json = "{\"title\":\"my_profile\",\"id\":\"123\"}"
57
+ stub_http_request(:put, /api.example.com:85\/v2\/profiles\/123.json/).
58
+ with(:body => /title=my_profile/).
59
+ to_return(:body => profile_json)
60
+
61
+ profile = Panda::Profile.new(:id => "123")
62
+ profile.title = "my_profile"
63
+
64
+ profile.new?.should == false
65
+ profile.save.should == true
66
+ end
67
+
68
+
69
+ it "should have a many relation on encodings" do
70
+ encoding_json = "[{\"abc\":\"efg\",\"id\":456}]"
71
+ profile_json = "{\"title\":\"my_profile\",\"id\":\"901\"}"
72
+ stub_http_request(:get, /api.example.com:85\/v2\/profiles\/901\/encodings.json/).
73
+ to_return(:body => encoding_json)
74
+
75
+ profile = Panda::Profile.new(:title => "my_source_url", :id => "901")
76
+ profile.encodings.first.id.should == 456
77
+ end
78
+
79
+ it "should reload the object" do
80
+ profile_json = "{\"title\":\"my_profile\",\"id\":\"123\"}"
81
+ stub_http_request(:get, /api.example.com:85\/v2\/profiles\/123.json/).
82
+ to_return(:body => profile_json)
83
+
84
+ profile = Panda::Profile.new(:id => "123", :title => "my_new_profile_title")
85
+ profile.title.should == "my_new_profile_title"
86
+ profile.reload
87
+ profile.id.should == "123"
88
+ profile.title.should == "my_profile"
89
+ end
90
+
91
+ it "shoud raise an exeception if it's a new object" do
92
+ profile = Panda::Profile.new(:title => "my_new_profile_title")
93
+ lambda {
94
+ profile.reload
95
+ }.should raise_error("RecordNotFound")
96
+ end
97
+
98
+ it "shoud raise an exeception if it's a new object" do
99
+ profile_json = "{\"title\":\"my_profile\",\"id\":\"123\"}"
100
+ stub_http_request(:get, /api.example.com:85\/v2\/profiles\/123.json/).
101
+ to_return(:body => profile_json)
102
+
103
+ profile = Panda::Profile.find(123)
104
+ profile.reload.should == profile
105
+ end
106
+
107
+ it "should not delegate scope if the method do not really exist in the scope" do
108
+ lambda {Panda::Profile.reload}.should raise_error(NoMethodError)
109
+ lambda {Panda::Profile.each}.should raise_error(NoMethodError)
110
+ lambda {Panda::Profile.size}.should raise_error(NoMethodError)
111
+ end
112
+
113
+ it "should tell if profile is using a preset" do
114
+ Panda::Profile.new(:title => "abc").preset?.should be_true
115
+ Panda::Profile.new(:preset_name => "abc").preset?.should be_false
116
+ end
117
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,7 +7,14 @@ require 'spec/autorun'
7
7
  require 'webmock/rspec'
8
8
  include WebMock
9
9
 
10
+ def hputs(*args)
11
+ puts ERB::Util.html_escape(args.join("\n")).gsub(/\r?\n/, '<br/>') + '<br/>'
12
+ end
10
13
 
11
14
  Spec::Runner.configure do |config|
12
-
15
+ config.before(:each) do
16
+ Panda.instance_variable_set("@connection", nil)
17
+ Panda.instance_variable_set("@cloud", nil)
18
+ Time.stub!(:now).and_return(mock("time", :iso8601 => "2009-11-04T17:54:11+00:00"))
19
+ end
13
20
  end
@@ -0,0 +1,305 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Panda::Video do
4
+ before(:each) do
5
+
6
+ Panda.configure do |c|
7
+ c.access_key = "my_access_key"
8
+ c.secret_key = "my_secret_key"
9
+ c.api_host = "api.example.com"
10
+ c.cloud_id = 'my_cloud_id'
11
+ c.api_port = 85
12
+ end
13
+
14
+ end
15
+
16
+ it "should create a video object" do
17
+ v = Panda::Video.new({ :test => "abc" })
18
+ v.test.should == "abc"
19
+ end
20
+
21
+ it "should tell video is new" do
22
+ v = Panda::Video.new({ :id => "abc" })
23
+ v.new?.should be_false
24
+ end
25
+
26
+ it "should not tell video is new" do
27
+ v = Panda::Video.new({ :attr => "abc" })
28
+ v.new?.should be_true
29
+ end
30
+
31
+ it "should find return all videos" do
32
+
33
+ videos_json = "[{\"source_url\":\"my_source_url\",\"id\":111},{\"source_url\":\"http://a.b.com/file2.mp4\",\"id\":222}]"
34
+
35
+ stub_http_request(:get, /api.example.com:85\/v2\/videos.json/).to_return(:body => videos_json)
36
+
37
+ videos = Panda::Video.all
38
+ videos.first.id.should == 111
39
+ videos.first.source_url.should == "my_source_url"
40
+ videos.size.should == 2
41
+ end
42
+
43
+ it "should find a videos having the correct id" do
44
+
45
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
46
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).to_return(:body => video_json)
47
+
48
+ video = Panda::Video.find("123")
49
+ video.id.should == "123"
50
+ video.source_url.should == "my_source_url"
51
+ end
52
+
53
+
54
+ it "should list all video's encodings" do
55
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
56
+ encodings_json = "[{\"abc\":\"my_source_url\",\"id\":\"456\"}]"
57
+
58
+ encodings = [Panda::Encoding.new({:abc => "my_source_url", :id => "456"})]
59
+
60
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).to_return(:body => video_json)
61
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).to_return(:body => encodings_json)
62
+
63
+ video = Panda::Video.find("123")
64
+ video.encodings.first.attributes.should == encodings.first.attributes
65
+ end
66
+
67
+ it "should delete a video using class" do
68
+ video_json = "{\"deleted\":\"ok\"}"
69
+ stub_http_request(:delete, /api.example.com:85\/v2\/videos\/123.json/).to_return(:body => video_json)
70
+
71
+ video = Panda::Video.new(:source_url => "my_source_url", :id => "123")
72
+ video.cloud
73
+ video.delete.should == true
74
+ end
75
+
76
+ it "should delete a video using instance" do
77
+ video_json = "{\"deleted\":\"ok\"}"
78
+ stub_http_request(:delete, /api.example.com:85\/v2\/videos\/123.json/).to_return(:body => video_json)
79
+
80
+ Panda::Video.delete("123")
81
+ end
82
+
83
+ it "should have an error object if something goes wrong" do
84
+ response = "{\"message\":\"no-abc\",\"error\":\"error-abc\"}"
85
+
86
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/abc.json/).to_return(:body => response)
87
+
88
+ lambda {
89
+ Panda::Video.find "abc"
90
+ }.should raise_error("error-abc: no-abc")
91
+ end
92
+
93
+ it "should have an error object if something goes wrong" do
94
+ response = "{\"message\":\"no-abc\",\"error\":\"error-abc\"}"
95
+
96
+ stub_http_request(:put, /api.example.com:85\/v2\/profiles\/abc.json/).to_return(:body => response)
97
+
98
+ obj = Panda::Profile.new(:id => "abc")
99
+ original_attrs = obj.attributes
100
+ obj.save
101
+
102
+ obj.errors.size.should == 1
103
+ obj.errors.first.message.should == "no-abc"
104
+ obj.errors.first.error_class.should == "error-abc"
105
+ obj.attributes.should == original_attrs
106
+
107
+ end
108
+
109
+ it "should connect to eu" do
110
+
111
+
112
+ Panda.configure do |c|
113
+ c.access_key = "my_access_key"
114
+ c.secret_key = "my_secret_key"
115
+ c.cloud_id = 'my_cloud_id'
116
+ c.region = "eu"
117
+ end
118
+
119
+ stub_http_request(:get, /api.eu.pandastream.com:80/).
120
+ to_return(:body => "{\"id\":\"123\"}")
121
+ Panda::Video.find "123"
122
+ end
123
+
124
+ it "should connect to eu and trigger the request" do
125
+ cloud_json = "{\"s3_videos_bucket\":\"my_bucket\",\"id\":\"my_cloud_id\"}"
126
+ stub_http_request(:get, /api.eu.pandastream.com:80\/v2\/clouds\/my_cloud_id.json/).
127
+ to_return(:body => cloud_json)
128
+
129
+ Panda.configure do |c|
130
+ c.access_key = "my_access_key"
131
+ c.secret_key = "my_secret_key"
132
+ c.cloud_id = 'my_cloud_id'
133
+ c.region = "eu"
134
+ end
135
+
136
+ stub_http_request(:get, /api.eu.pandastream.com:80/).
137
+ to_return(:body => "{\"id\":\"123\"}")
138
+ Panda::Video.find "123"
139
+ end
140
+
141
+ it "should use the correct connection" do
142
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
143
+ cloud_json = "{\"s3_videos_bucket\":\"my_bucket\",\"id\":\"cloud1\"}"
144
+ cloud2_json = "{\"s3_videos_bucket\":\"my_bucket\",\"id\":\"cloud2\"}"
145
+
146
+ stub_http_request(:get, /api.example.com:85\/v2\/clouds\/cloud1.json/).
147
+ to_return(:body => cloud_json)
148
+ stub_http_request(:get, /api.example.com:85\/v2\/clouds\/cloud2.json/).
149
+ to_return(:body => cloud2_json)
150
+
151
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).
152
+ to_return(:body => video_json)
153
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).
154
+ to_return(:body => video_json)
155
+
156
+ cloud = Panda::Cloud.new(:id => "cloud1")
157
+
158
+ cloud2 = Panda::Cloud.new(:id => "cloud2")
159
+
160
+ video = cloud.videos.find("123")
161
+ video2 = cloud2.videos.find("123")
162
+
163
+ video.cloud.id.should == "cloud1"
164
+ video2.cloud.id.should == "cloud2"
165
+
166
+ Panda::Video.cloud.id.should == "my_cloud_id"
167
+ end
168
+
169
+ it "should create a video using class method" do
170
+ video_json = "{\"source_url\":\"url_panda.mp4\",\"id\":\"123\"}"
171
+
172
+ stub_http_request(:post, /api.example.com:85\/v2\/videos.json/).
173
+ with(:body => /source_url=url_panda.mp4/).
174
+ to_return(:body => video_json)
175
+
176
+ video = Panda::Video.create(:source_url => "url_panda.mp4")
177
+ video.id.should == "123"
178
+ end
179
+
180
+ it "should return a json on attributes" do
181
+ video = Panda::Video.new(:attr => "value")
182
+ video.to_json.should == video.attributes.to_json
183
+ end
184
+
185
+ it "should create an encoding using video scope" do
186
+ encoding_json = "{\"source_url\":\"my_source_url\",\"id\":\"678\"}"
187
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
188
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).
189
+ to_return(:body => video_json)
190
+
191
+ stub_http_request(:post, /api.example.com:85\/v2\/encodings.json/).
192
+ with(:body => /profile_id=345/).
193
+ to_return(:body => encoding_json)
194
+
195
+ video = Panda::Video.find "123"
196
+ encoding = video.encodings.create(:profile_id => "345")
197
+ encoding.id.should == "678"
198
+ end
199
+
200
+ it "should not create a model having an id" do
201
+ video = Panda::Video.new(:id => "abc")
202
+ lambda {
203
+ video.create
204
+ }.should raise_error "Can't create attribute. Already have an id=abc"
205
+ end
206
+
207
+ it "should not create a model having an id" do
208
+ lambda {
209
+ Panda::Video.create(:id => "abc")
210
+ }.should raise_error "Can't create attribute. Already have an id=abc"
211
+ end
212
+
213
+ it "should not call the request twice" do
214
+ video_json = "{\"source_url\":\"url_panda.mp4\",\"id\":\"123\"}"
215
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).to_return(:body => video_json)
216
+ video = Panda::Video.find("123")
217
+
218
+ encodings_json = "[{\"abc\":\"my_source_url\",\"id\":\"456\"}]"
219
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).to_return(:body => encodings_json)
220
+
221
+ encodings = video.encodings
222
+ encodings.first
223
+ encodings.first
224
+
225
+ WebMock.should have_requested(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).once
226
+ end
227
+
228
+ it "should call the request if the scope has changed" do
229
+ video_json = "{\"source_url\":\"url_panda.mp4\",\"id\":\"123\"}"
230
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).to_return(:body => video_json)
231
+ video = Panda::Video.find("123")
232
+
233
+ encodings_json = "[{\"abc\":\"my_source_url\",\"id\":\"456\"}]"
234
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).to_return(:body => encodings_json)
235
+
236
+ encodings = video.encodings.status("success")
237
+
238
+ encodings.first
239
+ encodings.last
240
+
241
+ video.encodings.first
242
+
243
+ WebMock.should have_requested(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).once
244
+ end
245
+
246
+ it "should not call the request twice" do
247
+ video_json = "{\"source_url\":\"url_panda.mp4\",\"id\":\"123\"}"
248
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).to_return(:body => video_json)
249
+ video = Panda::Video.find("123")
250
+
251
+ encodings_json = "[{\"abc\":\"my_source_url\",\"id\":\"456\"}]"
252
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).to_return(:body => encodings_json)
253
+
254
+ encodings = video.encodings
255
+ encodings.first.id.should == "456"
256
+ encodings.reload
257
+
258
+ WebMock.should have_requested(:get, /api.example.com:85\/v2\/videos\/123\/encodings.json/).twice
259
+ end
260
+
261
+ it "should tell if the video is success" do
262
+ encoding = Panda::Video.new({:status => "success"})
263
+ encoding.success?.should == true
264
+ encoding.processing?.should == false
265
+ end
266
+
267
+ it "should tell if the video is success" do
268
+ encoding = Panda::Video.new({:status => "processing"})
269
+ encoding.success?.should == false
270
+ encoding.processing?.should == true
271
+ end
272
+
273
+ it "should tell if the video is success" do
274
+ encoding = Panda::Video.new({:status => "fail"})
275
+ encoding.success?.should == false
276
+ encoding.fail?.should == true
277
+ end
278
+
279
+ it "should return the most recent updated video" do
280
+ video_json = "[{\"source_url\":\"url_panda.mp4\",\"id\":\"123\"}]"
281
+ stub_http_request(:get, /api.example.com:85\/v2\/videos.json/).
282
+ with{|r| r.uri.query =~ /per_page=1/ }.
283
+ to_return(:body => video_json)
284
+ Panda::Video.first
285
+ end
286
+
287
+ it "should not delegate scope if the method do not really exist in the scope" do
288
+ lambda {Panda::Video.reload}.should raise_error(NoMethodError)
289
+ lambda {Panda::Video.each}.should raise_error(NoMethodError)
290
+ lambda {Panda::Video.size}.should raise_error(NoMethodError)
291
+ end
292
+
293
+ it "should lazy load the cloud" do
294
+ cloud_json = "{\"s3_videos_bucket\":\"my_bucket\",\"id\":\"my_cloud_id\"}"
295
+ stub_http_request(:get, /api.example.com:85\/v2\/clouds\/my_cloud_id.json/).
296
+ to_return(:body => cloud_json)
297
+
298
+ video_json = "{\"source_url\":\"my_source_url\",\"id\":\"123\"}"
299
+ stub_http_request(:get, /api.example.com:85\/v2\/videos\/123.json/).
300
+ to_return(:body => video_json)
301
+
302
+ video = Panda::Video.find "123"
303
+ video.cloud.s3_videos_bucket.should == "my_bucket"
304
+ end
305
+ end