-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gravatar.ns
43 lines (43 loc) · 1.29 KB
/
Gravatar.ns
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
Newspeak3
'Gravatar'
class Gravatar usingPlatform: platform = (|
WebUtils = platform squeak WebUtils.
HTTPSocket = platform squeak HTTPSocket.
ImageReadWriter = platform squeak ImageReadWriter.
ReadStream = platform squeak ReadStream.
|) (
public retrieveImageForEmail: emailString = (
^ self retrieveImageForEmail: emailString size: 80 rating: 'pg'
)
public retrieveImageForEmail: emailString size: sizeInteger rating: ratingString = (
| md5 newSize newRating url stream |
newSize:: sizeInteger > 2048
ifTrue: [2048]
ifFalse: [sizeInteger max: 1].
newRating:: ratingString isNil
ifTrue: ['pg']
ifFalse: [ratingString].
md5:: WebUtils md5Digest: emailString.
url:: 'http://www.gravatar.com/avatar/' , md5 , '?s=' , newSize, '&r=', ratingString.
stream:: HTTPSocket httpGet: url.
stream binary; reset.
stream
ifNil: [^ nil]
ifNotNil: [^ (ImageReadWriter formFromStream: stream) asMorph]
)
public retrieveProfileForEmail: emailString = (
| stream md5 url contents dict |
md5:: WebUtils md5Digest: emailString.
url:: 'http://www.gravatar.com/' , md5 , '.json'.
[stream:: HTTPSocket httpGet: url.
contents:: ReadStream on: stream contents.
dict:: WebUtils jsonDecode: contents.
dict
ifNil: [^ nil]
ifNotNil: [^ (dict at: 'entry')
at: 1]]
on: Error
do: [:e | ^ e ]
)
) : (
)