Class: YoutubeTools::Searcher
- Inherits:
-
Object
- Object
- YoutubeTools::Searcher
- Defined in:
- lib/youtube_tools/searcher.rb
Constant Summary collapse
- ORDER_BY =
%w(relevance published viewCount rating)
- YOUTUBE_LINK =
"http://gdata.youtube.com/feeds/api/videos"
Instance Attribute Summary collapse
-
#href ⇒ Object
Returns the value of attribute href.
-
#links ⇒ Object
Returns the value of attribute links.
-
#max_results ⇒ Object
Returns the value of attribute max_results.
-
#order_by ⇒ Object
Returns the value of attribute order_by.
-
#search_term ⇒ Object
Returns the value of attribute search_term.
-
#start_index ⇒ Object
Returns the value of attribute start_index.
-
#term ⇒ Object
Returns the value of attribute term.
Instance Method Summary collapse
-
#initialize(term, options = {}) ⇒ Searcher
constructor
A new instance of Searcher.
- #process ⇒ Object
- #set_max(max) ⇒ Object
- #set_order(index) ⇒ Object
- #set_start(val) ⇒ Object
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, ={}) @term = term @search_term = url_term term @order_by = [:order_by].nil? ? set_order(0) : set_order([:order_by]) @start_index = [:start_index].nil? ? 0 : set_start([:start_index]) @max_results = [:max_results].nil? ? 10 : set_max([:max_results]) @href = make_url @content = open(@href) @links = [] process end |
Instance Attribute Details
#href ⇒ Object
Returns the value of attribute href.
3 4 5 |
# File 'lib/youtube_tools/searcher.rb', line 3 def href @href end |
#links ⇒ Object
Returns the value of attribute links.
3 4 5 |
# File 'lib/youtube_tools/searcher.rb', line 3 def links @links end |
#max_results ⇒ Object
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_by ⇒ Object
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_term ⇒ Object
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_index ⇒ Object
Returns the value of attribute start_index.
3 4 5 |
# File 'lib/youtube_tools/searcher.rb', line 3 def start_index @start_index end |
#term ⇒ Object
Returns the value of attribute term.
3 4 5 |
# File 'lib/youtube_tools/searcher.rb', line 3 def term @term end |
Instance Method Details
#process ⇒ Object
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 |