lib/githu3/client.rb in githu3-0.0.3 vs lib/githu3/client.rb in githu3-0.0.4
- old
+ new
@@ -1,50 +1,48 @@
require 'faraday'
-require 'faraday_middleware'
require 'faraday/response/raise_githu3_error'
+require 'faraday/response/parse_json'
require 'uri'
+require 'forwardable'
require 'active_support/core_ext/hash'
module Githu3
class Client
+ extend Forwardable
+
BaseUrl = "https://api.github.com"
- attr_reader :conn, :rate_limit
+ def_delegator :conn, :rate_limit
+
+ attr_reader :conn, :auth
- def initialize(oauth_token=nil)
- headers = {}
- headers["Authorization"] = "token #{oauth_token}" if oauth_token
- @conn = Faraday.new({
- :url => Githu3::Client::BaseUrl,
- :headers => headers
- })
-
- @conn.use Faraday::Response::ParseJson
- @conn.use Faraday::Response::RaiseGithu3Error
-
- @rate_limit = {}
- @conn
+ def initialize(*args)
+ opts = args.extract_options!
+ @conn = Githu3::Connection.new opts do |c|
+ case args.length
+ when 1
+ @auth = :token
+ c.headers["Authorization"] = "token #{args.first}"
+ when 2
+ @auth = :basic
+ c.basic_auth(*args)
+ end
+ end
end
-
+
+ def authenticated?
+ !!@auth
+ end
+
def get *args
opts = args.extract_options!
uri = URI.parse(args.shift)
- uri_params = (opts[:params] || {}).stringify_keys
- unless uri.query.nil?
- uri_params.merge(uri.query.split("&").inject({}) { |m,p| k,v=p.split("=", 2); m.merge(k => v) } )
- end
- args.unshift uri.path
- res = @conn.get(*args) do |req|
- req.params = uri_params
- if opts[:params].is_a?(Hash)
- opts[:params].each { |k,v| req.params[k.to_s] = v.to_s }
- end
- end
- @rate_limit[:limit] = res.headers["x-ratelimit-limit"]
- @rate_limit[:remaining] = res.headers["x-ratelimit-remaining"]
+ uri.query = (opts[:params] || {}).stringify_keys.merge(Hash.from_url_params(uri.query || "")).to_url_params
+
+ res = @conn.get(uri, opts[:headers])
res
end
# Top level resources...
%w{ org repo team user }.each do |r|
@@ -54,35 +52,53 @@
end
# My stuf...
def me
+ require_auth
Githu3::User.new "/user", self
end
def orgs params={}
+ require_auth
Githu3::ResourceCollection.new(self, Githu3::Org, "/user/orgs", params)
end
def repos params={}
+ require_auth
Githu3::ResourceCollection.new(self, Githu3::Repo, "/user/repos", params)
end
+ def teams
+ require_auth
+ Githu3::ResourceCollection.new(self, Githu3::Team, "/user/teams")
+ end
+
def followers params={}
+ require_auth
Githu3::ResourceCollection.new(self, Githu3::User, "/user/followers", params)
end
def following params={}
+ require_auth
Githu3::ResourceCollection.new(self, Githu3::User, "/user/following", params)
end
def following?(other_user)
+ require_auth
begin
get("/user/following/#{other_user}").status == 204
rescue Githu3::NotFound
false
end
end
-
+
+ protected
+
+ def require_auth
+ raise Githu3::Unauthorized, "You must me logged in to use this method" unless authenticated?
+ yield if block_given?
+ end
+
end
end
\ No newline at end of file