-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChuckNorrisTalk.pck.st
268 lines (212 loc) · 8.04 KB
/
ChuckNorrisTalk.pck.st
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
'From Cuis7.1 [latest update: #6722] on 15 September 2024 at 6:07:43 pm'!
'Description menu entry and icon'!
!provides: 'ChuckNorrisTalk' 1 26!
!requires: 'WebClient' 1 19 nil!
!requires: 'Graphics-Files-Additional' 1 23 nil!
SystemOrganization addCategory: #ChuckNorrisTalk!
!classDefinition: #ChuckNorrisFact category: #ChuckNorrisTalk!
Object subclass: #ChuckNorrisFact
instanceVariableNames: 'id url value categories iconUrl'
classVariableNames: ''
poolDictionaries: ''
category: 'ChuckNorrisTalk'!
!classDefinition: 'ChuckNorrisFact class' category: #ChuckNorrisTalk!
ChuckNorrisFact class
instanceVariableNames: ''!
!classDefinition: #ChuckNorrisIoClient category: #ChuckNorrisTalk!
Object subclass: #ChuckNorrisIoClient
instanceVariableNames: 'queryStream'
classVariableNames: ''
poolDictionaries: ''
category: 'ChuckNorrisTalk'!
!classDefinition: 'ChuckNorrisIoClient class' category: #ChuckNorrisTalk!
ChuckNorrisIoClient class
instanceVariableNames: ''!
!classDefinition: #ChuckNorrisMorph category: #ChuckNorrisTalk!
LayoutMorph subclass: #ChuckNorrisMorph
instanceVariableNames: 'client fact chuckNorrisImage imageMorph factTextMorph'
classVariableNames: 'ChuckNorrisIcon'
poolDictionaries: ''
category: 'ChuckNorrisTalk'!
!classDefinition: 'ChuckNorrisMorph class' category: #ChuckNorrisTalk!
ChuckNorrisMorph class
instanceVariableNames: ''!
!ChuckNorrisFact commentStamp: '<historical>' prior: 0!
I am a Chuck Norris joke (ok, fact)!
!ChuckNorrisIoClient commentStamp: '<historical>' prior: 0!
I am a client for chucknorris.io, a free JSON API for hand curated Chuck Norris facts. https://api.chucknorris.io/!
!ChuckNorrisMorph commentStamp: 'tsl 1/3/2022 07:20:57' prior: 0!
A morph to show random facts.
Execute:
ChuckNorrisMorph open.
!
!ChuckNorrisFact methodsFor: 'printing' stamp: 'tsl 8/31/2020 15:23:38'!
printOn: aStream
super printOn: aStream.
aStream nextPut: $(;
nextPutAll: self value;
nextPut: $)! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'tsl 8/31/2020 14:48:54'!
categories
^ categories! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'tsl 8/31/2020 14:49:16'!
categories: anArray
categories := anArray! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'TSL 3/3/2019 14:22:27'!
iconUrl
^ iconUrl! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'TSL 3/3/2019 14:22:35'!
iconUrl: aString
iconUrl := aString! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'TSL 3/3/2019 14:19:38'!
id
^ id! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'TSL 3/3/2019 14:19:27'!
id: aString
id := aString! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'TSL 3/3/2019 14:20:18'!
url
^ url! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'TSL 3/3/2019 14:20:07'!
url: aString
url := aString! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'TSL 3/3/2019 15:38:56'!
value
^ value! !
!ChuckNorrisFact methodsFor: 'accessing' stamp: 'TSL 3/3/2019 15:39:03'!
value: aString
value := aString! !
!ChuckNorrisFact class methodsFor: 'as yet unclassified' stamp: 'tsl 8/31/2020 14:49:30'!
newFromJson: aJsonDictionary
^ self new
id: (aJsonDictionary at: #id);
url: (aJsonDictionary at: #url);
categories: (aJsonDictionary at: #categories);
iconUrl: (aJsonDictionary at: #icon_url);
value: (aJsonDictionary at: #value);
yourself ! !
!ChuckNorrisIoClient methodsFor: 'accessing' stamp: 'TSL 3/2/2019 10:49:12'!
baseUrl
^ 'https://api.chucknorris.io/jokes/'! !
!ChuckNorrisIoClient methodsFor: 'accessing' stamp: 'TSL 3/3/2019 15:53:55'!
categories
self initializeQueryStream.
self queryStream
nextPutAll: 'categories'.
^ self retrieveContents! !
!ChuckNorrisIoClient methodsFor: 'accessing' stamp: 'tsl 9/1/2024 10:36:18'!
chuckNorrisImage
^ (WebClient httpGet: 'https://api.chucknorris.io/img/chucknorris_logo_coloured_small.png') content! !
!ChuckNorrisIoClient methodsFor: 'accessing' stamp: 'TSL 3/2/2019 10:42:58'!
queryStream
^ queryStream! !
!ChuckNorrisIoClient methodsFor: 'accessing' stamp: 'TSL 3/3/2019 15:53:49'!
random
self initializeQueryStream.
self queryStream
nextPutAll: 'random'.
^ ChuckNorrisFact newFromJson: (self retrieveContents)! !
!ChuckNorrisIoClient methodsFor: 'accessing' stamp: 'TSL 3/24/2019 12:50:28'!
randomByCategory: aString
self initializeQueryStream.
self queryStream
nextPutAll: 'random';
nextPutAll: '?category=';
nextPutAll: aString .
[ ^ ChuckNorrisFact newFromJson: (self retrieveContents) ]
on: Error do: [ ^ nil].! !
!ChuckNorrisIoClient methodsFor: 'accessing' stamp: 'TSL 3/24/2019 12:51:14'!
search: aString
self initializeQueryStream.
self queryStream
nextPutAll: 'search';
nextPutAll: '?query=';
nextPutAll: aString .
[ ^ (self retrieveContents at: #result)
collect: [ :each | ChuckNorrisFact newFromJson: each ]]
on: Error do: [^ nil]! !
!ChuckNorrisIoClient methodsFor: 'private' stamp: 'TSL 3/2/2019 10:42:39'!
execute
^ (WebClient httpGet: self queryStream contents) content! !
!ChuckNorrisIoClient methodsFor: 'private' stamp: 'TSL 3/2/2019 10:43:40'!
retrieveContents
^ WebUtils jsonDecode: (self execute readStream) ! !
!ChuckNorrisIoClient methodsFor: 'initialization' stamp: 'TSL 3/2/2019 10:44:15'!
initialize
super initialize.
self initializeQueryStream.! !
!ChuckNorrisIoClient methodsFor: 'initialization' stamp: 'TSL 3/2/2019 10:45:32'!
initializeQueryStream
queryStream := (ReadWriteStream on: String new).
queryStream nextPutAll: self baseUrl.! !
!ChuckNorrisMorph methodsFor: 'initialization' stamp: 'tsl 1/3/2022 07:19:41'!
initialize
super initialize.
self color: Color blue.
client := ChuckNorrisIoClient new.
fact := client random.
imageMorph := ImageMorph new image: (Form fromBinaryStream: self chuckNorrisImage asByteArray readStream).
factTextMorph := TextModelMorph withText: self fact value.
self addMorph: imageMorph;
addMorph: factTextMorph layoutSpec: (LayoutSpec proportionalWidth: 0.8 proportionalHeight: 0.9);
addMorph: (PluggableButtonMorph model: [ self nextRandomFact ] action: #value label: 'Next').
! !
!ChuckNorrisMorph methodsFor: 'accessing' stamp: 'tsl 9/1/2024 10:37:04'!
chuckNorrisImage
chuckNorrisImage isNil ifTrue: [
chuckNorrisImage := client chuckNorrisImage ].
^ chuckNorrisImage! !
!ChuckNorrisMorph methodsFor: 'accessing' stamp: 'TSL 3/5/2019 16:22:05'!
client
^ client ! !
!ChuckNorrisMorph methodsFor: 'accessing' stamp: 'TSL 3/5/2019 17:49:47'!
fact
^ fact ! !
!ChuckNorrisMorph methodsFor: 'as yet unclassified' stamp: 'TSL 3/5/2019 18:01:47'!
nextRandomFact
fact := self client random.
self redrawNeeded.! !
!ChuckNorrisMorph methodsFor: 'drawing' stamp: 'TSL 3/24/2019 11:21:11'!
drawOn: aCanvas
super drawOn: aCanvas .
factTextMorph model: (TextModel withText: self fact value).
! !
!ChuckNorrisMorph class methodsFor: 'accessing' stamp: 'tsl 11/28/2023 13:55:41'!
chuckNorrisIcon
^ ChuckNorrisIcon ! !
!ChuckNorrisMorph class methodsFor: 'accessing' stamp: 'tsl 9/15/2024 18:06:50'!
open
| area window |
window := SystemWindow new.
window setLabel: 'Chuck Norris Fact'.
area := (self newColumn separation: 5) name: #AREA.
window addMorph: area .
window openInWorld.
window widgetsColor: Color blue.
window morphPosition: 1@1;
morphExtent: 550@480.
! !
!ChuckNorrisMorph class methodsFor: 'menu-world' stamp: 'tsl 11/28/2023 15:37:25'!
worldMenuOptions
| arr |
arr := `{{
#itemGroup -> 10.
#itemOrder -> 50.
#label -> 'Tell me more about Chuck Norris'.
#object -> ChuckNorrisMorph .
#selector -> #open.
#balloonText -> 'Tell me more about Chuck Norris'.
} asDictionary}`.
(arr at: 1) at: #icon put: self chuckNorrisIcon.
^ arr! !
!ChuckNorrisMorph class methodsFor: 'new-morph participation' stamp: 'tsl 11/28/2023 13:45:33'!
includeInNewMorphMenu
"Return true for all classes that can be instantiated from the menu"
^ false! !
!ChuckNorrisMorph class methodsFor: 'class initialization' stamp: 'tsl 11/28/2023 14:01:37'!
initialize
| dirName |
dirName := (self package) fullFileName upToLastPathSeparator.
ChuckNorrisIcon := ImageReadWriter formFromFileNamed: dirName , '/', 'chuckicon.png'.
! !
ChuckNorrisMorph initialize!