Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor data structure #48

Merged
merged 2 commits into from
Mar 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/github/nippou.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'github/nippou/concerns/sawyer_resource_github'
require 'github/nippou/concerns/string_markdown'

require 'github/nippou/commands'
require 'github/nippou/user_events'
require 'github/nippou/version'
64 changes: 9 additions & 55 deletions lib/github/nippou/commands.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
require 'thor'

module StringExMarkdown
refine String do
def markdown_escape
self.gsub(/([`<>])/, '\\\\\1')
end
end
end

module Github
module Nippou
class Commands < Thor
using StringExMarkdown
using SawyerResourceGithub
using StringMarkdown

default_task :list
class_option :since_date, type: :string,
Expand All @@ -23,11 +16,13 @@ class Commands < Thor

desc 'list', "Displays today's GitHub events formatted for Nippou"
def list
nippous.each do |url, detail|
line = "* [#{detail[:title]} - #{detail[:repo_basename]}](#{url}) by #{detail[:username]}"
if detail[:merged]
user_events.each do |user_event|
issue = user_event.issue(client)
line = "* [%s - %s](%s) by %s" %
[issue.title.markdown_escape, user_event.repo.name, user_event.html_url, issue.user.login]
if issue.merged
line << ' **merged!**'
elsif detail[:state] == 'closed'
elsif issue.state == 'closed'
line << ' **closed!**'
end
puts line
Expand All @@ -41,27 +36,10 @@ def version

private

def nippous
result = {}

user_events.each do |user_event|
case user_event.type
when 'IssuesEvent', 'IssueCommentEvent'
issue = user_event.payload.issue
result[issue.html_url] ||= hash_for_issue(user_event.repo.name, issue.number)
when 'PullRequestEvent', 'PullRequestReviewCommentEvent'
pr = user_event.payload.pull_request
result[pr.html_url] ||= hash_for_pr(user_event.repo.name, pr.number)
end
end

result.sort
end

def user_events
@user_events ||= UserEvents.new(
client, user, options[:since_date], options[:until_date]
).retrieve
).collect
end

def client
Expand Down Expand Up @@ -106,30 +84,6 @@ def access_token
exit!
end
end

def hash_for_issue(repo_name, issue_number)
issue = client.issue(repo_name, issue_number)

{
title: issue.title.markdown_escape,
repo_basename: repo_name,
username: issue.user.login,
merged: issue.merged,
state: issue.state,
}
end

def hash_for_pr(repo_name, pr_number)
pr = client.pull_request(repo_name, pr_number)

{
title: pr.title.markdown_escape,
repo_basename: repo_name,
username: pr.user.login,
merged: pr.merged,
state: pr.state,
}
end
end
end
end
29 changes: 29 additions & 0 deletions lib/github/nippou/concerns/sawyer_resource_github.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'octokit'

module SawyerResourceGithub
refine Sawyer::Resource do
def issue(client)
case
when self.issue?
client.issue(self.repo.name, self.payload.issue.number)
when self.pull_request?
client.pull_request(self.repo.name, self.payload.pull_request.number)
end
end

def issue?
self.type == 'IssuesEvent' ||
self.type == 'IssueCommentEvent'
end

def pull_request?
self.type == 'PullRequestEvent' ||
self.type == 'PullRequestReviewCommentEvent'
end

def html_url
type = self.issue? ? :issue : :pull_request
self.payload.try!(type).html_url
end
end
end
7 changes: 7 additions & 0 deletions lib/github/nippou/concerns/string_markdown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module StringMarkdown
refine String do
def markdown_escape
self.gsub(/([`<>])/, '\\\\\1')
end
end
end
28 changes: 26 additions & 2 deletions lib/github/nippou/user_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
module Github
module Nippou
class UserEvents
using SawyerResourceGithub

def initialize(client, user, since_date, until_date)
@client = client
@user = user
Expand All @@ -12,6 +14,12 @@ def initialize(client, user, since_date, until_date)
@range = @since_time..until_time
end

def collect
uniq(sort(filter(retrieve)))
end

private

def retrieve
user_events = @client.user_events(@user, per_page: 100)
last_response = @client.last_response
Expand All @@ -24,8 +32,6 @@ def retrieve
user_events.select { |user_event| in_range?(user_event) }
end

private

def continue?(last_response, user_events)
last_response.rels[:next] &&
user_events.last.created_at.getlocal >= @since_time
Expand All @@ -34,6 +40,24 @@ def continue?(last_response, user_events)
def in_range?(user_event)
@range.include?(user_event.created_at.getlocal)
end

def filter(user_events)
user_events.select do |user_event|
user_event.issue? || user_event.pull_request?
end
end

def sort(user_events)
user_events.sort do |a, b|
a.html_url <=> b.html_url
end
end

def uniq(user_events)
user_events.uniq do |user_event|
user_event.html_url
end
end
end
end
end