Class: WunderlistToGithub::GitHubSink

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

Overview

Collects Tasks from list in Wunderlist.

Instance Method Summary collapse

Constructor Details

#initialize(login, api_token) ⇒ GitHubSink

Returns a new instance of GitHubSink.



7
8
9
# File 'lib/wunderlist_to_github/sink.rb', line 7

def initialize(, api_token)
  @github = Github.new(login: , password: api_token)
end

Instance Method Details

#comment_text(task_hash) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/wunderlist_to_github/sink.rb', line 55

def comment_text(task_hash)
  comments = task_hash[:comments]
  if comments.respond_to?(:length) && comments.length.positive?
    "\n# Comments\n\n" + comments.join("\n")
  else
    ''
  end
end

#convert(task_hashes, user, repo) ⇒ Object

Accepts a hash of a task and creates an issue in the GitHub login’s repository. If given a block, the task that was just converted to a GitHub issue is passed back to record any side effects.



14
15
16
17
18
19
20
21
# File 'lib/wunderlist_to_github/sink.rb', line 14

def convert(task_hashes, user, repo)
  task_hashes.each do |t|
    issue_hash = convert_task_hash_to_issue_hash(t)
    create_issue(issue_hash, user, repo, t[:completed])

    yield(t) if block_given?
  end
end

#convert_task_hash_to_issue_hash(task_hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wunderlist_to_github/sink.rb', line 23

def convert_task_hash_to_issue_hash(task_hash)
  {
    title: task_hash[:title],
    body: "    _Imported from Wunderlist_\n    \#{note_text(task_hash)}\n    \#{subtask_text(task_hash)}\n    \#{comment_text(task_hash)}\n    BODY\n  }\nend\n"

#create_issue(issue_hash, user, repo, completed) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wunderlist_to_github/sink.rb', line 64

def create_issue(issue_hash, user, repo, completed)
  issue_hash[:user] = user
  issue_hash[:repo] = repo
  issue = @github.issues.create(issue_hash)
  return issue unless completed
  @github.issues.edit(
    user: user,
    repo: repo,
    number: issue[:number],
    state: 'closed'
  )
end

#note_text(task_hash) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/wunderlist_to_github/sink.rb', line 35

def note_text(task_hash)
  note = task_hash[:note]
  if note.respond_to?(:length) && note.length.positive?
    "\n# Note\n\n#{note}"
  else
    ''
  end
end

#subtask_text(task_hash) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/wunderlist_to_github/sink.rb', line 44

def subtask_text(task_hash)
  subtasks = task_hash[:subtasks]
  if subtasks.respond_to?(:length) && subtasks.length.positive?
    "\n# Subtasks\n\n" + subtasks.map do |s|
      " - [ ] #{s}"
    end.join("\n")
  else
    ''
  end
end