yt 0.5.10 → 0.5.11

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7f7f8e3686d58c93674547e4aac01644a01d0db
4
- data.tar.gz: f3739308a1230e14548efe5d68abeb76e66c4422
3
+ metadata.gz: 72f7e8f2bdbc10f6d4ee9e400032c623f9dcaf9c
4
+ data.tar.gz: 3f2eaf3a29cf656f4bf127ad92d4661603effa93
5
5
  SHA512:
6
- metadata.gz: 1fb1f949b74acab51e649a863733453dadbce615f5aef848f28ae1c7a25c274cf582d53ac5d3e1d3c7988758cf4561b65d10751607c5cf3dedab43c28dd04e7b
7
- data.tar.gz: 774e99b48115627b617f5bfbbc57cd66d039db2fefe5b32f83e73ebcb80d1a9874586efcc961114ba758c1cf59ffb42bfc5db00ddaa4c4b3dad48d3d11836b3e
6
+ metadata.gz: 66571f1ace7215bc148992f19758a974f1ff46cb832c7c2312e76ed4d2bafa41241fd355e09803b0e520fc8c38e7b4d91d3baab53ba81b421d5130751d0b8aba
7
+ data.tar.gz: 0d5d8ba68f7350f7d9d744d3202e616cb3bc64e322185c729b24de96bce99a6a44c9cc00b7fb843c00da41b400ccb7d505b8aa0511342bebbc63dcca73e44549
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yt (0.5.10)
4
+ yt (0.5.11)
5
5
  activesupport
6
6
 
7
7
  GEM
data/HISTORY.md CHANGED
@@ -17,6 +17,7 @@ v0.5 - 2014/05/16
17
17
  * Retry once YouTube earning queries that return error 400 "Invalid query. Query did not conform to the expectations"
18
18
  * Update RSpec to 3.0 (only required in development/testing)
19
19
  * New ContentOwner subclass of Account with access to partnered channels
20
+ * Automatically refresh the access token when it expires or becomes invalid
20
21
 
21
22
  v0.4 - 2014/05/09
22
23
  --------------------
data/README.md CHANGED
@@ -34,7 +34,7 @@ Available resources
34
34
  Yt::Account
35
35
  -----------
36
36
 
37
- Use [Yt::Account](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Account) to:
37
+ Use [Yt::Account](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Models/Account) to:
38
38
 
39
39
  * authenticate as a YouTube account
40
40
  * read attributes of the account
@@ -50,10 +50,28 @@ account.channel #=> #<Yt::Channel @id=...>
50
50
 
51
51
  *All the above methods require authentication (see below).*
52
52
 
53
+ Yt::ContentOwner
54
+ ----------------
55
+
56
+ Use [Yt::ContentOwner](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Models/ContentOwner) to:
57
+
58
+ * authenticate as a YouTube content owner
59
+ * list the channels partnered with a YouTube content owner
60
+
61
+ ```ruby
62
+ # A content owner is an account, therefore can be initialized with access token, refresh token or an authorization code
63
+ content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
64
+
65
+ content_owner.partnered_channels.count #=> 12
66
+ content_owner.partnered_channels.first #=> #<Yt::Channel @id=...>
67
+ ```
68
+
69
+ *All the above methods require authentication (see below).*
70
+
53
71
  Yt::Channel
54
72
  -----------
55
73
 
56
- Use [Yt::Channel](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Channel) to:
74
+ Use [Yt::Channel](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Models/Channel) to:
57
75
 
58
76
  * read attributes of a channel
59
77
  * access the videos of a channel
@@ -91,7 +109,7 @@ channel.delete_playlists title: 'New playlist' #=> [true]
91
109
  *The methods above require to be authenticated as a YouTube account (see below).*
92
110
 
93
111
  ```ruby
94
- content_owner = Yt::Account.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
112
+ content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
95
113
  channel = Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow', auth: content_owner
96
114
 
97
115
  channel.earning 5.days.ago #=> 12.23
@@ -104,7 +122,7 @@ channel.earnings since: 3.days.ago, until: 2.days.ago #=> {Wed, 28 May 2014 => 1
104
122
  Yt::Video
105
123
  -----------
106
124
 
107
- Use [Yt::Video](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Video) to:
125
+ Use [Yt::Video](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Models/Video) to:
108
126
 
109
127
  * read attributes of a video
110
128
  * access the annotations of a video
@@ -135,7 +153,7 @@ video.like #=> true
135
153
  Yt::Playlist
136
154
  ------------
137
155
 
138
- Use [Yt::Playlist](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Playlist) to:
156
+ Use [Yt::Playlist](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Models/Playlist) to:
139
157
 
140
158
  * read attributes of a playlist
141
159
  * access the items of a playlist
@@ -167,7 +185,7 @@ playlist.delete_playlist_items title: 'Fullscreen Creator Platform' #=> [true]
167
185
  Yt::Annotation
168
186
  --------------
169
187
 
170
- Use [Yt::Annotation](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Annotation) to:
188
+ Use [Yt::Annotation](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Models/Annotation) to:
171
189
 
172
190
  * read attributes of an annotation
173
191
 
@@ -347,7 +365,7 @@ To install on your system, run
347
365
 
348
366
  To use inside a bundled Ruby project, add this line to the Gemfile:
349
367
 
350
- gem 'yt', '~> 0.5.10'
368
+ gem 'yt', '~> 0.5.11'
351
369
 
352
370
  Since the gem follows [Semantic Versioning](http://semver.org),
353
371
  indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
@@ -21,6 +21,15 @@ module Yt
21
21
  URI::HTTPS.build(host: host, path: path, query: query).to_s
22
22
  end
23
23
 
24
+ # Obtains a new access token.
25
+ # Returns true if the new access token is different from the previous one
26
+ def refresh
27
+ old_access_token = authentication.access_token
28
+ authentication.expire
29
+ new_access_token = authentication.access_token
30
+ old_access_token != new_access_token
31
+ end
32
+
24
33
  private
25
34
 
26
35
  def current_authentication
@@ -13,6 +13,10 @@ module Yt
13
13
  @expires_at && @expires_at.past?
14
14
  end
15
15
 
16
+ def expire
17
+ @expires_at = 10.years.ago
18
+ end
19
+
16
20
  private
17
21
 
18
22
  def expiration_date(options = {})
@@ -28,6 +28,8 @@ module Yt
28
28
  def run
29
29
  if response.is_a? @expected_response
30
30
  response.tap{|response| response.body = parse_format response.body}
31
+ elsif run_again_with_refreshed_authentication?
32
+ run
31
33
  else
32
34
  raise error_for(response), request_error_message
33
35
  end
@@ -35,6 +37,16 @@ module Yt
35
37
 
36
38
  private
37
39
 
40
+ # If a request authorized with an access token returns 401, then the
41
+ # access token might have expired. If a refresh token is also present,
42
+ # try to run the request one more time with a refreshed access token.
43
+ def run_again_with_refreshed_authentication?
44
+ if response.is_a? Net::HTTPUnauthorized
45
+ @response = @http_request = @uri = nil
46
+ @auth.refresh
47
+ end if @auth.respond_to? :refresh
48
+ end
49
+
38
50
  def response
39
51
  @response ||= Net::HTTP.start(*net_http_options) do |http|
40
52
  http.request http_request
data/lib/yt/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yt
2
- VERSION = '0.5.10'
2
+ VERSION = '0.5.11'
3
3
  end
@@ -50,17 +50,31 @@ describe Yt::Associations::Authentications, :device_app do
50
50
 
51
51
  context 'that has expired' do
52
52
  let(:expires_at) { 1.day.ago.to_s }
53
- it { expect{account.authentication}.to raise_error Yt::Errors::Unauthorized }
53
+
54
+ context 'and no valid refresh token' do
55
+ it { expect{account.authentication}.to raise_error Yt::Errors::Unauthorized }
56
+ end
57
+
58
+ context 'and a valid refresh token' do
59
+ before { attrs[:refresh_token] = ENV['YT_TEST_DEVICE_REFRESH_TOKEN'] }
60
+ it { expect(account.authentication).to be_a Yt::Authentication }
61
+ end
54
62
  end
55
63
  end
56
64
 
57
65
  context 'that is invalid' do
58
66
  let(:access_token) { '--not-a-valid-access-token--' }
59
67
  let(:expires_at) { 1.day.from_now }
60
- it { expect(account.authentication).to be_a Yt::Authentication }
61
- it { expect{account.channel}.to raise_error Yt::Errors::Unauthorized }
62
- end
63
68
 
69
+ context 'and no valid refresh token' do
70
+ it { expect{account.channel}.to raise_error Yt::Errors::Unauthorized }
71
+ end
72
+
73
+ context 'and a valid refresh token' do
74
+ before { attrs[:refresh_token] = ENV['YT_TEST_DEVICE_REFRESH_TOKEN'] }
75
+ it { expect{account.channel}.not_to raise_error }
76
+ end
77
+ end
64
78
  end
65
79
 
66
80
  context 'given no token or code' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.10
4
+ version: 0.5.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Baccigalupo