yourub 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -6
  3. data/Gemfile +15 -1
  4. data/README.md +36 -25
  5. data/Rakefile +18 -0
  6. data/config/yourub.yml +1 -1
  7. data/lib/yourub/client.rb +38 -10
  8. data/lib/yourub/config.rb +34 -1
  9. data/lib/yourub/default.rb +1 -1
  10. data/lib/yourub/meta_search.rb +134 -0
  11. data/lib/yourub/rest/api.rb +6 -0
  12. data/lib/yourub/rest/categories.rb +39 -0
  13. data/lib/yourub/rest/request.rb +22 -4
  14. data/lib/yourub/rest/search.rb +11 -149
  15. data/lib/yourub/rest/videos.rb +38 -0
  16. data/lib/yourub/result.rb +41 -0
  17. data/lib/yourub/validator.rb +4 -11
  18. data/lib/yourub/version.rb +3 -3
  19. data/lib/yourub.rb +2 -3
  20. data/spec/fixtures/categories_list.json +1 -0
  21. data/spec/fixtures/categories_list_formatted.json +1 -0
  22. data/spec/fixtures/search_list.json +1 -0
  23. data/spec/fixtures/single_video.json +1 -0
  24. data/spec/fixtures/video_with_200_views.json +1 -0
  25. data/spec/fixtures/videos_list.json +1 -0
  26. data/spec/spec_helper.rb +11 -0
  27. data/spec/support/shared_context_result_load_fixture.rb +11 -0
  28. data/spec/support/shared_context_result_search_list.rb +16 -0
  29. data/spec/support/shared_context_result_search_list_with_single_videos.rb +24 -0
  30. data/spec/support/shared_context_stub_client_connection.rb +18 -0
  31. data/spec/yourub/client_spec.rb +29 -72
  32. data/spec/yourub/count_filter_spec.rb +4 -5
  33. data/spec/yourub/count_spec.rb +39 -0
  34. data/spec/yourub/integration.rb +105 -0
  35. data/spec/yourub/meta_search_spec.rb +141 -0
  36. data/spec/yourub/rest/categories_spec.rb +33 -0
  37. data/spec/yourub/rest/request_spec.rb +32 -0
  38. data/spec/yourub/rest/videos_spec.rb +24 -0
  39. data/spec/yourub/validator_spec.rb +54 -57
  40. data/yourub.gemspec +0 -5
  41. metadata +39 -66
  42. data/lib/yourub/cli.rb +0 -20
  43. data/lib/yourub/reader.rb +0 -21
  44. data/spec/fixtures/categories.json +0 -0
  45. data/spec/fixtures/category.json +0 -0
  46. data/spec/helper.rb +0 -26
@@ -3,157 +3,19 @@ require 'yourub/rest/request'
3
3
  module Yourub
4
4
  module REST
5
5
  module Search
6
-
7
- def search(criteria)
8
- begin
9
- @api_options= {
10
- :part => 'snippet',
11
- :type => 'video',
12
- :order => 'relevance',
13
- :safeSearch => 'none',
14
- }
15
-
16
- @categories, @videos = [], []
17
- @count_filter = {}
18
- @criteria = Yourub::Validator.confirm(criteria)
19
- search_by_criteria
20
- rescue ArgumentError => e
21
- Yourub.logger.error "#{e}"
22
- end
23
- end
24
-
25
- def search_by_criteria
26
- if @criteria.has_key? :id
27
- search_by_id
28
- else
29
- merge_criteria_with_api_options
30
- retrieve_categories
31
- retrieve_videos
32
- end
33
- end
34
-
35
- def search_by_id
36
- params = {
37
- :id => @criteria[:id],
38
- :part => 'snippet,statistics',
39
- }
40
- video_response = videos_list_request(params)
41
- entry = Yourub::Reader.parse_videos(video_response)
42
- add_video_to_search_result(entry.first) unless entry.nil?
43
- end
44
-
45
- def merge_criteria_with_api_options
46
- mappings = {query: :q, max_results: :maxResults, country: :regionCode}
47
- @api_options.merge! @criteria
48
- @api_options.keys.each do |k|
49
- @api_options[ mappings[k] ] = @api_options.delete(k) if mappings[k]
50
- end
51
- end
52
-
53
- def retrieve_categories
54
- if @criteria.has_key? :category
55
- get_categories_for_country(@criteria[:country])
56
- @categories = Yourub::Validator.valid_category(@categories, @criteria[:category])
57
- end
58
- end
59
-
60
- def get_categories_for_country(country)
61
- param = {"part" => "snippet","regionCode" => country }
62
- categories_list = video_categories_list_request(param)
63
- categories_list.data.items.each do |cat_result|
64
- category_name = parse_name(cat_result["snippet"]["title"])
65
- @categories.push(cat_result["id"] => category_name)
66
- end
67
- end
68
-
69
- def retrieve_videos
70
- consume_criteria do |criteria|
71
- begin
72
- req = search_list_request(criteria)
73
- get_details_and_store req
74
- rescue StandardError => e
75
- Yourub.logger.error "Error #{e} retrieving videos for the criteria: #{criteria.to_s}"
76
- end
77
- end
78
- end
79
-
80
- def consume_criteria
81
- to_consume = @api_options
82
- if @criteria[:country]
83
- @criteria[:country].each do |country|
84
- to_consume[:regionCode] = country
85
- consume_categories(to_consume) do |cat|
86
- yield cat
87
- end
88
- end
89
- else
90
- yield to_consume
91
- end
92
- end
93
-
94
- def consume_categories(to_consume)
95
- if @categories.size > 0
96
- @categories.each do |cat|
97
- to_consume[:videoCategoryId] = cat.keys[0].to_i
98
- yield to_consume
99
- end
100
- else
101
- yield to_consume
102
- end
103
- end
104
-
105
- def get_details_and_store(video_list)
106
- video_list.data.items.each do |video_item|
107
- params = video_params(video_item.id.videoId)
108
- v = videos_list_request(params)
109
- v = Yourub::Reader.parse_videos(v)
110
- add_video_to_search_result(v.first) if v
111
- end
112
- end
113
-
114
- def search_list_request(params)
115
- send_request("search", "list", params)
116
- end
117
-
118
- def videos_list_request(params)
119
- send_request("videos", "list", params)
120
- end
121
-
122
- def video_categories_list_request(params)
123
- send_request("video_categories", "list", params)
124
- end
125
-
126
- def send_request(resource_type, method, params)
127
- #byebug
128
- Yourub::REST::Request.new(self, resource_type, method, params)
129
- end
130
-
131
- def video_params(result_video_id)
132
- fields = URI::encode(
133
- 'items(id,snippet(title,thumbnails),statistics(viewCount))'
134
- )
135
- { :id => result_video_id,
136
- :part => 'snippet,statistics,id',
137
- :fields => fields }
138
- end
139
-
140
- def add_video_to_search_result(entry)
141
- if Yourub::CountFilter.accept?(entry)
142
- @videos.push(entry)
6
+ class << self
7
+ # @param client[Yourub::Client]
8
+ # @param params[hash]
9
+ #
10
+ #
11
+ # @example
12
+ # client = Yourub::Client.new
13
+ # req = Yourub::REST::Search.list(client, params)
14
+ #
15
+ def list(client, params)
16
+ Yourub::REST::Request.new(client,"search", "list", params)
143
17
  end
144
18
  end
145
-
146
- def parse_name(name)
147
- return name.gsub("/", "-").downcase.gsub(/\s+/, "")
148
- end
149
-
150
- def get_views(id)
151
- params = {:id => id, :part => 'statistics'}
152
- request = videos_list_request(params)
153
- v = Yourub::Reader.parse_videos(request)
154
- v ? Yourub::CountFilter.get_views_count(v.first) : nil
155
- end
156
-
157
19
  end
158
20
  end
159
21
  end
@@ -0,0 +1,38 @@
1
+ require 'yourub/rest/request'
2
+
3
+ module Yourub
4
+ module REST
5
+ module Videos
6
+ class << self
7
+ # @param client[Yourub::Client]
8
+ # @param params[hash]
9
+ #
10
+ #
11
+ # @example
12
+ # client = Yourub::Client.new
13
+ # req = Yourub::REST::Videos.list(client, params)
14
+ #
15
+
16
+ def list(client, params)
17
+ Yourub::REST::Request.new(client, "videos", "list", params)
18
+ end
19
+
20
+ def single_video(client, video_id)
21
+ params = single_video_params(video_id)
22
+ list(client, params)
23
+ end
24
+
25
+ private
26
+
27
+ def single_video_params(video_id)
28
+ fields = URI::encode(
29
+ 'items(id,snippet(title,thumbnails),statistics(viewCount))'
30
+ )
31
+ { :id => video_id,
32
+ :part => 'snippet,statistics,id',
33
+ :fields => fields }
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ module Yourub
2
+ module Result
3
+ class << self
4
+
5
+ def format(request)
6
+ data = nil
7
+ if request.status == 200
8
+ data = JSON.parse(request.data.items.to_json)
9
+ end
10
+ data
11
+ end
12
+
13
+ #resp example
14
+ # [
15
+ # {"kind"=>"youtube#video",
16
+ # "etag"=>"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/WkWGKMfWxthgmGABcMV_RsED5d8\"",
17
+ # "id"=>"mN0Dbj-xHY0",
18
+ # "snippet"=>{
19
+ # "publishedAt"=>"2013-10-17T22:08:28.000Z",
20
+ # "channelId"=>"UCqhnX4jA0A5paNd1v-zEysw",
21
+ # "title"=>"GoPro: Hiero Day",
22
+ # "description"=>"Shot 100% on the HD HERO3® camera from ‪http://GoPro.com.\n\nTo celebrate Hieroglyphics' influential force in underground music and art for over 20 years, Oakland, CA Mayor Jean Quan, proclaimed September 3rd as Hiero Day.\n\nMusic\nSouls of Mischief \"Cab Fare\"",
23
+ # "thumbnails"=>{
24
+ # "default"=>{"url"=>"https://i.ytimg.com/vi/mN0Dbj-xHY0/default.jpg", "width"=>120, "height"=>90},
25
+ # "medium"=>{"url"=>"https://i.ytimg.com/vi/mN0Dbj-xHY0/mqdefault.jpg", "width"=>320, "height"=>180},
26
+ # "high"=>{"url"=>"https://i.ytimg.com/vi/mN0Dbj-xHY0/hqdefault.jpg", "width"=>480, "height"=>360},
27
+ # "standard"=>{"url"=>"https://i.ytimg.com/vi/mN0Dbj-xHY0/sddefault.jpg", "width"=>640, "height"=>480},
28
+ # "maxres"=>{"url"=>"https://i.ytimg.com/vi/mN0Dbj-xHY0/maxresdefault.jpg", "width"=>1280, "height"=>720}
29
+ # },
30
+ # "channelTitle"=>"GoPro",
31
+ # "categoryId"=>"17",
32
+ # "liveBroadcastContent"=>"none"
33
+ # },
34
+ # "statistics"=>{
35
+ # "viewCount"=>"143272", "likeCount"=>"949", "dislikeCount"=>"219", "favoriteCount"=>"0", "commentCount"=>"91"
36
+ # }}
37
+ # ]
38
+
39
+ end
40
+ end
41
+ end
@@ -7,8 +7,8 @@ module Yourub
7
7
  'HU','IN','IE','IL','IT','JP','JO','MY','MX','MA','NL','NZ','PE','PH',
8
8
  'PL','RU','SA','SG','ZA','KR','ES','SE','CH','TW','AE','US']
9
9
  ORDERS = ['date', 'rating', 'relevance', 'title', 'videoCount', 'viewCount']
10
- VALID_PARAMS = [:country, :category, :query, :id, :max_results, :count_filter, :order ]
11
- MINIMUM_PARAMS = [:country, :category, :query, :id]
10
+ VALID_PARAMS = [:country, :category, :query, :max_results, :count_filter, :order ]
11
+ MINIMUM_PARAMS = [:country, :category, :query]
12
12
 
13
13
  def confirm(criteria)
14
14
  valid_format?(criteria)
@@ -17,7 +17,6 @@ module Yourub
17
17
  remove_empty_and_non_valid_params
18
18
  minimum_param_present?
19
19
 
20
- keep_only_the_id_if_present
21
20
  validate_order
22
21
  countries_to_array
23
22
  add_default_country_if_category_is_present
@@ -46,12 +45,6 @@ module Yourub
46
45
  @criteria.keep_if{|k,v| ( (VALID_PARAMS.include? k) && v.size > 0) }
47
46
  end
48
47
 
49
- def keep_only_the_id_if_present
50
- if @criteria.has_key? :id
51
- @criteria.keep_if{|k, _| k == :id}
52
- end
53
- end
54
-
55
48
  def countries_to_array
56
49
  if @criteria.has_key? :country
57
50
  if @criteria[:country].is_a?(String)
@@ -89,7 +82,7 @@ module Yourub
89
82
  ) unless( criteria.is_a? Hash )
90
83
  end
91
84
 
92
- def minimum_param_present?
85
+ def minimum_param_present?
93
86
  if @criteria.none?{|k,_| MINIMUM_PARAMS.include? k}
94
87
  raise ArgumentError.new(
95
88
  "minimum params to start a search is at least one of: #{MINIMUM_PARAMS.join(',')}"
@@ -101,7 +94,7 @@ module Yourub
101
94
  if @criteria.has_key? :order
102
95
  raise ArgumentError.new(
103
96
  "the given order is not in the available ones: #{ORDERS.join(',')}"
104
- ) unless( ORDERS.include? @criteria[:order] )
97
+ ) unless( ORDERS.include? @criteria[:order] )
105
98
  end
106
99
  end
107
100
 
@@ -1,6 +1,6 @@
1
1
  module Yourub
2
- MAJOR = 1
3
- MINOR = 1
4
- PATCH = 1
2
+ MAJOR = 2
3
+ MINOR = 0
4
+ PATCH = 0
5
5
  VERSION = [MAJOR, MINOR, PATCH].compact.join('.')
6
6
  end
data/lib/yourub.rb CHANGED
@@ -5,13 +5,12 @@ require 'open-uri'
5
5
 
6
6
  require 'yourub/config'
7
7
  require 'yourub/client'
8
- #require 'yourub/search'
9
- #require 'yourub/request'
8
+ require 'yourub/meta_search'
9
+ require 'yourub/result'
10
10
  require 'yourub/version'
11
11
  require 'yourub/logger'
12
12
  require 'yourub/count_filter'
13
13
  require 'yourub/validator'
14
- require 'yourub/reader'
15
14
 
16
15
  if defined?(Rails)
17
16
  require 'yourub/railtie'
@@ -0,0 +1 @@
1
+ [ {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/Xy1mB4_yLrHy_BmKmPBggty2mZQ\"", "id":"1", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Film & Animation", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/UZ1oLIIz2dxIhO45ZTFR3a3NyTA\"", "id":"2", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Autos & Vehicles", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/nqRIq97-xe5XRZTxbknKFVe5Lmg\"", "id":"10", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Music", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/HwXKamM1Q20q9BN-oBJavSGkfDI\"", "id":"15", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Pets & Animals", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/9GQMSRjrZdHeb1OEM1XVQ9zbGec\"", "id":"17", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Sports", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/FJwVpGCVZ1yiJrqZbpqe68Sy_OE\"", "id":"18", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Short Movies", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/M-3iD9dwK7YJCafRf_DkLN8CouA\"", "id":"19", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Travel & Events", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/WmA0qYEfjWsAoyJFSw2zinhn2wM\"", "id":"20", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Gaming", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/EapFaGYG7K0StIXVf8aba249tdM\"", "id":"21", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Videoblogging", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/xId8RX7vRN8rqkbYZbNIytUQDRo\"", "id":"22", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"People & Blogs", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/G9LHzQmx44rX2S5yaga_Aqtwz8M\"", "id":"23", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Comedy", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/UVB9oxX2Bvqa_w_y3vXSLVK5E_s\"", "id":"24", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Entertainment", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/QiLK0ZIrFoORdk_g2l_XR_ECjDc\"", "id":"25", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"News & Politics", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/r6Ck6Z0_L0rG37VJQR200SGNA_w\"", "id":"26", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Howto & Style", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/EoYkczo9I3RCf96RveKTOgOPkUM\"", "id":"27", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Education", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/w5HjcTD82G_XA3xBctS30zS-JpQ\"", "id":"28", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Science & Technology", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/SalkJoBWq_smSEqiAx_qyri6Wa8\"", "id":"29", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Nonprofits & Activism", "assignable":true}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/lL7uWDr_071CHxifjYG1tJrp4Uo\"", "id":"30", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Movies", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/WnuVfjO-PyFLO7NTRQIbrGE62nk\"", "id":"31", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Anime/Animation", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/ctpH2hGA_UZ3volJT_FTlOg9M00\"", "id":"32", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Action/Adventure", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/L0kR3-g1BAo5UD1PLVbQ7LkkDtQ\"", "id":"33", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Classics", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/pUZOAC_s9sfiwar639qr_wAB-aI\"", "id":"34", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Comedy", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/Xb5JLhtyNRN3AQq021Ds-OV50Jk\"", "id":"35", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Documentary", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/u8WXzF4HIhtEi805__sqjuA4lEk\"", "id":"36", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Drama", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/D04PP4Gr7wc4IV_O9G66Z4A8KWQ\"", "id":"37", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Family", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/i5-_AceGXQCEEMWU0V8CcQm_vLQ\"", "id":"38", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Foreign", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/rtlxd0zOixA9QHdIZB26-St5qgQ\"", "id":"39", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Horror", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/N1TrDFLRppxZgBowCJfJCvh0Dpg\"", "id":"40", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Sci-Fi/Fantasy", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/7UMGi6zRySqXopr_rv4sZq6Za2E\"", "id":"41", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Thriller", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/RScXhi324h8usyIetreAVb-uKeM\"", "id":"42", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Shorts", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/0n9MJVCDLpA8q7aiGVrFsuFsd0A\"", "id":"43", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Shows", "assignable":false}}, {"kind":"youtube#videoCategory", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/x5NxSf5fz8hn4loSN4rvhwzD_pY\"", "id":"44", "snippet":{"channelId":"UCBR8-60-B28hp2BmDPdntcQ", "title":"Trailers", "assignable":false}}]
@@ -0,0 +1 @@
1
+ [{"1":"film&animation"}, {"2":"autos&vehicles"}, {"10":"music"}, {"15":"pets&animals"}, {"17":"sports"}, {"18":"shortmovies"}, {"19":"travel&events"}, {"20":"gaming"}, {"21":"videoblogging"}, {"22":"people&blogs"}, {"23":"comedy"}, {"24":"entertainment"}, {"25":"news&politics"}, {"26":"howto&style"}, {"27":"education"}, {"28":"science&technology"}, {"29":"nonprofits&activism"}, {"30":"movies"}, {"31":"anime-animation"}, {"32":"action-adventure"}, {"33":"classics"}, {"34":"comedy"}, {"35":"documentary"}, {"36":"drama"}, {"37":"family"}, {"38":"foreign"}, {"39":"horror"}, {"40":"sci-fi-fantasy"}, {"41":"thriller"}, {"42":"shorts"}, {"43":"shows"}, {"44":"trailers"}]
@@ -0,0 +1 @@
1
+ [{"kind":"youtube#searchResult", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/FaWZkxrj-a-YsuOD3bLfaC_Db1w\"", "id":{"kind":"youtube#video", "videoId":"Vo0Cazxj_yc"}, "snippet":{"publishedAt":"2010-10-25T12:43:01.000Z", "channelId":"UCIJ0lLcABPdYGp7pRMGccAQ", "title":"PEOPLE ARE AWESOME", "description":"Click here for our new video PEOPLE ARE AWESOME 2014: http://youtu.be/VWf8CXwPoqI http://www.twitter.com/PAAVideos ...", "thumbnails":{"default":{"url":"https://i.ytimg.com/vi/Vo0Cazxj_yc/default.jpg"}, "medium":{"url":"https://i.ytimg.com/vi/Vo0Cazxj_yc/mqdefault.jpg"}, "high":{"url":"https://i.ytimg.com/vi/Vo0Cazxj_yc/hqdefault.jpg"}}, "channelTitle":"Hadoukentheband", "liveBroadcastContent":"none"}}, {"kind":"youtube#searchResult", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/LVnLC4-FA4Y30ID1TBJQsny-AoM\"", "id":{"kind":"youtube#video", "videoId":"M0jmSsQ5ptw"}, "snippet":{"publishedAt":"2012-11-11T21:53:12.000Z", "channelId":"UCblfuW_4rakIf2h6aqANefA", "title":"The Athlete Machine - Red Bull Kluge", "description":"Get the behind the scenes footage here: http://youtu.be/OvvX34q0QMk Watch the Spiral Drift: http://youtu.be/_u76kWWo6GM http://www.redbullusa.com/kluge ...", "thumbnails":{"default":{"url":"https://i.ytimg.com/vi/M0jmSsQ5ptw/default.jpg"}, "medium":{"url":"https://i.ytimg.com/vi/M0jmSsQ5ptw/mqdefault.jpg"}, "high":{"url":"https://i.ytimg.com/vi/M0jmSsQ5ptw/hqdefault.jpg"}}, "channelTitle":"redbull", "liveBroadcastContent":"none"}}, {"kind":"youtube#searchResult", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/pwYBU2vxB3eVIvcYvqWKZYeDWp0\"", "id":{"kind":"youtube#video", "videoId":"83rinbq_8uI"}, "snippet":{"publishedAt":"2014-04-08T07:32:26.000Z", "channelId":"UC-KQIG4-dyR1kIHCQFOJ-hQ", "title":"The Greatness Of Ronaldinho Respect!", "description":"If you like my work, become a fan on facebook and follow me on twitter: https://facebook.com/HeilRJ https://twitter.com/HeilRJ Make a donation: https://www.p...", "thumbnails":{"default":{"url":"https://i.ytimg.com/vi/83rinbq_8uI/default.jpg"}, "medium":{"url":"https://i.ytimg.com/vi/83rinbq_8uI/mqdefault.jpg"}, "high":{"url":"https://i.ytimg.com/vi/83rinbq_8uI/hqdefault.jpg"}}, "channelTitle":"HeilRJ03", "liveBroadcastContent":"none"}},{"kind":"youtube#searchResult", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/sFmalCqM626keSRPmEroHgq2B-A\"", "id":{"kind":"youtube#video", "videoId":"0gWxHFMog9w"}, "snippet":{"publishedAt":"2013-08-19T13:00:46.000Z", "channelId":"UCRijo3ddMTht_IHyNSNXpNQ", "title":"Stereotypes: Pickup Basketball", "description":"Pickup ballers. Love 'em or hate 'em, we all know 'em. ---------------------------------------- PLAY our iPHONE GAME - http://smarturl.it/DPGameiPhone V...", "thumbnails":{"default":{"url":"https://i.ytimg.com/vi/0gWxHFMog9w/default.jpg"}, "medium":{"url":"https://i.ytimg.com/vi/0gWxHFMog9w/mqdefault.jpg"}, "high":{"url":"https://i.ytimg.com/vi/0gWxHFMog9w/hqdefault.jpg"}}, "channelTitle":"corycotton", "liveBroadcastContent":"none"}}]
@@ -0,0 +1 @@
1
+ [#<Google::APIClient::Schema::Youtube::V3::Video:0x3ff0d68e9aac DATA:{"id"=>"KDOLHClNTOI", "snippet"=>{"title"=>"When We Left Earth - The NASA Missions", "thumbnails"=>{"default"=>{"url"=>"https://i.ytimg.com/vi/KDOLHClNTOI/default.jpg", "width"=>120, "height"=>90}, "medium"=>{"url"=>"https://i.ytimg.com/vi/KDOLHClNTOI/mqdefault.jpg", "width"=>320, "height"=>180}, "high"=>{"url"=>"https://i.ytimg.com/vi/KDOLHClNTOI/hqdefault.jpg", "width"=>480, "height"=>360}}}, "statistics"=>{"viewCount"=>"767700"}}>]
@@ -0,0 +1 @@
1
+ [{"id":"ljwjEyJJtJA","snippet":{"title":"Watch: NASA launches Orion space capsule aboard Delta IV","thumbnails":{"default":{"url":"https://i.ytimg.com/vi/ljwjEyJJtJA/default.jpg","width":120,"height":90},"medium":{"url":"https://i.ytimg.com/vi/ljwjEyJJtJA/mqdefault.jpg","width":320,"height":180},"high":{"url":"https://i.ytimg.com/vi/ljwjEyJJtJA/hqdefault.jpg","width":480,"height":360},"standard":{"url":"https://i.ytimg.com/vi/ljwjEyJJtJA/sddefault.jpg","width":640,"height":480}}},"statistics":{"viewCount":"200"}}]
@@ -0,0 +1 @@
1
+ [{"kind":"youtube#searchResult", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/0PFW7gykxDyqGyscm_aHVBl84H8\"", "id":{"kind":"youtube#video", "videoId":"Jo_jE4LbNmM"}, "snippet":{"publishedAt":"2015-01-04T15:10:39.000Z", "channelId":"UCFzteOVJWTjODMf5wopD4wg", "title":"Granatkin Memorial 2015 Azerbaijan - Russia-2", "description":"Azerbaijan - Russia-2.", "thumbnails":{"default":{"url":"https://i.ytimg.com/vi/Jo_jE4LbNmM/default.jpg"}, "medium":{"url":"https://i.ytimg.com/vi/Jo_jE4LbNmM/mqdefault.jpg"}, "high":{"url":"https://i.ytimg.com/vi/Jo_jE4LbNmM/hqdefault.jpg"}}, "channelTitle":"", "liveBroadcastContent":"none"}}, {"kind":"youtube#searchResult", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/qF0FXRMIy7QjDNg0OsoF_bre2A4\"", "id":{"kind":"youtube#video", "videoId":"e6xwaem8TTM"}, "snippet":{"publishedAt":"2015-01-04T14:42:11.000Z", "channelId":"UCFzteOVJWTjODMf5wopD4wg", "title":"Granatkin Memorial 2015 Moldova - Lithuania", "description":"Moldova - Lithuania.", "thumbnails":{"default":{"url":"https://i.ytimg.com/vi/e6xwaem8TTM/default.jpg"}, "medium":{"url":"https://i.ytimg.com/vi/e6xwaem8TTM/mqdefault.jpg"}, "high":{"url":"https://i.ytimg.com/vi/e6xwaem8TTM/hqdefault.jpg"}}, "channelTitle":"", "liveBroadcastContent":"live"}}, {"kind":"youtube#searchResult", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/3UcTqyL8W5flVWDaSsj_Cd5U7Vg\"", "id":{"kind":"youtube#video", "videoId":"LP2bTAEti-8"}, "snippet":{"publishedAt":"2015-01-04T14:44:08.000Z", "channelId":"UCkb5yplo4fIbuDOuIYKALeQ", "title":"OCK -WAC", "description":"OCK - WAC.", "thumbnails":{"default":{"url":"https://i.ytimg.com/vi/LP2bTAEti-8/default.jpg"}, "medium":{"url":"https://i.ytimg.com/vi/LP2bTAEti-8/mqdefault.jpg"}, "high":{"url":"https://i.ytimg.com/vi/LP2bTAEti-8/hqdefault.jpg"}}, "channelTitle":"", "liveBroadcastContent":"live"}}, {"kind":"youtube#searchResult", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/tpBl-obNpd4RlpJFF-1-foUiCnU\"", "id":{"kind":"youtube#video", "videoId":"R6MceF30GBw"}, "snippet":{"publishedAt":"2015-01-04T14:41:48.000Z", "channelId":"UCFbbfl042MHNUNttAGlEOlA", "title":"TARIMSPOR -", "description":"TARIMSPOR - GOL : TARIMSPOR 19\"DK. ONUR ER TARIMSPOR (1) - LKLER SPOR (0) GOL : TARIMSPOR ...", "thumbnails":{"default":{"url":"https://i.ytimg.com/vi/R6MceF30GBw/default.jpg"}, "medium":{"url":"https://i.ytimg.com/vi/R6MceF30GBw/mqdefault.jpg"}, "high":{"url":"https://i.ytimg.com/vi/R6MceF30GBw/hqdefault.jpg"}}, "channelTitle":"", "liveBroadcastContent":"none"}}, {"kind":"youtube#searchResult", "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/uW7gbcCwbzGlX9AwQSVO7O9SbGQ\"", "id":{"kind":"youtube#video", "videoId":"YtBtTfVozXs"}, "snippet":{"publishedAt":"2015-01-04T14:33:37.000Z", "channelId":"UCvXzEblUa0cfny4HAJ_ZOWw", "title":"SEGUI JUVENTUS - INTER SU INTER CHANNEL", "description":"Inter Channel, dal 2000, il canale di informazione e intrattenimento della societ F.C. Internazionale. Ogni giorno una finestra su ci che accade nella realt ...", "thumbnails":{"default":{"url":"https://i.ytimg.com/vi/YtBtTfVozXs/default.jpg"}, "medium":{"url":"https://i.ytimg.com/vi/YtBtTfVozXs/mqdefault.jpg"}, "high":{"url":"https://i.ytimg.com/vi/YtBtTfVozXs/hqdefault.jpg"}}, "channelTitle":"INTER", "liveBroadcastContent":"none"}}]
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  #WebMock.disable_net_connect!(allow_localhost: true)
2
2
  require 'webmock/rspec'
3
+ Dir["./spec/support/**/*.rb"].each {|f| require f}
3
4
 
4
5
  RSpec.configure do |c|
5
6
  c.filter_run_including :focus => true
7
+ c.run_all_when_everything_filtered = true
6
8
  end
7
9
 
8
10
  def a_get(path)
@@ -13,6 +15,15 @@ def stub_get(path)
13
15
  stub_request(:get, Yourub::REST::Request::BASE_URL + path)
14
16
  end
15
17
 
18
+ def fixture_path
19
+ File.expand_path('../fixtures', __FILE__)
20
+ end
21
+
22
+ def fixture(file)
23
+ file = File.read(fixture_path + '/' + file)
24
+ JSON.parse(file)
25
+ end
26
+
16
27
  def capture_warning
17
28
  begin
18
29
  old_stderr = $stderr
@@ -0,0 +1,11 @@
1
+ shared_context "result load fixture" do |fixture_file|
2
+ let(:result){ double }
3
+ let(:loaded_fixture) { fixture(fixture_file) }
4
+
5
+ before do
6
+ allow(result).to receive(:status).and_return(200)
7
+ allow(result).to receive_message_chain(
8
+ :data, :items).and_return(loaded_fixture)
9
+ allow(Yourub::REST::Request).to receive(:new).and_return(result)
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ shared_context "search list result load fixture" do |fixture_file|
2
+ let(:search_list_result){ double }
3
+ let(:loaded_fixture) { fixture(fixture_file) }
4
+
5
+ before do
6
+ # search list req
7
+ allow(search_list_result).to receive_message_chain(
8
+ :data, :items).and_return(loaded_fixture)
9
+ search_list_result.data.items.each do |single_video|
10
+ allow(single_video).to receive_message_chain(
11
+ :id, :videoId).and_return(1)
12
+ end
13
+
14
+ allow(Yourub::REST::Search).to receive(:list).and_return(search_list_result)
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ shared_context "search list result load fixture with single video" do |fixture_file|
2
+ let(:search_list_result) { double }
3
+ let(:video_result) { double }
4
+ let(:loaded_fixture) { fixture(fixture_file) }
5
+ let(:video_with_200_views) { fixture('video_with_200_views.json') }
6
+
7
+ before do
8
+ # stub search list req
9
+ allow(search_list_result).to receive_message_chain(
10
+ :data, :items).and_return(loaded_fixture)
11
+ search_list_result.data.items.each do |single_video|
12
+ allow(single_video).to receive_message_chain(
13
+ :id, :videoId).and_return(1)
14
+
15
+ # stub single video request
16
+ allow(video_result).to receive(:status).and_return(200)
17
+ allow(video_result).to receive_message_chain(
18
+ :data, :items).and_return(video_with_200_views)
19
+ end
20
+
21
+ allow(Yourub::REST::Search).to receive(:list).and_return(search_list_result)
22
+ allow(Yourub::REST::Videos).to receive(:single_video).and_return(video_result)
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ shared_context "stub client connection" do
2
+ let(:client) { Yourub::Client.new }
3
+ let(:discovered_api) { double('youtube_api') }
4
+ let(:stubbed_response) { OpenStruct.new(data: 'bla', status: 200) }
5
+
6
+ before do
7
+ allow(discovered_api).to receive_message_chain(
8
+ :videos, :list).and_return('videos.list')
9
+ allow(discovered_api).to receive_message_chain(
10
+ :search, :list).and_return('search.list')
11
+ allow(discovered_api).to receive_message_chain(
12
+ :video_categories, :list).and_return('video_categories.list')
13
+ allow(client).to receive(:youtube_api).and_return(discovered_api)
14
+ allow(client).to receive(:execute!).and_return(stubbed_response)
15
+ end
16
+ end
17
+
18
+
@@ -1,81 +1,38 @@
1
+ require_relative '../spec_helper.rb'
1
2
  require 'yourub'
2
- #require 'byebug'
3
- #require_relative '../spec_helper'
4
3
 
5
4
  describe Yourub::Client do
5
+ it 'initialize a client through the config file' do
6
+ allow(Yourub::Config).to receive(:developer_key).and_return('secret')
7
+ allow(Yourub::Config).to receive(:application_name).and_return('youtube')
8
+ allow(Yourub::Config).to receive(:application_version).and_return(10)
9
+
10
+ client = Yourub::Client.new
11
+ expect(client.config.developer_key).to eq('secret')
12
+ expect(client.config.application_name).to eq('youtube')
13
+ expect(client.config.application_version).to eq(10)
14
+ end
6
15
 
7
- context "on initialize" do
8
- let(:subject) { Yourub::Client.new }
9
-
10
- it "return an error if the given country does not exist" do
11
- subject.search(country: "MOON")
12
- expect(lambda{subject}).not_to raise_error()
13
- end
14
-
15
- it "give me a list of valid countries" do
16
- expect(subject.countries).to be_a_kind_of(Array)
17
- end
18
-
19
- it "retrieves more infos with the option" do
20
- filter = {views: ">= 100"}
21
- subject.search(country: "US", category: "Sports", count_filter: filter, extended_info: true)
22
- expect(subject.videos.first.has_key? "statistics").to be_true
23
- end
24
-
25
- it "retrieves videos that have more than 100 views" do
26
- filter = {views: ">= 100"}
27
- subject.search(country: "US", category: "Sports", count_filter: filter)
28
- expect(subject.videos).to_not be_empty
29
- end
30
-
31
- it "retrieves videos for all the categories" do
32
- subject.search(country: "US", category: "all")
33
- expect(subject.videos).to_not be_empty
34
- end
35
-
36
- it "accept an 'order' parameter within the others" do
37
- subject.search(country: "US", category: "Sports", order: 'date')
38
- expect(subject.videos).to_not be_empty
39
- end
40
-
41
- it "retrieves 5 videos for each given category" do
42
- subject.search(country: "US, DE", category: "Sports", max_results: 5)
43
- expect(subject.videos.count).to eq(10)
44
- end
45
-
46
- it "retrieves 5 videos for each given category, also if they are passed as array" do
47
- subject.search(country: ["US", "DE"], category: "Sports", max_results: 5)
48
- expect(subject.videos.count).to eq(10)
49
- end
50
-
51
- it "retrieves the given number of video for the given category" do
52
- subject.search(category: "Sports", max_results: 2)
53
- expect(subject.videos.count).to eq(2)
54
- end
55
-
56
- it "retrieves the given number of video for the given word" do
57
- subject.search(query: "casa", max_results: 3)
58
- expect(subject.videos.count).to eq(3)
59
- end
60
-
61
- it "retrieves the given number of video for the given country" do
62
- subject.search(country: "US", max_results: 5)
63
- expect(subject.videos.count).to eq(5)
64
- end
65
-
66
- it "retrieves a video for the given id" do
67
- subject.search(id: "mN0Dbj-xHY0")
68
- expect(subject.videos.first["id"]).to eql("mN0Dbj-xHY0")
69
- end
16
+ it 'initialize the client accepting an hash of option' do
17
+ options = { developer_key: 'Super',
18
+ application_name: 'yourub',
19
+ application_version: 2.0,
20
+ log_level: 3 }
70
21
 
71
- it "retrieves the view count for given id" do
72
- expect(subject.get_views("mN0Dbj-xHY0")).to be_a_kind_of(Integer)
73
- end
22
+ client = Yourub::Client.new(options)
23
+ expect(client.config.developer_key).to eq('Super')
24
+ expect(client.config.application_name).to eq('yourub')
25
+ expect(client.config.application_version).to eq(2.0)
26
+ end
74
27
 
75
- it "return nil for a not existing video" do
76
- subject.search(id: "fffffffffffffffffffff")
77
- expect(subject.videos).to be_empty
78
- end
28
+ it 'use a default for the log level, if not provided' do
29
+ options = { developer_key: 'Super' }
30
+ client = Yourub::Client.new(options)
31
+ expect(client.config.log_level).to eq('WARN')
79
32
  end
80
33
 
34
+ it 'raise an argument error if there is no a developer_key in the option' do
35
+ options = { application_name: 'my app' }
36
+ expect(lambda{ Yourub::Client.new(options) }).to raise_error(ArgumentError)
37
+ end
81
38
  end
@@ -20,17 +20,16 @@ describe Yourub::CountFilter do
20
20
 
21
21
  it "accept the video if satisfy the condition" do
22
22
  Yourub::CountFilter.filter = {views: ">= 100"}
23
- expect(subject).to be_true
23
+ expect(subject).to eq true
24
24
  end
25
25
 
26
26
  it "accept the video if filter is nil" do
27
27
  Yourub::CountFilter.filter = nil
28
- expect(subject).to be_true
28
+ expect(subject).to eq true
29
29
  end
30
30
 
31
31
  it "not accept the video if it does not satisfy the condition" do
32
32
  Yourub::CountFilter.filter = {views: "<= 100"}
33
- expect(subject).to be_false
33
+ expect(subject).to eq false
34
34
  end
35
-
36
- end
35
+ end