ruby-trello-czuger 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +182 -0
  3. data/lib/trello.rb +163 -0
  4. data/lib/trello/action.rb +68 -0
  5. data/lib/trello/association.rb +14 -0
  6. data/lib/trello/association_proxy.rb +42 -0
  7. data/lib/trello/attachment.rb +40 -0
  8. data/lib/trello/authorization.rb +187 -0
  9. data/lib/trello/basic_data.rb +132 -0
  10. data/lib/trello/board.rb +211 -0
  11. data/lib/trello/card.rb +467 -0
  12. data/lib/trello/checklist.rb +143 -0
  13. data/lib/trello/client.rb +120 -0
  14. data/lib/trello/comment.rb +62 -0
  15. data/lib/trello/configuration.rb +68 -0
  16. data/lib/trello/core_ext/array.rb +6 -0
  17. data/lib/trello/core_ext/hash.rb +6 -0
  18. data/lib/trello/core_ext/string.rb +6 -0
  19. data/lib/trello/cover_image.rb +8 -0
  20. data/lib/trello/has_actions.rb +9 -0
  21. data/lib/trello/item.rb +37 -0
  22. data/lib/trello/item_state.rb +30 -0
  23. data/lib/trello/json_utils.rb +64 -0
  24. data/lib/trello/label.rb +108 -0
  25. data/lib/trello/label_name.rb +31 -0
  26. data/lib/trello/list.rb +114 -0
  27. data/lib/trello/member.rb +112 -0
  28. data/lib/trello/multi_association.rb +12 -0
  29. data/lib/trello/net.rb +39 -0
  30. data/lib/trello/notification.rb +61 -0
  31. data/lib/trello/organization.rb +68 -0
  32. data/lib/trello/plugin_datum.rb +34 -0
  33. data/lib/trello/token.rb +37 -0
  34. data/lib/trello/webhook.rb +103 -0
  35. data/spec/action_spec.rb +149 -0
  36. data/spec/array_spec.rb +13 -0
  37. data/spec/association_spec.rb +26 -0
  38. data/spec/basic_auth_policy_spec.rb +51 -0
  39. data/spec/board_spec.rb +442 -0
  40. data/spec/card_spec.rb +822 -0
  41. data/spec/checklist_spec.rb +296 -0
  42. data/spec/client_spec.rb +257 -0
  43. data/spec/configuration_spec.rb +95 -0
  44. data/spec/hash_spec.rb +15 -0
  45. data/spec/integration/how_to_authorize_spec.rb +53 -0
  46. data/spec/integration/how_to_use_boards_spec.rb +48 -0
  47. data/spec/integration/integration_test.rb +40 -0
  48. data/spec/item_spec.rb +75 -0
  49. data/spec/json_utils_spec.rb +73 -0
  50. data/spec/label_spec.rb +205 -0
  51. data/spec/list_spec.rb +253 -0
  52. data/spec/member_spec.rb +159 -0
  53. data/spec/notification_spec.rb +143 -0
  54. data/spec/oauth_policy_spec.rb +160 -0
  55. data/spec/organization_spec.rb +71 -0
  56. data/spec/spec_helper.rb +435 -0
  57. data/spec/string_spec.rb +55 -0
  58. data/spec/token_spec.rb +89 -0
  59. data/spec/trello_spec.rb +134 -0
  60. data/spec/webhook_spec.rb +130 -0
  61. metadata +200 -0
@@ -0,0 +1,143 @@
1
+ module Trello
2
+ # A Checklist holds items which are like a "task" list. Checklists are linked to a card.
3
+ #
4
+ # @!attribute [r] id
5
+ # @return [String]
6
+ # @!attribute [rw] name
7
+ # @return [String]
8
+ # @!attribute [r] description
9
+ # @return [String]
10
+ # @!attribute [r] closed
11
+ # @return [Boolean]
12
+ # @!attribute [rw] position
13
+ # @return [Object]
14
+ # @!attribute [r] url
15
+ # @return [String]
16
+ # @!attribute [r] check_items
17
+ # @return [Object]
18
+ # @!attribute [r] board_id
19
+ # @return [String] A 24-character hex string
20
+ # @!attribute [r] list_id
21
+ # @return [String] A 24-character hex string
22
+ # @!attribute [r] member_ids
23
+ # @return [Array<String>] An array of 24-character hex strings
24
+ class Checklist < BasicData
25
+ register_attributes :id, :name, :description, :closed, :position, :url, :check_items, :board_id, :list_id, :card_id, :member_ids,
26
+ readonly: [:id, :description, :closed, :url, :check_items, :board_id, :list_id, :card_id, :member_ids]
27
+ validates_presence_of :id, :board_id, :list_id
28
+ validates_length_of :name, in: 1..16384
29
+
30
+ class << self
31
+ # Locate a specific checklist by its id.
32
+ def find(id, params = {})
33
+ client.find(:checklist, id, params)
34
+ end
35
+
36
+ def create(options)
37
+ client.create(:checklist,
38
+ 'name' => options[:name],
39
+ 'idCard' => options[:card_id])
40
+ end
41
+ end
42
+
43
+ # Update the fields of a checklist.
44
+ #
45
+ # Supply a hash of string keyed data retrieved from the Trello API representing
46
+ # a checklist.
47
+ def update_fields(fields)
48
+ attributes[:id] = fields['id'] || attributes[:id]
49
+ attributes[:name] = fields['name'] || fields[:name] || attributes[:name]
50
+ attributes[:description] = fields['desc'] || attributes[:description]
51
+ attributes[:closed] = fields['closed'] if fields.has_key?('closed')
52
+ attributes[:url] = fields['url'] || attributes[:url]
53
+ attributes[:check_items] = fields['checkItems'] if fields.has_key?('checkItems')
54
+ attributes[:position] = fields['pos'] || attributes[:position]
55
+ attributes[:board_id] = fields['idBoard'] || attributes[:board_id]
56
+ attributes[:card_id] = fields['idCard'] || fields[:card_id] || attributes[:card_id]
57
+ attributes[:list_id] = fields['idList'] || attributes[:list_id]
58
+ attributes[:member_ids] = fields['idMembers'] || attributes[:member_ids]
59
+ self
60
+ end
61
+
62
+ # Check if the checklist is currently active.
63
+ def closed?
64
+ closed
65
+ end
66
+
67
+ # Save a record.
68
+ def save
69
+ return update! if id
70
+
71
+ from_response(client.post("/checklists", {
72
+ name: name,
73
+ idCard: card_id
74
+ }))
75
+ end
76
+
77
+ def update!
78
+ from_response(client.put("/checklists/#{id}", {name: name, pos: position}))
79
+ end
80
+
81
+ # Return a list of items on the checklist.
82
+ def items
83
+ check_items.map do |item_fields|
84
+ Item.new(item_fields)
85
+ end
86
+ end
87
+
88
+ # Return a reference to the board the checklist is on.
89
+ one :board, path: :checklists, using: :board_id
90
+
91
+ # Return a reference to the card the checklist is on.
92
+ one :card, path: :checklists, using: :card_id
93
+
94
+ # Return a reference to the list the checklist is on.
95
+ one :list, path: :lists, using: :list_id
96
+
97
+ # Return a list of members active in this checklist.
98
+ def members
99
+ members = member_ids.map do |member_id|
100
+ Member.find(member_id)
101
+ end
102
+ MultiAssociation.new(self, members).proxy
103
+ end
104
+
105
+ # Add an item to the checklist
106
+ def add_item(name, checked = false, position = 'bottom')
107
+ client.post("/checklists/#{id}/checkItems", {name: name, checked: checked, pos: position})
108
+ end
109
+
110
+ # Update a checklist item's state, e.g.: "complete" or "incomplete"
111
+ def update_item_state(item_id, state)
112
+ state = ( state ? 'complete' : 'incomplete' ) unless state.is_a?(String)
113
+ client.put(
114
+ "/cards/#{card_id}/checkItem/#{item_id}",
115
+ state: state
116
+ )
117
+ end
118
+
119
+ # Delete a checklist item
120
+ def delete_checklist_item(item_id)
121
+ client.delete("/checklists/#{id}/checkItems/#{item_id}")
122
+ end
123
+
124
+ # Delete a checklist
125
+ def delete
126
+ client.delete("/checklists/#{id}")
127
+ end
128
+
129
+ # Copy a checklist (i.e., same attributes, items, etc.)
130
+ def copy
131
+ checklist_copy = self.class.create(name: self.name, board_id: self.board_id, card_id: self.card_id)
132
+ copy_items_to(checklist_copy)
133
+ return checklist_copy
134
+ end
135
+
136
+ private
137
+ def copy_items_to(another_checklist)
138
+ items.each do |item|
139
+ another_checklist.add_item(item.name, item.complete?)
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,120 @@
1
+ require 'addressable/uri'
2
+ require 'forwardable'
3
+ require 'active_support/inflector'
4
+
5
+ module Trello
6
+ class Client
7
+ extend Forwardable
8
+ include Authorization
9
+
10
+ def_delegators :configuration, :credentials, *Configuration.configurable_attributes
11
+
12
+ def initialize(attrs = {})
13
+ self.configuration.attributes = attrs
14
+ end
15
+
16
+ def get(path, params = {})
17
+ uri = Addressable::URI.parse("https://api.trello.com/#{API_VERSION}#{path}")
18
+ uri.query_values = params unless params.empty?
19
+ invoke_verb(:get, uri)
20
+ end
21
+
22
+ def post(path, body = {})
23
+ uri = Addressable::URI.parse("https://api.trello.com/#{API_VERSION}#{path}")
24
+ invoke_verb(:post, uri, body)
25
+ end
26
+
27
+ def put(path, body = {})
28
+ uri = Addressable::URI.parse("https://api.trello.com/#{API_VERSION}#{path}")
29
+ invoke_verb(:put, uri, body)
30
+ end
31
+
32
+ def delete(path)
33
+ uri = Addressable::URI.parse("https://api.trello.com/#{API_VERSION}#{path}")
34
+ invoke_verb(:delete, uri)
35
+ end
36
+
37
+ # Finds given resource by id
38
+ #
39
+ # Examples:
40
+ # client.find(:board, "board1234")
41
+ # client.find(:member, "user1234")
42
+ #
43
+ def find(path, id, params = {})
44
+ response = get("/#{path.to_s.pluralize}/#{id}", params)
45
+ trello_class = class_from_path(path)
46
+ trello_class.parse response do |data|
47
+ data.client = self
48
+ end
49
+ end
50
+
51
+ # Finds given resource by path with params
52
+ def find_many(trello_class, path, params = {})
53
+ response = get(path, params)
54
+ trello_class.parse_many response do |data|
55
+ data.client = self
56
+ end
57
+ end
58
+
59
+ # Creates resource with given options (attributes)
60
+ #
61
+ # Examples:
62
+ # client.create(:member, options)
63
+ # client.create(:board, options)
64
+ #
65
+ def create(path, options)
66
+ trello_class = class_from_path(path)
67
+ trello_class.save options do |data|
68
+ data.client = self
69
+ end
70
+ end
71
+
72
+ def configure
73
+ yield configuration if block_given?
74
+ end
75
+
76
+ def configuration
77
+ @configuration ||= Configuration.new
78
+ end
79
+
80
+ def auth_policy
81
+ @auth_policy ||= auth_policy_class.new(credentials)
82
+ end
83
+
84
+ private
85
+
86
+ def invoke_verb(name, uri, body = nil)
87
+ request = Request.new name, uri, {}, body
88
+ response = TInternet.execute auth_policy.authorize(request)
89
+
90
+ return '' unless response
91
+
92
+ if response.code.to_i == 401 && response.body =~ /expired token/
93
+ Trello.logger.error("[401 #{name.to_s.upcase} #{uri}]: Your access token has expired.")
94
+ raise InvalidAccessToken, response.body
95
+ end
96
+
97
+ unless [200, 201].include? response.code
98
+ Trello.logger.error("[#{response.code} #{name.to_s.upcase} #{uri}]: #{response.body}")
99
+ raise Error, response.body
100
+ end
101
+
102
+ response.body
103
+ end
104
+
105
+ def auth_policy_class
106
+ if configuration.oauth?
107
+ OAuthPolicy
108
+ elsif configuration.basic?
109
+ BasicAuthPolicy
110
+ else
111
+ AuthPolicy
112
+ end
113
+ end
114
+
115
+ def class_from_path(path_or_class)
116
+ return path_or_class if path_or_class.is_a?(Class)
117
+ Trello.const_get(path_or_class.to_s.singularize.camelize)
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,62 @@
1
+ module Trello
2
+ # A Comment is a string with a creation date; it resides inside a Card and belongs to a User.
3
+ #
4
+ # @!attribute [r] action_id
5
+ # @return [String]
6
+ # @!attribute [r] text
7
+ # @return [String]
8
+ # @!attribute [r] date
9
+ # @return [Datetime]
10
+ # @!attribute [r] member_creator_id
11
+ # @return [String]
12
+ class Comment < BasicData
13
+ register_attributes :action_id, :text, :date, :member_creator_id,
14
+ readonly: [ :action_id, :text, :date, :member_creator_id ]
15
+ validates_presence_of :action_id, :text, :date, :member_creator_id
16
+ validates_length_of :text, in: 1..16384
17
+
18
+ class << self
19
+ # Locate a specific action and return a new Comment object.
20
+ def find(action_id)
21
+ client.find(:action, action_id, filter: commentCard)
22
+ end
23
+ end
24
+
25
+ # Update the attributes of a Comment
26
+ #
27
+ # Supply a hash of string keyed data retrieved from the Trello API representing
28
+ # a Comment.
29
+ def update_fields(fields)
30
+ attributes[:action_id] = fields['id'] || attributes[:action_id]
31
+ attributes[:text] = fields['data']['text'] || attributes[:text]
32
+ attributes[:date] = Time.iso8601(fields['date']) if fields.has_key?('date')
33
+ attributes[:member_creator_id] = fields['idMemberCreator'] || attributes[:member_creator_id]
34
+ self
35
+ end
36
+
37
+ # Returns the board this comment is located
38
+ def board
39
+ Board.from_response client.get("/actions/#{action_id}/board")
40
+ end
41
+
42
+ # Returns the card the comment is located
43
+ def card
44
+ Card.from_response client.get("/actions/#{action_id}/card")
45
+ end
46
+
47
+ # Returns the list the comment is located
48
+ def list
49
+ List.from_response client.get("/actions/#{action_id}/list")
50
+ end
51
+
52
+ # Deletes the comment from the card
53
+ def delete
54
+ ruta = "/actions/#{action_id}"
55
+ client.delete(ruta)
56
+ end
57
+
58
+
59
+ # Returns the member who created the comment.
60
+ one :member_creator, via: Member, path: :members, using: :member_creator_id
61
+ end
62
+ end
@@ -0,0 +1,68 @@
1
+ module Trello
2
+ class Configuration
3
+ CONFIGURABLE_ATTRIBUTES = [
4
+ :developer_public_key,
5
+ :member_token,
6
+ :consumer_key,
7
+ :consumer_secret,
8
+ :oauth_token,
9
+ :oauth_token_secret,
10
+ :callback,
11
+ :return_url
12
+ ]
13
+
14
+ attr_accessor *CONFIGURABLE_ATTRIBUTES
15
+
16
+ def self.configurable_attributes
17
+ CONFIGURABLE_ATTRIBUTES
18
+ end
19
+
20
+ def initialize(attrs = {})
21
+ self.attributes = attrs
22
+ end
23
+
24
+ def attributes=(attrs = {})
25
+ attrs.each { |key, value| instance_variable_set("@#{key}", value) }
26
+ end
27
+
28
+ def credentials
29
+ case
30
+ when oauth?
31
+ oauth_credentials
32
+ when basic?
33
+ basic_credentials
34
+ else
35
+ {}
36
+ end
37
+ end
38
+
39
+ def oauth?
40
+ consumer_key && consumer_secret
41
+ end
42
+
43
+ def basic?
44
+ developer_public_key && member_token
45
+ end
46
+
47
+ private
48
+
49
+ def oauth_credentials
50
+ {
51
+ consumer_key: consumer_key,
52
+ consumer_secret: consumer_secret,
53
+ oauth_token: oauth_token,
54
+ oauth_token_secret: oauth_token_secret,
55
+ return_url: return_url,
56
+ callback: callback,
57
+ }.delete_if { |key, value| value.nil? }
58
+ end
59
+
60
+ def basic_credentials
61
+ {
62
+ developer_public_key: developer_public_key,
63
+ member_token: member_token
64
+ }
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,6 @@
1
+ warn "Use of trello/core_ext/array is deprecated. Use Trello::JsonUtils instead"
2
+ class Array
3
+ def jsoned_into(obj)
4
+ obj.from_json(self)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ warn "Use of trello/core_ext/hash is deprecated. Use Trello::JsonUtils instead"
2
+ class Hash
3
+ def jsoned_into(obj)
4
+ obj.from_json(self)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ warn "Use of trello/core_ext/string is deprecated. Use Trello::JsonUtils instead"
2
+ class String
3
+ def json_into(obj, encoding = 'UTF-8')
4
+ obj.from_response(self, encoding)
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module Trello
2
+ # The trello cover image
3
+ #
4
+ # This is normally an attachment that the user (or trello) has set
5
+ # as the cover image
6
+ class CoverImage < Attachment
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module Trello
2
+ module HasActions
3
+ # Returns a list of the actions associated with this object.
4
+ def actions(options = {})
5
+ actions = Action.from_response client.get("#{request_prefix}/actions", { filter: :all }.merge(options))
6
+ MultiAssociation.new(self, actions).proxy
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ module Trello
2
+ # An Item is a basic task that can be checked off and marked as completed.
3
+ #
4
+ # @!attribute [r] id
5
+ # @return [String]
6
+ # @!attribute [r] name
7
+ # @return [String]
8
+ # @!attribute [r] type
9
+ # @return [Object]
10
+ # @!attribute [r] state
11
+ # @return [Object]
12
+ # @!attribute [r] pos
13
+ # @return [Object]
14
+ class Item < BasicData
15
+ register_attributes :id, :name, :type, :state, :pos, readonly: [ :id, :name, :type, :state, :pos ]
16
+ validates_presence_of :id, :type
17
+
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
31
+ end
32
+
33
+ def complete?
34
+ state == "complete"
35
+ end
36
+ end
37
+ end