ruby-trello 2.3.1 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (240) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +49 -23
  3. data/lib/trello/action.rb +27 -21
  4. data/lib/trello/attachment.rb +40 -27
  5. data/lib/trello/basic_data.rb +104 -9
  6. data/lib/trello/board.rb +88 -102
  7. data/lib/trello/card.rb +34 -235
  8. data/lib/trello/checklist.rb +17 -71
  9. data/lib/trello/client.rb +1 -1
  10. data/lib/trello/comment.rb +39 -26
  11. data/lib/trello/configuration.rb +8 -0
  12. data/lib/trello/cover_image.rb +20 -0
  13. data/lib/trello/custom_field.rb +31 -84
  14. data/lib/trello/custom_field_item.rb +36 -58
  15. data/lib/trello/custom_field_option.rb +40 -13
  16. data/lib/trello/item.rb +27 -16
  17. data/lib/trello/item_state.rb +7 -15
  18. data/lib/trello/json_utils.rb +11 -5
  19. data/lib/trello/label.rb +14 -101
  20. data/lib/trello/label_name.rb +22 -25
  21. data/lib/trello/list.rb +31 -44
  22. data/lib/trello/member.rb +77 -32
  23. data/lib/trello/net/faraday.rb +46 -0
  24. data/lib/trello/net/rest_client.rb +41 -0
  25. data/lib/trello/net.rb +1 -28
  26. data/lib/trello/notification.rb +27 -20
  27. data/lib/trello/organization.rb +53 -25
  28. data/lib/trello/plugin_datum.rb +11 -16
  29. data/lib/trello/schema/attribute/base.rb +79 -0
  30. data/lib/trello/schema/attribute/board_pref.rb +45 -0
  31. data/lib/trello/schema/attribute/custom_field_display.rb +45 -0
  32. data/lib/trello/schema/attribute/default.rb +45 -0
  33. data/lib/trello/schema/attribute_builder.rb +42 -0
  34. data/lib/trello/schema/attribute_registration.rb +47 -0
  35. data/lib/trello/schema/serializer/default.rb +17 -0
  36. data/lib/trello/schema/serializer/labels.rb +23 -0
  37. data/lib/trello/schema/serializer/time.rb +25 -0
  38. data/lib/trello/schema/serializer/webhooks.rb +25 -0
  39. data/lib/trello/schema.rb +31 -0
  40. data/lib/trello/token.rb +15 -12
  41. data/lib/trello/webhook.rb +18 -71
  42. data/lib/trello.rb +62 -7
  43. data/spec/action_spec.rb +1 -2
  44. data/spec/board_spec.rb +43 -103
  45. data/spec/card_spec.rb +51 -50
  46. data/spec/cassettes/action_find_with_id_and_get_all_fields.yml +104 -0
  47. data/spec/cassettes/board_find_with_id_and_get_all_fields.yml +96 -0
  48. data/spec/cassettes/board_find_with_id_and_get_specific_fields.yml +96 -0
  49. data/spec/cassettes/can_add_a_member_to_a_board.yml +191 -0
  50. data/spec/cassettes/can_arvhice_all_cards_of_list.yml +397 -0
  51. data/spec/cassettes/can_get_actions_of_board.yml +188 -0
  52. data/spec/cassettes/can_get_actions_of_list.yml +202 -0
  53. data/spec/cassettes/can_get_actions_of_members.yml +199 -0
  54. data/spec/cassettes/can_get_actions_of_organization.yml +201 -0
  55. data/spec/cassettes/can_get_boards_of_custom_field.yml +198 -0
  56. data/spec/cassettes/can_get_boards_of_label.yml +197 -0
  57. data/spec/cassettes/can_get_boards_of_organization.yml +207 -0
  58. data/spec/cassettes/can_get_cards_of_board.yml +193 -0
  59. data/spec/cassettes/can_get_cards_of_list.yml +201 -0
  60. data/spec/cassettes/can_get_cards_of_member.yml +204 -0
  61. data/spec/cassettes/can_get_checklists_of_board.yml +187 -0
  62. data/spec/cassettes/can_get_comments_board.yml +109 -0
  63. data/spec/cassettes/can_get_comments_card.yml +111 -0
  64. data/spec/cassettes/can_get_comments_list.yml +109 -0
  65. data/spec/cassettes/can_get_custom_field_options_of_custom_field.yml +197 -0
  66. data/spec/cassettes/can_get_custom_fields_of_board.yml +187 -0
  67. data/spec/cassettes/can_get_label_names_of_board.yml +187 -0
  68. data/spec/cassettes/can_get_labels_of_board.yml +187 -0
  69. data/spec/cassettes/can_get_lists_of_board.yml +187 -0
  70. data/spec/cassettes/can_get_members_of_board.yml +188 -0
  71. data/spec/cassettes/can_get_members_of_organization.yml +200 -0
  72. data/spec/cassettes/can_get_notifications_of_member.yml +199 -0
  73. data/spec/cassettes/can_get_organization_of_board.yml +189 -0
  74. data/spec/cassettes/can_get_organizations_of_member.yml +203 -0
  75. data/spec/cassettes/can_get_plugin_data_of_board.yml +187 -0
  76. data/spec/cassettes/can_move_all_cards_to_another_list.yml +886 -0
  77. data/spec/cassettes/can_success_create_a_board.yml +98 -0
  78. data/spec/cassettes/can_success_create_a_board_with_full_parameters.yml +97 -0
  79. data/spec/cassettes/can_success_create_a_board_without_clone_from_other.yml +98 -0
  80. data/spec/cassettes/can_success_create_a_card_when_due_is_datetime.yml +98 -0
  81. data/spec/cassettes/can_success_create_a_card_when_due_is_time.yml +98 -0
  82. data/spec/cassettes/can_success_create_a_checklist.yml +102 -0
  83. data/spec/cassettes/can_success_create_a_custom_field.yml +102 -0
  84. data/spec/cassettes/can_success_create_a_custom_field_option.yml +102 -0
  85. data/spec/cassettes/can_success_create_a_label.yml +102 -0
  86. data/spec/cassettes/can_success_create_a_list.yml +102 -0
  87. data/spec/cassettes/can_success_create_a_list_with_full_parameters.yml +102 -0
  88. data/spec/cassettes/can_success_create_a_organization.yml +104 -0
  89. data/spec/cassettes/can_success_create_a_webhook.yml +102 -0
  90. data/spec/cassettes/can_success_delete_a_custom_field_option.yml +389 -0
  91. data/spec/cassettes/can_success_delete_checklist.yml +284 -0
  92. data/spec/cassettes/can_success_get_card_of_board_by_card_id.yml +188 -0
  93. data/spec/cassettes/can_success_remove_member_from_board.yml +186 -0
  94. data/spec/cassettes/can_success_return_all_boards_of_current_user.yml +195 -0
  95. data/spec/cassettes/can_success_upate_a_board.yml +191 -0
  96. data/spec/cassettes/can_success_upate_a_list.yml +296 -0
  97. data/spec/cassettes/can_success_update_bong_a_board.yml +39 -39
  98. data/spec/cassettes/can_success_update_bong_a_board_with_specific_fields.yml +284 -0
  99. data/spec/cassettes/can_success_update_bong_a_checklist_with_specific_fields.yml +296 -0
  100. data/spec/cassettes/can_success_update_bong_a_custom_field.yml +200 -0
  101. data/spec/cassettes/can_success_update_bong_a_label.yml +200 -0
  102. data/spec/cassettes/can_success_update_bong_a_list.yml +199 -0
  103. data/spec/cassettes/can_success_update_bong_a_list_with_specific_fields.yml +296 -0
  104. data/spec/cassettes/can_success_update_bong_a_organization.yml +203 -0
  105. data/spec/cassettes/can_success_update_bong_a_webhook.yml +296 -0
  106. data/spec/cassettes/checklist_can_add_an_item.yml +296 -0
  107. data/spec/cassettes/checklist_can_update_the_item_state.yml +296 -0
  108. data/spec/cassettes/checklist_find_with_id_and_get_all_fields.yml +100 -0
  109. data/spec/cassettes/comment_delete.yml +331 -0
  110. data/spec/cassettes/comment_find_with_id.yml +116 -0
  111. data/spec/cassettes/custom_field_find_with_id_and_get_all_fields.yml +100 -0
  112. data/spec/cassettes/custom_field_find_with_id_and_get_specific_fields.yml +100 -0
  113. data/spec/cassettes/custom_field_item_save_1.yml +1 -1
  114. data/spec/cassettes/custom_field_item_save_2.yml +1 -1
  115. data/spec/cassettes/custom_field_option_find_with_id.yml +100 -0
  116. data/spec/cassettes/get_board_of_list.yml +197 -0
  117. data/spec/cassettes/get_boards_of_member.yml +207 -0
  118. data/spec/cassettes/label_delete.yml +285 -0
  119. data/spec/cassettes/label_find_with_id.yml +100 -0
  120. data/spec/cassettes/list_find_with_id_and_get_all_fields.yml +100 -0
  121. data/spec/cassettes/list_find_with_id_and_get_specific_fields.yml +100 -0
  122. data/spec/cassettes/member_find_with_id_and_get_all_fields.yml +101 -0
  123. data/spec/cassettes/member_find_with_id_and_get_specific_fields.yml +100 -0
  124. data/spec/cassettes/notification_find_with_id.yml +103 -0
  125. data/spec/cassettes/organization_find_with_id_and_get_all_fields.yml +102 -0
  126. data/spec/cassettes/organization_find_with_id_and_get_specific_fields.yml +100 -0
  127. data/spec/cassettes/token_find_with_token_string.yml +100 -0
  128. data/spec/cassettes/webhook_delete.yml +286 -0
  129. data/spec/cassettes/webhook_find_with_id.yml +100 -0
  130. data/spec/checklist_spec.rb +27 -43
  131. data/spec/client_spec.rb +57 -3
  132. data/spec/configuration_spec.rb +13 -0
  133. data/spec/custom_field_item_spec.rb +2 -2
  134. data/spec/custom_field_option_spec.rb +2 -2
  135. data/spec/custom_field_spec.rb +25 -13
  136. data/spec/integration/action/find_spec.rb +23 -0
  137. data/spec/integration/basic_data/many_spec.rb +4 -0
  138. data/spec/integration/basic_data/schema_spec.rb +156 -0
  139. data/spec/integration/board/add_member_spec.rb +20 -0
  140. data/spec/integration/board/all_spec.rb +20 -0
  141. data/spec/integration/board/associations/actions_spec.rb +18 -0
  142. data/spec/integration/board/associations/cards_spec.rb +18 -0
  143. data/spec/integration/board/associations/checklists_spec.rb +18 -0
  144. data/spec/integration/board/associations/custom_fields_spec.rb +18 -0
  145. data/spec/integration/board/associations/label_names_spec.rb +17 -0
  146. data/spec/integration/board/associations/labels_spec.rb +18 -0
  147. data/spec/integration/board/associations/lists_spec.rb +18 -0
  148. data/spec/integration/board/associations/members_spec.rb +18 -0
  149. data/spec/integration/board/associations/organization_spec.rb +19 -0
  150. data/spec/integration/board/associations/plugin_data_spec.rb +18 -0
  151. data/spec/integration/board/create_spec.rb +95 -0
  152. data/spec/integration/board/find_card_spec.rb +18 -0
  153. data/spec/integration/board/find_spec.rb +42 -0
  154. data/spec/integration/board/remove_member_spec.rb +21 -0
  155. data/spec/integration/board/save_spec.rb +37 -0
  156. data/spec/integration/board/update!_spec.rb +33 -2
  157. data/spec/integration/card/associations/plugin_data_spec.rb +1 -1
  158. data/spec/integration/card/create_spec.rb +36 -2
  159. data/spec/integration/card/delete_spec.rb +2 -2
  160. data/spec/integration/card/find_spec.rb +1 -1
  161. data/spec/integration/card/save_spec.rb +2 -2
  162. data/spec/integration/checklist/add_item_spec.rb +20 -0
  163. data/spec/integration/checklist/create_spec.rb +23 -0
  164. data/spec/integration/checklist/delete_spec.rb +17 -0
  165. data/spec/integration/checklist/find_spec.rb +22 -0
  166. data/spec/integration/checklist/update!_spec.rb +21 -0
  167. data/spec/integration/checklist/update_item_state_spec.rb +20 -0
  168. data/spec/integration/comment/association/board_spec.rb +17 -0
  169. data/spec/integration/comment/association/card_spec.rb +17 -0
  170. data/spec/integration/comment/association/list_spec.rb +17 -0
  171. data/spec/integration/comment/delete_spec.rb +20 -0
  172. data/spec/integration/comment/find_spec.rb +17 -0
  173. data/spec/integration/custom_field/association/board_spec.rb +18 -0
  174. data/spec/integration/custom_field/association/custom_field_options_spec.rb +18 -0
  175. data/spec/integration/custom_field/create_spec.rb +29 -0
  176. data/spec/integration/custom_field/find_spec.rb +41 -0
  177. data/spec/integration/custom_field/update!_spec.rb +25 -0
  178. data/spec/integration/custom_field_option/create_spec.rb +23 -0
  179. data/spec/integration/custom_field_option/delete_spec.rb +19 -0
  180. data/spec/integration/custom_field_option/find_spec.rb +21 -0
  181. data/spec/integration/integration_test.rb +3 -3
  182. data/spec/integration/label/association/board_spec.rb +18 -0
  183. data/spec/integration/label/create_spec.rb +23 -0
  184. data/spec/integration/label/delete_spec.rb +21 -0
  185. data/spec/integration/label/find_spec.rb +20 -0
  186. data/spec/integration/label/update!_spec.rb +25 -0
  187. data/spec/integration/list/archive_all_cards_spec.rb +20 -0
  188. data/spec/integration/list/associations/actions_spec.rb +18 -0
  189. data/spec/integration/list/associations/board_spec.rb +18 -0
  190. data/spec/integration/list/associations/cards_spec.rb +18 -0
  191. data/spec/integration/list/create_spec.rb +44 -0
  192. data/spec/integration/list/find_spec.rb +37 -0
  193. data/spec/integration/list/move_all_cards_spec.rb +31 -0
  194. data/spec/integration/list/save_spec.rb +40 -0
  195. data/spec/integration/list/update!_spec.rb +43 -0
  196. data/spec/integration/member/associations/actions_spec.rb +17 -0
  197. data/spec/integration/member/associations/boards_spec.rb +18 -0
  198. data/spec/integration/member/associations/cards_spec.rb +18 -0
  199. data/spec/integration/member/associations/notifications_spec.rb +18 -0
  200. data/spec/integration/member/associations/organizations_spec.rb +18 -0
  201. data/spec/integration/member/find_spec.rb +39 -0
  202. data/spec/integration/member/update!_spec.rb +18 -0
  203. data/spec/integration/notification/find_spec.rb +27 -0
  204. data/spec/integration/organization/associations/actions_spec.rb +18 -0
  205. data/spec/integration/organization/associations/boards_spec.rb +18 -0
  206. data/spec/integration/organization/associations/members_spec.rb +18 -0
  207. data/spec/integration/organization/create_spec.rb +23 -0
  208. data/spec/integration/organization/find_spec.rb +37 -0
  209. data/spec/integration/organization/update!_spec.rb +28 -0
  210. data/spec/integration/token/find_spec.rb +24 -0
  211. data/spec/integration/webhook/create_spec.rb +27 -0
  212. data/spec/integration/webhook/delete_spec.rb +21 -0
  213. data/spec/integration/webhook/find_spec.rb +22 -0
  214. data/spec/integration/webhook/update!_spec.rb +24 -0
  215. data/spec/item_spec.rb +2 -2
  216. data/spec/label_spec.rb +31 -33
  217. data/spec/list_spec.rb +32 -14
  218. data/spec/notification_spec.rb +3 -3
  219. data/spec/organization_spec.rb +6 -5
  220. data/spec/spec_helper.rb +31 -14
  221. data/spec/token_spec.rb +10 -11
  222. data/spec/unit/trello/basic_data_spec.rb +67 -8
  223. data/spec/unit/trello/board/new_spec.rb +370 -0
  224. data/spec/unit/trello/board/save_spec.rb +49 -0
  225. data/spec/unit/trello/board/update_fields_spec.rb +129 -0
  226. data/spec/unit/trello/board/update_spec.rb +197 -0
  227. data/spec/unit/trello/member/new_spec.rb +62 -0
  228. data/spec/unit/trello/member/update_fields_spec.rb +80 -0
  229. data/spec/unit/trello/schema/attribute/base_spec.rb +349 -0
  230. data/spec/unit/trello/schema/attribute/board_pref_spec.rb +208 -0
  231. data/spec/unit/trello/schema/attribute/default_spec.rb +236 -0
  232. data/spec/unit/trello/schema/attribute_builder_spec.rb +55 -0
  233. data/spec/unit/trello/schema/attribute_registration_spec.rb +59 -0
  234. data/spec/unit/trello/schema/serializer/default_spec.rb +19 -0
  235. data/spec/unit/trello/schema/serializer/time_spec.rb +39 -0
  236. data/spec/unit/trello/schema_spec.rb +25 -0
  237. data/spec/webhook_spec.rb +23 -7
  238. metadata +444 -102
  239. data/lib/trello/register_attributes.rb +0 -54
  240. data/spec/integration/basic_data/register_attributes_spec.rb +0 -75
@@ -1,53 +1,54 @@
1
1
  module Trello
2
2
  # A Custom Field can be activated on a board. Values are stored at the card level.
3
3
  #
4
- # @!attribute id
4
+ # @!attribute [r] id
5
5
  # @return [String]
6
- # @!attribute model_id
6
+ # @!attribute [rw] model_id
7
7
  # @return [String]
8
- # @!attribute model_type
8
+ # @!attribute [rw] model_type
9
9
  # @return [String]
10
- # @!attribute field_group
10
+ # @!attribute [r] field_group
11
11
  # @return [String]
12
- # @!attribute name
12
+ # @!attribute [rw] name
13
13
  # @return [String]
14
- # @!attribute pos
14
+ # @!attribute [rw] position
15
15
  # @return [Float]
16
- # @!attribute type
16
+ # @!attribute [rw] type
17
17
  # @return [String]
18
- # @!attribute options
18
+ # @!attribute [rw] checkbox_options
19
19
  # @return [Array<Hash>]
20
+ # @!attribute [w] enable_display_on_card
21
+ # @return [Boolean]
20
22
  class CustomField < BasicData
21
- register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
22
- validates_presence_of :id, :model_id, :model_type, :name, :type, :pos
23
23
 
24
- SYMBOL_TO_STRING = {
25
- id: 'id',
26
- name: 'name',
27
- model_id: 'idModel',
28
- model_type: 'modelType',
29
- field_group: 'fieldGroup',
30
- type: 'type',
31
- pos: 'pos'
32
- }
24
+ schema do
25
+ # Readonly
26
+ attribute :id, readonly: true, primary_key: true
27
+ attribute :field_group, readonly: true, remote_key: 'fieldGroup'
28
+
29
+ # Writable
30
+ attribute :name
31
+ attribute :position, remote_key: 'pos'
32
+ attribute :enable_display_on_card, remote_key: 'cardFront', class_name: 'CustomFieldDisplay'
33
+
34
+ # Writable but for create only
35
+ attribute :model_id, remote_key: 'idModel', create_only: true
36
+ attribute :model_type, remote_key: 'modelType', create_only: true
37
+ attribute :type, create_only: true
38
+ attribute :checkbox_options, remote_key: 'options', create_only: true
39
+ end
40
+
41
+ validates_presence_of :id, :model_id, :model_type, :name, :type, :position
33
42
 
34
43
  class << self
35
44
  # Find a custom field by its id.
36
45
  def find(id, params = {})
37
46
  client.find('customFields', id, params)
38
47
  end
48
+ end
39
49
 
40
- # Create a new custom field and save it on Trello.
41
- def create(options)
42
- client.create('customFields',
43
- 'name' => options[:name],
44
- 'idModel' => options[:model_id],
45
- 'modelType' => options[:model_type],
46
- 'fieldGroup' => options[:field_group],
47
- 'type' => options[:type],
48
- 'pos' => options[:pos]
49
- )
50
- end
50
+ def collection_name
51
+ 'customFields'
51
52
  end
52
53
 
53
54
  # References Board where this custom field is located
@@ -57,43 +58,6 @@ module Trello
57
58
  # If type == 'list'
58
59
  many :custom_field_options, path: 'options'
59
60
 
60
- def update_fields(fields)
61
- send('name_will_change!') if fields_has_key?(fields, :name)
62
- send('pos_will_change!') if fields_has_key?(fields, :pos)
63
-
64
- initialize_fields(fields)
65
- end
66
-
67
- def initialize(fields = {})
68
- initialize_fields(fields)
69
- end
70
-
71
- # Saves a record.
72
- def save
73
- # If we have an id, just update our fields.
74
- return update! if id
75
-
76
- from_response client.post("/customFields", {
77
- name: name,
78
- idModel: model_id,
79
- modelType: model_type,
80
- type: type,
81
- pos: pos,
82
- fieldGroup: field_group
83
- })
84
- end
85
-
86
- # Update an existing custom field.
87
- def update!
88
- @previously_changed = changes
89
- # extract only new values to build payload
90
- payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
91
- @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
92
- changes_applied if respond_to?(:changes_applied)
93
-
94
- client.put("/customFields/#{id}", payload)
95
- end
96
-
97
61
  # Delete this custom field
98
62
  # Also deletes all associated values across all cards
99
63
  def delete
@@ -110,22 +74,5 @@ module Trello
110
74
  def delete_option(option_id)
111
75
  client.delete("/customFields/#{id}/options/#{option_id}")
112
76
  end
113
-
114
- private
115
-
116
- def fields_has_key?(fields, key)
117
- fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
118
- end
119
-
120
- def initialize_fields(fields)
121
- attributes[:id] = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
122
- attributes[:name] = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
123
- attributes[:model_id] = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
124
- attributes[:model_type] = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
125
- attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
126
- attributes[:type] = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
127
- attributes[:pos] = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
128
- self
129
- end
130
77
  end
131
78
  end
@@ -1,9 +1,30 @@
1
1
  module Trello
2
2
  # A custom field item contains the value for a custom field on a particular card.
3
3
  #
4
+ # @!attribute [r] id
5
+ # @return [String]
6
+ # @!attribute [r] model_id
7
+ # @return [String]
8
+ # @!attribute [r] model_type
9
+ # @return [String]
10
+ # @!attribute [r] custom_field_id
11
+ # @return [String]
12
+ # @!attribute [r] option_id
13
+ # @return [String]
14
+ # @!attribute [rw] value
15
+ # @return [Hash]
4
16
  class CustomFieldItem < BasicData
5
- register_attributes :id, :model_id, :model_type, :custom_field_id, :value, :option_id,
6
- readonly: [ :id, :custom_field_id, :model_id, :model_type, :option_id ]
17
+
18
+ schema do
19
+ attribute :id, readonly: true, primary_key: true
20
+ attribute :model_id, readonly: true, remote_key: 'idModel'
21
+ attribute :model_type, readonly: true, remote_key: 'modelType'
22
+ attribute :custom_field_id, readonly: true, remote_key: 'idCustomField'
23
+ attribute :option_id, readonly: true, remote_key: 'idValue'
24
+
25
+ attribute :value
26
+ end
27
+
7
28
  validates_presence_of :id, :model_id, :custom_field_id
8
29
 
9
30
  # References the card with this custom field value
@@ -12,57 +33,33 @@ module Trello
12
33
  # References the parent custom field that this item is an instance of
13
34
  one :custom_field, path: 'customFields', using: :custom_field_id
14
35
 
15
- # Update the fields of a custom field item.
16
- #
17
- # Supply a hash of string keyed data retrieved from the Trello API representing
18
- # an item state.
19
- def update_fields(fields)
20
- if fields_has_key?(fields, 'value')
21
- send('value_will_change!')
22
- elsif fields_has_key?(fields, 'idValue')
23
- send('option_id_will_change!')
24
- end
25
-
26
- initialize_fields(fields)
27
- end
36
+ def save
37
+ return update! if id
28
38
 
29
- def initialize(fields = {})
30
- initialize_fields(fields)
31
- end
39
+ payload = {}
32
40
 
33
- def update!
34
- @previously_changed = changes
35
- # extract only new values to build payload
36
- payload = Hash[changes.map { |key, values| [key.to_sym, values[1]] }]
37
- @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
38
- changes_applied if respond_to?(:changes_applied)
41
+ schema.attrs.each do |_, attribute|
42
+ payload = attribute.build_payload_for_create(attributes, payload)
43
+ end
39
44
 
40
- client.put("/card/#{model_id}/customField/#{custom_field_id}/item", payload)
45
+ put(element_path, payload)
41
46
  end
42
47
 
43
- # Saves a record.
44
- #
45
- # @raise [Trello::Error] if the card could not be saved
46
- #
47
- # @return [String] The JSON representation of the saved custom field item returned by
48
- # the Trello API.
49
- def save
50
- # If we have an id, just update our fields.
51
- return update! if id
48
+ def collection_path
49
+ "/cards/#{model_id}/#{collection_name}"
50
+ end
52
51
 
53
- from_response client.put("/card/#{model_id}/customField/#{custom_field_id}/item", {
54
- value: value
55
- })
52
+ def element_path
53
+ "/cards/#{model_id}/customField/#{custom_field_id}/item"
56
54
  end
57
55
 
58
56
  # You can't "delete" a custom field item, you can only clear the value
59
57
  def remove
60
58
  params = { value: {} }
61
- client.put("/card/#{model_id}/customField/#{custom_field_id}/item", params)
59
+ client.put(element_path, params)
62
60
  end
63
61
 
64
62
  # Type is saved at the CustomField level
65
- # Can equally be derived from :value, with a little parsing work: {"number": 42}
66
63
  def type
67
64
  custom_field.type
68
65
  end
@@ -75,24 +72,5 @@ module Trello
75
72
  option.value
76
73
  end
77
74
  end
78
-
79
- private
80
-
81
- def fields_has_key?(fields, key)
82
- fields.key?(key.to_s) || fields.key?(key.to_sym)
83
- end
84
-
85
- def initialize_fields(fields)
86
- attributes[:id] = fields['id'] || fields[:id] || attributes[:id]
87
- attributes[:model_id] = fields['idModel'] || fields[:model_id] || attributes[:model_id]
88
- attributes[:custom_field_id] = fields['idCustomField'] || fields[:custom_field_id] || attributes[:custom_field_id]
89
- attributes[:model_type] = fields['modelType'] || fields[:model_type] || attributes[:model_type]
90
- # Dropdown custom field items do not have a value, they have an ID that maps to
91
- # a different endpoint to get the value
92
- attributes[:option_id] = fields['idValue'] || fields[:idValue] if fields_has_key?(fields, 'idValue')
93
- # value format: { "text": "hello world" }
94
- attributes[:value] = fields['value'] || fields[:value] if fields_has_key?(fields, 'value')
95
- self
96
- end
97
75
  end
98
76
  end
@@ -1,22 +1,49 @@
1
1
  module Trello
2
2
  # A custom field option contains the individual items in a custom field dropdown menu.
3
3
  #
4
+ # @!attribute [r] id
5
+ # @return [String]
6
+ # @!attribute [r] custom_field_id
7
+ # @return [String]
8
+ # @!attribute [rw] value
9
+ # @return [Hash]
10
+ # @!attribute [rw] color
11
+ # @return [String]
12
+ # @!attribute [rw] position
13
+ # @return [Float]
4
14
  class CustomFieldOption < BasicData
5
- register_attributes :id, :value, :pos, :color,
6
- readonly: [:id]
15
+
16
+ schema do
17
+ # Readonly
18
+ attribute :id, remote_key: '_id', readonly: true, primary_key: true
19
+ attribute :custom_field_id, remote_key: 'idCustomField', readonly: true
20
+
21
+ # Writable but for create only
22
+ attribute :value, create_only: true
23
+ attribute :color, create_only: true
24
+ attribute :position, remote_key: 'pos', create_only: true
25
+ end
26
+
7
27
  validates_presence_of :id, :value
8
28
 
9
- # Update the fields of a custom field option.
10
- #
11
- # Supply a hash of string keyed data retrieved from the Trello API representing
12
- # an item state.
13
- def update_fields(fields)
14
- attributes[:id] = fields['_id'] || fields[:id] || attributes[:id]
15
- attributes[:color] = fields['color'] || fields[:color] || attributes[:color]
16
- attributes[:pos] = fields['pos'] || fields[:pos] || attributes[:pos]
17
- # value format: { "text": "hello world" }
18
- attributes[:value] = fields['value'] || fields[:value] || attributes[:value]
19
- self
29
+ def self.find(id, params = {})
30
+ params = params.with_indifferent_access
31
+ custom_field_id = params.delete(:custom_field_id)
32
+ return if custom_field_id.nil?
33
+
34
+ from_response client.get("/customFields/#{custom_field_id}/options/#{id}")
35
+ end
36
+
37
+ def delete
38
+ client.delete(element_path)
39
+ end
40
+
41
+ def collection_path
42
+ "/customFields/#{custom_field_id}/options"
43
+ end
44
+
45
+ def element_path
46
+ "/customFields/#{custom_field_id}/options/#{id}"
20
47
  end
21
48
  end
22
49
  end
data/lib/trello/item.rb CHANGED
@@ -5,31 +5,42 @@ module Trello
5
5
  # @return [String]
6
6
  # @!attribute [r] name
7
7
  # @return [String]
8
+ # @!attribute [r] name_data
9
+ # @return [String]
8
10
  # @!attribute [r] type
9
11
  # @return [Object]
10
12
  # @!attribute [r] state
11
13
  # @return [Object]
12
- # @!attribute [r] pos
14
+ # @!attribute [r] position
13
15
  # @return [Object]
16
+ # @!attribute [r] due_date
17
+ # @return [Datetime]
18
+ # @!attribute [r] member_id
19
+ # @return [String]
20
+ # @!attribute [r] checklist_id
21
+ # @return [String]
22
+ # @!attribute [w] checked
23
+ # @return [Boolean]
14
24
  class Item < BasicData
15
- register_attributes :id, :name, :type, :state, :pos, readonly: [ :id, :name, :type, :state, :pos ]
16
- validates_presence_of :id, :type
17
25
 
18
- # Updates the fields of an item.
19
- #
20
- # Supply a hash of string keyed data retrieved from the Trello API representing
21
- # an item.
22
- def update_fields(fields)
23
- attributes[:id] = fields['id'] || attributes[:id]
24
- attributes[:card_id] = fields['idCard'] || attributes[:card_id]
25
- attributes[:checklist_id] = fields['idChecklist'] || attributes[:checklist_id]
26
- attributes[:name] = fields['name'] || attributes[:name]
27
- attributes[:type] = fields['type'] || attributes[:type]
28
- attributes[:state] = fields['state'] || attributes[:state]
29
- attributes[:pos] = fields['pos'] || attributes[:pos]
30
- self
26
+ schema do
27
+ # Readonly
28
+ attribute :id, readonly: true, primary_key: true
29
+ attribute :type, readonly: true
30
+ attribute :name_data, readonly: true, remote_key: 'nameData'
31
+ attribute :due_date, readonly: true, remote_key: 'due', serializer: 'Time'
32
+ attribute :member_id, readonly: true, remote_key: 'idMember'
33
+ attribute :state, readonly: true
34
+ attribute :checklist_id, readonly: true, remote_key: 'idChecklist'
35
+
36
+ # Writable and write only
37
+ attribute :name, write_only: true
38
+ attribute :position, write_only: true, remote_key: 'pos'
39
+ attribute :checked, write_only: true
31
40
  end
32
41
 
42
+ validates_presence_of :id
43
+
33
44
  def complete?
34
45
  state == "complete"
35
46
  end
@@ -5,26 +5,18 @@ module Trello
5
5
  # @return [String]
6
6
  # @!attribute [r] state
7
7
  # @return [Object]
8
- # @!attribute [r] item_id
9
- # @return [String]
10
8
  class CheckItemState < BasicData
11
- register_attributes :id, :state, :item_id, readonly: [ :id, :state, :item_id ]
12
- validates_presence_of :id, :item_id
13
-
14
- # Update the fields of an item state.
15
- #
16
- # Supply a hash of string keyed data retrieved from the Trello API representing
17
- # an item state.
18
- def update_fields(fields)
19
- attributes[:id] = fields['id'] || attributes[:id]
20
- attributes[:state] = fields['state'] || attributes[:state]
21
- attributes[:item_id] = fields['idCheckItem'] || attributes[:item_id]
22
- self
9
+ schema do
10
+ #Readonly
11
+ attribute :id, remote_key: 'idCheckItem', readonly: true, primary_key: true
12
+ attribute :state
23
13
  end
24
14
 
15
+ validates_presence_of :id
16
+
25
17
  # Return the item this state belongs to.
26
18
  def item
27
- Item.find(item_id)
19
+ Item.find(id)
28
20
  end
29
21
  end
30
22
  end
@@ -7,7 +7,7 @@ module Trello
7
7
 
8
8
  module InstanceMethods
9
9
  def from_response(*args)
10
- update_fields parse_json(*args)
10
+ initialize_fields parse_json(*args)
11
11
  end
12
12
 
13
13
  def parse_json(*args)
@@ -32,8 +32,8 @@ module Trello
32
32
  # thing.a == 42
33
33
  # thing.b == "foo"
34
34
  #
35
- def from_response(data, encoding = 'UTF-8')
36
- from_json(parse_json(data, encoding))
35
+ def from_response(response, encoding = 'UTF-8')
36
+ from_json(parse_json(response, encoding))
37
37
  end
38
38
 
39
39
  def from_json(json)
@@ -47,8 +47,14 @@ module Trello
47
47
  end
48
48
  end
49
49
 
50
- def parse_json(string, encoding = 'UTF-8')
51
- JSON.parse(string.force_encoding(encoding))
50
+ def parse_json(data, encoding = 'UTF-8')
51
+ # Trello.http_client.parse_json(data, encoding)
52
+ case data
53
+ when Trello::Response
54
+ JSON.parse(data.body.force_encoding(encoding))
55
+ else
56
+ JSON.parse(data.force_encoding(encoding))
57
+ end
52
58
  rescue JSON::ParserError => json_error
53
59
  if json_error.message =~ /model not found/
54
60
  Trello.logger.error "Could not find that record."
data/lib/trello/label.rb CHANGED
@@ -6,121 +6,34 @@ module Trello
6
6
  # @return [String]
7
7
  # @!attribute [rw] color
8
8
  # @return [String]
9
+ # @!attribute [rw] name
10
+ # @return [String]
11
+ # @!attribute [rw] board_id
12
+ # @return [String]
9
13
  class Label < BasicData
10
- register_attributes :id, :name, :board_id, :uses,
11
- readonly: [ :id, :uses, :board_id ]
12
- validates_presence_of :id, :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
- VALID_LABEL_COLOURS = %w{green yellow orange red purple blue sky lime pink black} << ''
25
-
26
- # Find a specific label by its id.
27
- def find(id, params = {})
28
- client.find(:label, id, params)
29
- end
30
-
31
- # Create a new label and save it on Trello.
32
- def create(options)
33
- client.create(:label,
34
- 'name' => options[:name],
35
- 'idBoard' => options[:board_id],
36
- 'color' => options[:color],
37
- )
38
- end
39
-
40
- # Label colours
41
- def label_colours
42
- VALID_LABEL_COLOURS
43
- end
44
- end
45
-
46
- define_attribute_methods [:color]
47
-
48
- def color
49
- @__attributes[:color]
50
- end
51
-
52
- def color= colour
53
- unless Label.label_colours.include? colour
54
- errors.add(:label, "color '#{colour}' does not exist")
55
- return Trello.logger.warn "The label colour '#{colour}' does not exist."
56
- end
57
14
 
58
- self.send(:"color_will_change!") unless colour == @__attributes[:color]
59
- @__attributes[:color] = colour
60
- end
15
+ schema do
16
+ # Readonly
17
+ attribute :id, readonly: true, primary_key: true
61
18
 
62
- # Update the fields of a label.
63
- #
64
- # Supply a hash of stringkeyed data retrieved from the Trello API representing
65
- # a label.
66
- def update_fields(fields)
67
- send('name_will_change!') if fields_has_key?(fields, :name)
68
- send('color_will_change!') if fields_has_key?(fields, :color)
19
+ # Writable
20
+ attribute :name
21
+ attribute :color
69
22
 
70
- initialize_fields(fields)
23
+ # Writable but for create only
24
+ attribute :board_id, create_only: true, remote_key: 'idBoard'
71
25
  end
72
26
 
73
- def initialize(fields = {})
74
- initialize_fields(fields)
75
- end
27
+ validates_presence_of :id, :board_id, :name
28
+ validates_length_of :name, in: 1..16384
76
29
 
77
30
  # Returns a reference to the board this label is currently connected.
78
31
  one :board, path: :boards, using: :board_id
79
32
 
80
- # Saves a record.
81
- def save
82
- # If we have an id, just update our fields.
83
- return update! if id
84
-
85
- from_response client.post("/labels", {
86
- name: name,
87
- color: color,
88
- idBoard: board_id,
89
- })
90
- end
91
-
92
- # Update an existing record.
93
- # Warning, this updates all fields using values already in memory. If
94
- # an external resource has updated these fields, you should refresh!
95
- # this object before making your changes, and before updating the record.
96
- def update!
97
- @previously_changed = changes
98
- # extract only new values to build payload
99
- payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
100
- @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
101
- changes_applied if respond_to?(:changes_applied)
102
-
103
- client.put("/labels/#{id}", payload)
104
- end
105
-
106
33
  # Delete this label
107
34
  def delete
108
35
  client.delete("/labels/#{id}")
109
36
  end
110
37
 
111
- private
112
-
113
- def fields_has_key?(fields, key)
114
- fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
115
- end
116
-
117
- def initialize_fields(fields)
118
- attributes[:id] = fields['id'] || attributes[:id]
119
- attributes[:name] = fields['name'] || fields[:name] || attributes[:name]
120
- attributes[:color] = fields['color'] || fields[:color] || attributes[:color]
121
- attributes[:board_id] = fields['idBoard'] || fields[:board_id] || attributes[:board_id]
122
- attributes[:uses] = fields['uses'] if fields.has_key?('uses')
123
- self
124
- end
125
38
  end
126
39
  end
@@ -1,31 +1,28 @@
1
1
  module Trello
2
-
3
2
  # A colored Label attached to a card
3
+ #
4
+ # @!attribute [r] yellow
5
+ # @!attribute [r] red
6
+ # @!attribute [r] orange
7
+ # @!attribute [r] green
8
+ # @!attribute [r] purple
9
+ # @!attribute [r] blue
10
+ # @!attribute [r] sky
11
+ # @!attribute [r] pink
12
+ # @!attribute [r] lime
13
+ # @!attribute [r] black
4
14
  class LabelName < BasicData
5
- register_attributes :yellow, :red, :orange, :green, :purple, :blue, :sky, :pink, :lime, :black
6
-
7
- # Update the fields of a label.
8
- #
9
- # Supply a hash of stringkeyed data retrieved from the Trello API representing
10
- # a label.
11
- def update_fields(fields)
12
- attributes[:yellow] = fields['yellow'] || attributes[:yellow]
13
- attributes[:red] = fields['red'] || attributes[:red]
14
- attributes[:orange] = fields['orange'] || attributes[:orange]
15
- attributes[:green] = fields['green'] || attributes[:green]
16
- attributes[:purple] = fields['purple'] || attributes[:purple]
17
- attributes[:blue] = fields['blue'] || attributes[:blue]
18
- attributes[:sky] = fields['sky'] || attributes[:sky]
19
- attributes[:pink] = fields['pink'] || attributes[:pink]
20
- attributes[:lime] = fields['lime'] || attributes[:lime]
21
- attributes[:black] = fields['black'] || attributes[:black]
22
-
23
- self
15
+ schema do
16
+ attribute :yellow, readonly: true
17
+ attribute :red, readonly: true
18
+ attribute :orange, readonly: true
19
+ attribute :green, readonly: true
20
+ attribute :purple, readonly: true
21
+ attribute :blue, readonly: true
22
+ attribute :sky, readonly: true
23
+ attribute :pink, readonly: true
24
+ attribute :lime, readonly: true
25
+ attribute :black, readonly: true
24
26
  end
25
-
26
- one :board, path: :boards, using: :board_id
27
-
28
- many :cards, filter: :all
29
-
30
27
  end
31
28
  end