diff --git a/lib/octokit/authentication.rb b/lib/octokit/authentication.rb new file mode 100644 index 000000000..3d9d933ad --- /dev/null +++ b/lib/octokit/authentication.rb @@ -0,0 +1,21 @@ +module Octokit + module Authentication + def authentication + if login && token + {:login => "#{login}/token", :password => token} + elsif login && password + {:login => login, :password => password} + else + {} + end + end + + def authenticated? + !authentication.empty? + end + + def oauthed? + !oauth_token.nil? + end + end +end diff --git a/lib/octokit/client.rb b/lib/octokit/client.rb index bcb7b141c..11b86ee94 100644 --- a/lib/octokit/client.rb +++ b/lib/octokit/client.rb @@ -1,8 +1,8 @@ +require 'octokit/authentication' require 'octokit/connection' require 'octokit/repository' require 'octokit/request' -require 'octokit/client/authentication' require 'octokit/client/commits' require 'octokit/client/issues' require 'octokit/client/network' @@ -27,9 +27,9 @@ def initialize(options={}) end end - include Octokit::Client::Authentication - include Octokit::Client::Connection - include Octokit::Client::Request + include Octokit::Authentication + include Octokit::Connection + include Octokit::Request include Octokit::Client::Commits include Octokit::Client::Issues diff --git a/lib/octokit/client/authentication.rb b/lib/octokit/client/authentication.rb deleted file mode 100644 index 4d0a280c6..000000000 --- a/lib/octokit/client/authentication.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Octokit - class Client - module Authentication - def authentication - if login && token - {:login => "#{login}/token", :password => token} - elsif login && password - {:login => login, :password => password} - else - {} - end - end - - def authenticated? - !authentication.empty? - end - - def oauthed? - !oauth_token.nil? - end - end - end -end diff --git a/lib/octokit/connection.rb b/lib/octokit/connection.rb index 3004d7713..1e15f01b9 100644 --- a/lib/octokit/connection.rb +++ b/lib/octokit/connection.rb @@ -2,42 +2,40 @@ require 'faraday/response/raise_octokit_error' module Octokit - class Client - # @private - module Connection - private + # @private + module Connection + private - def connection(authenticate=true, raw=false, version=2, force_urlencoded=false) - if [1, 2].include? version - url = "https://github.com/" - elsif version >= 3 - url = "https://api.github.com/" - end + def connection(authenticate=true, raw=false, version=2, force_urlencoded=false) + if [1, 2].include? version + url = "https://github.com/" + elsif version >= 3 + url = "https://api.github.com/" + end - options = { - :proxy => proxy, - :ssl => { :verify => false }, - :url => url, - } + options = { + :proxy => proxy, + :ssl => { :verify => false }, + :url => url, + } - options.merge!(:params => {:access_token => oauth_token}) if oauthed? && !authenticated? + options.merge!(:params => {:access_token => oauth_token}) if oauthed? && !authenticated? - connection = Faraday.new(options) do |builder| - if version >= 3 && !force_urlencoded - builder.use Faraday::Request::JSON - else - builder.use Faraday::Request::UrlEncoded - end - builder.use Faraday::Response::RaiseOctokitError - unless raw - builder.use Faraday::Response::Rashify - builder.use Faraday::Response::ParseJson - end - builder.adapter(adapter) + connection = Faraday.new(options) do |builder| + if version >= 3 && !force_urlencoded + builder.use Faraday::Request::JSON + else + builder.use Faraday::Request::UrlEncoded + end + builder.use Faraday::Response::RaiseOctokitError + unless raw + builder.use Faraday::Response::Rashify + builder.use Faraday::Response::ParseJson end - connection.basic_auth authentication[:login], authentication[:password] if authenticate and authenticated? - connection + builder.adapter(adapter) end + connection.basic_auth authentication[:login], authentication[:password] if authenticate and authenticated? + connection end end end diff --git a/lib/octokit/request.rb b/lib/octokit/request.rb index 0bf6fd7b8..b9876da80 100644 --- a/lib/octokit/request.rb +++ b/lib/octokit/request.rb @@ -1,43 +1,40 @@ require 'multi_json' module Octokit - class Client - module Request - def get(path, options={}, version=api_version, authenticate=true, raw=false, force_urlencoded=false) - request(:get, path, options, version, authenticate, raw, force_urlencoded) - end + module Request + def get(path, options={}, version=api_version, authenticate=true, raw=false, force_urlencoded=false) + request(:get, path, options, version, authenticate, raw, force_urlencoded) + end - def post(path, options={}, version=api_version, authenticate=true, raw=false, force_urlencoded=false) - request(:post, path, options, version, authenticate, raw, force_urlencoded) - end + def post(path, options={}, version=api_version, authenticate=true, raw=false, force_urlencoded=false) + request(:post, path, options, version, authenticate, raw, force_urlencoded) + end - def put(path, options={}, version=api_version, authenticate=true, raw=false, force_urlencoded=false) - request(:put, path, options, version, authenticate, raw, force_urlencoded) - end + def put(path, options={}, version=api_version, authenticate=true, raw=false, force_urlencoded=false) + request(:put, path, options, version, authenticate, raw, force_urlencoded) + end - def delete(path, options={}, version=api_version, authenticate=true, raw=false, force_urlencoded=false) - request(:delete, path, options, version, authenticate, raw, force_urlencoded) - end + def delete(path, options={}, version=api_version, authenticate=true, raw=false, force_urlencoded=false) + request(:delete, path, options, version, authenticate, raw, force_urlencoded) + end - private + private - def request(method, path, options, version, authenticate, raw, force_urlencoded) - response = connection(authenticate, raw, version, force_urlencoded).send(method) do |request| - case method - when :get, :delete - request.url(path, options) - when :post, :put - request.path = path - if version >= 3 && !force_urlencoded - request.body = MultiJson.encode(options) unless options.empty? - else - request.body = options unless options.empty? - end + def request(method, path, options, version, authenticate, raw, force_urlencoded) + response = connection(authenticate, raw, version, force_urlencoded).send(method) do |request| + case method + when :get, :delete + request.url(path, options) + when :post, :put + request.path = path + if version >= 3 && !force_urlencoded + request.body = MultiJson.encode(options) unless options.empty? + else + request.body = options unless options.empty? end end - raw ? response : response.body end - + raw ? response : response.body end end end