Class: YoutubeTools::Searcher

Inherits:
Object
  • Object
show all
Defined in:
lib/youtube_tools/searcher.rb

Constant Summary collapse

ORDER_BY =
%w(relevance published viewCount rating)
"http://gdata.youtube.com/feeds/api/videos"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term, options = {}) ⇒ Searcher

Returns a new instance of Searcher.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/youtube_tools/searcher.rb', line 8

def initialize(term, options={})
	@term = term
	@search_term = url_term term			
	@order_by = options[:order_by].nil? ? set_order(0) : set_order(options[:order_by])
	@start_index = options[:start_index].nil? ? 0 : set_start(options[:start_index])
	@max_results = options[:max_results].nil? ? 10 : set_max(options[:max_results])
	@href = make_url
	@content = open(@href)
	@links = []
	process
end

Instance Attribute Details

#hrefObject

Returns the value of attribute href.



3
4
5
# File 'lib/youtube_tools/searcher.rb', line 3

def href
  @href
end

Returns the value of attribute links.



3
4
5
# File 'lib/youtube_tools/searcher.rb', line 3

def links
  @links
end

#max_resultsObject

Returns the value of attribute max_results.



3
4
5
# File 'lib/youtube_tools/searcher.rb', line 3

def max_results
  @max_results
end

#order_byObject

Returns the value of attribute order_by.



3
4
5
# File 'lib/youtube_tools/searcher.rb', line 3

def order_by
  @order_by
end

#search_termObject

Returns the value of attribute search_term.



3
4
5
# File 'lib/youtube_tools/searcher.rb', line 3

def search_term
  @search_term
end

#start_indexObject

Returns the value of attribute start_index.



3
4
5
# File 'lib/youtube_tools/searcher.rb', line 3

def start_index
  @start_index
end

#termObject

Returns the value of attribute term.



3
4
5
# File 'lib/youtube_tools/searcher.rb', line 3

def term
  @term
end

Instance Method Details

#processObject



20
21
22
23
24
25
26
27
28
# File 'lib/youtube_tools/searcher.rb', line 20

def process
	doc = Hpricot(@content)

	@links = doc.search("//entry").collect do |entry|
		{ :title => entry.get_elements_by_tag_name("title").text, 
			:link =>  entry.get_elements_by_tag_name("link").first.get_attribute("href")
		}  		
	end
end

#set_max(max) ⇒ Object



40
41
42
43
# File 'lib/youtube_tools/searcher.rb', line 40

def set_max(max)
	return max if max > 0 && max <= 50
	10
end

#set_order(index) ⇒ Object



30
31
32
33
# File 'lib/youtube_tools/searcher.rb', line 30

def set_order(index)
	return ORDER_BY[index] if index > 0 && index < 4
	ORDER_BY[0]
end

#set_start(val) ⇒ Object



35
36
37
38
# File 'lib/youtube_tools/searcher.rb', line 35

def set_start(val)
	return val if val >= 0
	0
end