yourub 0.1.1 → 1.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.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/Gemfile +3 -4
- data/README.md +52 -40
- data/README.md~ +91 -0
- data/lib/yourub/client.rb +184 -0
- data/lib/yourub/reader.rb +19 -0
- data/lib/yourub/validator.rb +92 -0
- data/lib/yourub/version.rb +4 -1
- data/lib/yourub.rb +3 -1
- data/spec/client_spec.rb +57 -0
- data/spec/validator_spec.rb +94 -0
- data/yourub.gemspec +2 -2
- metadata +39 -21
- data/lib/yourub/search.rb +0 -178
- data/spec/search_spec.rb +0 -52
- data/yourub.sublime-workspace +0 -338
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62dceba4187f657c56c4257e6b8694ea3cda1d28
|
4
|
+
data.tar.gz: e7b4c9de60739238e490380e6c0a94df15d25e73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c83ec59e574c7d0fc9859a79bbf3035c81550461f01f93f4d6cd3be4184a5613974ac9f263ace49edb488007d11fdcc7954201437bc6bc08583fa78262fa7413
|
7
|
+
data.tar.gz: 020afea31419fa548e7fb11725e3fbceb489014f5c1e75758564ff54da4bac80f63e8599afe2b9bcf2ae1b86a9cdf2e7e65d5f9876c9577761e238078ec4b8c8
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
[](https://codeclimate.com/github/edap/yourub)
|
2
2
|
|
3
3
|
# Yourub
|
4
|
-
|
5
|
-
Yourub is a gem that parse the Youtube API. It is possible to look for a given nation, a category, a given video ID or searching by the of views. It's updated to the version 3 of the youtube api
|
4
|
+
Yourub is a gem that search videos on youtebe using the YouTube API v3.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -31,47 +30,60 @@ If you are using rails, create a app/config/yourub.yml file as follow:
|
|
31
30
|
application_version: "version_number_of_your_app"
|
32
31
|
log_level: WARN
|
33
32
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
33
|
+
### Available parameters
|
34
|
+
`:query` String, example `aliens`
|
35
|
+
`:id` Sting, a valid youtube video id, example `NisCkxU544c`. If this parameter is set, tha others are ignored
|
36
|
+
`: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`
|
37
|
+
`:category` String, example `comedy`
|
38
|
+
`:count_filters` Hash, example `{views: ">= 100"}` or `{views: "== 600"}`
|
39
|
+
`:max_results` Integer, between 1 and 50
|
40
|
+
|
41
|
+
It's necessary at least one of this parameters to start a search: `:country`, `:category`, `:query`, `:id`
|
42
|
+
|
43
|
+
### Examples
|
44
|
+
|
45
|
+
For example, to find videos in the category "sports" in the country "de"
|
46
|
+
```ruby
|
47
|
+
client = Yourub::Client.new
|
48
|
+
client.search(country: "DE", category: "sports")
|
49
|
+
client.videos
|
50
|
+
```
|
51
|
+
|
52
|
+
To find video for a specific thema, use the parameter query
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
client = Yourub::Client.new
|
56
|
+
client.search(query: "aliens")
|
57
|
+
client.videos
|
58
|
+
```
|
59
|
+
|
60
|
+
### Extend Info
|
61
|
+
As default Yourub will give you back few values, example:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
client = Yourub::Client.new
|
65
|
+
client.search(id: "mN0Dbj-xHY0")
|
66
|
+
```
|
67
|
+
will report id, title, the default thumb, and the url where you can watch the video
|
68
|
+
```
|
69
|
+
[{"id"=>"mN0Dbj-xHY0", "title"=>"GoPro: Hiero Day", "thumb"=>"https://i1.ytimg.com/vi/mN0Dbj-xHY0/default.jpg", "url"=>"https://www.youtube.com/watch?v=mN0Dbj-xHY0"}]
|
70
|
+
```
|
71
|
+
|
72
|
+
To have more values back, use the option `extended_info`
|
73
|
+
```ruby
|
74
|
+
client = Yourub::Client.new
|
75
|
+
client.extended_info = true
|
76
|
+
client.search(id: "NisCkxU544c")
|
77
|
+
```
|
78
|
+
It will give you back much more information
|
79
|
+
```
|
80
|
+
[{"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"}}]
|
81
|
+
```
|
69
82
|
|
70
83
|
##TODO
|
71
84
|
|
72
|
-
1.
|
73
|
-
2.
|
74
|
-
3. adding a CLI
|
85
|
+
1. adding a CLI
|
86
|
+
2. add `:order` in the search criteria
|
75
87
|
|
76
88
|
## Contributing
|
77
89
|
|
data/README.md~
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
[](https://codeclimate.com/github/edap/yourub)
|
2
|
+
|
3
|
+
# Yourub
|
4
|
+
Yourub is a gem that search videos on youtebe using the YouTube API v3.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'yourub'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install yourub
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Get a developer key as explained [here](http://www.youtube.com/watch?v=Im69kzhpR3I)
|
23
|
+
If you are using rails, create a app/config/yourub.yml file as follow:
|
24
|
+
|
25
|
+
yourub:
|
26
|
+
developer_key: 'yourdeveloperkey'
|
27
|
+
youtube_api_service_name: 'youtube'
|
28
|
+
youtube_api_version: 'v3'
|
29
|
+
application_name: "nameofyourapp"
|
30
|
+
application_version: "version_number_of_your_app"
|
31
|
+
log_level: WARN
|
32
|
+
|
33
|
+
### Available parameters
|
34
|
+
`:query` String, example `aliens`
|
35
|
+
`:id` Sting, a valid youtube video id, example `NisCkxU544c`. If this parameter is set, tha others are ignored
|
36
|
+
`: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`
|
37
|
+
`:category` String, example `comedy`
|
38
|
+
`:count_filters` Hash, example `{views: ">= 100"}` or `{views: "== 600"}`
|
39
|
+
`:max_results` Integer, between 1 and 50
|
40
|
+
|
41
|
+
It's necessary at least one of this parameters to start a search: `:country`, `:category`, `:query`, `:id`
|
42
|
+
|
43
|
+
### Examples
|
44
|
+
|
45
|
+
For example, to find videos in the category "sports" in the country "de"
|
46
|
+
```ruby
|
47
|
+
client = Yourub::Client.new
|
48
|
+
client.search(country: "DE", category: "sports")
|
49
|
+
client.videos
|
50
|
+
```
|
51
|
+
|
52
|
+
To find video for a specific thema, use the parameter query
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
client = Yourub::Client.new
|
56
|
+
client.search(query: "aliens")
|
57
|
+
client.videos
|
58
|
+
```
|
59
|
+
|
60
|
+
### Extend Info
|
61
|
+
As default Yourub will give you back few values, example:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
client = Yourub::Client.new
|
65
|
+
client.search(id: "mN0Dbj-xHY0")
|
66
|
+
```
|
67
|
+
will report id, title, the default thumb, and the url where you can watch the video
|
68
|
+
[{"id"=>"mN0Dbj-xHY0", "title"=>"GoPro: Hiero Day", "thumb"=>"https://i1.ytimg.com/vi/mN0Dbj-xHY0/default.jpg", "url"=>"https://www.youtube.com/watch?v=mN0Dbj-xHY0"}]
|
69
|
+
```
|
70
|
+
|
71
|
+
To have more values back, use the option `extended_info`
|
72
|
+
```ruby
|
73
|
+
client = Yourub::Client.new
|
74
|
+
client.extended_info = true
|
75
|
+
client.search(id: "NisCkxU544c")
|
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"}}]
|
77
|
+
```
|
78
|
+
|
79
|
+
|
80
|
+
##TODO
|
81
|
+
|
82
|
+
1. adding a CLI
|
83
|
+
2. add `:order` in the search criteria
|
84
|
+
|
85
|
+
## Contributing
|
86
|
+
|
87
|
+
1. Fork it
|
88
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
89
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
90
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
91
|
+
5. Create new Pull Request
|
@@ -0,0 +1,184 @@
|
|
1
|
+
module Yourub
|
2
|
+
class Client
|
3
|
+
|
4
|
+
attr_reader :videos
|
5
|
+
attr_accessor :config, :extended_info
|
6
|
+
|
7
|
+
def initialize()
|
8
|
+
@extended_info = false
|
9
|
+
@categories, @videos = [], []
|
10
|
+
@api_options= {
|
11
|
+
:part => 'snippet',
|
12
|
+
:type => 'video',
|
13
|
+
:eventType => 'completed',
|
14
|
+
:order => 'date',
|
15
|
+
:safeSearch => 'none',
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def config
|
20
|
+
Yourub::Config
|
21
|
+
end
|
22
|
+
|
23
|
+
def client
|
24
|
+
@client ||= Google::APIClient.new(
|
25
|
+
:key => config.developer_key,
|
26
|
+
:application_name => config.application_name,
|
27
|
+
:application_version => config.application_version,
|
28
|
+
:authorization => nil,
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
def youtube
|
33
|
+
@youtube ||= client.discovered_api(config.youtube_api_service_name,
|
34
|
+
config.youtube_api_version)
|
35
|
+
end
|
36
|
+
|
37
|
+
def search(criteria)
|
38
|
+
begin
|
39
|
+
@videos = []
|
40
|
+
@criteria = Yourub::Validator.confirm(criteria)
|
41
|
+
search_by_criteria
|
42
|
+
rescue ArgumentError => e
|
43
|
+
Yourub.logger.error "#{e}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def search_by_criteria
|
48
|
+
if @criteria.has_key? :id
|
49
|
+
search_by_id
|
50
|
+
else
|
51
|
+
merge_criteria_with_api_options
|
52
|
+
retrieve_categories
|
53
|
+
retrieve_videos
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def search_by_id
|
58
|
+
params = {
|
59
|
+
:id => @criteria[:id],
|
60
|
+
:part => 'snippet,statistics',
|
61
|
+
}
|
62
|
+
video_response = client.execute!(
|
63
|
+
:api_method => youtube.videos.list,
|
64
|
+
:parameters => params
|
65
|
+
)
|
66
|
+
entry = Yourub::Reader.parse_videos(video_response).first
|
67
|
+
add_video_to_search_result(entry) unless entry.nil?
|
68
|
+
end
|
69
|
+
|
70
|
+
def merge_criteria_with_api_options
|
71
|
+
mappings = {query: :q, max_results: :maxResults, country: :regionCode}
|
72
|
+
@api_options.merge! @criteria
|
73
|
+
@api_options.keys.each do |k|
|
74
|
+
@api_options[ mappings[k] ] = @api_options.delete(k) if mappings[k]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def retrieve_categories
|
79
|
+
if @criteria.has_key? :category
|
80
|
+
get_categories_for_country(@criteria[:country])
|
81
|
+
@categories = Yourub::Validator.valid_category(@categories, @criteria[:category])
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def get_categories_for_country(country)
|
86
|
+
categories_list = video_categories_list_request(country)
|
87
|
+
categories_list.data.items.each do |cat_result|
|
88
|
+
category_name = parse_name(cat_result["snippet"]["title"])
|
89
|
+
@categories.push(cat_result["id"] => category_name)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def retrieve_videos
|
94
|
+
consume_criteria do |criteria|
|
95
|
+
req = search_list_request(criteria)
|
96
|
+
if @extended_info || @criteria[:count_filter]
|
97
|
+
get_details_and_store req
|
98
|
+
else
|
99
|
+
videos = Yourub::Reader.parse_videos(req)
|
100
|
+
videos.each{|v| add_video_to_search_result(v) }
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def consume_criteria
|
106
|
+
to_consume = @api_options
|
107
|
+
if @criteria[:country]
|
108
|
+
@criteria[:country].each do |country|
|
109
|
+
to_consume[:regionCode] = country
|
110
|
+
consume_categories(to_consume) do |cat|
|
111
|
+
yield cat
|
112
|
+
end
|
113
|
+
end
|
114
|
+
else
|
115
|
+
yield to_consume
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def consume_categories(to_consume)
|
120
|
+
if @categories.size > 0
|
121
|
+
@categories.each do |cat|
|
122
|
+
to_consume[:videoCategoryId] = cat.keys[0].to_i
|
123
|
+
yield to_consume
|
124
|
+
end
|
125
|
+
else
|
126
|
+
yield to_consume
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def get_details_and_store(video_list)
|
131
|
+
video_list.data.items.each do |video_item|
|
132
|
+
v = videos_list_request video_item.id.videoId
|
133
|
+
v = Yourub::Reader.parse_videos(v).first
|
134
|
+
add_video_to_search_result(v)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def search_list_request(options)
|
139
|
+
search_response = client.execute!(
|
140
|
+
:api_method => youtube.search.list,
|
141
|
+
:parameters => options
|
142
|
+
)
|
143
|
+
end
|
144
|
+
|
145
|
+
def videos_list_request(result_video_id)
|
146
|
+
params = video_params(result_video_id)
|
147
|
+
video_response = client.execute!(
|
148
|
+
:api_method => youtube.videos.list,
|
149
|
+
:parameters => params
|
150
|
+
)
|
151
|
+
end
|
152
|
+
|
153
|
+
def video_categories_list_request(country)
|
154
|
+
categories_list = client.execute!(
|
155
|
+
:api_method => youtube.video_categories.list,
|
156
|
+
:parameters => {"part" => "snippet","regionCode" => country }
|
157
|
+
)
|
158
|
+
end
|
159
|
+
|
160
|
+
def video_params(result_video_id)
|
161
|
+
parameters = {
|
162
|
+
:id => result_video_id,
|
163
|
+
:part => 'snippet,statistics,id',
|
164
|
+
}
|
165
|
+
unless @extended_info
|
166
|
+
fields = 'items(id,snippet(title,thumbnails),statistics(viewCount))'
|
167
|
+
parameters[:fields] = URI::encode(fields)
|
168
|
+
end
|
169
|
+
return parameters
|
170
|
+
end
|
171
|
+
|
172
|
+
def add_video_to_search_result(entry)
|
173
|
+
video = @extended_info ? entry : Yourub::Reader.parse_entry(entry)
|
174
|
+
if Yourub::CountFilter.accept?(entry, @criteria[:count_filter])
|
175
|
+
@videos.push(video)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def parse_name(name)
|
180
|
+
return name.gsub("/", "-").downcase.gsub(/\s+/, "")
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Yourub
|
2
|
+
module Reader
|
3
|
+
class << self
|
4
|
+
def parse_videos(videos)
|
5
|
+
JSON.parse(videos.data.to_json)['items']
|
6
|
+
end
|
7
|
+
|
8
|
+
def parse_entry(entry)
|
9
|
+
video_id = entry["id"]["videoId"] || entry['id']
|
10
|
+
founded_video = {
|
11
|
+
'id' => video_id,
|
12
|
+
'title' => entry['snippet']['title'],
|
13
|
+
'thumb' => entry['snippet']['thumbnails']['default']['url'],
|
14
|
+
'url' => 'https://www.youtube.com/watch?v='<< video_id
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module Yourub
|
2
|
+
module Validator
|
3
|
+
class << self
|
4
|
+
#attr_reader :videos, :countrys, :categories
|
5
|
+
|
6
|
+
DEFAULT_COUNTRY = "US" #if not given, a country it's necessary to retrieve a categories list
|
7
|
+
COUNTRIES = [
|
8
|
+
'AR','AU','AT','BE','BR','CA','CL','CO','CZ','EG','FR','DE','GB','HK',
|
9
|
+
'HU','IN','IE','IL','IT','JP','JO','MY','MX','MA','NL','NZ','PE','PH',
|
10
|
+
'PL','RU','SA','SG','ZA','KR','ES','SE','CH','TW','AE','US']
|
11
|
+
|
12
|
+
VALID_PARAMS = [:country, :category, :query, :id, :max_results, :count_filter]
|
13
|
+
MINIMUM_PARAMS = [:country, :category, :query, :id]
|
14
|
+
|
15
|
+
def confirm(criteria)
|
16
|
+
@criteria = criteria
|
17
|
+
valid_format?
|
18
|
+
remove_empty_and_non_valid_params
|
19
|
+
minimum_param_present?
|
20
|
+
|
21
|
+
keep_only_the_id_if_present
|
22
|
+
countries_to_array
|
23
|
+
add_default_country_if_category_is_present
|
24
|
+
validate_countries
|
25
|
+
|
26
|
+
@criteria
|
27
|
+
end
|
28
|
+
|
29
|
+
def remove_empty_and_non_valid_params
|
30
|
+
@criteria.keep_if{|k,v| ( (VALID_PARAMS.include? k) && (v.size > 0)) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def keep_only_the_id_if_present
|
34
|
+
if @criteria.has_key? :id
|
35
|
+
@criteria.keep_if{|k, _| k == :id}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def countries_to_array
|
40
|
+
if @criteria.has_key? :country
|
41
|
+
@criteria[:country] = @criteria[:country].split(',').collect(&:strip)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_default_country_if_category_is_present
|
46
|
+
if (@criteria.has_key? :category) && (!@criteria.has_key? :country)
|
47
|
+
@criteria[:country] = [ DEFAULT_COUNTRY ]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def valid_category(categories, selected_category)
|
52
|
+
categories = categories.select {|k| k.has_value?(selected_category.downcase)}
|
53
|
+
if categories.first.nil?
|
54
|
+
raise ArgumentError.new(
|
55
|
+
"The category #{selected_category} does not exists in the following ones: #{categories.join(',')}")
|
56
|
+
end
|
57
|
+
return categories
|
58
|
+
end
|
59
|
+
|
60
|
+
def valid_format?
|
61
|
+
raise ArgumentError.new(
|
62
|
+
"give an hash as search criteria"
|
63
|
+
) unless( @criteria.is_a? Hash )
|
64
|
+
end
|
65
|
+
|
66
|
+
def minimum_param_present?
|
67
|
+
if @criteria.none?{|k,_| MINIMUM_PARAMS.include? k}
|
68
|
+
raise ArgumentError.new(
|
69
|
+
"minimum params to start a search is at least one of: #{MINIMUM_PARAMS.join(',')}"
|
70
|
+
)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def validate_countries
|
75
|
+
if @criteria.has_key? :country
|
76
|
+
raise ArgumentError.new(
|
77
|
+
"the given country is not in the available ones: #{COUNTRIES.join(',')}"
|
78
|
+
) unless( (@criteria[:country] & COUNTRIES).size > 0 )
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def validate_max_results
|
83
|
+
raise ArgumentError.new(
|
84
|
+
'max 50 videos pro categories or country'
|
85
|
+
) unless(
|
86
|
+
@criteria[:max_results].to_i < 50 || @criteria[:max_results].to_i == 0
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/yourub/version.rb
CHANGED
data/lib/yourub.rb
CHANGED
@@ -5,10 +5,12 @@ require 'open-uri'
|
|
5
5
|
require 'google/api_client'
|
6
6
|
|
7
7
|
require 'yourub/config'
|
8
|
+
require 'yourub/client'
|
8
9
|
require 'yourub/version'
|
9
10
|
require 'yourub/logger'
|
10
11
|
require 'yourub/count_filter'
|
11
|
-
require 'yourub/
|
12
|
+
require 'yourub/validator'
|
13
|
+
require 'yourub/reader'
|
12
14
|
|
13
15
|
if defined?(Rails)
|
14
16
|
require 'yourub/railtie'
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'yourub'
|
2
|
+
|
3
|
+
describe Yourub::Client do
|
4
|
+
|
5
|
+
context "on initialize" do
|
6
|
+
let(:subject) { Yourub::Client.new }
|
7
|
+
|
8
|
+
it "return an error if the given country does not exist" do
|
9
|
+
subject.search(country: "MOON")
|
10
|
+
expect(lambda{subject}).not_to raise_error()
|
11
|
+
end
|
12
|
+
|
13
|
+
it "retrieves more infos with the option extended_info = true" do
|
14
|
+
filter = {views: ">= 100"}
|
15
|
+
subject.extended_info = true
|
16
|
+
subject.search(country: "US", category: "Sports", count_filter: filter)
|
17
|
+
expect(subject.videos.first.has_key? "statistics").to be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "retrieves videos that have more than 100 views" do
|
21
|
+
filter = {views: ">= 100"}
|
22
|
+
subject.search(country: "US", category: "Sports", count_filter: filter)
|
23
|
+
expect(subject.videos).to be_a_kind_of(Array)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "retrieves 5 videos for each given category" do
|
27
|
+
subject.search(country: "US, DE", category: "Sports", max_results: 5)
|
28
|
+
expect(subject.videos.count).to eq(10)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "retrieves the given number of video for the given category" do
|
32
|
+
subject.search(category: "Sports", max_results: 2)
|
33
|
+
expect(subject.videos.count).to eq(2)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "retrieves the given number of video for the given word" do
|
37
|
+
subject.search(query: "casa", max_results: 3)
|
38
|
+
expect(subject.videos.count).to eq(3)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "retrieves the given number of video for the given country" do
|
42
|
+
subject.search(country: "US", max_results: 5)
|
43
|
+
expect(subject.videos.count).to eq(5)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "retrieves a video for the given id" do
|
47
|
+
subject.search(id: "mN0Dbj-xHY0")
|
48
|
+
expect(subject.videos.first["id"]).to eql("mN0Dbj-xHY0")
|
49
|
+
end
|
50
|
+
|
51
|
+
it "return nil for a not existing video" do
|
52
|
+
subject.search(id: "fffffffffffffffffffff")
|
53
|
+
expect(subject.videos).to be_empty
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|