ruby-trello 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 260b90a4f5cc53cb0c695edb040c75144a24a2f0
4
- data.tar.gz: 1b6e24750bb32ae29d798912b43a60e98ece7088
3
+ metadata.gz: 45ec7bdcb469bf63f80f7291c787a4f4a8ec653f
4
+ data.tar.gz: cc4a463550cfe8036d1c4e4cef421389dd6535f8
5
5
  SHA512:
6
- metadata.gz: 913d7972b22fce3998ea166fc1dc94c8d4e699ef2f1b2b686fd585fb951b430769b74dbb8d3e2d90bd3e99791da8b5018f15839bbf79a636917e52fbb1d492c6
7
- data.tar.gz: f68c3097b8e2c8cf043587b98aa7559b8aeb14c35891bea11de1edab13cf6e04be0adaf48df7dd0db4be4afb17211fd21cc5e865105435c5fd139f1fc6ac18fd
6
+ metadata.gz: fdd6a111315be371b94c6c84cd57742faea4519556136b2db601baed79ef46c670afde112408a1668563c4fdd513fc319221b72721b50f0103e240e69c6d5cfd
7
+ data.tar.gz: 002b5cca51dd9640a148968a685c25da8b7b627c9446c33e6cb87c4eadc800a5566a14aed9a4205931dc90881cc63093548865894dfb9860b3dca3b59ca02c29
data/lib/trello/board.rb CHANGED
@@ -154,8 +154,12 @@ module Trello
154
154
  # Returns a reference to the organization this board belongs to.
155
155
  one :organization, path: :organizations, using: :organization_id
156
156
 
157
- def labels
158
- labels = client.get("/boards/#{id}/labelnames").json_into(LabelName)
157
+ def labels names=true
158
+ if names
159
+ labels = client.get("/boards/#{id}/labelnames").json_into(LabelName)
160
+ else
161
+ labels = client.get("/boards/#{id}/labels").json_into(Label)
162
+ end
159
163
  MultiAssociation.new(self, labels).proxy
160
164
  end
161
165
 
data/lib/trello/card.rb CHANGED
@@ -319,28 +319,33 @@ module Trello
319
319
  labels = client.get("/cards/#{id}/labels").json_into(Label)
320
320
  MultiAssociation.new(self, labels).proxy
321
321
  end
322
-
323
- # Label colours
324
- def label_colours
325
- %w{green yellow orange red purple blue sky lime pink black}
326
- end
327
322
 
328
323
  # Add a label
329
- def add_label(colour)
330
- unless label_colours.include? colour
331
- errors.add(:label, "colour '#{colour}' does not exist")
332
- return Trello.logger.warn "The label colour '#{colour}' does not exist."
324
+ def add_label(value)
325
+ if value.is_a? String
326
+ colour = value
327
+ unless Label.label_colours.include? colour
328
+ errors.add(:label, "colour '#{colour}' does not exist")
329
+ return Trello.logger.warn "The label colour '#{colour}' does not exist."
330
+ end
331
+ client.post("/cards/#{id}/labels", { value: colour })
332
+ elsif value.is_a? Label
333
+ client.post("/cards/#{id}/idLabels", {value: value.id})
333
334
  end
334
- client.post("/cards/#{id}/labels", { value: colour })
335
335
  end
336
336
 
337
337
  # Remove a label
338
- def remove_label(colour)
339
- unless label_colours.include? colour
340
- errors.add(:label, "colour '#{colour}' does not exist")
341
- return Trello.logger.warn "The label colour '#{colour}' does not exist." unless label_colours.include? colour
338
+ def remove_label(value)
339
+ if value.is_a? String
340
+ colour = value
341
+ unless Label.label_colours.include? colour
342
+ errors.add(:label, "colour '#{colour}' does not exist")
343
+ return Trello.logger.warn "The label colour '#{colour}' does not exist." unless Label.label_colours.include? colour
344
+ end
345
+ client.delete("/cards/#{id}/labels/#{colour}")
346
+ elsif value.is_a? Label
347
+ client.delete("/cards/#{id}/idLabels/#{value.id}")
342
348
  end
343
- client.delete("/cards/#{id}/labels/#{colour}")
344
349
  end
345
350
 
346
351
  # Add an attachment to this card
data/lib/trello/label.rb CHANGED
@@ -7,18 +7,100 @@ module Trello
7
7
  # @!attribute [rw] color
8
8
  # @return [String]
9
9
  class Label < BasicData
10
- register_attributes :name, :color
10
+ register_attributes :id, :name, :board_id, :uses,
11
+ readonly: [ :id, :uses, :board_id ]
12
+ validates_presence_of :id, :uses, :board_id, :name
13
+ validates_length_of :name, in: 1..16384
14
+
15
+ SYMBOL_TO_STRING = {
16
+ id: 'id',
17
+ name: 'name',
18
+ board_id: 'idBoard',
19
+ color: 'color',
20
+ uses: 'uses'
21
+ }
22
+
23
+ class << self
24
+ # Find a specific card by its id.
25
+ def find(id, params = {})
26
+ client.find(:label, id, params)
27
+ end
28
+
29
+ # Create a new card and save it on Trello.
30
+ def create(options)
31
+ client.create(:label,
32
+ 'name' => options[:name],
33
+ 'idBoard' => options[:board_id],
34
+ 'color' => options[:color],
35
+ )
36
+ end
37
+
38
+ # Label colours
39
+ def label_colours
40
+ %w{green yellow orange red purple blue sky lime pink black}
41
+ end
42
+ end
43
+
44
+ define_attribute_methods [:color]
45
+
46
+ def color
47
+ @attributes[:color]
48
+ end
49
+
50
+ def color= colour
51
+ unless Label.label_colours.include? colour
52
+ errors.add(:label, "color '#{colour}' does not exist")
53
+ return Trello.logger.warn "The label colour '#{colour}' does not exist."
54
+ end
55
+
56
+ self.send(:"color_will_change!") unless colour == @attributes[:color]
57
+ @attributes[:color] = colour
58
+ end
11
59
 
12
60
  # Update the fields of a label.
13
61
  #
14
62
  # Supply a hash of stringkeyed data retrieved from the Trello API representing
15
63
  # a label.
16
64
  def update_fields(fields)
65
+ attributes[:id] = fields['id']
17
66
  attributes[:name] = fields['name']
18
67
  attributes[:color] = fields['color']
68
+ attributes[:board_id] = fields['idBoard']
69
+ attributes[:uses] = fields['uses']
19
70
  self
20
71
  end
21
72
 
22
- end
73
+ # Returns a reference to the board this label is currently connected.
74
+ one :board, path: :boards, using: :board_id
75
+
76
+ # Saves a record.
77
+ def save
78
+ # If we have an id, just update our fields.
79
+ return update! if id
80
+
81
+ client.post("/labels", {
82
+ name: name,
83
+ color: color,
84
+ idBoard: board_id,
85
+ }).json_into(self)
86
+ end
87
+
88
+ # Update an existing record.
89
+ # Warning, this updates all fields using values already in memory. If
90
+ # an external resource has updated these fields, you should refresh!
91
+ # this object before making your changes, and before updating the record.
92
+ def update!
93
+ @previously_changed = changes
94
+ # extract only new values to build payload
95
+ payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
96
+ @changed_attributes.clear
23
97
 
98
+ client.put("/labels/#{id}", payload)
99
+ end
100
+
101
+ # Delete this card
102
+ def delete
103
+ client.delete("/labels/#{id}")
104
+ end
105
+ end
24
106
  end
data/spec/board_spec.rb CHANGED
@@ -83,6 +83,26 @@ module Trello
83
83
  end
84
84
 
85
85
  context "labels" do
86
+ it "gets the specific labels for the board" do
87
+ client.stub(:get).with("/boards/abcdef123456789123456789/labels").
88
+ and_return label_payload
89
+ labels = board.labels false
90
+ labels.count.should eq(4)
91
+
92
+
93
+ expect(labels[2].color).to eq('red')
94
+ expect(labels[2].id).to eq('abcdef123456789123456789')
95
+ expect(labels[2].board_id).to eq('abcdef123456789123456789')
96
+ expect(labels[2].name).to eq('deploy')
97
+ expect(labels[2].uses).to eq(2)
98
+
99
+ expect(labels[3].color).to eq('blue')
100
+ expect(labels[3].id).to eq('abcdef123456789123456789')
101
+ expect(labels[3].board_id).to eq('abcdef123456789123456789')
102
+ expect(labels[3].name).to eq('on hold')
103
+ expect(labels[3].uses).to eq(6)
104
+ end
105
+
86
106
  it "gets the specific labels for the board" do
87
107
  client.stub(:get).with("/boards/abcdef123456789123456789/labelnames").
88
108
  and_return label_name_payload
data/spec/card_spec.rb CHANGED
@@ -255,13 +255,19 @@ module Trello
255
255
  client.stub(:get).with("/cards/abcdef123456789123456789/labels").
256
256
  and_return label_payload
257
257
  labels = card.labels
258
- expect(labels.size).to eq(2)
258
+ expect(labels.size).to eq(4)
259
259
 
260
260
  expect(labels[0].color).to eq('yellow')
261
+ expect(labels[0].id).to eq('abcdef123456789123456789')
262
+ expect(labels[0].board_id).to eq('abcdef123456789123456789')
261
263
  expect(labels[0].name).to eq('iOS')
264
+ expect(labels[0].uses).to eq(3)
262
265
 
263
266
  expect(labels[1].color).to eq('purple')
267
+ expect(labels[1].id).to eq('abcdef123456789123456789')
268
+ expect(labels[1].board_id).to eq('abcdef123456789123456789')
264
269
  expect(labels[1].name).to eq('Issue or bug')
270
+ expect(labels[1].uses).to eq(1)
265
271
  end
266
272
 
267
273
  it "can add a label" do
@@ -278,6 +284,12 @@ module Trello
278
284
  expect(card.errors).to be_empty
279
285
  end
280
286
 
287
+ it "can remove a label instance" do
288
+ client.should_receive(:delete).once.with("/cards/abcdef123456789123456789/idLabels/abcdef123456789123456789")
289
+ label = Label.new(label_details.first)
290
+ card.remove_label(label)
291
+ end
292
+
281
293
  it "can add a label of any valid color" do
282
294
  %w(green yellow orange red purple blue sky lime pink black).each do |color|
283
295
  client.stub(:post).with("/cards/abcdef123456789123456789/labels", { :value => color }).
@@ -287,6 +299,12 @@ module Trello
287
299
  end
288
300
  end
289
301
 
302
+ it "can add a label instance" do
303
+ client.should_receive(:post).once.with("/cards/abcdef123456789123456789/idLabels", {:value => "abcdef123456789123456789"})
304
+ label = Label.new(label_details.first)
305
+ card.add_label label
306
+ end
307
+
290
308
  it "throws an error when trying to add a label with an unknown colour" do
291
309
  client.stub(:post).with("/cards/abcdef123456789123456789/labels", { value: 'green' }).
292
310
  and_return "not important"
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ module Trello
4
+ describe Label do
5
+ include Helpers
6
+
7
+ let(:label) { client.find(:label, 'abcdef123456789123456789') }
8
+ let(:client) { Client.new }
9
+
10
+ before(:each) do
11
+ client.stub(:get).with("/labels/abcdef123456789123456789", {}).
12
+ and_return JSON.generate(label_details.first)
13
+ end
14
+
15
+ context "finding" do
16
+ let(:client) { Trello.client }
17
+
18
+ it "delegates to Trello.client#find" do
19
+ client.should_receive(:find).with(:label, 'abcdef123456789123456789', {})
20
+ Label.find('abcdef123456789123456789')
21
+ end
22
+
23
+ it "is equivalent to client#find" do
24
+ Label.find('abcdef123456789123456789').should eq(label)
25
+ end
26
+ end
27
+
28
+ context "creating" do
29
+ let(:client) { Trello.client }
30
+
31
+ it "creates a new record" do
32
+ label = Label.new(label_details.first)
33
+ label.should be_valid
34
+ end
35
+
36
+ it 'must not be valid if not given a name' do
37
+ label = Label.new('idBoard' => lists_details.first['board_id'])
38
+ label.should_not be_valid
39
+ end
40
+
41
+ it 'must not be valid if not given a board id' do
42
+ label = Label.new('name' => lists_details.first['name'])
43
+ label.should_not be_valid
44
+ end
45
+
46
+ it 'creates a new record and saves it on Trello', refactor: true do
47
+ payload = {
48
+ name: 'Test Label',
49
+ board_id: 'abcdef123456789123456789',
50
+ }
51
+
52
+ result = JSON.generate(cards_details.first.merge(payload.merge(idBoard: boards_details.first['id'])))
53
+
54
+ expected_payload = {name: "Test Label", color: nil, idBoard: "abcdef123456789123456789" }
55
+
56
+ client.should_receive(:post).with("/labels", expected_payload).and_return result
57
+
58
+ label = Label.create(label_details.first.merge(payload.merge(board_id: boards_details.first['id'])))
59
+
60
+ label.class.should be Label
61
+ end
62
+ end
63
+
64
+ context "updating" do
65
+ it "updating name does a put on the correct resource with the correct value" do
66
+ expected_new_name = "xxx"
67
+
68
+ payload = {
69
+ name: expected_new_name,
70
+ }
71
+
72
+ client.should_receive(:put).once.with("/labels/abcdef123456789123456789", payload)
73
+
74
+ label.name = expected_new_name
75
+ label.save
76
+ end
77
+
78
+ it "updating color does a put on the correct resource with the correct value" do
79
+ expected_new_color = "purple"
80
+
81
+ payload = {
82
+ color: expected_new_color,
83
+ }
84
+
85
+ client.should_receive(:put).once.with("/labels/abcdef123456789123456789", payload)
86
+
87
+ label.color = expected_new_color
88
+ label.save
89
+ end
90
+
91
+ it "can update with any valid color" do
92
+ %w(green yellow orange red purple blue sky lime pink black).each do |color|
93
+ client.stub(:put).with("/labels/abcdef123456789123456789", {color: color}).
94
+ and_return "not important"
95
+ label.color = color
96
+ label.save
97
+ expect(label.errors).to be_empty
98
+ end
99
+ end
100
+
101
+ it "throws an error when trying to update a label with an unknown colour" do
102
+ client.stub(:put).with("/labels/abcdef123456789123456789", {}).
103
+ and_return "not important"
104
+ label.color = 'mauve'
105
+ label.save
106
+ expect(label.errors.full_messages.to_sentence).to eq("Label color 'mauve' does not exist")
107
+ end
108
+ end
109
+
110
+ context "deleting" do
111
+ it "deletes the label" do
112
+ client.should_receive(:delete).with("/labels/#{label.id}")
113
+ label.delete
114
+ end
115
+ end
116
+
117
+ context "fields" do
118
+ it "gets its id" do
119
+ label.id.should_not be_nil
120
+ end
121
+
122
+ it "gets its name" do
123
+ label.name.should_not be_nil
124
+ end
125
+
126
+ it "gets its usage" do
127
+ label.uses.should_not be_nil
128
+ end
129
+
130
+ it "gets its color" do
131
+ label.color.should_not be_nil
132
+ end
133
+ end
134
+
135
+ context "boards" do
136
+ it "has a board" do
137
+ client.stub(:get).with("/boards/abcdef123456789123456789", {}).and_return JSON.generate(boards_details.first)
138
+ label.board.should_not be_nil
139
+ end
140
+ end
141
+ end
142
+ end
data/spec/spec_helper.rb CHANGED
@@ -275,8 +275,10 @@ module Helpers
275
275
 
276
276
  def label_details
277
277
  [
278
- {'color' => 'yellow', 'name' => 'iOS'},
279
- {'color' => 'purple', 'name' => 'Issue or bug'}
278
+ {'color' => 'yellow', 'name' => 'iOS', 'id' => 'abcdef123456789123456789', 'uses' => 3, 'idBoard' => 'abcdef123456789123456789'},
279
+ {'color' => 'purple', 'name' => 'Issue or bug', 'id' => 'abcdef123456789123456789', 'uses' => 1, 'idBoard' => 'abcdef123456789123456789'},
280
+ {'color' => 'red', 'name' => 'deploy', 'id' => 'abcdef123456789123456789', 'uses' => 2, 'idBoard' => 'abcdef123456789123456789'},
281
+ {'color' => 'blue', 'name' => 'on hold', 'id' => 'abcdef123456789123456789', 'uses' => 6, 'idBoard' => 'abcdef123456789123456789'}
280
282
  ]
281
283
  end
282
284
 
@@ -284,21 +286,6 @@ module Helpers
284
286
  JSON.generate(label_details)
285
287
  end
286
288
 
287
- def label_name_details
288
- [
289
- {'yellow' => 'bug'},
290
- {'red' => 'urgent'},
291
- {'green' => 'deploy'},
292
- {'blue' => 'on hold'},
293
- {'orange' => 'new feature'},
294
- {'purple' => 'experimental'}
295
- ]
296
- end
297
-
298
- def label_name_payload
299
- JSON.generate(label_name_details)
300
- end
301
-
302
289
  def webhooks_details
303
290
  [
304
291
  {
@@ -311,6 +298,21 @@ module Helpers
311
298
  ]
312
299
  end
313
300
 
301
+ def label_name_details
302
+ [
303
+ {'yellow' => 'bug'},
304
+ {'red' => 'urgent'},
305
+ {'green' => 'deploy'},
306
+ {'blue' => 'on hold'},
307
+ {'orange' => 'new feature'},
308
+ {'purple' => 'experimental'}
309
+ ]
310
+ end
311
+
312
+ def label_name_payload
313
+ JSON.generate(label_name_details)
314
+ end
315
+
314
316
  def webhooks_payload
315
317
  JSON.generate(webhooks_details)
316
318
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-trello
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Tregunna
@@ -132,6 +132,7 @@ files:
132
132
  - spec/integration/integration_test.rb
133
133
  - spec/item_spec.rb
134
134
  - spec/item_state_spec.rb
135
+ - spec/label_spec.rb
135
136
  - spec/list_spec.rb
136
137
  - spec/member_spec.rb
137
138
  - spec/notification_spec.rb
@@ -183,6 +184,7 @@ test_files:
183
184
  - spec/integration/integration_test.rb
184
185
  - spec/item_spec.rb
185
186
  - spec/item_state_spec.rb
187
+ - spec/label_spec.rb
186
188
  - spec/list_spec.rb
187
189
  - spec/member_spec.rb
188
190
  - spec/notification_spec.rb