yourub 1.1.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -6
- data/Gemfile +15 -1
- data/README.md +36 -25
- data/Rakefile +18 -0
- data/config/yourub.yml +1 -1
- data/lib/yourub/client.rb +38 -10
- data/lib/yourub/config.rb +34 -1
- data/lib/yourub/default.rb +1 -1
- data/lib/yourub/meta_search.rb +134 -0
- data/lib/yourub/rest/api.rb +6 -0
- data/lib/yourub/rest/categories.rb +39 -0
- data/lib/yourub/rest/request.rb +22 -4
- data/lib/yourub/rest/search.rb +11 -149
- data/lib/yourub/rest/videos.rb +38 -0
- data/lib/yourub/result.rb +41 -0
- data/lib/yourub/validator.rb +4 -11
- data/lib/yourub/version.rb +3 -3
- data/lib/yourub.rb +2 -3
- data/spec/fixtures/categories_list.json +1 -0
- data/spec/fixtures/categories_list_formatted.json +1 -0
- data/spec/fixtures/search_list.json +1 -0
- data/spec/fixtures/single_video.json +1 -0
- data/spec/fixtures/video_with_200_views.json +1 -0
- data/spec/fixtures/videos_list.json +1 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/support/shared_context_result_load_fixture.rb +11 -0
- data/spec/support/shared_context_result_search_list.rb +16 -0
- data/spec/support/shared_context_result_search_list_with_single_videos.rb +24 -0
- data/spec/support/shared_context_stub_client_connection.rb +18 -0
- data/spec/yourub/client_spec.rb +29 -72
- data/spec/yourub/count_filter_spec.rb +4 -5
- data/spec/yourub/count_spec.rb +39 -0
- data/spec/yourub/integration.rb +105 -0
- data/spec/yourub/meta_search_spec.rb +141 -0
- data/spec/yourub/rest/categories_spec.rb +33 -0
- data/spec/yourub/rest/request_spec.rb +32 -0
- data/spec/yourub/rest/videos_spec.rb +24 -0
- data/spec/yourub/validator_spec.rb +54 -57
- data/yourub.gemspec +0 -5
- metadata +39 -66
- data/lib/yourub/cli.rb +0 -20
- data/lib/yourub/reader.rb +0 -21
- data/spec/fixtures/categories.json +0 -0
- data/spec/fixtures/category.json +0 -0
- data/spec/helper.rb +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3dd41972b4391d0680edfb6a53c4ffac3251ec10
|
4
|
+
data.tar.gz: 56d7304673339a8ee102c0ac93e5736f877453f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 197357a9f8930cf34a8230c871d7315df3361b3db59f6c42df6ace9e5308770fd9cff323b961fd02d4936006d6dd9e7edbeeb3fe7bc1f3d8636350538c909e35
|
7
|
+
data.tar.gz: e69618e8f18d5aa6eb9d8a76bf0f949cac2ed1effcd57ccce6a837917e1628d8966338c41bc7c7391b6bb9b3f825e99d8026d02b10cf9871b49bca9402376736
|
data/.gitignore
CHANGED
@@ -1,20 +1,17 @@
|
|
1
1
|
*.gem
|
2
2
|
*.rbc
|
3
3
|
*.sublime-workspace
|
4
|
+
spec/yourub/integration_spec.rb
|
4
5
|
.bundle
|
5
6
|
.config/*
|
6
7
|
.yardoc
|
7
8
|
Gemfile.lock
|
8
9
|
InstalledFiles
|
9
10
|
_yardoc
|
10
|
-
coverage
|
11
|
-
doc
|
11
|
+
coverage/*
|
12
|
+
doc/*
|
12
13
|
lib/bundler/man
|
13
14
|
lib/yourub/DEVELOPER_KEY
|
14
15
|
lib/yourub/cli.rb
|
15
16
|
pkg
|
16
17
|
rdoc
|
17
|
-
spec/reports
|
18
|
-
test/tmp
|
19
|
-
test/version_tmp
|
20
|
-
tmp
|
data/Gemfile
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
|
3
|
+
gem 'rake'
|
4
|
+
gem 'yard'
|
5
|
+
|
6
|
+
group :development do
|
7
|
+
gem 'byebug'
|
8
|
+
end
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem 'coveralls'
|
12
|
+
gem 'rspec', '>= 3.00'
|
13
|
+
gem 'rubocop', '>= 0.27'
|
14
|
+
gem 'simplecov', '>= 0.9'
|
15
|
+
gem 'webmock'
|
16
|
+
end
|
17
|
+
|
4
18
|
gemspec
|
5
19
|
|
data/README.md
CHANGED
@@ -20,7 +20,8 @@ Or install it yourself as:
|
|
20
20
|
## Usage
|
21
21
|
|
22
22
|
Get a developer key as explained [here](http://www.youtube.com/watch?v=Im69kzhpR3I)
|
23
|
-
|
23
|
+
After that, you have 2 ways to use the client. If you want to use it in your
|
24
|
+
rails app, create a app/config/yourub.yml file as follow:
|
24
25
|
```
|
25
26
|
yourub_defaults: &yourub_defaults
|
26
27
|
developer_key: 'YoUrDevEl0PerKey'
|
@@ -40,44 +41,57 @@ test:
|
|
40
41
|
<<: *yourub_defaults
|
41
42
|
```
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
`:category` String, example `comedy`. It accept the wildcard `all`, that retrieves videos for all the category for the given nation, or the default one (US) if no one is given
|
51
|
-
|
52
|
-
`:count_filter` Hash, example `{views: ">= 100"}` or `{views: "== 600"}`
|
44
|
+
If you want to use it from the console, or in another program, you can simply
|
45
|
+
pass an hash containing the needed option
|
46
|
+
```ruby
|
47
|
+
options = { developer_key: 'mySecretKey',
|
48
|
+
application_name: 'yourub',
|
49
|
+
application_version: 2.0,
|
50
|
+
log_level: 3 }
|
53
51
|
|
54
|
-
|
52
|
+
client = Yourub::Client.new(options)
|
53
|
+
```
|
55
54
|
|
56
|
-
`:order` String, one of these: 'date', 'rating', 'relevance', 'title', 'videocount', 'viewcount'. The default one is `'relevance'`
|
57
55
|
|
58
|
-
It's necessary at least one of this parameters to start a search: `:country`, `:category`, `:query`, `:id`
|
59
|
-
|
60
56
|
### Examples
|
61
57
|
|
62
58
|
For example, to find the most recent videos in the category "sports" in Germany
|
63
59
|
```ruby
|
64
60
|
client = Yourub::Client.new
|
65
|
-
client.search(country: "DE", category: "sports", order: 'date')
|
66
|
-
|
61
|
+
client.search(country: "DE", category: "sports", order: 'date') do |v|
|
62
|
+
puts v
|
63
|
+
end
|
67
64
|
```
|
68
65
|
|
69
66
|
To find video for a specific thema, use the parameter query
|
70
67
|
|
71
68
|
```ruby
|
72
69
|
client = Yourub::Client.new
|
73
|
-
client.search(query: "aliens")
|
74
|
-
|
70
|
+
client.search(query: "aliens") do |v|
|
71
|
+
puts v
|
72
|
+
end
|
75
73
|
```
|
76
74
|
that is the content of the response:
|
77
75
|
```
|
78
76
|
[{"kind"=>"youtube#video", "etag"=>"\"N5Eg36Gl054SUNiWWc-Su3t5O-k/U6AzLXvcnZt2WFqpnq9_dksV7DA\"", "id"=>"NisCkxU544c", "snippet"=>{"publishedAt"=>"2009-04-05T05:20:10.000Z", "channelId"=>"UCCHcEUksSVKsRDH86j77Ntg", "title"=>"Like A Boss (ft. Seth Rogen) - Uncensored Version", "description"=>"http://www.itunes.com/thelonelyisland\r\n\r\nThe new single from The Lonely Island's debut album \"INCREDIBAD\" In stores now!\r\n\r\nFeaturing Seth Rogen.\r\n\r\nThe Lonely Island is Andy Samberg, Akiva Schaffer & Jorma Taccone.", "thumbnails"=>{"default"=>{"url"=>"https://i1.ytimg.com/vi/NisCkxU544c/default.jpg", "width"=>120, "height"=>90}, "medium"=>{"url"=>"https://i1.ytimg.com/vi/NisCkxU544c/mqdefault.jpg", "width"=>320, "height"=>180}, "high"=>{"url"=>"https://i1.ytimg.com/vi/NisCkxU544c/hqdefault.jpg", "width"=>480, "height"=>360}, "standard"=>{"url"=>"https://i1.ytimg.com/vi/NisCkxU544c/sddefault.jpg", "width"=>640, "height"=>480}, "maxres"=>{"url"=>"https://i1.ytimg.com/vi/NisCkxU544c/maxresdefault.jpg", "width"=>1280, "height"=>720}}, "channelTitle"=>"thelonelyisland", "categoryId"=>"23", "liveBroadcastContent"=>"none"}, "statistics"=>{"viewCount"=>"120176425", "likeCount"=>"594592", "dislikeCount"=>"15121", "favoriteCount"=>"0", "commentCount"=>"208109"}}]
|
79
77
|
```
|
78
|
+
### Available parameters
|
79
|
+
`:query` String, example `aliens`
|
80
|
+
|
81
|
+
`:id` Sting, a valid youtube video id, example `NisCkxU544c`. If this parameter is set, tha others are ignored
|
82
|
+
|
83
|
+
`:country` String, one or more alpha-2 country codes [ISO 3166-1](http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_names_and_code_elements.htm), example `US` or `IT, DE`. The default one is `US`
|
84
|
+
|
85
|
+
`:category` String, example `comedy`. It accept the wildcard `all`, that retrieves videos for all the category for the given nation, or the default one (US) if no one is given
|
86
|
+
|
87
|
+
`:count_filter` Hash, example `{views: ">= 100"}` or `{views: "== 600"}`
|
80
88
|
|
89
|
+
`:max_results` Integer, between 1 and 50
|
90
|
+
|
91
|
+
`:order` String, one of these: 'date', 'rating', 'relevance', 'title', 'videocount', 'viewcount'. The default one is `'relevance'`
|
92
|
+
|
93
|
+
It's necessary at least one of this parameters to start a search: `:country`, `:category`, `:query`, `:id`
|
94
|
+
|
81
95
|
### Methods
|
82
96
|
`search`, search youtube videos for the given parameters
|
83
97
|
```ruby
|
@@ -93,13 +107,10 @@ client.get_views("G2b0OIkTraI")
|
|
93
107
|
```
|
94
108
|
|
95
109
|
|
96
|
-
To have more values back, use the option `extended_info`
|
97
|
-
##TODO
|
98
110
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
4. Implementing playlist
|
111
|
+
##TODO
|
112
|
+
* the result should be yield, and not saved in an instance variable
|
113
|
+
* Implementing playlist
|
103
114
|
|
104
115
|
## Contributing
|
105
116
|
|
data/Rakefile
CHANGED
@@ -1 +1,19 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
require 'bundler'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
task :erd do
|
6
|
+
FORMAT = 'svg'
|
7
|
+
`bundle exec ruby ./etc/erd.rb > ./etc/erd.dot`
|
8
|
+
`dot -T #{FORMAT} ./etc/erd.dot -o ./etc/erd.#{FORMAT}`
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rspec/core/rake_task'
|
12
|
+
RSpec::Core::RakeTask.new(:spec)
|
13
|
+
|
14
|
+
task test: :spec
|
15
|
+
|
16
|
+
require 'yard'
|
17
|
+
YARD::Rake::YardocTask.new
|
18
|
+
|
19
|
+
task default: [:spec]
|
data/config/yourub.yml
CHANGED
data/lib/yourub/client.rb
CHANGED
@@ -1,14 +1,39 @@
|
|
1
|
-
require 'yourub/
|
1
|
+
require 'yourub/meta_search'
|
2
2
|
require 'google/api_client'
|
3
3
|
|
4
4
|
module Yourub
|
5
5
|
class Client < Google::APIClient
|
6
|
-
include Yourub::
|
6
|
+
include Yourub::MetaSearch
|
7
7
|
|
8
8
|
attr_reader :videos
|
9
|
-
attr_accessor :config
|
10
9
|
|
11
|
-
|
10
|
+
# The Yourub::Client is a subclass of the Google::APIClient.
|
11
|
+
#
|
12
|
+
# @see http://www.rubydoc.info/github/google/google-api-ruby-client/Google/APIClient
|
13
|
+
# In order to initialize the client, you have either to provide
|
14
|
+
# a configuration file 'config/yourub.yml' in your main application folder
|
15
|
+
# or to pass an hash in the initialization.
|
16
|
+
# @example
|
17
|
+
# #passing an hash
|
18
|
+
# options = { developer_key: "a_secret_key",
|
19
|
+
# application_name: "my_app",
|
20
|
+
# application_version: 2.0,
|
21
|
+
# log_level: "INFO"}
|
22
|
+
# client = Yourub::Client.new(options)
|
23
|
+
#
|
24
|
+
# If you don't provide all the values, default values will be used.The only
|
25
|
+
# mandatory value is the developer_key
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# #assuming that you have a file in config/yourub.yml (see the example in
|
29
|
+
# #the source repository)
|
30
|
+
# client = Yourub::Client.new()
|
31
|
+
|
32
|
+
def initialize(options = {})
|
33
|
+
unless options.empty?
|
34
|
+
Yourub::Config.override_config_file(options)
|
35
|
+
end
|
36
|
+
|
12
37
|
args = {
|
13
38
|
:key => config.developer_key,
|
14
39
|
:application_name => config.application_name,
|
@@ -18,18 +43,21 @@ module Yourub
|
|
18
43
|
super(args)
|
19
44
|
end
|
20
45
|
|
21
|
-
|
22
|
-
Yourub::Config
|
23
|
-
end
|
24
|
-
|
46
|
+
# @return countries [Array] contain a list of the available alpha-2 country codes ISO 3166-1
|
25
47
|
def countries
|
26
48
|
Yourub::Validator.available_countries
|
27
49
|
end
|
28
50
|
|
51
|
+
# it returns the youtube service object
|
52
|
+
# @see http://www.rubydoc.info/github/google/google-api-ruby-client/Google/APIClient#discovered_api-instance_method
|
29
53
|
def youtube_api
|
30
|
-
@youtube_api ||= self.discovered_api(
|
31
|
-
config.youtube_api_version
|
54
|
+
@youtube_api ||= self.discovered_api(
|
55
|
+
config.youtube_api_service_name, config.youtube_api_version
|
56
|
+
)
|
32
57
|
end
|
33
58
|
|
59
|
+
def config
|
60
|
+
Yourub::Config
|
61
|
+
end
|
34
62
|
end
|
35
63
|
end
|
data/lib/yourub/config.rb
CHANGED
@@ -12,10 +12,43 @@ module Yourub
|
|
12
12
|
def_delegators :@config, :developer_key,:youtube_api_service_name, :log_level,
|
13
13
|
:youtube_api_version, :application_name, :application_version
|
14
14
|
|
15
|
+
MANDATORY_KEYS = [:developer_key, :application_name, :application_version, :log_level]
|
16
|
+
|
17
|
+
# It loads the configuration file
|
18
|
+
# this method is used in the railtie.rb and in the default.rb files
|
19
|
+
# @example
|
20
|
+
# Yourub::Config.load!(File.join("config", "yourub.yml"), 'yourub_defaults')
|
15
21
|
def load!(file_path, environment)
|
16
22
|
@config = OpenStruct.new YAML.load_file(file_path)[environment]
|
17
23
|
end
|
18
|
-
end
|
19
24
|
|
25
|
+
|
26
|
+
def override_config_file(hash)
|
27
|
+
opt = valid_options(hash)
|
28
|
+
@config = OpenStruct.new opt
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def valid_options(hash)
|
34
|
+
opt = default_options.merge(hash)
|
35
|
+
raise ArgumentError.new(
|
36
|
+
"please provide an hash containing at least a valid developer_key"
|
37
|
+
) if opt[:developer_key].empty?
|
38
|
+
return opt
|
39
|
+
end
|
40
|
+
|
41
|
+
def default_options
|
42
|
+
{
|
43
|
+
developer_key: "",
|
44
|
+
youtube_api_service_name: "youtube",
|
45
|
+
youtube_api_version: "v3",
|
46
|
+
application_name: "yourub",
|
47
|
+
application_version: Yourub::VERSION,
|
48
|
+
log_level: "WARN"
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
20
53
|
end
|
21
54
|
end
|
data/lib/yourub/default.rb
CHANGED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'yourub/rest/api'
|
2
|
+
|
3
|
+
module Yourub
|
4
|
+
module MetaSearch
|
5
|
+
include Yourub::REST::API
|
6
|
+
|
7
|
+
# Search through the youtube API, executing multiple queries where necessary
|
8
|
+
# @param criteria [Hash]
|
9
|
+
# @example
|
10
|
+
# client = Yourub::Client.new
|
11
|
+
# client.search(country: "DE", category: "sports", order: 'date')
|
12
|
+
def search(criteria)
|
13
|
+
begin
|
14
|
+
@api_options= {
|
15
|
+
:part => 'snippet',
|
16
|
+
:type => 'video',
|
17
|
+
:order => 'relevance',
|
18
|
+
:safeSearch => 'none',
|
19
|
+
}
|
20
|
+
@categories = []
|
21
|
+
@count_filter = {}
|
22
|
+
@criteria = Yourub::Validator.confirm(criteria)
|
23
|
+
search_by_criteria do |result|
|
24
|
+
yield result
|
25
|
+
end
|
26
|
+
rescue ArgumentError => e
|
27
|
+
Yourub.logger.error "#{e}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# return the number of times a video was watched
|
32
|
+
# @param video_id[Integer]
|
33
|
+
# @example
|
34
|
+
# client = Yourub::Client.new
|
35
|
+
# client.get_views("G2b0OIkTraI")
|
36
|
+
def get_views(video_id)
|
37
|
+
params = { :id => video_id, :part => 'statistics' }
|
38
|
+
request = Yourub::REST::Videos.list(self,params)
|
39
|
+
v = Yourub::Result.format(request).first
|
40
|
+
v ? Yourub::CountFilter.get_views_count(v) : nil
|
41
|
+
end
|
42
|
+
|
43
|
+
# return an hash containing the metadata for the given video
|
44
|
+
# @param video_id[Integer]
|
45
|
+
# @example
|
46
|
+
# client = Yourub::Client.new
|
47
|
+
# client.get("G2b0OIkTraI")
|
48
|
+
def get(video_id)
|
49
|
+
params = {:id => video_id, :part => 'snippet,statistics'}
|
50
|
+
request = Yourub::REST::Videos.list(self,params)
|
51
|
+
Yourub::Result.format(request).first
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def search_by_criteria
|
57
|
+
merge_criteria_with_api_options
|
58
|
+
retrieve_categories
|
59
|
+
retrieve_videos do |res|
|
60
|
+
yield res
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def merge_criteria_with_api_options
|
65
|
+
mappings = {query: :q, max_results: :maxResults, country: :regionCode}
|
66
|
+
@api_options.merge! @criteria
|
67
|
+
@api_options.keys.each do |k|
|
68
|
+
@api_options[ mappings[k] ] = @api_options.delete(k) if mappings[k]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def retrieve_categories
|
73
|
+
if @criteria.has_key? :category
|
74
|
+
@categories = Yourub::REST::Categories.for_country(self,@criteria[:country])
|
75
|
+
@categories = Yourub::Validator.valid_category(@categories, @criteria[:category])
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def retrieve_videos
|
80
|
+
consume_criteria do |criteria|
|
81
|
+
begin
|
82
|
+
req = Yourub::REST::Search.list(self, criteria)
|
83
|
+
get_details_for_each_video(req) do |v|
|
84
|
+
yield v
|
85
|
+
end
|
86
|
+
rescue StandardError => e
|
87
|
+
Yourub.logger.error "Error #{e} retrieving videos for the criteria: #{criteria.to_s}"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def consume_criteria
|
93
|
+
to_consume = @api_options
|
94
|
+
if @criteria[:country]
|
95
|
+
@criteria[:country].each do |country|
|
96
|
+
to_consume[:regionCode] = country
|
97
|
+
consume_categories(to_consume) do |cat|
|
98
|
+
yield cat
|
99
|
+
end
|
100
|
+
end
|
101
|
+
else
|
102
|
+
yield to_consume
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def consume_categories(to_consume)
|
107
|
+
if @categories.size > 0
|
108
|
+
@categories.each do |cat|
|
109
|
+
to_consume[:videoCategoryId] = cat.keys[0].to_i
|
110
|
+
yield to_consume
|
111
|
+
end
|
112
|
+
else
|
113
|
+
yield to_consume
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def get_details_for_each_video(video_list)
|
118
|
+
video_list.data.items.each do |video_item|
|
119
|
+
v = Yourub::REST::Videos.single_video(self, video_item.id.videoId)
|
120
|
+
v = Yourub::Result.format(v)
|
121
|
+
if v && Yourub::CountFilter.accept?(v.first)
|
122
|
+
yield v.first
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def add_video_to_search_result(entry)
|
128
|
+
if Yourub::CountFilter.accept?(entry)
|
129
|
+
@videos.push(entry)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
end
|
data/lib/yourub/rest/api.rb
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
#require 'yourub/rest/search'
|
2
|
+
require 'yourub/rest/request'
|
3
|
+
require 'yourub/rest/categories'
|
1
4
|
require 'yourub/rest/search'
|
5
|
+
require 'yourub/rest/videos'
|
2
6
|
|
3
7
|
module Yourub
|
4
8
|
module REST
|
@@ -7,6 +11,8 @@ module Yourub
|
|
7
11
|
# include Yourub::REST::Playlists
|
8
12
|
# include Yourub::REST::Channels
|
9
13
|
include Yourub::REST::Search
|
14
|
+
include Yourub::REST::Categories
|
15
|
+
include Yourub::REST::Videos
|
10
16
|
end
|
11
17
|
end
|
12
18
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'yourub/rest/request'
|
2
|
+
|
3
|
+
module Yourub
|
4
|
+
module REST
|
5
|
+
module Categories
|
6
|
+
class << self
|
7
|
+
# it returns an Array containing the categories for the given country,
|
8
|
+
# or the DEFAULT_COUNTRY if no countries was given.
|
9
|
+
# @param client[Yourub::Client]
|
10
|
+
# @param country[Array]
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# client = Yourub::Client.new
|
15
|
+
# categories = Yourub::REST::Categories.for_country(client, ["US"])
|
16
|
+
#
|
17
|
+
def for_country(client, country)
|
18
|
+
categories = []
|
19
|
+
param = {"part" => "snippet","regionCode" => country }
|
20
|
+
categories_list = video_categories_list_request(client, param)
|
21
|
+
categories_list.data.items.each do |cat_result|
|
22
|
+
category_name = parse_name(cat_result["snippet"]["title"])
|
23
|
+
categories.push(cat_result["id"] => category_name)
|
24
|
+
end
|
25
|
+
return categories
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def video_categories_list_request(client, params)
|
30
|
+
Yourub::REST::Request.new(client,"video_categories", "list", params)
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse_name(name)
|
34
|
+
return name.gsub("/", "-").downcase.gsub(/\s+/, "")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/yourub/rest/request.rb
CHANGED
@@ -2,25 +2,43 @@ module Yourub
|
|
2
2
|
module REST
|
3
3
|
class Request
|
4
4
|
attr_reader :data, :status
|
5
|
-
|
5
|
+
#
|
6
|
+
# Once initializated, the Request object forward a request to
|
7
|
+
# youtube api, once the request is completed, the variables @status and @data are populated
|
8
|
+
#
|
6
9
|
# @param client [Yourub::Client]
|
7
10
|
# @param resource_type [String]
|
8
11
|
# @param method [String]
|
9
12
|
# @param params [Hash]
|
13
|
+
#
|
10
14
|
# @return [Youtube::Request]
|
15
|
+
# @example
|
16
|
+
# Yourub::REST::Request.new(client,"video_categories", "list", param)
|
17
|
+
#
|
11
18
|
def initialize(client, resource_type, method, params)
|
12
19
|
@client = client
|
13
|
-
@resource_type = resource_type
|
14
|
-
@method = method
|
20
|
+
@resource_type = resource_type.to_sym
|
21
|
+
@method = method.to_sym
|
15
22
|
@params = params
|
16
23
|
perform
|
17
24
|
end
|
18
25
|
|
26
|
+
private
|
27
|
+
#
|
28
|
+
# call the 'execute!' method on the client
|
29
|
+
#
|
30
|
+
# == Returns :
|
31
|
+
# The Request object with the variable @status and @data initialized
|
32
|
+
# @status [Int]
|
33
|
+
# @data [String]
|
34
|
+
#
|
19
35
|
def perform
|
36
|
+
api_method =@client.youtube_api.send(@resource_type).send(@method)
|
20
37
|
r = @client.execute!(
|
21
|
-
:api_method =>
|
38
|
+
:api_method => api_method,
|
22
39
|
:parameters => @params
|
23
40
|
)
|
41
|
+
#byebug
|
24
42
|
@data = r.data
|
25
43
|
@status = r.status
|
26
44
|
end
|