youtube_pop 0.0.3 → 0.0.4
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.
- data/.rspec +1 -0
- data/lib/youtube_pop/standard_api.rb +47 -53
- data/lib/youtube_pop/version.rb +1 -1
- data/spec/youtube_pop_spec.rb +10 -0
- metadata +3 -2
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
@@ -1,26 +1,19 @@
|
|
1
1
|
require 'youtube_pop/version'
|
2
|
+
require 'ostruct'
|
2
3
|
require 'faraday'
|
3
|
-
require '
|
4
|
-
|
4
|
+
require 'json'
|
5
5
|
module YoutubePop
|
6
|
-
|
6
|
+
|
7
7
|
class Error < RuntimeError
|
8
|
-
attr_reader :
|
9
|
-
def initialize(msg,
|
8
|
+
attr_reader :source_exception
|
9
|
+
def initialize(msg, e=$!)
|
10
10
|
super(msg)
|
11
|
-
@
|
11
|
+
@source_exception = e
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
Entry = Class.new(OpenStruct)
|
14
16
|
|
15
|
-
class Entry
|
16
|
-
attr_accessor :published, :updated, :content, :link, :author_name
|
17
|
-
end
|
18
|
-
|
19
|
-
# Searches for the following from the response:
|
20
|
-
#
|
21
|
-
# feed/entry/published, feed/entry/updated
|
22
|
-
# feed/entry/content, feed/entry/link rel="alternate" href=
|
23
|
-
# feed/entry/author/name
|
24
17
|
class StandardApi
|
25
18
|
def initialize
|
26
19
|
options = {
|
@@ -29,82 +22,83 @@ module YoutubePop
|
|
29
22
|
}
|
30
23
|
@conn = Faraday.new(:url => 'https://gdata.youtube.com', :options => options) do |builder|
|
31
24
|
builder.request :url_encoded
|
32
|
-
builder.response :logger
|
33
25
|
builder.adapter :net_http
|
34
26
|
end
|
35
27
|
end
|
36
28
|
|
37
29
|
def top_rated
|
38
|
-
response =
|
39
|
-
|
30
|
+
response = response_from url_for("top_rated")
|
31
|
+
get_entries(response)
|
40
32
|
end
|
41
33
|
|
42
34
|
def top_favorites
|
43
|
-
response =
|
44
|
-
|
35
|
+
response = response_from url_for("top_favorites")
|
36
|
+
get_entries(response)
|
45
37
|
end
|
46
38
|
|
47
39
|
def most_shared
|
48
|
-
response =
|
49
|
-
|
40
|
+
response = response_from url_for("most_shared")
|
41
|
+
get_entries(response)
|
50
42
|
end
|
51
43
|
|
52
44
|
def most_viewed
|
53
|
-
response =
|
54
|
-
|
45
|
+
response = response_from url_for("most_viewed")
|
46
|
+
get_entries(response)
|
55
47
|
end
|
56
48
|
|
57
49
|
def most_popular
|
58
|
-
response =
|
59
|
-
|
50
|
+
response = response_from url_for("most_popular")
|
51
|
+
get_entries(response)
|
60
52
|
end
|
61
53
|
|
62
54
|
def most_recent
|
63
|
-
response =
|
64
|
-
|
55
|
+
response = response_from url_for("most_recent")
|
56
|
+
get_entries(response)
|
65
57
|
end
|
66
58
|
|
67
59
|
def most_discussed
|
68
|
-
response =
|
69
|
-
|
60
|
+
response = response_from url_for("most_discussed")
|
61
|
+
get_entries(response)
|
70
62
|
end
|
71
63
|
|
72
64
|
def most_responded
|
73
|
-
response =
|
74
|
-
|
65
|
+
response = response_from url_for("most_responded")
|
66
|
+
get_entries(response)
|
75
67
|
end
|
76
68
|
|
77
69
|
def recently_featured
|
78
|
-
response =
|
79
|
-
|
70
|
+
response = response_from url_for("recently_featured")
|
71
|
+
get_entries(response)
|
80
72
|
end
|
81
73
|
|
82
74
|
def trending_videos
|
83
|
-
response =
|
84
|
-
|
75
|
+
response = response_from url_for("on_the_web")
|
76
|
+
get_entries(response)
|
85
77
|
end
|
86
78
|
|
87
79
|
private
|
88
80
|
|
81
|
+
def response_from(url)
|
82
|
+
@conn.get url
|
83
|
+
rescue Exception => e
|
84
|
+
raise Error, "Problem retrieving data from #{url}"
|
85
|
+
end
|
86
|
+
|
87
|
+
def url_for(path)
|
88
|
+
"/feeds/api/standardfeeds/#{path}?alt=json"
|
89
|
+
end
|
90
|
+
|
89
91
|
def get_entries(response)
|
90
|
-
|
91
|
-
doc
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
entry = Entry.new
|
100
|
-
entry.published = published
|
101
|
-
entry.updated = updated
|
102
|
-
entry.content = content
|
103
|
-
entry.link = link
|
104
|
-
entry.author_name = author_name
|
105
|
-
entries << entry
|
92
|
+
doc = JSON.parse(response.body)
|
93
|
+
doc['feed']['entry'].map do |entry|
|
94
|
+
Entry.new(
|
95
|
+
published: entry['published']['$t'],
|
96
|
+
updated: entry['updated']['$t'],
|
97
|
+
content: entry['content'],
|
98
|
+
link: entry['link'][0]['href'],
|
99
|
+
author_name: entry['author']
|
100
|
+
)
|
106
101
|
end
|
107
|
-
entries
|
108
102
|
end
|
109
103
|
end
|
110
104
|
end
|
data/lib/youtube_pop/version.rb
CHANGED
data/spec/youtube_pop_spec.rb
CHANGED
@@ -4,6 +4,16 @@ describe YoutubePop::StandardApi do
|
|
4
4
|
before do
|
5
5
|
@youtube_pop = YoutubePop::StandardApi.new
|
6
6
|
end
|
7
|
+
|
8
|
+
describe "error handling" do
|
9
|
+
it "raises gem specific errors" do
|
10
|
+
@error_pop = YoutubePop::StandardApi.new
|
11
|
+
@error_pop.instance_variable_set(:@conn, nil)
|
12
|
+
expect {
|
13
|
+
@error_pop.top_rated
|
14
|
+
}.to raise_exception(YoutubePop::Error)
|
15
|
+
end
|
16
|
+
end
|
7
17
|
|
8
18
|
describe '#top_rated' do
|
9
19
|
before do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: youtube_pop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -67,6 +67,7 @@ extensions: []
|
|
67
67
|
extra_rdoc_files: []
|
68
68
|
files:
|
69
69
|
- .gitignore
|
70
|
+
- .rspec
|
70
71
|
- Gemfile
|
71
72
|
- LICENSE
|
72
73
|
- README.md
|