Sha256: c1953cd91a8f9840ea69649e9253fca5c0bfb982db591fcbfc70734c1975a364

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Githu3

  class Client
  
    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/", 
        :headers => headers
      })
      @conn.use Faraday::Response::ParseJson
      @rate_limit = {}
      @conn
    end
  
    def get *args
      opts = args.extract_options!
      res = @conn.get(*args) do |req|
        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
    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)
      end
    end

    # My stuf...

    def me
      Githu3::User.new "/user", self
    end
    
    def orgs
      get("/user/orgs").map { |o| Githu3::Org.new(o, self) }
    end
    
    def repos(params={})
      get("/user/repos", :params => params).map { |r| Githu3::Repo.new(r,self) }
    end
  
    def followers
      get("/user/followers").map { |u| Githu3::User.new(u, self) }
    end
  
    def following
      get("/user/following").map { |u| Githu3::User.new(u, self) }
    end
    
    def following?(other_user)
      @conn.get("/user/following/#{other_user}").status == 204
    end
  
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
githu3-0.0.2 lib/githu3/client.rb