Class: Howcast::Client
- Inherits:
-
Object
- Object
- Howcast::Client
- Defined in:
- lib/howcast/client.rb,
lib/howcast/client/base.rb,
lib/howcast/client/video.rb,
lib/howcast/client/search.rb,
lib/howcast/client/category.rb
Overview
– Copyright © 2008 Howcast Media Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++
Defined Under Namespace
Modules: WatchAttrAccessors Classes: Category, Video
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
-
#advanced_search(params) ⇒ Object
Provides low-level access to the Howcast video search API.
-
#category(id) ⇒ Object
Provides access to the Howcast category API.
-
#initialize(options = {}) ⇒ Client
constructor
Creates a new howcast client to interact with the Howcast API.
-
#search(query, options = {}) ⇒ Object
Provides access to the Howcast video search API.
-
#video(id) ⇒ Object
Provides access to the Howcast video API.
-
#videos(options = {}) ⇒ Object
Provides access to the Howcast list videos API.
Constructor Details
#initialize(options = {}) ⇒ Client
Creates a new howcast client to interact with the Howcast API
Inputs
Options include:
-
key
– REQUIRED - API key: obtainable from www.howcast.com/api_keys/new
Exceptions
-
Howcast::ApiKeyNotFound
– raised if the options value is nil
43 44 45 46 |
# File 'lib/howcast/client/base.rb', line 43 def initialize(={}) raise Howcast::ApiKeyNotFound if [:key].nil? @key = [:key] end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
30 31 32 |
# File 'lib/howcast/client/base.rb', line 30 def key @key end |
Class Method Details
.base_uri ⇒ Object
58 59 60 |
# File 'lib/howcast/client/base.rb', line 58 def base_uri @base_uri ||= URI.parse("http://www.howcast.com") end |
.base_uri=(new_base_uri) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/howcast/client/base.rb', line 49 def base_uri= new_base_uri @base_uri = case new_base_uri when URI then new_base_uri when String then URI.parse new_base_uri when Hash then URI::HTTP.build new_base_uri else; raise ArgumentError, "can't convert URI: #{new_base_uri.inspect}" end end |
Instance Method Details
#advanced_search(params) ⇒ Object
Provides low-level access to the Howcast video search API.
Inputs
-
params
– A hash of params that will be URL encoded and appended to the URI. You’ll have to know what you’re doing.
Other than its arguments, this method is identical to search
.
69 70 71 |
# File 'lib/howcast/client/search.rb', line 69 def advanced_search params do_search params end |
#category(id) ⇒ Object
67 68 69 70 |
# File 'lib/howcast/client/category.rb', line 67 def category(id) response = establish_connection("categories/#{id}.xml") parse_single_category_xml(response.at(:category)) end |
#search(query, options = {}) ⇒ Object
Provides access to the Howcast video search API.
Inputs
-
query
– The string query which you want to search for The options are:
** :page
– The page number to retrieve (defaults to 1). There are
10 videos per page.
** :mode
– Mode to search, using :extended will allow
title:something searches
Outputs
An array of video objects
Exceptions
-
Howcast::ApiNotFound
– raised if the requested sort and filter is malformed or not available (404) -
ArgumentError
– raised when the required 1 argument isn’t supplied
Examples
Get the first page of howcast videos matching ‘poker’.
Howcast::Client.new.video_search("poker")
Get the third page of howcast videos matching ‘traveling’
Howcast::Client.new.video_search("traveling", :page => 3)
55 56 57 58 59 |
# File 'lib/howcast/client/search.rb', line 55 def search(query, = {}) defaults = {:view => "videos", :q => query} params = defaults.merge do_search params end |
#video(id) ⇒ Object
83 84 85 86 |
# File 'lib/howcast/client/video.rb', line 83 def video(id) response = establish_connection("videos/#{id}.xml") parse_single_xml(response.at(:video), Video) end |
#videos(options = {}) ⇒ Object
Provides access to the Howcast list videos API.
Inputs
The options are:
-
:page
– The page number to retrieve (defaults to 1). There are 20 videos per page. -
:sort
– One ofmost_recent
(default) |most_viewed
|top_rated
-
:filter
– One ofall
|howcast_studios
(default)
Outputs
An array of video objects
Exceptions
-
Howcast::ApiNotFound
– raised if the requested sort and filter is malformed or not available (404)
Examples
Get the first page of most recent howcast studios videos.
Howcast::Client.new.videos
Get the third page of top favorites which are featured
Howcast::Client.new.videos(:page => 3, :sort => "top_favorites", :filter => "top_rated")
109 110 111 112 |
# File 'lib/howcast/client/video.rb', line 109 def videos( = {}) uri = videos_url() (establish_connection(uri)/:video).inject([]){ |r, i| r << parse_single_xml(i, Video)} end |