viddler-ruby 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/viddler/api_exception.rb +13 -0
- data/lib/viddler/client.rb +14 -0
- data/lib/viddler/version.rb +1 -1
- data/lib/viddler-ruby.rb +2 -1
- data/spec/api_exception_spec.rb +29 -0
- data/spec/client_spec.rb +15 -3
- metadata +6 -4
@@ -0,0 +1,13 @@
|
|
1
|
+
module Viddler
|
2
|
+
class ApiException < ::StandardError
|
3
|
+
attr_accessor :code, :description, :details
|
4
|
+
|
5
|
+
def initialize(code, description, details)
|
6
|
+
self.code = code.to_i
|
7
|
+
self.description = description
|
8
|
+
self.details = details
|
9
|
+
|
10
|
+
super("\##{self.code}: #{self.description} (#{self.details})")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/viddler/client.rb
CHANGED
@@ -77,6 +77,8 @@ module Viddler
|
|
77
77
|
arguments[:api_key] = api_key
|
78
78
|
arguments[:sessionid] = sessionid if authenticated?
|
79
79
|
JSON.parse RestClient.get(DEFAULT_ENDPOINT + method + '.json', :params => arguments)
|
80
|
+
rescue RestClient::ExceptionWithResponse => e
|
81
|
+
raise_api_exception e
|
80
82
|
end
|
81
83
|
|
82
84
|
# Public: Make a POST call to the Viddler API.
|
@@ -95,6 +97,8 @@ module Viddler
|
|
95
97
|
arguments[:api_key] = api_key
|
96
98
|
arguments[:sessionid] = sessionid if authenticated?
|
97
99
|
JSON.parse RestClient.post(DEFAULT_ENDPOINT + method + '.json', :params => arguments)
|
100
|
+
rescue RestClient::ExceptionWithResponse => e
|
101
|
+
raise_api_exception e
|
98
102
|
end
|
99
103
|
|
100
104
|
# Public: Upload a video to the Viddler API.
|
@@ -130,6 +134,16 @@ module Viddler
|
|
130
134
|
ordered_arguments[:file] = file
|
131
135
|
|
132
136
|
JSON.parse RestClient.post(endpoint, ordered_arguments)
|
137
|
+
rescue RestClient::ExceptionWithResponse => e
|
138
|
+
raise_api_exception e
|
139
|
+
end
|
140
|
+
|
141
|
+
private
|
142
|
+
|
143
|
+
def raise_api_exception(exception)
|
144
|
+
resp = JSON.parse(exception.response)
|
145
|
+
|
146
|
+
raise Viddler::ApiException.new(resp['error']['code'], resp['error']['description'], resp['error']['details']) if resp['error']
|
133
147
|
end
|
134
148
|
end
|
135
149
|
end
|
data/lib/viddler/version.rb
CHANGED
data/lib/viddler-ruby.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Viddler::ApiException, ".new" do
|
4
|
+
before(:each) do
|
5
|
+
@exception = Viddler::ApiException.new("300", "The Description", "The Details")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "sets code" do
|
9
|
+
@exception.code.should == 300
|
10
|
+
end
|
11
|
+
|
12
|
+
it "sets description" do
|
13
|
+
@exception.description.should == "The Description"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "sets details" do
|
17
|
+
@exception.details.should == "The Details"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Viddler::ApiException, ".to_s" do
|
22
|
+
before(:each) do
|
23
|
+
@exception = Viddler::ApiException.new("300", "The Description", "The Details")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns proper string" do
|
27
|
+
@exception.to_s.should == '#300: The Description (The Details)'
|
28
|
+
end
|
29
|
+
end
|
data/spec/client_spec.rb
CHANGED
@@ -67,7 +67,11 @@ describe Viddler::Client, "#get" do
|
|
67
67
|
@client.get('viddler.api.getInfo')
|
68
68
|
end
|
69
69
|
|
70
|
-
it "raises ApiException if an error occurs"
|
70
|
+
it "raises ApiException if an error occurs" do
|
71
|
+
error = RestClient::ExceptionWithResponse.new '{"error":{"code":"9","description":"session invalid","details":"details"}}'
|
72
|
+
RestClient.stub!(:get).and_raise(error)
|
73
|
+
lambda {@client.get('viddler.api.getInfo')}.should raise_error(Viddler::ApiException, "#9: session invalid (details)")
|
74
|
+
end
|
71
75
|
|
72
76
|
context "with authenticated client" do
|
73
77
|
before(:each) do
|
@@ -102,7 +106,11 @@ describe Viddler::Client, "#post" do
|
|
102
106
|
@client.post('viddler.api.getInfo')
|
103
107
|
end
|
104
108
|
|
105
|
-
it "raises ApiException if an error occurs"
|
109
|
+
it "raises ApiException if an error occurs" do
|
110
|
+
error = RestClient::ExceptionWithResponse.new '{"error":{"code":"9","description":"session invalid","details":"details"}}'
|
111
|
+
RestClient.stub!(:post).and_raise(error)
|
112
|
+
lambda {@client.post('viddler.api.getInfo')}.should raise_error(Viddler::ApiException, "#9: session invalid (details)")
|
113
|
+
end
|
106
114
|
|
107
115
|
context "with authenticated client" do
|
108
116
|
before(:each) do
|
@@ -151,5 +159,9 @@ describe Viddler::Client, "#upload" do
|
|
151
159
|
@client.upload(@file, :param1 => 'asdf').should == 'asdfasdf'
|
152
160
|
end
|
153
161
|
|
154
|
-
it "raises an ApiException on API error"
|
162
|
+
it "raises an ApiException on API error" do
|
163
|
+
error = RestClient::ExceptionWithResponse.new '{"error":{"code":"9","description":"session invalid","details":"details"}}'
|
164
|
+
RestClient.stub!(:post).and_raise(error)
|
165
|
+
lambda {@client.upload(@file, :param1 => 'asdf')}.should raise_error(Viddler::ApiException, "#9: session invalid (details)")
|
166
|
+
end
|
155
167
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: viddler-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.3
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kyle Slattery
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-12-
|
18
|
+
date: 2010-12-15 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -96,8 +96,10 @@ files:
|
|
96
96
|
- Rakefile
|
97
97
|
- autotest/discover.rb
|
98
98
|
- lib/viddler-ruby.rb
|
99
|
+
- lib/viddler/api_exception.rb
|
99
100
|
- lib/viddler/client.rb
|
100
101
|
- lib/viddler/version.rb
|
102
|
+
- spec/api_exception_spec.rb
|
101
103
|
- spec/client_spec.rb
|
102
104
|
- spec/spec_helper.rb
|
103
105
|
- viddler-ruby.gemspec
|