Skip to content

Commit 79bb2a2

Browse files
Steve Klebanoffforest
authored andcommitted
Make comment (#5)
* Fix JSON request to always specify content type so sending in a JSON body works * Add ability to post comment to pivotal story * Updating README to include POSTing a comment
1 parent db7a937 commit 79bb2a2

File tree

6 files changed

+115
-1
lines changed

6 files changed

+115
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ Based heavily on the [Tentacat](https://github.com/edgurgel/tentacat) and [ExTwi
1717
* [ ] Accounts
1818
* [ ] Activity
1919
* [ ] Attachments
20-
* [ ] Comments
20+
* [x] Comments
21+
* [ ] Get
22+
* [ ] Put
23+
* [x] Post
24+
* [ ] Delete
2125
* [ ] Epic
2226
* [ ] Epics
2327
* [ ] Exports

lib/extracker/comments.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule ExTracker.Comments do
2+
import ExTracker
3+
alias Extracker.Client
4+
5+
@doc """
6+
Create a single `comment`.
7+
8+
## Example
9+
10+
ExTracker.Comments.create(client, '1027488', '66727974', %{text: "Hello World From Elixir"})
11+
12+
More info at: https://www.pivotaltracker.com/help/api/rest/v5#projects_project_id_stories_story_id_comments_post
13+
"""
14+
@spec create(Client.t, pos_integer, pos_integer, {atom, binary}) :: ExTracker.Record.Comment.t
15+
def create(client, project_id, story_id, params) do
16+
post("projects/#{project_id}/stories/#{story_id}/comments", client, params)
17+
|> ExTracker.Parser.parse_comment
18+
end
19+
end

lib/extracker/parser.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ defmodule ExTracker.Parser do
127127
object |> Enum.map(&ExTracker.Parser.parse_story/1)
128128
end
129129

130+
@doc """
131+
Parse comment from the API response json.
132+
"""
133+
@spec parse_comment(Map.t) :: ExTracker.Record.Comment.t
134+
def parse_comment(object) do
135+
struct(ExTracker.Record.Comment, object)
136+
end
137+
130138
@doc """
131139
Parse story from the API response json.
132140
"""

lib/extracker/record.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ defmodule ExTracker.Record.Account do
149149
}
150150
end
151151

152+
defmodule ExTracker.Record.Comment do
153+
defstruct [:id, :created_at, :kind, :person_id, :story_id, :text, :updated_at]
154+
155+
@type t :: %ExTracker.Record.Comment{
156+
id: pos_integer,
157+
created_at: binary,
158+
kind: binary,
159+
person_id: pos_integer,
160+
story_id: pos_integer,
161+
text: binary,
162+
updated_at: binary
163+
}
164+
end
165+
152166
defmodule ExTracker.Record.Story do
153167
defstruct [:id, :project_id, :name, :description, :story_type, :current_state,
154168
:estimate, :accepted_at, :deadline, :requested_by_id, :owner_ids,

test/comments_test.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule ExTracker.CommentsTest do
2+
use ExUnit.Case
3+
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
4+
5+
import ExTracker.Comments
6+
7+
doctest ExTracker.Comments
8+
9+
alias ExTracker.Support.Helpers
10+
alias ExTracker.Record.Comment
11+
12+
@client ExTracker.Client.new(%{access_token: Helpers.pt_user_1.token})
13+
@project_id Helpers.pt_user_1.project_id
14+
@story_id Helpers.pt_user_1.story_id
15+
16+
setup_all do
17+
HTTPoison.start
18+
end
19+
20+
test "create/4" do
21+
use_cassette "comments#create" do
22+
%Comment{text: text} = create(
23+
@client, @project_id, @story_id, %{text: "Sup from test"}
24+
)
25+
assert text == "Sup from test"
26+
end
27+
end
28+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[
2+
{
3+
"request": {
4+
"body": "{\"text\":\"Sup from test\"}",
5+
"headers": {
6+
"Content-Type": "application/json",
7+
"User-agent": "extracker",
8+
"X-TrackerToken": "d55c3bc1f74346b843ca84ba340b29bf"
9+
},
10+
"method": "post",
11+
"options": [],
12+
"request_body": "",
13+
"url": "https://www.pivotaltracker.com/services/v5/projects/1027488/stories/66727974/comments"
14+
},
15+
"response": {
16+
"body": "{\"kind\":\"comment\",\"id\":140902341,\"story_id\":66727974,\"text\":\"Sup from test\",\"person_id\":1266314,\"created_at\":\"2016-07-02T00:13:35Z\",\"updated_at\":\"2016-07-02T00:13:35Z\"}",
17+
"headers": {
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Transfer-Encoding": "chunked",
20+
"Status": "200 OK",
21+
"Cache-Control": "max-age=0, private, must-revalidate",
22+
"Date": "Sat, 02 Jul 2016 00:13:35 GMT",
23+
"X-Tracker-Project-Version": "148",
24+
"X-Request-Id": "ec8dc444fb0ad05752bd7bbea3c7c6fd",
25+
"X-UA-Compatible": "IE=Edge,chrome=1",
26+
"ETag": "\"35779047e45b3cfc9b691b6b2e56a47e\"",
27+
"X-Runtime": "0.171839",
28+
"X-Rack-Cache": "invalidate, pass",
29+
"X-Powered-By": "Phusion Passenger Enterprise",
30+
"Server": "nginx + Phusion Passenger",
31+
"Access-Control-Allow-Origin": "*",
32+
"Access-Control-Allow-Credentials": "false",
33+
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
34+
"Access-Control-Allow-Headers": "X-TrackerToken,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Tracker-Warn-Unless-Project-Version-Is",
35+
"X-Tracker-Client-Pinger-Interval": "12"
36+
},
37+
"status_code": 200,
38+
"type": "ok"
39+
}
40+
}
41+
]

0 commit comments

Comments
 (0)