Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Hey, a spec! Connection#request works now.

Renamed pod.rb to collection.rb
Updated .gitignore
  • Loading branch information...
commit 4162b03f5f00ff33cfc230d1f7994f761e38bb49 1 parent b0b6145
Kyle Kingsbury authored
View
2  .gitignore
@@ -1,4 +1,6 @@
db/
+doc/
+example/
pkg/
log/
._*
View
2  lib/vodpod.rb
@@ -18,7 +18,7 @@ module Vodpod
require "#{ROOT}/vodpod/record"
require "#{ROOT}/vodpod/user"
require "#{ROOT}/vodpod/tag"
- require "#{ROOT}/vodpod/pod"
+ require "#{ROOT}/vodpod/collection"
require "#{ROOT}/vodpod/video"
# Performs URI escaping so that you can construct proper
View
0  lib/vodpod/pod.rb → lib/vodpod/collection.rb
File renamed without changes
View
31 lib/vodpod/connection.rb
@@ -15,13 +15,13 @@ def initialize(params = {})
end
# Request via GET
- def get(*paths, params = {})
- request :get, *paths, params
+ def get(*args)
+ request :get, *args
end
# Request via POST
- def post(*paths, params = {})
- request :post, *paths, params
+ def post(*args)
+ request :post, *args
end
# Perform a JSON request to the Vodpod API for a given path and parameter
@@ -32,15 +32,19 @@ def post(*paths, params = {})
#
# Example
# request :get, :users, :aphyr, :include => [:name]
- def request(method, *paths, params = {})
+ def request(method, *args)
defaults = {
:api_key => @api_key,
:auth => @auth
}
- # Join path fragments
- path = Vodpod::BASE_URI + paths.map{|e| Vodpod::escape(e)}.join('/') + '.json'
-
+ # Get parameters
+ if args.last.kind_of? Hash
+ params = args.pop
+ else
+ params = {}
+ end
+
# Construct query fragment
query = defaults.merge(params).inject('?') { |s, (k, v)|
if v.kind_of? Array
@@ -49,6 +53,9 @@ def request(method, *paths, params = {})
s << "#{Vodpod::escape(k)}=#{Vodpod::escape(v)}&"
}[0..-2]
+ # Join path fragments
+ path = Vodpod::BASE_URI + args.map{|e| Vodpod::escape(e)}.join('/') + '.json'
+
begin
# Get URI
case method
@@ -77,7 +84,13 @@ def request(method, *paths, params = {})
raise Error, "server returned invalid json: #{e.message}" + "\n\n" + res
end
- data
+ # Check for errors
+ if data[0].false?
+ raise Error, data[1]['message']
+ end
+
+ # Return data section
+ data[1]
end
end
end
View
18 spec/test.rb
@@ -0,0 +1,18 @@
+#!/usr/bin/env ruby
+require 'rubygems'
+require 'bacon'
+require File.join(File.dirname(__FILE__), '../lib/vodpod')
+
+module Vodpod
+ describe 'Vodpod' do
+ before do
+ @v = Vodpod.start :api_key => ARGV.first
+ end
+
+ should 'connect to vodpod.com' do
+ should.raise(Error) do
+ @v.get(:me)
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.