Skip to content

Commit

Permalink
More restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 15, 2011
1 parent 6ddb179 commit 2f8abf6
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 86 deletions.
21 changes: 21 additions & 0 deletions 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
8 changes: 4 additions & 4 deletions 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'
Expand All @@ -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
Expand Down
23 changes: 0 additions & 23 deletions lib/octokit/client/authentication.rb

This file was deleted.

58 changes: 28 additions & 30 deletions lib/octokit/connection.rb
Expand Up @@ -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
55 changes: 26 additions & 29 deletions 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

0 comments on commit 2f8abf6

Please sign in to comment.