Class: Werd::ProjectWordCounter

Inherits:
Object
  • Object
show all
Includes:
Contracts
Defined in:
lib/werd/project_word_counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, project_id) ⇒ ProjectWordCounter

Returns a new instance of ProjectWordCounter.



7
8
9
# File 'lib/werd/project_word_counter.rb', line 7

def initialize token, project_id
  @token, @project_id = token, project_id
end

Instance Attribute Details

#project_idObject (readonly)

Returns the value of attribute project_id.



4
5
6
# File 'lib/werd/project_word_counter.rb', line 4

def project_id
  @project_id
end

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/werd/project_word_counter.rb', line 4

def token
  @token
end

Instance Method Details

#clientObject



25
26
27
# File 'lib/werd/project_word_counter.rb', line 25

def client
  @client ||= TrackerApi::Client.new(token: token)
end

#documentObject



35
36
37
38
39
# File 'lib/werd/project_word_counter.rb', line 35

def document
  project.stories.map do |s|
    [s.name, s.description]
  end.flatten.join("\n")
end

#projectObject



30
31
32
# File 'lib/werd/project_word_counter.rb', line 30

def project
  client.project project_id
end

#wordsObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/werd/project_word_counter.rb', line 12

def words
  counts = Hash.new 0
  document.split(/\W/).select do |str|
    str.size > 0
  end.map do |chunk|
    chunk.downcase
  end.each do |word|
    counts[word] += 1
  end
  counts.to_a.sort_by(&:last).reverse
end