Class: Howcast::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Creates a new howcast client to interact with the Howcast API

Inputs

Options include:

Exceptions

  • Howcast::ApiKeyNotFound – raised if the options value is nil



42
43
44
45
# File 'lib/howcast/client/base.rb', line 42

def initialize(options={})
  raise Howcast::ApiKeyNotFound if options[:key].nil?
  @key = options[:key]
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



29
30
31
# File 'lib/howcast/client/base.rb', line 29

def key
  @key
end

Instance Method Details

#category(id) ⇒ Object

Provides access to the Howcast category API.

Inputs

  • id – The id of the category to lookup

Outputs

Category object if the id exists or nil if the id doesn’t exist or is malformed

Exceptions

  • Howcast::ApiNotFound

Examples

Get the Howcast category with id 2

Howcast::Client.new.category(2)


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)


51
52
53
54
# File 'lib/howcast/client/search.rb', line 51

def search(query, options = {})
  uri = search_url(query, options)
  (establish_connection(uri)/:video).inject([]){ |r, i| r << parse_single_xml(i, Video)}
end

#video(id) ⇒ Object

Provides access to the Howcast video API.

Inputs

  • id – The id of the video to lookup

Outputs

Video object if the id exists or nil if the id doesn’t exist or is malformed

Exceptions

  • Howcast::ApiNotFound

Examples

Get the Howcast video with id 2

Howcast::Client.new.video(2)


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 of most_recent (default) | most_viewed | top_rated

  • :filter – One of all | 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(options = {})
   uri = videos_url(options)
   (establish_connection(uri)/:video).inject([]){ |r, i| r << parse_single_xml(i, Video)}
end