trelloapi 0.1.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.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +12 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +5 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +11 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/.DS_Store +0 -0
  13. data/lib/trello.rb +163 -0
  14. data/lib/trello/.DS_Store +0 -0
  15. data/lib/trello/action.rb +68 -0
  16. data/lib/trello/association.rb +14 -0
  17. data/lib/trello/association_proxy.rb +42 -0
  18. data/lib/trello/attachment.rb +40 -0
  19. data/lib/trello/authorization.rb +187 -0
  20. data/lib/trello/basic_data.rb +132 -0
  21. data/lib/trello/board.rb +201 -0
  22. data/lib/trello/card.rb +456 -0
  23. data/lib/trello/checklist.rb +142 -0
  24. data/lib/trello/client.rb +120 -0
  25. data/lib/trello/comment.rb +62 -0
  26. data/lib/trello/configuration.rb +68 -0
  27. data/lib/trello/core_ext/array.rb +6 -0
  28. data/lib/trello/core_ext/hash.rb +6 -0
  29. data/lib/trello/core_ext/string.rb +6 -0
  30. data/lib/trello/cover_image.rb +8 -0
  31. data/lib/trello/has_actions.rb +9 -0
  32. data/lib/trello/item.rb +37 -0
  33. data/lib/trello/item_state.rb +30 -0
  34. data/lib/trello/json_utils.rb +64 -0
  35. data/lib/trello/label.rb +108 -0
  36. data/lib/trello/label_name.rb +31 -0
  37. data/lib/trello/list.rb +114 -0
  38. data/lib/trello/member.rb +112 -0
  39. data/lib/trello/multi_association.rb +12 -0
  40. data/lib/trello/net.rb +39 -0
  41. data/lib/trello/notification.rb +61 -0
  42. data/lib/trello/organization.rb +68 -0
  43. data/lib/trello/plugin_datum.rb +34 -0
  44. data/lib/trello/token.rb +36 -0
  45. data/lib/trello/webhook.rb +103 -0
  46. data/trello.gemspec +41 -0
  47. metadata +161 -0
@@ -0,0 +1,142 @@
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']
49
+ attributes[:name] = fields['name'] || fields[:name]
50
+ attributes[:description] = fields['desc']
51
+ attributes[:closed] = fields['closed']
52
+ attributes[:url] = fields['url']
53
+ attributes[:check_items] = fields['checkItems']
54
+ attributes[:position] = fields['pos']
55
+ attributes[:board_id] = fields['idBoard']
56
+ attributes[:card_id] = fields['idCard'] || fields[:card_id]
57
+ attributes[:list_id] = fields['idList']
58
+ attributes[:member_ids] = fields['idMembers']
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
+ client.put(
113
+ "/cards/#{card_id}/checklist/#{id}/checkItem/#{item_id}/state",
114
+ value: state,
115
+ )
116
+ end
117
+
118
+ # Delete a checklist item
119
+ def delete_checklist_item(item_id)
120
+ client.delete("/checklists/#{id}/checkItems/#{item_id}")
121
+ end
122
+
123
+ # Delete a checklist
124
+ def delete
125
+ client.delete("/checklists/#{id}")
126
+ end
127
+
128
+ # Copy a checklist (i.e., same attributes, items, etc.)
129
+ def copy
130
+ checklist_copy = self.class.create(name: self.name, board_id: self.board_id, card_id: self.card_id)
131
+ copy_items_to(checklist_copy)
132
+ return checklist_copy
133
+ end
134
+
135
+ private
136
+ def copy_items_to(another_checklist)
137
+ items.each do |item|
138
+ another_checklist.add_item(item.name, item.complete?)
139
+ end
140
+ end
141
+ end
142
+ 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']
31
+ attributes[:text] = fields['data']['text']
32
+ attributes[:date] = Time.iso8601(fields['date'])
33
+ attributes[:member_creator_id] = fields['idMemberCreator']
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']
24
+ attributes[:card_id] = fields['idCard']
25
+ attributes[:checklist_id] = fields['idChecklist']
26
+ attributes[:name] = fields['name']
27
+ attributes[:type] = fields['type']
28
+ attributes[:state] = fields['state']
29
+ attributes[:pos] = fields['pos']
30
+ self
31
+ end
32
+
33
+ def complete?
34
+ state == "complete"
35
+ end
36
+ end
37
+ end