yourub 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,94 @@
1
+ require 'yourub'
2
+
3
+ describe Yourub::Validator do
4
+ let(:subject) { Yourub::Validator.confirm(criteria) }
5
+
6
+ context "passing an hash containing the search criteria" do
7
+ context "with some valid criteria" do
8
+ context "with valid nation but unknown parameter city" do
9
+ let(:criteria) { {country: 'US', city: 'Berlin'} }
10
+ it "return criteria including nation but excluding the city" do
11
+ expect(subject).to eq({country: ['US']})
12
+ end
13
+ end
14
+
15
+ context "with a given category without a country" do
16
+ let(:criteria) { {country: '', category: 'Sport'} }
17
+ it "add the default country" do
18
+ expect(subject).to eq({category: 'Sport', :country=>["US"]})
19
+ end
20
+ end
21
+
22
+ context "with all the available parameters present" do
23
+ let(:criteria) { {country: 'IT',
24
+ category: 'Sport',
25
+ max_results: 2,
26
+ count_filter: {},
27
+ query: '',
28
+ id: ''} }
29
+ it "return only them params that has a value" do
30
+ expect(subject).to eq({country: ["IT"], category: 'Sport', max_results: 2 })
31
+ end
32
+ end
33
+
34
+ context "when the parameter 'id' is set" do
35
+ let(:criteria) { {country: 'US', id: '1111111111'} }
36
+ it "ignore the other params and leave only the id" do
37
+ expect(subject).to eq({ id: '1111111111' })
38
+ end
39
+ end
40
+ end
41
+
42
+ context "if none of the criteria are valid" do
43
+ context "with non valid criteria" do
44
+ let(:criteria) { {big: 1, bang: 2} }
45
+ it 'raise an argument error' do
46
+ expect(lambda{subject}).to raise_error(ArgumentError)
47
+ end
48
+ end
49
+
50
+ context "with criteria in the wrong format, like a string" do
51
+ let(:criteria) { "country = US" }
52
+ it 'raise an argument error' do
53
+ expect(lambda{subject}).to raise_error(ArgumentError)
54
+ end
55
+ end
56
+
57
+ context "with an invalid country" do
58
+ let(:criteria) { {country: 'MOON'} }
59
+ it 'raise an argument error' do
60
+ expect(lambda{subject}).to raise_error(ArgumentError)
61
+ end
62
+ end
63
+ end
64
+
65
+ context "with valid criteria but not enough to start a search" do
66
+ context "only with maximum and count filter" do
67
+ let(:criteria) { {max_results: 10, count_filter: {views: ">= 100"}} }
68
+ it 'raise an argument error' do
69
+ expect(lambda{subject}).to raise_error(ArgumentError)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ context "passing a list of available categories and the selected one" do
76
+ let(:subject) { Yourub::Validator.valid_category(categories, selected_one) }
77
+ let(:categories) { [{"10"=>"music"}, {"15"=>"pets&animals"}, {"17"=>"sports"}, {"18"=>"shortmovies"}] }
78
+
79
+ context "with an invalid category" do
80
+ let(:selected_one) { "spaghetti" }
81
+ it 'raise an argument error' do
82
+ expect(lambda{subject}).to raise_error(ArgumentError)
83
+ end
84
+ end
85
+
86
+ context "with a valid category" do
87
+ let(:selected_one) { "sports" }
88
+ it 'return it' do
89
+ expect(subject.first.has_value? "sports").to be_true
90
+ end
91
+ end
92
+ end
93
+
94
+ end
data/yourub.gemspec CHANGED
@@ -10,8 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["lastexxit@gmail.com "]
11
11
  spec.description = %q{Youtube API v3 parser}
12
12
  spec.summary = %q{Yourub is a gem that fetch the most recent videos from
13
- the Youtube API for the given nation, category and number
14
- of views}
13
+ the Youtube API for the given criteria}
15
14
  spec.homepage = ""
16
15
  spec.license = "MIT"
17
16
 
@@ -22,6 +21,7 @@ Gem::Specification.new do |spec|
22
21
 
23
22
  spec.add_development_dependency "bundler", "~> 1.3"
24
23
  spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "byebug"
25
25
  spec.add_development_dependency "google-api-client"
26
26
 
27
27
  spec.add_development_dependency "rspec", "~> 2.14"
metadata CHANGED
@@ -1,83 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yourub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Davide Prati
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-20 00:00:00.000000000 Z
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: google-api-client
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - '>='
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - '>='
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - ~>
73
+ - - "~>"
60
74
  - !ruby/object:Gem::Version
61
75
  version: '2.14'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - ~>
80
+ - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '2.14'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: thor
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - ~>
87
+ - - "~>"
74
88
  - !ruby/object:Gem::Version
75
89
  version: '0.18'
76
90
  type: :runtime
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - ~>
94
+ - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0.18'
83
97
  description: Youtube API v3 parser
@@ -88,27 +102,30 @@ executables:
88
102
  extensions: []
89
103
  extra_rdoc_files: []
90
104
  files:
91
- - .gitignore
105
+ - ".gitignore"
92
106
  - Gemfile
93
107
  - LICENSE.txt
94
108
  - README.md
109
+ - README.md~
95
110
  - Rakefile
96
111
  - bin/yourub
97
112
  - config/yourub.yml
98
113
  - lib/yourub.rb
99
114
  - lib/yourub/cli.rb
115
+ - lib/yourub/client.rb
100
116
  - lib/yourub/config.rb
101
117
  - lib/yourub/count_filter.rb
102
118
  - lib/yourub/default.rb
103
119
  - lib/yourub/logger.rb
104
120
  - lib/yourub/railtie.rb
105
- - lib/yourub/search.rb
121
+ - lib/yourub/reader.rb
122
+ - lib/yourub/validator.rb
106
123
  - lib/yourub/version.rb
124
+ - spec/client_spec.rb
107
125
  - spec/count_filter_spec.rb
108
- - spec/search_spec.rb
126
+ - spec/validator_spec.rb
109
127
  - yourub.gemspec
110
128
  - yourub.sublime-project
111
- - yourub.sublime-workspace
112
129
  homepage: ''
113
130
  licenses:
114
131
  - MIT
@@ -119,21 +136,22 @@ require_paths:
119
136
  - lib
120
137
  required_ruby_version: !ruby/object:Gem::Requirement
121
138
  requirements:
122
- - - '>='
139
+ - - ">="
123
140
  - !ruby/object:Gem::Version
124
141
  version: '0'
125
142
  required_rubygems_version: !ruby/object:Gem::Requirement
126
143
  requirements:
127
- - - '>='
144
+ - - ">="
128
145
  - !ruby/object:Gem::Version
129
146
  version: '0'
130
147
  requirements: []
131
148
  rubyforge_project:
132
- rubygems_version: 2.0.3
149
+ rubygems_version: 2.2.0.rc.1
133
150
  signing_key:
134
151
  specification_version: 4
135
152
  summary: Yourub is a gem that fetch the most recent videos from the Youtube API for
136
- the given nation, category and number of views
153
+ the given criteria
137
154
  test_files:
155
+ - spec/client_spec.rb
138
156
  - spec/count_filter_spec.rb
139
- - spec/search_spec.rb
157
+ - spec/validator_spec.rb
data/lib/yourub/search.rb DELETED
@@ -1,178 +0,0 @@
1
- module Yourub
2
- class Search
3
-
4
- attr_reader :videos, :nations, :categories
5
- attr_accessor :config
6
-
7
- def initialize(nation: "US", category: 'all', max_results: 2, video_id: nil, filter: nil)
8
- local_variables.each do |k|
9
- v = eval(k.to_s)
10
- instance_variable_set("@#{k}", v) unless v.nil?
11
- end
12
-
13
- valid?
14
- @extended_video_info = false
15
- @categories, @videos = [], []
16
- @options = default_search_video_options
17
-
18
- starting_search
19
- end
20
-
21
- def config
22
- Yourub::Config
23
- end
24
-
25
- def starting_search
26
- unless @video_id.nil?
27
- @extended_video_info = true
28
- return video_info_request @video_id
29
- end
30
- retrieve_categories
31
- retrieve_videos
32
- end
33
-
34
- NATIONS = [
35
- 'AR','AU','AT','BE','BR','CA','CL','CO','CZ','EG','FR','DE','GB','HK',
36
- 'HU','IN','IE','IL','IT','JP','JO','MY','MX','MA','NL','NZ','PE','PH',
37
- 'PL','RU','SA','SG','ZA','KR','ES','SE','CH','TW','AE','US']
38
-
39
- def valid?
40
- validate_nation
41
- validate_max_results
42
- end
43
-
44
- def validate_nation
45
- raise ArgumentError.new('params not available') unless(
46
- NATIONS.include?(@nation)
47
- )
48
- end
49
-
50
- def validate_max_results
51
- raise ArgumentError.new('max 50 videos pro categories or nation') unless(
52
- @max_results.to_i < 50 || @max_results.to_i == 0
53
- )
54
- end
55
-
56
- def client
57
- @client ||= Google::APIClient.new(
58
- :key => config.developer_key,
59
- :application_name => config.application_name,
60
- :application_version => config.application_version,
61
- :authorization => nil,
62
- )
63
- end
64
-
65
- def youtube
66
- @youtube ||= client.discovered_api(config.youtube_api_service_name,
67
- config.youtube_api_version)
68
- end
69
-
70
- def categories_request
71
- categories_list = client.execute!(
72
- :api_method => youtube.video_categories.list,
73
- :parameters => {"part" => "snippet","regionCode" => @nation }
74
- )
75
- end
76
-
77
- def retrieve_categories
78
- retrieve_all_categories
79
- if @category != 'all'
80
- retrieve_only_one_category
81
- end
82
- end
83
-
84
- def retrieve_all_categories
85
- categories_request.data.items.each do |cat_result|
86
- @categories.push(cat_result["id"] => cat_result["snippet"]["title"])
87
- end
88
- end
89
-
90
- def retrieve_only_one_category
91
- @categories = @categories.select {|k| k.has_value?(@category)}
92
- if @categories.first.nil?
93
- raise ArgumentError.new('The category #{@category} does not exists')
94
- end
95
- end
96
-
97
- def default_search_video_options
98
- opt = {
99
- :maxResults => @max_results,
100
- :regionCode => @nation,
101
- :type => 'video',
102
- :order => 'date',
103
- :safeSearch => 'none'
104
- #:videoCategoryId => '10'
105
- }
106
- end
107
-
108
- def retrieve_videos
109
- #@nations.each do |nat|
110
- @categories.each do |cat|
111
- @options[:videoCategoryId] = cat.keys[0].to_i
112
- #@options[:regionCode] = nat
113
- @options[:part] = 'id'
114
- read_response videos_list_request
115
- end
116
- #end
117
- end
118
-
119
- def read_response(video_list)
120
- video_list.data.items.each do |video_item|
121
- video_info_request video_item.id.videoId
122
- end
123
- end
124
-
125
- def videos_list_request
126
- search_response = client.execute!(
127
- :api_method => youtube.search.list,
128
- :parameters => @options
129
- )
130
- end
131
-
132
- def video_info_request(result_video_id)
133
- params = video_params(result_video_id)
134
- video_response = client.execute!(
135
- :api_method => youtube.videos.list,
136
- :parameters => params
137
- )
138
- store_video(result_video_id, video_response)
139
- end
140
-
141
- def video_params(result_video_id)
142
- parameters = {
143
- :id => result_video_id,
144
- :part => 'snippet,statistics',
145
- }
146
- unless @extended_video_info
147
- fields = 'items(snippet(title,thumbnails),statistics(viewCount))'
148
- parameters[:fields] = URI::encode(fields)
149
- end
150
- return parameters
151
- end
152
-
153
- def add_video_to_search_result(entry, video_id)
154
- if @extended_video_info
155
- founded_video = entry
156
- else
157
- founded_video = {
158
- 'title' => entry['snippet']['title'],
159
- 'url' => 'https://www.youtube.com/watch?v='<< video_id
160
- }
161
- end
162
- @videos.push(founded_video)
163
- end
164
-
165
- def store_video(video_id, video_response)
166
- begin
167
- result = JSON.parse(video_response.data.to_json)
168
- entry = result['items'].first
169
- if Yourub::CountFilter.accept?(entry, @filter)
170
- add_video_to_search_result(entry, video_id)
171
- end
172
- rescue StandardError => e
173
- Yourub.logger.error "Error #{e} reading the video stream"
174
- end
175
- end
176
-
177
- end
178
- end
data/spec/search_spec.rb DELETED
@@ -1,52 +0,0 @@
1
- require 'yourub'
2
-
3
- describe Yourub::Search do
4
-
5
- context "on initialize" do
6
- # it "retrieves all the available categories for the default country" do
7
- # result = Yourub::Search.new()
8
- # expect(result.categories).to be_a_kind_of(Array)
9
- # end
10
-
11
- # it "retrieves all the available categories for a given country" do
12
- # result = Yourub::Search.new(nation: "US")
13
- # expect(result.categories).to be_a_kind_of(Array)
14
- # end
15
-
16
- it "return an error if the given country does not exists" do
17
- expect{ Yourub::Search.new(nation: "MOON") }.to raise_error(ArgumentError)
18
- end
19
-
20
- it "return an error if the given category does not exists" do
21
- expect{ Yourub::Search.new(nation: "US", category: "Puzzles") }.to raise_error
22
- end
23
-
24
- it "initialize the instance variabe @category with the given category" do
25
- result = Yourub::Search.new(nation: "US", category: "Sports")
26
- expect(result.categories.first.values).to include("Sports")
27
- expect(result.categories.first.key("Sports").to_i).to be_a_kind_of(Integer)
28
- end
29
-
30
- it "retrieves 2 videos pro category if no max_results is specified" do
31
- result = Yourub::Search.new(nation: "US", category: "Sports")
32
- expect(result.videos.count).to eq(2)
33
- end
34
-
35
- it "retrieves 5 videos pro category max_results = 5" do
36
- result = Yourub::Search.new(nation: "US", category: "Sports", max_results: 5)
37
- expect(result.videos.count).to eq(5)
38
- end
39
-
40
- it "retrieves videos that have more than 100 views" do
41
- filter = {views: ">= 100"}
42
- result = Yourub::Search.new(nation: "US", category: "Sports", max_results: 5, filter: filter)
43
- expect(result.videos).to be_a_kind_of(Array)
44
- end
45
-
46
- it "retrieves a video for the given id" do
47
- result = Yourub::Search.new(video_id: "mN0Dbj-xHY0")
48
- expect(result.videos.count).to eq(1)
49
- end
50
- end
51
-
52
- end