Skip to content

Commit 312df44

Browse files
committed
finish 4th part
1 parent ca8e298 commit 312df44

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

config/config.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file is responsible for configuring your application
22
# and its dependencies with the aid of the Mix.Config module.
33
use Mix.Config
4-
alias WmTweeter.Resources.{Hello, Assets, TweetList}
4+
alias WmTweeter.Resources.{Hello, Assets, TweetList, Tweet}
55

66
# This configuration is loaded before any dependency and is restricted
77
# to this project. If another project depends on this project, this
@@ -34,6 +34,7 @@ config :wm_tweeter, :web_config,
3434
ip: {127, 0, 0, 1},
3535
port: 4000,
3636
dispatch: [
37+
{['tweets'], &(:wrq.method(&1) == :POST), Tweet, []},
3738
{['tweets'], TweetList, []},
3839
{[:hello], Hello, []},
3940
{[:*], Assets, []}

lib/wm_tweeter/resources/tweet.ex

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule WmTweeter.Resources.Tweet do
2+
def init(_), do: {:ok, {nil, []}}
3+
def ping(req_data, state), do: {:pong, req_data, state}
4+
5+
def allowed_methods(req_data, state) do
6+
{[:POST], req_data, state}
7+
end
8+
9+
def resource_exists(req_data, state) do
10+
{false, req_data, state}
11+
end
12+
13+
def post_is_create(req_data, state) do
14+
{true, req_data, state}
15+
end
16+
17+
def allow_missing_post(req_data, state) do
18+
{true, req_data, state}
19+
end
20+
21+
def create_path(req_data, {_id, attrs}) do
22+
new_id = System.monotonic_time
23+
{'/tweets/#{new_id}', req_data, {new_id, attrs}}
24+
end
25+
26+
def content_types_accepted(req_data, state) do
27+
{[{'application/json', :from_json}], req_data, state}
28+
end
29+
30+
def from_json(req_data, {id, attrs}) do
31+
try do
32+
req_body = :wrq.req_body(req_data)
33+
{:struct, [{"tweet", {:struct, attrs}}]} = :mochijson2.decode(req_body) |> IO.inspect
34+
{"message", msg} = List.keyfind(attrs, "message", 0)
35+
{"avatar", avatar} = List.keyfind(attrs, "avatar", 0)
36+
new_attrs = [avatar: avatar, message: msg, time: :erlang.timestamp]
37+
:ets.insert(:tweets, [{id, new_attrs}])
38+
{true, req_data, {id, new_attrs}}
39+
rescue _err in [MatchError, CaseClauseError] ->
40+
{false, req_data, {id, attrs}}
41+
end
42+
end
43+
end

priv/www/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ <h1>Tweeter</h1>
1818
</div>
1919

2020
<div id="add-tweet-form" class="add-tweet-form">
21+
<select id="add-tweet-avatar" class="person">
22+
<option value="http://upload.wikimedia.org/wikipedia/en/thumb/f/f4/The_Wire_Jimmy_McNulty.jpg/250px-The_Wire_Jimmy_McNulty.jpg">Jimmy</option>
23+
<option value="http://upload.wikimedia.org/wikipedia/en/thumb/1/15/The_Wire_Bunk.jpg/250px-The_Wire_Bunk.jpg">Bunk</option>
24+
<option value="http://upload.wikimedia.org/wikipedia/en/thumb/6/6c/The_Wire_Kima.jpg/250px-The_Wire_Kima.jpg">Kima</option>
25+
<option value="http://upload.wikimedia.org/wikipedia/en/thumb/7/73/The_Wire_Bubbles.jpg/250px-The_Wire_Bubbles.jpg">Bubbles</option>
26+
<option value="http://upload.wikimedia.org/wikipedia/en/thumb/2/2f/The_Wire_Avon.jpg/250px-The_Wire_Avon.jpg">Avon</option>
27+
<option value="http://upload.wikimedia.org/wikipedia/en/thumb/b/b7/The_Wire_Stringer_Bell.jpg/250px-The_Wire_Stringer_Bell.jpg">Stringer</option>
28+
<option value="http://upload.wikimedia.org/wikipedia/en/thumb/7/78/The_Wire_Omar.jpg/250px-The_Wire_Omar.jpg">Omar</option>
29+
</select>
2130
<input id="add-tweet-message" type="text" class="message" />
2231
<a id="add-tweet-submit" class="button post">POST!</a>
2332
</div>

priv/www/js/app.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
$(document).ready(() => {
2+
$('#add-tweet').click(function() {
3+
$('#add-tweet-form').toggle();
4+
});
5+
6+
// Submit the form on click of the "POST!" button
7+
$('#add-tweet-submit').click(function() {
8+
var tweetMessageField = $('#add-tweet-message')
9+
var tweetMessageAvatar = $('#add-tweet-avatar')
10+
var tweetMessageForm = $('#add-tweet-form')
11+
var tweetMessage = tweetMessageField.val()
12+
var tweetAvatar = tweetMessageAvatar.val()
13+
14+
$.ajax({
15+
type: 'POST',
16+
url: '/tweets',
17+
contentType: 'application/json',
18+
data: JSON.stringify({ tweet: {
19+
avatar: tweetAvatar,
20+
message: tweetMessage }}),
21+
success: (d) => {
22+
tweetMessageField.val('');
23+
tweetMessageForm.toggle();
24+
}
25+
})
26+
})
27+
228
let generateTweet = (tweet) => {
329
return '<li><div class="avatar" style="background: url(' +
430
tweet.avatar +

0 commit comments

Comments
 (0)