-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.elm
177 lines (119 loc) · 3.64 KB
/
App.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
module App exposing (..)
import Date exposing (Date)
import Html
import Html.Attributes
import Html.Events
import Time exposing (Time, hour, second)
import Utils
-- import Html.Attributes
import Http
import HttpBuilder
import Json.Decode as Json
import Task
type Msg
= FetchSucceed (HttpBuilder.Response (List Tweet))
| FetchFail (HttpBuilder.Error String)
| Tick Time
| Refresh Time
| NewSearch String
twitterBearerToken : String
twitterBearerToken =
"AAAAAAAAAAAAAAAAAAAAAJc9xAAAAAAAEqIw9D7P%2FVH3tw3WZyFcIyhmUv4%3D5eWCMgEwC4YSfnMzkH7PZ0HeN4ql6i0hfZQ9EZ6KYXPsoXqGPy"
twitterApiUrl : String
twitterApiUrl =
"https://cors-anywhere.herokuapp.com/https://api.twitter.com/1.1/search/tweets.json?q="
-- MODEL
type alias Author =
String
type alias Text =
String
type alias Tweet =
{ author : Author, text : Text, date : Date }
type alias Model =
{ tweets : List Tweet
, error : Maybe String
, currentTime : Time
, currentSearch : String
}
init : ( Model, Cmd Msg )
init =
( { tweets = []
, error = Nothing
, currentTime = 0
, currentSearch = "#elm"
}
, ( getTweets "#elm")
)
-- VIEW
view : Model -> Html.Html Msg
view model =
Html.div
[]
[ Html.h1 [] [ Html.text "Tweet Elm Wall" ]
, Html.text (Maybe.withDefault "" model.error)
, Html.br [] []
, Html.ul [] (List.map (displayTweet model.currentTime) model.tweets)
, Html.input [ Html.Events.onInput NewSearch, Html.Attributes.value model.currentSearch ] []
, Html.button [ Html.Events.onClick (Refresh model.currentTime) ] [ Html.text "Refresh" ]
]
displayTweet : Time -> Tweet -> Html.Html Msg
displayTweet currentTime tweet =
let
author =
"Author: " ++ tweet.author
in
Html.li []
[ Html.text tweet.text
, Html.br [] []
, Html.text author
, Html.text " — "
, Html.text (Utils.timeAgo (Date.toTime tweet.date) currentTime)
]
-- UPDATE
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Tick newTime ->
( { model | currentTime = newTime }, Cmd.none )
Refresh _ ->
( model, ( getTweets model.currentSearch ) )
NewSearch query ->
( { model | currentSearch = query }, Cmd.none )
FetchSucceed response ->
( { model | tweets = response.data }, Cmd.none )
FetchFail error ->
( { model | error = Just (toString error) }, Cmd.none )
getTweets : String -> Cmd Msg
getTweets query =
Task.perform
FetchFail
FetchSucceed
(HttpBuilder.get (twitterApiUrl ++ (Http.uriEncode query))
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ twitterBearerToken)
|> HttpBuilder.send (HttpBuilder.jsonReader decodeTweet) HttpBuilder.stringReader
)
decodeTweet : Json.Decoder (List Tweet)
decodeTweet =
Json.at [ "statuses" ] (Json.list getTweet)
getTweet : Json.Decoder Tweet
getTweet =
Json.object3 Tweet getAuthor getText getDate
getAuthor : Json.Decoder Author
getAuthor =
Json.at [ "user", "name" ] Json.string
getText : Json.Decoder Text
getText =
Json.at [ "text" ] Json.string
getDate : Json.Decoder Date
getDate =
Json.at [ "created_at" ] normalizeDate
normalizeDate : Json.Decoder Date
normalizeDate =
Json.map (\d -> Result.withDefault (Date.fromTime 0) (Date.fromString d)) Json.string
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Time.every second Tick
, Time.every hour Refresh
]