Module: Inq::Sources::GithubHelpers

Included in:
Inq::Sources::Github::Contributions, Inq::Sources::Github::Issues
Defined in:
lib/inq/sources/github_helpers.rb

Overview

Helper functions used by GitHub-related sources.

Instance Method Summary collapse

Instance Method Details

#average_age_for(issues_or_pulls) ⇒ Object

Given an Array of issues or pulls, return the average age of them. Returns nil if no issues or pulls are provided.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/inq/sources/github_helpers.rb', line 17

def average_age_for(issues_or_pulls)
  return nil if issues_or_pulls.empty?

  ages = issues_or_pulls.map { |iop| time_ago_in_seconds(iop["createdAt"]) }
  average_age_in_seconds = ages.reduce(:+) / ages.length

  values =
    period_pairs_for(average_age_in_seconds) \
      .reject { |(v, _)| v.zero? } \
      .map { |(v, k)| pluralize(k, v) }

  value = values[0, 2].join(" and ")

  "approximately #{value}"
end

#newest_for(issues_or_pulls) ⇒ Object

Given an Array of issues or pulls, return the newest. Returns nil if no issues or pulls are provided.



47
48
49
50
51
# File 'lib/inq/sources/github_helpers.rb', line 47

def newest_for(issues_or_pulls)
  return nil if issues_or_pulls.empty?

  sort_iops_by_created_at(issues_or_pulls).last
end

#obj_to_array_of_hashes(object) ⇒ Object



11
12
13
# File 'lib/inq/sources/github_helpers.rb', line 11

def obj_to_array_of_hashes(object)
  object.to_a.map(&:to_h)
end

#oldest_for(issues_or_pulls) ⇒ Object

Given an Array of issues or pulls, return the oldest. Returns nil if no issues or pulls are provided.



39
40
41
42
43
# File 'lib/inq/sources/github_helpers.rb', line 39

def oldest_for(issues_or_pulls)
  return nil if issues_or_pulls.empty?

  sort_iops_by_created_at(issues_or_pulls).first
end

#sort_iops_by_created_at(issues_or_pulls) ⇒ Object



33
34
35
# File 'lib/inq/sources/github_helpers.rb', line 33

def sort_iops_by_created_at(issues_or_pulls)
  issues_or_pulls.sort_by { |x| DateTime.parse(x["createdAt"]) }
end