Class: Vodpod::Connection
- Inherits:
-
Object
- Object
- Vodpod::Connection
- Defined in:
- lib/vodpod/connection.rb
Overview
Connection to vodpod.com; retreives JSON data and handles API key/auth.
Constant Summary collapse
- TIMEOUT =
5
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#auth ⇒ Object
Returns the value of attribute auth.
Instance Method Summary collapse
-
#collection(user, collection, *args) ⇒ Object
Gets a collection by user and collection key.
-
#collections(user, *args) ⇒ Object
Gets collections belonging to a user.
-
#get(*args) ⇒ Object
Request via GET.
-
#initialize(params = {}) ⇒ Connection
constructor
Creates a new connection.
-
#me(*args) ⇒ Object
Returns the user associated with this API key.
-
#post(*args) ⇒ Object
Request via POST.
-
#ready? ⇒ Boolean
Pings the API with our API key to check whether or not we are ready to make requests.
-
#request(method, *args) ⇒ Object
Perform a JSON request to the Vodpod API for a given path and parameter hash.
-
#search(query, opts = {}) ⇒ Object
Searches for videos.
-
#user(key, *args) ⇒ Object
Gets a user by key.
-
#video(*args) ⇒ Object
Retrieves a specific video by key.
-
#videos(*args) ⇒ Object
Gets a list of videos Three senses:.
Constructor Details
#initialize(params = {}) ⇒ Connection
Creates a new connection. Parameters:
:api_key => API key :auth_key => Auth key :timeout => How many seconds to wait before giving up on API calls.
13 14 15 16 17 |
# File 'lib/vodpod/connection.rb', line 13 def initialize(params = {}) @api_key = params[:api_key] @auth_key = params[:auth_key] @timeout = params[:timeout] || TIMEOUT end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
5 6 7 |
# File 'lib/vodpod/connection.rb', line 5 def api @api end |
#auth ⇒ Object
Returns the value of attribute auth.
6 7 8 |
# File 'lib/vodpod/connection.rb', line 6 def auth @auth end |
Instance Method Details
#collection(user, collection, *args) ⇒ Object
Gets a collection by user and collection key.
20 21 22 |
# File 'lib/vodpod/connection.rb', line 20 def collection(user, collection, *args) Collection.new self, get(:users, user, :collections, collection, *args) end |
#collections(user, *args) ⇒ Object
Gets collections belonging to a user.
25 26 27 |
# File 'lib/vodpod/connection.rb', line 25 def collections(user, *args) RecordSet.new self, Collection, get(:users, user, :collections, *args) end |
#get(*args) ⇒ Object
Request via GET
30 31 32 |
# File 'lib/vodpod/connection.rb', line 30 def get(*args) request :get, *args end |
#me(*args) ⇒ Object
Returns the user associated with this API key.
35 36 37 |
# File 'lib/vodpod/connection.rb', line 35 def me(*args) User.new self, get(:me, *args) end |
#post(*args) ⇒ Object
Request via POST
40 41 42 |
# File 'lib/vodpod/connection.rb', line 40 def post(*args) request :post, *args end |
#ready? ⇒ Boolean
Pings the API with our API key to check whether or not we are ready to make requests.
46 47 48 |
# File 'lib/vodpod/connection.rb', line 46 def ready? get or false end |
#request(method, *args) ⇒ Object
Perform a JSON request to the Vodpod API for a given path and parameter hash. Returns a parsed JSON document. Automatically provides api_key and auth params if you do not specify them. Method should be one of :get or :post–you should use the #get or #post methods for convenience. Array values for parameters are joined by commas.
Example
request :get, :users, :aphyr, :include => [:name]
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/vodpod/connection.rb', line 58 def request(method, *args) defaults = { :api_key => @api_key, :auth_key => @auth } # 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 v = v.join(',') end 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 when :get # GET request uri = URI.parse(path + query) res = Net::HTTP.start(uri.host, uri.port) do |http| http.open_timeout = @timeout http.read_timeout = @timeout http.get(uri.path + query) end when :post # POST request uri = URI.parse(path) res = Net::HTTP.start(uri.host, uri.port) do |http| http.open_timeout = @timeout http.read_timeout = @timeout http.post(uri.path, query[1..-1]) end else # Don't know how to do that kind of request raise Error.new("Unsupported request method #{method.inspect}; should be one of :get, :post.") end rescue => e raise Error.new("Error retrieving #{uri.path}#{query}: #{e.}") end # Parse response as JSON begin data = JSON.parse res.body rescue => e raise Error, "server returned invalid json: #{e.}" + "\n\n" + res end # Check for errors if data[0] == false raise Error, data[1]['message'] end # Return data section data[1] end |
#search(query, opts = {}) ⇒ Object
Searches for videos
126 127 128 129 |
# File 'lib/vodpod/connection.rb', line 126 def search(query, opts = {}) opts = {:query => query}.merge opts RecordSet.new self, Video, get(:search, opts) end |
#user(key, *args) ⇒ Object
Gets a user by key
132 133 134 |
# File 'lib/vodpod/connection.rb', line 132 def user(key, *args) User.new self, get(:users, key, *args) end |
#video(*args) ⇒ Object
Retrieves a specific video by key. Three senses:
-
video(video) => Video
-
video(user, video) => CollectionVideo
-
video(user, collection, video) => CollectionVideo
Which sense is determined by the index of the integer video key, so you can safely chain calls like:
video(123, :comments, :page => 2)
to get the 2nd page of comments associated with the video.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/vodpod/connection.rb', line 149 def video(*args) key = args.find { |e| Integer === e } i = args.index(key) case i when 0 Video.new self, get(:videos, *args) when 1 CollectionVideo.new self, get(:users, args.shift, :videos, *args) when 2 CollectionVideo.new self, get(:users, args.shift, :collections, args.shift, :videos, *args) else raise ArgumentError, "usage: video(video), video(user, video), video(user, collection, video)" end end |
#videos(*args) ⇒ Object
Gets a list of videos Three senses:
-
videos() => An array of Videos
-
videos(user) => An array of CollectionVideos
-
videos(user, collection) => An array of CollectionVideos
The sense is determined by the number of arguments before a hash (or if no options hash is given, the number of arguments).
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/vodpod/connection.rb', line 174 def videos(*args) opts = args.find { |e| Hash === e } i = args.index(opts) || args.size case i when 0 RecordSet.new self, Video, get(:videos, *args) when 1 RecordSet.new self, CollectionVideo, get(:users, args.shift, :videos, *args) when 2 RecordSet.new self, CollectionVideo, get(:users, args.shift, :collections, args.shift, :videos, *args) else raise ArgumentError, "usage: videos(), videos(user), videos(user, collection)" end end |