Skip to content

Commit

Permalink
adding faraday support
Browse files Browse the repository at this point in the history
  • Loading branch information
chebyte committed Jul 1, 2011
1 parent 0030eb6 commit 743e72f
Show file tree
Hide file tree
Showing 17 changed files with 351 additions and 556 deletions.
4 changes: 4 additions & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ lib/youtube_it/model/video.rb
lib/youtube_it/parser.rb
lib/youtube_it/record.rb
lib/youtube_it/request/base_search.rb
lib/youtube_it/request/error.rb
lib/youtube_it/request/standard_search.rb
lib/youtube_it/request/user_search.rb
lib/youtube_it/request/video_search.rb
lib/youtube_it/request/video_upload.rb
lib/youtube_it/response/video_search.rb
lib/youtube_it/middleware/faraday_authheader.rb
lib/youtube_it/middleware/faraday_oauth.rb
lib/youtube_it/middleware/faraday_youtubeit.rb
lib/youtube_it/version.rb
test/helper.rb
test/test_chain_io.rb
Expand Down
7 changes: 2 additions & 5 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Add A Comment:
$ client.add_comment(video_id, "test comment!")

List Favorites:
$ client.favorites
$ client.favorites(user) # current user by default

Add Favorite:
$ client.add_favorite(video_id)
Expand All @@ -121,7 +121,7 @@ Unsubscribe To A Channel:
$ client.unsubscribe_channel(subscription_id)

List Playlists:
$ client.playlists
$ client.playlists(user) # current user by default

Select Playlist:
$ client.playlist(playlist_id)
Expand All @@ -141,9 +141,6 @@ Add Video To Playlist:

Remove Video From Playlist:
$ client.remove_video_from_playlist(playlist_id, playlist_entry_id)

Select Playlists From User:
$ client.playlists_for(user)

== ACCESS CONTROL LIST

Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ begin
gem.email = "kylejginavan@gmail.com"
gem.homepage = "http://github.com/kylejginavan/youtube_it"
gem.add_dependency('oauth','>=0.4.4')
gem.add_dependency('simple_oauth', '>=0.1.5')
gem.add_dependency('faraday','>=0.7.3')
gem.add_dependency('builder')
gem.authors = ["kylejginavan","chebyte", "mseppae"]
end
Expand Down
5 changes: 5 additions & 0 deletions lib/youtube_it.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'rexml/document'
require 'builder'
require 'oauth'
require 'faraday'

class YouTubeIt

Expand Down Expand Up @@ -61,10 +62,14 @@ def self.create_default_logger
model/user
model/video
request/base_search
request/error
request/user_search
request/standard_search
request/video_upload
request/video_search
response/video_search
middleware/faraday_authheader.rb
middleware/faraday_oauth.rb
middleware/faraday_youtubeit.rb
chain_io
).each{|m| require File.dirname(__FILE__) + '/youtube_it/' + m }
27 changes: 16 additions & 11 deletions lib/youtube_it/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,20 @@ def delete_favorite(video_id)
client.delete_favorite(video_id)
end

def favorites(opts = {})
client.favorites(opts)
def favorites(user = nil, opts = {})
client.favorites(user, opts)
end

def profile(user_id = nil)
client.profile(user_id)
def profile(user = nil)
client.profile(user)
end

def playlist(playlist_id)
client.playlist playlist_id
end

def playlists
client.playlists
end

def playlists_for(user)
client.playlists_for(user)
def playlists(user = nil)
client.playlists(user)
end

def add_playlist(options)
Expand Down Expand Up @@ -329,6 +325,15 @@ def access_token
@access_token = ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
end

def config_token
{
:consumer_key => @consumer_key,
:consumer_secret => @consumer_secret,
:token => @atoken,
:token_secret => @asecret
}
end

def authorize_from_request(rtoken,rsecret,verifier)
request_token = ::OAuth::RequestToken.new(consumer,rtoken,rsecret)
access_token = request_token.get_access_token({:oauth_verifier => verifier})
Expand All @@ -348,7 +353,7 @@ def current_user

def client
# IMPORTANT: make sure authorize_from_access is called before client is fetched
@client ||= YouTubeIt::Upload::VideoUpload.new(:username => current_user, :dev_key => @dev_key, :access_token => access_token)
@client ||= YouTubeIt::Upload::VideoUpload.new(:username => current_user, :dev_key => @dev_key, :access_token => access_token, :config_token => config_token)
end

end
Expand Down
13 changes: 13 additions & 0 deletions lib/youtube_it/middleware/faraday_authheader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Faraday
class Request::AuthHeader < Faraday::Middleware

def call(env)
env[:request_headers].merge!(@headers)
@app.call(env)
end

def initialize(app, headers)
@app, @headers = app, headers
end
end
end
21 changes: 21 additions & 0 deletions lib/youtube_it/middleware/faraday_oauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Faraday
class Request::OAuth < Faraday::Middleware
dependency 'simple_oauth'

def call(env)
params = env[:body].is_a?(Hash) ? env[:body] : {}

signature_params = params.reject{ |k,v| v.respond_to?(:content_type) }

header = SimpleOAuth::Header.new(env[:method], env[:url], signature_params, @options)

env[:request_headers]['Authorization'] = header.to_s

@app.call(env)
end

def initialize(app, options)
@app, @options = app, options
end
end
end
30 changes: 30 additions & 0 deletions lib/youtube_it/middleware/faraday_youtubeit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Faraday
class Response::YouTubeIt < Response::Middleware
def parse_upload_error_from(string)
begin
REXML::Document.new(string).elements["//errors"].inject('') do | all_faults, error|
if error.elements["internalReason"]
msg_error = error.elements["internalReason"].text
elsif error.elements["location"]
msg_error = error.elements["location"].text[/media:group\/media:(.*)\/text\(\)/,1]
else
msg_error = "Unspecified error"
end
code = error.elements["code"].text if error.elements["code"]
all_faults + sprintf("%s: %s\n", msg_error, code)
end
rescue
string[/<TITLE>(.+)<\/TITLE>/, 1] || string
end
end

def on_complete(env) #this method is called after finish request
msg = parse_upload_error_from(env[:body].gsub(/\n/, ''))
if env[:status] == 403 || env[:status] == 401
raise ::AuthenticationError.new(msg, env[:status])
elsif env[:status] / 10 != 20
raise ::UploadError.new(msg, env[:status])
end
end
end
end
Loading

0 comments on commit 743e72f

Please sign in to comment.