Module: Yourub::REST::Search
- Included in:
- API
- Defined in:
- lib/yourub/rest/search.rb
Instance Method Summary collapse
- #add_video_to_search_result(entry) ⇒ Object
- #consume_categories(to_consume) ⇒ Object
- #consume_criteria ⇒ Object
- #get_categories_for_country(country) ⇒ Object
- #get_details_and_store(video_list) ⇒ Object
- #get_views(id) ⇒ Object
- #merge_criteria_with_api_options ⇒ Object
- #parse_name(name) ⇒ Object
- #retrieve_categories ⇒ Object
- #retrieve_videos ⇒ Object
- #search(criteria) ⇒ Object
- #search_by_criteria ⇒ Object
- #search_by_id ⇒ Object
- #search_list_request(params) ⇒ Object
- #send_request(resource_type, method, params) ⇒ Object
- #video_categories_list_request(params) ⇒ Object
- #video_params(result_video_id) ⇒ Object
- #videos_list_request(params) ⇒ Object
Instance Method Details
#add_video_to_search_result(entry) ⇒ Object
140 141 142 143 144 |
# File 'lib/yourub/rest/search.rb', line 140 def add_video_to_search_result(entry) if Yourub::CountFilter.accept?(entry) @videos.push(entry) end end |
#consume_categories(to_consume) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/yourub/rest/search.rb', line 94 def consume_categories(to_consume) if @categories.size > 0 @categories.each do |cat| to_consume[:videoCategoryId] = cat.keys[0].to_i yield to_consume end else yield to_consume end end |
#consume_criteria ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/yourub/rest/search.rb', line 80 def consume_criteria to_consume = @api_options if @criteria[:country] @criteria[:country].each do |country| to_consume[:regionCode] = country consume_categories(to_consume) do |cat| yield cat end end else yield to_consume end end |
#get_categories_for_country(country) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/yourub/rest/search.rb', line 60 def get_categories_for_country(country) param = {"part" => "snippet","regionCode" => country } categories_list = video_categories_list_request(param) categories_list.data.items.each do |cat_result| category_name = parse_name(cat_result["snippet"]["title"]) @categories.push(cat_result["id"] => category_name) end end |
#get_details_and_store(video_list) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/yourub/rest/search.rb', line 105 def get_details_and_store(video_list) video_list.data.items.each do |video_item| params = video_params(video_item.id.videoId) v = videos_list_request(params) v = Yourub::Reader.parse_videos(v) add_video_to_search_result(v.first) if v end end |
#get_views(id) ⇒ Object
150 151 152 153 154 155 |
# File 'lib/yourub/rest/search.rb', line 150 def get_views(id) params = {:id => id, :part => 'statistics'} request = videos_list_request(params) v = Yourub::Reader.parse_videos(request) v ? Yourub::CountFilter.get_views_count(v.first) : nil end |
#merge_criteria_with_api_options ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/yourub/rest/search.rb', line 45 def mappings = {query: :q, max_results: :maxResults, country: :regionCode} @api_options.merge! @criteria @api_options.keys.each do |k| @api_options[ mappings[k] ] = @api_options.delete(k) if mappings[k] end end |
#parse_name(name) ⇒ Object
146 147 148 |
# File 'lib/yourub/rest/search.rb', line 146 def parse_name(name) return name.gsub("/", "-").downcase.gsub(/\s+/, "") end |
#retrieve_categories ⇒ Object
53 54 55 56 57 58 |
# File 'lib/yourub/rest/search.rb', line 53 def retrieve_categories if @criteria.has_key? :category get_categories_for_country(@criteria[:country]) @categories = Yourub::Validator.valid_category(@categories, @criteria[:category]) end end |
#retrieve_videos ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/yourub/rest/search.rb', line 69 def retrieve_videos consume_criteria do |criteria| begin req = search_list_request(criteria) get_details_and_store req rescue StandardError => e Yourub.logger.error "Error #{e} retrieving videos for the criteria: #{criteria.to_s}" end end end |
#search(criteria) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/yourub/rest/search.rb', line 7 def search(criteria) begin @api_options= { :part => 'snippet', :type => 'video', :order => 'relevance', :safeSearch => 'none', } @categories, @videos = [], [] @count_filter = {} @criteria = Yourub::Validator.confirm(criteria) search_by_criteria rescue ArgumentError => e Yourub.logger.error "#{e}" end end |
#search_by_criteria ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/yourub/rest/search.rb', line 25 def search_by_criteria if @criteria.has_key? :id search_by_id else retrieve_categories retrieve_videos end end |
#search_by_id ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/yourub/rest/search.rb', line 35 def search_by_id params = { :id => @criteria[:id], :part => 'snippet,statistics', } video_response = videos_list_request(params) entry = Yourub::Reader.parse_videos(video_response) add_video_to_search_result(entry.first) unless entry.nil? end |
#search_list_request(params) ⇒ Object
114 115 116 |
# File 'lib/yourub/rest/search.rb', line 114 def search_list_request(params) send_request("search", "list", params) end |
#send_request(resource_type, method, params) ⇒ Object
126 127 128 129 |
# File 'lib/yourub/rest/search.rb', line 126 def send_request(resource_type, method, params) #byebug Yourub::REST::Request.new(self, resource_type, method, params) end |
#video_categories_list_request(params) ⇒ Object
122 123 124 |
# File 'lib/yourub/rest/search.rb', line 122 def video_categories_list_request(params) send_request("video_categories", "list", params) end |
#video_params(result_video_id) ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/yourub/rest/search.rb', line 131 def video_params(result_video_id) fields = URI::encode( 'items(id,snippet(title,thumbnails),statistics(viewCount))' ) { :id => result_video_id, :part => 'snippet,statistics,id', :fields => fields } end |
#videos_list_request(params) ⇒ Object
118 119 120 |
# File 'lib/yourub/rest/search.rb', line 118 def videos_list_request(params) send_request("videos", "list", params) end |