Sha256: c5de455a67e5c7afb747e8e0cc0fd6595d40c68f075473291bde600796037893

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

require 'open-uri'
require 'json'

module Contributions
  class GithubAPI

    # Public: Get just the user's repositories that are forks.
    #
    # Returns an Array.
    def self.forks(username)
      forks = self.repos(username).select { |r| r["fork"] == true }
      repo_names = forks.map { |r| r["owner"]["login"] + '/' + r["name"] }
      repo_names.map { |r| self.parent(r) }
    end

    # Public: Get the name of the user.
    #
    # username - github username.
    #
    # Returns a String.
    def self.name(username)
      self.user(username)["name"]
    end

    # Public: Get the name of the forked repository.
    #
    # repository - a 'username/repository_name' string.
    #
    # Returns a String.
    def self.parent(repository)
      username, repo_name = repository.split('/')
      repo_info = self.repository(repository)
      repo_info["parent"]["owner"]["login"] + '/' + repo_name
    end

    # Public: Get all the user's repositories.
    #
    # Returns an Array.
    def self.repos(username)
      JSON.parse(open("https://api.github.com/users/#{username}/repos?per_page=100") { |f| f.read } )
    end

    # Internal: Get the user info (all of it) from github.
    #
    # username - github username.
    #
    # Returns a Hash.
    def self.user(username)
      JSON.parse(open("https://api.github.com/users/#{username}") { |f| f.read } )
    end

    # Internal: Get the repository info (all of it) from github.
    #
    # repository - a 'username/repository_name' string.
    #
    # Returns a Hash.
    def self.repository(repository)
      JSON.parse(open("https://api.github.com/repos/#{repository}") { |f| f.read } )
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
contributions-0.2.0 lib/contributions/github_api.rb
contributions-0.1.2 lib/contributions/github_api.rb
contributions-0.1.1 lib/contributions/github_api.rb