Class: Trello::Card

Inherits:
BasicData show all
Includes:
HasActions
Defined in:
lib/trello/card.rb

Overview

A Card is a container that can house checklists and comments; it resides inside a List.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasActions

#actions

Methods inherited from BasicData

#==, #initialize, many, one, #refresh!, register_attributes

Constructor Details

This class inherits a constructor from Trello::BasicData

Class Method Details

.create(options) ⇒ Object

Create a new card and save it on Trello.



19
20
21
22
23
# File 'lib/trello/card.rb', line 19

def create(options)
  new('name'   => options[:name],
      'idList' => options[:list_id],
      'desc'   => options[:description]).save
end

.find(id) ⇒ Object

Find a specific card by its id.



14
15
16
# File 'lib/trello/card.rb', line 14

def find(id)
  super(:cards, id)
end

Instance Method Details

#add_attachment(attachment, name = '') ⇒ Object

Add an attachment to this card



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/trello/card.rb', line 186

def add_attachment(attachment, name='')
  if attachment.is_a? File
    Client.post("/cards/#{id}/attachments", {
        :file => attachment,
        :name => name
      })
  else
    Client.post("/cards/#{id}/attachments", {
        :url => attachment,
        :name => name
      })
  end
end

#add_checklist(checklist) ⇒ Object

Add a checklist to this card



129
130
131
132
133
# File 'lib/trello/card.rb', line 129

def add_checklist(checklist)
  Client.post("/cards/#{id}/checklists", {
    :value => checklist.id
  })
end

#add_comment(text) ⇒ Object

Add a comment with the supplied text.



124
125
126
# File 'lib/trello/card.rb', line 124

def add_comment(text)
  Client.post("/cards/#{id}/actions/comments", :text => text)
end

#add_label(colour) ⇒ Object

Add a label



168
169
170
171
172
173
174
# File 'lib/trello/card.rb', line 168

def add_label(colour)
  unless %w{green yellow orange red purple blue}.include? colour
    errors.add(:label, "colour '#{colour}' does not exist")
    return Trello.logger.warn "The label colour '#{colour}' does not exist."
  end
  Client.post("/cards/#{id}/labels", { :value => colour })
end

#add_member(member) ⇒ Object

Add a member to this card



150
151
152
153
154
# File 'lib/trello/card.rb', line 150

def add_member(member)
  Client.post("/cards/#{id}/members", {
    :value => member.id
  })
end

#attachmentsObject

Retrieve a list of attachments



201
202
203
204
# File 'lib/trello/card.rb', line 201

def attachments
  attachments = Client.get("/cards/#{id}/attachments").json_into(Attachment)
  MultiAssociation.new(self, attachments).proxy
end

#check_item_statesObject



55
56
57
58
# File 'lib/trello/card.rb', line 55

def check_item_states
  states = Client.get("/cards/#{self.id}/checkItemStates").json_into(CheckItemState)
  MultiAssociation.new(self, states).proxy
end

#closeObject



109
110
111
# File 'lib/trello/card.rb', line 109

def close
  self.closed = true
end

#close!Object



113
114
115
116
# File 'lib/trello/card.rb', line 113

def close!
  close
  save
end

#closed?Boolean

Check if the card is not active anymore.

Returns:

  • (Boolean)


105
106
107
# File 'lib/trello/card.rb', line 105

def closed?
  closed
end

#labelsObject

Retrieve a list of labels



162
163
164
165
# File 'lib/trello/card.rb', line 162

def labels
  labels = Client.get("/cards/#{id}/labels").json_into(Label)
  MultiAssociation.new(self, labels).proxy
end

#membersObject

Returns a list of members who are assigned to this card.



65
66
67
68
69
70
# File 'lib/trello/card.rb', line 65

def members
  members = member_ids.map do |member_id|
    Client.get("/members/#{member_id}").json_into(Member)
  end
  MultiAssociation.new(self, members).proxy
end

#move_to_board(new_board, new_list = nil) ⇒ Object

Move this card to the given board (and optional list on this board)



143
144
145
146
147
# File 'lib/trello/card.rb', line 143

def move_to_board(new_board, new_list = nil)
  payload = { :value => new_board.id }
  payload[:idList] = new_list.id if new_list
  Client.put("/cards/#{id}/idBoard", payload)
end

#move_to_list(list) ⇒ Object

Move this card to the given list



136
137
138
139
140
# File 'lib/trello/card.rb', line 136

def move_to_list(list)
  Client.put("/cards/#{id}/idList", {
    :value => list.id
  })
end

#remove_attachment(attachment) ⇒ Object

Remove an attachment from this card



207
208
209
# File 'lib/trello/card.rb', line 207

def remove_attachment(attachment)
  Client.delete("/cards/#{id}/attachments/#{attachment.id}")
end

#remove_label(colour) ⇒ Object

Remove a label



177
178
179
180
181
182
183
# File 'lib/trello/card.rb', line 177

def remove_label(colour)
  unless %w{green yellow orange red purple blue}.include? colour
    errors.add(:label, "colour '#{colour}' does not exist")
    return Trello.logger.warn "The label colour '#{colour}' does not exist." unless %w{green yellow orange red purple blue}.include? colour
  end
  Client.delete("/cards/#{id}/labels/#{colour}")
end

#remove_member(member) ⇒ Object

Remove a member from this card



157
158
159
# File 'lib/trello/card.rb', line 157

def remove_member(member)
  Client.delete("/cards/#{id}/members/#{member.id}")
end

#request_prefixObject

:nodoc:



212
213
214
# File 'lib/trello/card.rb', line 212

def request_prefix
  "/cards/#{id}"
end

#saveObject

Saves a record.



73
74
75
76
77
78
79
80
81
82
# File 'lib/trello/card.rb', line 73

def save
  # If we have an id, just update our fields.
  return update! if id

  Client.post("/cards", {
    :name   => name,
    :desc   => description,
    :idList => list_id
  }).json_into(self)
end

#update!Object

Update an existing record. Warning, this updates all fields using values already in memory. If an external resource has updated these fields, you should refresh! this object before making your changes, and before updating the record.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/trello/card.rb', line 88

def update!
  @previously_changed = changes
  @changed_attributes.clear

  Client.put("/cards/#{id}", {
    :name      => name,
    :desc      => description,
    :due       => due && due.utc.iso8601,
    :closed    => closed,
    :idList    => list_id,
    :idBoard   => board_id,
    :idMembers => member_ids,
    :pos => pos
  })
end

#update_fields(fields) ⇒ Object

Update the fields of a card.

Supply a hash of string keyed data retrieved from the Trello API representing a card.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/trello/card.rb', line 30

def update_fields(fields)
  attributes[:id]          = fields['id']
  attributes[:short_id]    = fields['idShort']
  attributes[:name]        = fields['name']
  attributes[:description] = fields['desc']
  attributes[:due]         = Time.iso8601(fields['due']) rescue nil
  attributes[:closed]      = fields['closed']
  attributes[:url]         = fields['url']
  attributes[:board_id]    = fields['idBoard']
  attributes[:member_ids]  = fields['idMembers']
  attributes[:list_id]     = fields['idList']
  attributes[:pos]         = fields['pos']
  self
end

#valid?Boolean

Is the record valid?

Returns:

  • (Boolean)


119
120
121
# File 'lib/trello/card.rb', line 119

def valid?
  name && list_id
end