diff --git a/lib/tracker_api.rb b/lib/tracker_api.rb index 33faeca..8b0678e 100644 --- a/lib/tracker_api.rb +++ b/lib/tracker_api.rb @@ -57,6 +57,8 @@ module Endpoints autoload :Tasks, 'tracker_api/endpoints/tasks' autoload :Comments, 'tracker_api/endpoints/comments' autoload :Comment, 'tracker_api/endpoints/comment' + autoload :Blockers, 'tracker_api/endpoints/blockers' + autoload :Blocker, 'tracker_api/endpoints/blocker' autoload :Webhook, 'tracker_api/endpoints/webhook' autoload :Webhooks, 'tracker_api/endpoints/webhooks' autoload :StoryTransitions, 'tracker_api/endpoints/story_transitions' @@ -90,6 +92,7 @@ module Shared autoload :Task, 'tracker_api/resources/task' autoload :TimeZone, 'tracker_api/resources/time_zone' autoload :Comment, 'tracker_api/resources/comment' + autoload :Blocker, 'tracker_api/resources/blocker' autoload :Webhook, 'tracker_api/resources/webhook' autoload :StoryTransition, 'tracker_api/resources/story_transition' autoload :FileAttachment, 'tracker_api/resources/file_attachment' diff --git a/lib/tracker_api/endpoints/blocker.rb b/lib/tracker_api/endpoints/blocker.rb new file mode 100644 index 0000000..d8fec5c --- /dev/null +++ b/lib/tracker_api/endpoints/blocker.rb @@ -0,0 +1,33 @@ +module TrackerApi + module Endpoints + class Blocker + attr_accessor :client + + def initialize(client) + @client = client + end + + def create(project_id, story_id, params={}) + data = client.post("/projects/#{project_id}/stories/#{story_id}/blockers", params: params).body + Resources::Blocker.new({ client: client, project_id: project_id }.merge(data)) + end + + def update(blocker, params={}) + raise ArgumentError, 'Valid blocker required to update.' unless blocker.instance_of?(Resources::Blocker) + + path = "/projects/#{blocker.project_id}/stories/#{blocker.story_id}/blockers/#{blocker.id}" + data = client.put(path, params: params).body + + blocker.attributes = data + blocker.clean! + blocker + end + + def delete(blocker) + raise ArgumentError, 'Valid blocker required to update.' unless blocker.instance_of?(Resources::Blocker) + + client.delete("/projects/#{blocker.project_id}/stories/#{blocker.story_id}/blockers/#{blocker.id}").body + end + end + end +end diff --git a/lib/tracker_api/endpoints/blockers.rb b/lib/tracker_api/endpoints/blockers.rb new file mode 100644 index 0000000..423bd04 --- /dev/null +++ b/lib/tracker_api/endpoints/blockers.rb @@ -0,0 +1,22 @@ +module TrackerApi + module Endpoints + class Blockers + attr_accessor :client + + def initialize(client) + @client = client + end + + def get(project_id, story_id, params={}) + data = client.paginate("/projects/#{project_id}/stories/#{story_id}/blockers", params: params) + raise Errors::UnexpectedData, 'Array of blockers expected' unless data.is_a? Array + + data.map do |blocker| + Resources::Blocker.new({ client: client, + project_id: project_id, + story_id: story_id }.merge(blocker)) + end + end + end + end +end diff --git a/lib/tracker_api/resources/blocker.rb b/lib/tracker_api/resources/blocker.rb new file mode 100644 index 0000000..eec4d4c --- /dev/null +++ b/lib/tracker_api/resources/blocker.rb @@ -0,0 +1,36 @@ +module TrackerApi + module Resources + class Blocker + include Shared::Base + + attribute :client + + attribute :project_id, Integer + attribute :story_id, Integer + attribute :description, String + attribute :resolved, Boolean + attribute :created_at, DateTime + attribute :updated_at, DateTime + attribute :kind, String + + class UpdateRepresenter < Representable::Decorator + include Representable::JSON + + property :id + property :description + end + + def save + raise ArgumentError, 'Cannot update a blcoker with an unknown story_id.' if story_id.nil? + + Endpoints::Blocker.new(client).update(self, UpdateRepresenter.new(Blocker.new(self.dirty_attributes))) + end + + def delete + raise ArgumentError, 'Cannot delete a blcoker with an unknown story_id.' if story_id.nil? + + Endpoints::Blocker.new(client).delete(self) + end + end + end +end diff --git a/lib/tracker_api/resources/story.rb b/lib/tracker_api/resources/story.rb index 3e1ebad..5c56da5 100644 --- a/lib/tracker_api/resources/story.rb +++ b/lib/tracker_api/resources/story.rb @@ -129,6 +129,18 @@ def comments(reload: false) end end + # Provides a list of all the comments on the story. + # + # @param [Hash] params + # @return [Array[Comment]] + def blockers(reload: false) + if !reload && @blockers.present? + @blockers + else + @blockers = Endpoints::Blockers.new(client).get(project_id, id) + end + end + # Provides a list of all the tasks on the story. # # @param [Hash] params @@ -180,6 +192,12 @@ def create_comment(params) comment end + # @param [Hash] params attributes to create the comment with + # @return [Comment] newly created Comment + def create_blocker(params) + Endpoints::Blocker.new(client).create(project_id, id, params) + end + # Save changes to an existing Story. def save raise ArgumentError, 'Can not update a story with an unknown project_id.' if project_id.nil?