Class: Howcast::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/howcast/client.rb,
lib/howcast/client/base.rb,
lib/howcast/client/type.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

Classes: Category, Homepage, Marker, Playlist, Type, 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



49
50
51
52
# File 'lib/howcast/client/base.rb', line 49

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

Instance Attribute Details

#keyObject

Returns the value of attribute key.



36
37
38
# File 'lib/howcast/client/base.rb', line 36

def key
  @key
end

Class Method Details

.base_uriObject



64
65
66
# File 'lib/howcast/client/base.rb', line 64

def base_uri
  @base_uri ||= URI.parse("http://api.howcast.com")
end

.base_uri=(new_base_uri) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/howcast/client/base.rb', line 55

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


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

def categories(options = {})
   fetch_categories.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)


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

def category(id)
  response = establish_connection("categories/#{id}.xml")
  parse_single_category_xml(response.at(:category))
end

#category_id_for(key) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/howcast/client/category.rb', line 92

def category_id_for key
  fetch_categories do |categories|
    node       = (categories/"//category/name[text-downcase()='#{key.downcase}']/../permalink")
    node       = (categories/"//category/permalink[text-downcase()*='#{key.downcase}']") if node.empty?
    node.text.split("/").last unless node.empty?
  end
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


63
64
65
66
67
# File 'lib/howcast/client/homepage.rb', line 63

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)


71
72
73
74
# File 'lib/howcast/client/playlist.rb', line 71

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


67
68
69
70
# File 'lib/howcast/client/user.rb', line 67

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)


98
99
100
101
# File 'lib/howcast/client/video.rb', line 98

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


124
125
126
127
128
# File 'lib/howcast/client/video.rb', line 124

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