Skip to content

Commit

Permalink
Merge branch 'track-500-errors-separately'
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiob committed May 23, 2014
2 parents 41abb57 + fd0e1b7 commit 98d5dc0
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
yt (0.5.5)
yt (0.5.6)
activesupport

GEM
Expand Down
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -10,6 +10,7 @@ v0.5 - 2014/05/16
* Move models in Yt::Models but still auto-include them in the main namespace
* New Authentication model to separate `access_token` and `refresh_token` from Account
* New types of Errors that render more verbose errors and the failing request in cURL syntax
* Separate Error class for 500 error, so they can be easily found in app logs

v0.4 - 2014/05/09
--------------------
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -300,7 +300,7 @@ To install on your system, run

To use inside a bundled Ruby project, add this line to the Gemfile:

gem 'yt', '~> 0.5.5'
gem 'yt', '~> 0.5.6'

Since the gem follows [Semantic Versioning](http://semver.org),
indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
Expand Down
13 changes: 13 additions & 0 deletions lib/yt/errors/server_error.rb
@@ -0,0 +1,13 @@
require 'yt/errors/request_error'

module Yt
module Errors
class ServerError < RequestError
private

def explanation
'A request to YouTube API caused an unexpected server error'
end
end
end
end
5 changes: 4 additions & 1 deletion lib/yt/models/request.rb
Expand Up @@ -6,6 +6,7 @@
require 'yt/config'
require 'yt/errors/missing_auth'
require 'yt/errors/request_error'
require 'yt/errors/server_error'

module Yt
module Models
Expand All @@ -27,10 +28,12 @@ def run
case response
when @expected_response
response.tap{|response| response.body = parse_format response.body}
when Net::HTTPServerError
raise Errors::ServerError, request_error_message
when Net::HTTPUnauthorized
raise Errors::MissingAuth, request_error_message
else
raise Yt::Error, request_error_message
raise Errors::RequestError, request_error_message
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/yt/version.rb
@@ -1,3 +1,3 @@
module Yt
VERSION = '0.5.5'
VERSION = '0.5.6'
end
16 changes: 13 additions & 3 deletions spec/associations/device_auth/subscriptions_spec.rb
@@ -1,11 +1,11 @@
require 'spec_helper'
require 'yt/models/channel'

# NOTE: This test is slow because we *must* wait for some seconds between
# subscribing and unsubscribing to a channel, otherwise YouTube will show
# wrong (cached) data, such as a user is subscribed when he is not.
describe Yt::Associations::Subscriptions, :device_app do
describe '#subscription' do
# NOTE: These tests are slow because we *must* wait some seconds between
# subscribing and unsubscribing to a channel, otherwise YouTube will show
# wrong (cached) data, such as a user is subscribed when he is not.
context 'given an existing channel' do
let(:channel) { Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow', auth: $account }

Expand All @@ -21,5 +21,15 @@
it { expect(channel.unsubscribe!).to be_true }
end
end

# NOTE: This test is just a reflection of YouTube irrational behavior of
# raising a 500 error when you try to subscribe to your own channel, rather
# than a more logical 4xx error. Hopefully this will get fixed and this
# test removed.
context 'given my own channel' do
let(:channel) { Yt::Channel.new id: $account.channel.id, auth: $account }

it { expect{channel.subscribe}.to raise_error Yt::Errors::ServerError }
end
end
end
20 changes: 14 additions & 6 deletions spec/models/request_spec.rb
Expand Up @@ -4,19 +4,27 @@
describe Yt::Request do
subject(:request) { Yt::Request.new attrs }
let(:attrs) { {host: 'example.com'} }
before { Net::HTTP.stub(:start).and_return response }
before { response.stub(:body) }

describe '#run' do
context 'given a request that returns a 500 code' do
let(:response) { Net::HTTPServerError.new nil, nil, nil }
it { expect{request.run}.to fail }
end

context 'given a request that returns a 401 code' do
let(:response) { Net::HTTPUnauthorized.new nil, nil, nil }
it { expect{request.run}.to fail }
end

context 'given a request that returns a non-2XX code' do
let(:not_found) { Net::HTTPNotFound.new nil, nil, nil }
before { Net::HTTP.stub(:start).and_return not_found }
before { not_found.stub(:body) }
let(:response) { Net::HTTPNotFound.new nil, nil, nil }
it { expect{request.run}.to fail }
end

context 'given a request that returns a 2XX code' do
let(:ok) { Net::HTTPOK.new nil, nil, nil }
before { Net::HTTP.stub(:start).and_return ok }
before { ok.stub(:body) }
let(:response) { Net::HTTPOK.new nil, nil, nil }
it { expect{request.run}.not_to fail }
end
end
Expand Down

0 comments on commit 98d5dc0

Please sign in to comment.