lib/githu3/client.rb in githu3-0.0.2 vs lib/githu3/client.rb in githu3-0.0.3

- old
+ new

@@ -1,67 +1,87 @@ +require 'faraday' +require 'faraday_middleware' +require 'faraday/response/raise_githu3_error' +require 'uri' +require 'active_support/core_ext/hash' module Githu3 class Client - + + BaseUrl = "https://api.github.com" + attr_reader :conn, :rate_limit def initialize(oauth_token=nil) headers = {} headers["Authorization"] = "token #{oauth_token}" if oauth_token @conn = Faraday.new({ - :url => "https://api.github.com/", + :url => Githu3::Client::BaseUrl, :headers => headers }) + @conn.use Faraday::Response::ParseJson + @conn.use Faraday::Response::RaiseGithu3Error + @rate_limit = {} @conn 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) - req.params ||= {} 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"] - res.body + res end # Top level resources... %w{ org repo team user }.each do |r| define_method r do |ident| - Githu3.const_get(r.camelize).new(get("/#{r.pluralize}/#{ident}"), self) + Githu3.const_get(r.camelize).new(get("/#{r.pluralize}/#{ident}").body, self) end end # My stuf... def me Githu3::User.new "/user", self end - def orgs - get("/user/orgs").map { |o| Githu3::Org.new(o, self) } + def orgs params={} + Githu3::ResourceCollection.new(self, Githu3::Org, "/user/orgs", params) end - def repos(params={}) - get("/user/repos", :params => params).map { |r| Githu3::Repo.new(r,self) } + def repos params={} + Githu3::ResourceCollection.new(self, Githu3::Repo, "/user/repos", params) end - def followers - get("/user/followers").map { |u| Githu3::User.new(u, self) } + def followers params={} + Githu3::ResourceCollection.new(self, Githu3::User, "/user/followers", params) end - def following - get("/user/following").map { |u| Githu3::User.new(u, self) } + def following params={} + Githu3::ResourceCollection.new(self, Githu3::User, "/user/following", params) end def following?(other_user) - @conn.get("/user/following/#{other_user}").status == 204 + begin + get("/user/following/#{other_user}").status == 204 + rescue Githu3::NotFound + false + end end end end \ No newline at end of file