Class: Yourub::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yourub/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/yourub/client.rb', line 7

def initialize()
  @extended_info = false
  @categories, @videos = [], []
  @api_options= {
    :part            => 'snippet',
    :type            => 'video',
    :eventType       => 'completed',
    :order           => 'relevance',
    :safeSearch      => 'none',
  }
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/yourub/client.rb', line 5

def config
  @config
end

#extended_infoObject

Returns the value of attribute extended_info.



5
6
7
# File 'lib/yourub/client.rb', line 5

def extended_info
  @extended_info
end

#videosObject (readonly)

Returns the value of attribute videos.



4
5
6
# File 'lib/yourub/client.rb', line 4

def videos
  @videos
end

Instance Method Details

#add_video_to_search_result(entry) ⇒ Object



176
177
178
179
180
181
# File 'lib/yourub/client.rb', line 176

def add_video_to_search_result(entry)
  video = @extended_info ? entry : Yourub::Reader.parse_entry(entry)
  if Yourub::CountFilter.accept?(entry, @criteria[:count_filter])
    @videos.push(video)
  end
end

#clientObject



27
28
29
30
31
32
33
34
# File 'lib/yourub/client.rb', line 27

def client
  @client ||= Google::APIClient.new(
    :key => config.developer_key,
    :application_name => config.application_name,
    :application_version => config.application_version,
    :authorization => nil,
  )
end

#consume_categories(to_consume) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/yourub/client.rb', line 123

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_criteriaObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/yourub/client.rb', line 109

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

#countriesObject



23
24
25
# File 'lib/yourub/client.rb', line 23

def countries
  Yourub::Validator.available_countries
end

#get_categories_for_country(country) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/yourub/client.rb', line 89

def get_categories_for_country(country)
  categories_list = video_categories_list_request(country)
  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



134
135
136
137
138
139
140
# File 'lib/yourub/client.rb', line 134

def get_details_and_store(video_list)
  video_list.data.items.each do |video_item|
    v = videos_list_request video_item.id.videoId
    v = Yourub::Reader.parse_videos(v).first
    add_video_to_search_result(v)
  end
end

#merge_criteria_with_api_optionsObject



74
75
76
77
78
79
80
# File 'lib/yourub/client.rb', line 74

def merge_criteria_with_api_options
  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



183
184
185
# File 'lib/yourub/client.rb', line 183

def parse_name(name)
  return name.gsub("/", "-").downcase.gsub(/\s+/, "")
end

#retrieve_categoriesObject



82
83
84
85
86
87
# File 'lib/yourub/client.rb', line 82

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_videosObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/yourub/client.rb', line 97

def retrieve_videos
  consume_criteria do |criteria|
    req = search_list_request(criteria)
    if @extended_info || @criteria[:count_filter]
      get_details_and_store req
    else
      videos = Yourub::Reader.parse_videos(req)
      videos.each{|v| add_video_to_search_result(v) }
    end
  end
end

#search(criteria) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/yourub/client.rb', line 41

def search(criteria)
  begin
    @videos = []
    @criteria = Yourub::Validator.confirm(criteria)
    search_by_criteria
  rescue ArgumentError => e
    Yourub.logger.error "#{e}"
  end
end

#search_by_criteriaObject



51
52
53
54
55
56
57
58
59
# File 'lib/yourub/client.rb', line 51

def search_by_criteria
  if @criteria.has_key? :id
    search_by_id
  else
    merge_criteria_with_api_options
    retrieve_categories
    retrieve_videos
  end
end

#search_by_idObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/yourub/client.rb', line 61

def search_by_id
  params = {
    :id => @criteria[:id],
    :part => 'snippet,statistics',
  }
  video_response = client.execute!(
    :api_method => youtube.videos.list,
    :parameters => params
  )
  entry = Yourub::Reader.parse_videos(video_response).first
  add_video_to_search_result(entry) unless entry.nil?
end

#search_list_request(options) ⇒ Object



142
143
144
145
146
147
# File 'lib/yourub/client.rb', line 142

def search_list_request(options)
  search_response = client.execute!(
    :api_method => youtube.search.list, 
    :parameters => options
  )
end

#video_categories_list_request(country) ⇒ Object



157
158
159
160
161
162
# File 'lib/yourub/client.rb', line 157

def video_categories_list_request(country)
  categories_list = client.execute!(
    :api_method => youtube.video_categories.list,
    :parameters => {"part" => "snippet","regionCode" => country }
  )
end

#video_params(result_video_id) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/yourub/client.rb', line 164

def video_params(result_video_id)
  parameters = {
    :id => result_video_id,
    :part => 'snippet,statistics,id',
  }
  unless @extended_info
    fields = 'items(id,snippet(title,thumbnails),statistics(viewCount))'
    parameters[:fields] = URI::encode(fields)
  end
  return parameters
end

#videos_list_request(result_video_id) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/yourub/client.rb', line 149

def videos_list_request(result_video_id)
  params = video_params(result_video_id)
  video_response = client.execute!(
    :api_method => youtube.videos.list,
    :parameters => params
  )
end

#youtubeObject



36
37
38
39
# File 'lib/yourub/client.rb', line 36

def youtube
  @youtube ||= client.discovered_api(config.youtube_api_service_name,
    config.youtube_api_version)
end