Class: Trello2WR

Inherits:
Object
  • Object
show all
Includes:
Trello, Trello::Authorization
Defined in:
lib/trello2wr.rb

Constant Summary collapse

@@debug =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sprint = nil, week) ⇒ Trello2WR

Returns a new instance of Trello2WR.



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

def initialize(sprint=nil, week)
  @config = load_config

  authenticate

  @sprint = sprint
  @week = week

  @username = @config['trello']['username']
  @user = find_member(@username)
  @board = find_board
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



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

def board
  @board
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

#weekObject (readonly)

Returns the value of attribute week.



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

def week
  @week
end

Instance Method Details

#authenticateObject



25
26
27
28
29
# File 'lib/trello2wr.rb', line 25

def authenticate
  Trello::Authorization.const_set :AuthPolicy, OAuthPolicy
  OAuthPolicy.consumer_credential = OAuthCredential.new @config['trello']['developer_public_key'], @config['trello']['developer_secret']
  OAuthPolicy.token = OAuthCredential.new @config['trello']['member_token'], nil
end

#bodyObject

Prepare mail body



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/trello2wr.rb', line 82

def body
  body = ''
  ['Done', 'In review', 'To Do', 'Doing'].each do |list_name|
    if list_name.downcase.include? 'done'
      body += "Accomplishments:\n"
    elsif list_name.downcase.include? 'review'
      body += "\nIn review:\n"
    elsif list_name.downcase.include? 'to do'
      body += "\nObjectives:\n"
    end

    self.cards(board, list_name).each do |card|
      if list_name.downcase.include? 'doing'
        body += "- #{card.name} (##{card.short_id}) [WIP]\n"
      else
        body += "- #{card.name} (##{card.short_id})\n"
      end
    end
  end

  body += "\n\nNOTE: (#<number>) are Trello board card IDs"

  escape(body)
end

#cards(board, list_name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/trello2wr.rb', line 56

def cards(board, list_name)
  self.log("*** Getting cards for '#{list_name}' list")

  if board
    lists = board.lists

    if list_name == 'Done'
      lists = lists.select{|l| l.name.include?('Done')}
      list = @sprint ? lists.select{|l| l.name.include?(@sprint.to_s) }.first : lists.sort_by{|l| l.id }.last

      self.log("*** Getting cards for '#{list.name}' list (week #{@week})")
      list.cards.select{|c| c.last_activity_date.to_datetime.cweek == @week && c.member_ids.include?(user.id) }
    else
      lists.find{|l| l.name == list_name}.cards.select{|c| c.member_ids.include? self.user.id}
    end
  else
    raise "ERROR: Board '#{list_name}' not found!"
  end
end

#construct_mail_to_url(recipient, subject, body) ⇒ Object



107
108
109
110
111
112
# File 'lib/trello2wr.rb', line 107

def construct_mail_to_url(recipient, subject, body)
  headers = { subject: subject, body: body }
  headers[:cc] = @config['email']['cc'] if @config['email'].has_key?('cc') && @config['email']['cc'].present?

  URI::MailTo.build({:to => recipient, :headers => headers.stringify_keys}).to_s.inspect
end

#escape(string) ⇒ Object



114
115
116
# File 'lib/trello2wr.rb', line 114

def escape(string)
  URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end

#exportObject



118
119
120
121
122
123
124
125
# File 'lib/trello2wr.rb', line 118

def export
  mailto = self.construct_mail_to_url(@config['email']['recipient'], subject, body)
  self.log("*** Preparing email, please wait ...")

  system("#{@config['email']['client']} #{mailto}")

  self.log("*** DONE")
end

#find_boardObject



50
51
52
53
54
# File 'lib/trello2wr.rb', line 50

def find_board
  board = @config['trello']['boards'].first
  self.log("*** Getting lists for '#{board}' board")
  @user.boards.find{|b| b.name == board}
end

#find_member(username) ⇒ Object



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

def find_member(username)
  self.log("*** Searching for user '#{username}'")

  begin
    Member.find(username)
  rescue Trello::Error
    raise "ERROR: user '#{username}' not found!}"
  end
end

#load_configObject



31
32
33
34
35
36
37
38
# File 'lib/trello2wr.rb', line 31

def load_config
  # Read keys from ~/trello2wr/config.yml
  if File.exist? File.expand_path("~/.trello2wr/config.yml")
    YAML.load_file(File.expand_path("~/.trello2wr/config.yml"))
  else
    raise "ERROR: Config file not found!"
  end
end

#log(message) ⇒ Object



127
128
129
# File 'lib/trello2wr.rb', line 127

def log(message)
  puts message if @@debug
end

#subjectObject

Prepare mail header



77
78
79
# File 'lib/trello2wr.rb', line 77

def subject
  self.escape("A&O Week ##{self.week} #{self.user.username}")
end