Class: Howcast::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/howcast/client.rb,
lib/howcast/client/base.rb,
lib/howcast/client/user.rb,
lib/howcast/client/video.rb,
lib/howcast/client/marker.rb,
lib/howcast/client/search.rb,
lib/howcast/client/category.rb,
lib/howcast/client/homepage.rb,
lib/howcast/client/playlist.rb

Overview

– Copyright © 2010 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, Homepage, Marker, Playlist, User, Video

Instance Attribute Summary collapse

Class Method 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



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

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

Instance Attribute Details

#keyObject

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_uriObject



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

#categories(options = {}) ⇒ Object

Provides access to the Howcast categories API.

Inputs

  • none

Outputs

Array of top level category objects

Examples

Get the top level Howcast categories

Howcast::Client.new.categories


86
87
88
89
# File 'lib/howcast/client/category.rb', line 86

def categories(options = {})
   uri = "categories.xml"
   establish_connection(uri).at('categories').children_of_type('category').inject([]){ |r, i| r << parse_single_category_xml(i)}
end

#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

#homepageObject

Provides access to the Howcast homepage API.

Outputs

Homepage object

Exceptions

  • Howcast::ApiNotFound

Examples

Create the Howcast homepage object

Howcast::Client.new.homepage


61
62
63
64
65
# File 'lib/howcast/client/homepage.rb', line 61

def homepage
  homepage = parse_single_xml(establish_connection("homepage/staff_videos.xml"), Homepage)
  homepage.playlists = parse_playlists(establish_connection("homepage/staff_playlists.xml"))
  homepage
end

#playlist(id, options = {}) ⇒ Object

Provides access to the Howcast playlist API.

Inputs

  • id – The id of the playlist to lookup

Outputs

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

Exceptions

  • Howcast::ApiNotFound

Examples

Get the Howcast playlist with id 12345

Howcast::Client.new.playlist(12345)


69
70
71
72
# File 'lib/howcast/client/playlist.rb', line 69

def playlist(id, options = {})
  response = establish_connection("playlists/#{id}.xml")
  parse_single_xml(response, Playlist)
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, options = {})
  defaults = {:view => "videos", :q => query}
  params = defaults.merge options
  do_search params
end

#user(login, options = {}) ⇒ Object

Provides access to the Howcast user API.

Inputs

  • login – The login/username of the user to lookup

Outputs

User object if the login exists or nil if the login doesn’t exist or is malformed

Exceptions

  • Howcast::ApiNotFound

Examples

Get the Howcast user with login ‘someone’

Howcast::Client.new.user('someone')


65
66
67
68
# File 'lib/howcast/client/user.rb', line 65

def user(, options = {})
  response = establish_connection("users/#{}/profile/videos#{"/#{options[:page]}" if options[:page]}.xml")
  parse_single_xml(response, User)
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)


85
86
87
88
# File 'lib/howcast/client/video.rb', line 85

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")


111
112
113
114
# File 'lib/howcast/client/video.rb', line 111

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