Class: Contributions::Contributions

Inherits:
Object
  • Object
show all
Defined in:
lib/contributions/contributions.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Contributions

opts - a Hash with, at the very least, a username. Optional

arguments include :remove (to ignore some repository),
:add (to add), and :only (to focus).


9
10
11
12
# File 'lib/contributions/contributions.rb', line 9

def initialize(opts={})
  @username = opts.delete(:username)
  setup_repositories(opts)
end

Instance Method Details

#add(repos) ⇒ Object

Public: Add a repository (or array of repositories).

repos - a ‘username/repository’ String or Array of such strings.

Returns the updated array of repositories.



19
20
21
22
23
# File 'lib/contributions/contributions.rb', line 19

def add(repos)
  @repositories.add(repos)

  repositories
end

#contributionsObject

Internal: attr_reader for @contributions. This method really only exists for testing.

Returns a Hash.



119
120
121
# File 'lib/contributions/contributions.rb', line 119

def contributions
  @contributions
end

#contributions=(hash) ⇒ Object

Internal: attr_accessor for @contributions. This method really only exists for testing.

hash - a hash.

Returns a Hash.



111
112
113
# File 'lib/contributions/contributions.rb', line 111

def contributions=(hash)
  @contributions = hash
end

#contributions_as_hashObject

Public: Return a user’s OSS contributions as a hash. If the hash hasn’t already been determined, the contributions are all looked up and stashed in an ivar: @contributions. If @contributions already exists, it is returned without the costly lookup being performed.

Returns a Hash.



32
33
34
35
# File 'lib/contributions/contributions.rb', line 32

def contributions_as_hash
  load_contributions unless @contributions
  @contributions
end

#get_contributions(repository) ⇒ Object

Internal: Get the user’s contributions to the repository.

Returns a Hash.



137
138
139
# File 'lib/contributions/contributions.rb', line 137

def get_contributions(repository)
  Git.contributions GithubAPI.name(@username), repository
end

#load_contributionsObject

Public: Determine a user’s contributions and load the Returns a Hash.



41
42
43
44
45
46
47
48
49
# File 'lib/contributions/contributions.rb', line 41

def load_contributions
  @contributions = Hash.new
  repositories.each do |f|
    conts = get_contributions(f)
    @contributions[f] = conts unless conts.empty?
  end

  @contributions
end

#only(repos) ⇒ Object

Public: Replace the user’s forked repositories with the specified repositories.

repos - a ‘username/repository_name’ string, or an array of such

strings.

Returns the updated array of repositories.



58
59
60
61
62
# File 'lib/contributions/contributions.rb', line 58

def only(repos)
  @repositories.only repos

  repositories
end

#project_namesObject

Public: Provide the names for all the forked projects.

Example:

user.repositories
# => ['r/r', 's/s']
user.project_names
# => ['r', 's']

Returns an Array.



74
75
76
# File 'lib/contributions/contributions.rb', line 74

def project_names
  repositories.map { |s| s.match(/[^\/]*$/)[0] }
end

#remove(repos) ⇒ Object

Public: Remove a repository (or array of repositories).

repos - a ‘username/repository’ String or Array of such strings.

Returns the updated array of repositories.



83
84
85
86
87
# File 'lib/contributions/contributions.rb', line 83

def remove(repos)
  @repositories.remove(repos)

  repositories
end

#repositoriesObject

Public: Accessor method for the @repositories ivar.

Returns an Array of ‘username/repository_name’ strings.



101
102
103
# File 'lib/contributions/contributions.rb', line 101

def repositories
  @repositories.list
end

#repositories=(array) ⇒ Object

Public: Accessor method for the @repositories ivar.

array - an array of ‘username/repository_name’ strings.

Returns nothing.



94
95
96
# File 'lib/contributions/contributions.rb', line 94

def repositories=(array)
  @repositories = RepositoryList.new(array)
end

#setup_repositories(opts) ⇒ Object

Internal: Generate an array of forked repositories for the user. This array is set as the @repositories variable.

opts - an array with, possible, keys for :only, :remove, and :add.

Returns an Array of repositories.



129
130
131
132
# File 'lib/contributions/contributions.rb', line 129

def setup_repositories(opts)
  @repositories = RepositoryList.new(GithubAPI.forks(@username))
  update(opts)
end

#update(opts) ⇒ Object

Internal: Combine the user’s explicit preferences with an array of forks.

Returns an Array.



145
146
147
148
149
150
151
# File 'lib/contributions/contributions.rb', line 145

def update(opts)
  opts.each_pair do |k,v|
    @repositories.send(k.to_sym, v)
  end

  repositories
end