youtube_pop 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
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 'nokogiri'
4
-
4
+ require 'json'
5
5
  module YoutubePop
6
- # Base error class for the extension. Credit to gem youtube_it
6
+
7
7
  class Error < RuntimeError
8
- attr_reader :code
9
- def initialize(msg, code = 0)
8
+ attr_reader :source_exception
9
+ def initialize(msg, e=$!)
10
10
  super(msg)
11
- @code = code
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 = @conn.get '/feeds/api/standardfeeds/top_rated/'
39
- entries = get_entries(response)
30
+ response = response_from url_for("top_rated")
31
+ get_entries(response)
40
32
  end
41
33
 
42
34
  def top_favorites
43
- response = @conn.get '/feeds/api/standardfeeds/top_favorites'
44
- entries = get_entries(response)
35
+ response = response_from url_for("top_favorites")
36
+ get_entries(response)
45
37
  end
46
38
 
47
39
  def most_shared
48
- response = @conn.get '/feeds/api/standardfeeds/most_shared'
49
- entries = get_entries(response)
40
+ response = response_from url_for("most_shared")
41
+ get_entries(response)
50
42
  end
51
43
 
52
44
  def most_viewed
53
- response = @conn.get '/feeds/api/standardfeeds/most_viewed'
54
- entries = get_entries(response)
45
+ response = response_from url_for("most_viewed")
46
+ get_entries(response)
55
47
  end
56
48
 
57
49
  def most_popular
58
- response = @conn.get '/feeds/api/standardfeeds/most_popular'
59
- entries = get_entries(response)
50
+ response = response_from url_for("most_popular")
51
+ get_entries(response)
60
52
  end
61
53
 
62
54
  def most_recent
63
- response = @conn.get '/feeds/api/standardfeeds/most_recent'
64
- entries = get_entries(response)
55
+ response = response_from url_for("most_recent")
56
+ get_entries(response)
65
57
  end
66
58
 
67
59
  def most_discussed
68
- response = @conn.get '/feeds/api/standardfeeds/most_discussed'
69
- entries = get_entries(response)
60
+ response = response_from url_for("most_discussed")
61
+ get_entries(response)
70
62
  end
71
63
 
72
64
  def most_responded
73
- response = @conn.get '/feeds/api/standardfeeds/most_responded'
74
- entries = get_entries(response)
65
+ response = response_from url_for("most_responded")
66
+ get_entries(response)
75
67
  end
76
68
 
77
69
  def recently_featured
78
- response = @conn.get '/feeds/api/standardfeeds/recently_featured'
79
- entries = get_entries(response)
70
+ response = response_from url_for("recently_featured")
71
+ get_entries(response)
80
72
  end
81
73
 
82
74
  def trending_videos
83
- response = @conn.get '/feeds/api/standardfeeds/on_the_web'
84
- entries = get_entries(response)
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
- entries = []
91
- doc = Nokogiri::XML(response.body)
92
- doc.css('entry').map do |entry|
93
- published = entry.css('published').text
94
- updated = entry.css('updated').text
95
- content = entry.css('content').text
96
- link = entry.at_css("link")['href']
97
- author_name = entry.at_css("author uri").text
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
@@ -1,3 +1,3 @@
1
1
  module YoutubePop
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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.3
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-16 00:00:00.000000000 Z
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