Skip to content

Commit

Permalink
refactor single-resource api calls into a singular endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasu committed Nov 16, 2016
1 parent 717e3a3 commit d93ca0f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/tracker_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module Endpoints
autoload :Task, 'tracker_api/endpoints/task'
autoload :Tasks, 'tracker_api/endpoints/tasks'
autoload :Comments, 'tracker_api/endpoints/comments'
autoload :Comment, 'tracker_api/endpoints/comment'
end

module Resources
Expand Down
27 changes: 27 additions & 0 deletions lib/tracker_api/endpoints/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module TrackerApi
module Endpoints
class Comment
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}/comments", params: params).body
Resources::Comment.new({ client: client, project_id: project_id }.merge(data))
end

def update(comment, params={})
raise ArgumentError, 'Valid comment required to update.' unless comment.instance_of?(Resources::Comment)

data = client.put("/projects/#{comment.project_id}/stories/#{comment.story_id}/comments/#{comment.id}",
params: params).body

comment.attributes = data
comment.clean!
comment
end
end
end
end
16 changes: 0 additions & 16 deletions lib/tracker_api/endpoints/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ def get(project_id, story_id, params={})
story_id: story_id }.merge(comment))
end
end

def create(project_id, story_id, params={})
data = client.post("/projects/#{project_id}/stories/#{story_id}/comments", params: params).body
Resources::Comment.new({ client: client, project_id: project_id }.merge(data))
end

def update(comment, params={})
raise ArgumentError, 'Valid comment required to update.' unless comment.instance_of?(Resources::Comment)

data = client.put("/projects/#{comment.project_id}/stories/#{comment.story_id}/comments/#{comment.id}",
params: params).body

comment.attributes = data
comment.clean!
comment
end
end
end
end
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UpdateRepresenter < Representable::Decorator
def save
raise ArgumentError, 'Cannot update a comment with an unknown story_id.' if story_id.nil?

Endpoints::Comments.new(client).update(self, UpdateRepresenter.new(Comment.new(self.dirty_attributes)))
Endpoints::Comment.new(client).update(self, UpdateRepresenter.new(Comment.new(self.dirty_attributes)))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def create_task(params)
# @param [Hash] params attributes to create the comment with
# @return [Comment] newly created Comment
def create_comment(params)
Endpoints::Comments.new(client).create(project_id, id, params)
Endpoints::Comment.new(client).create(project_id, id, params)
end

# Save changes to an existing Story.
Expand Down

0 comments on commit d93ca0f

Please sign in to comment.