Created
January 2, 2014 19:33
-
-
Save iamjono/8225153 to your computer and use it in GitHub Desktop.
Retrieves the Gravatar for the given email address (Lasso 9)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
define gravatar => type { | |
/* ============================================================== | |
Retrieves the Gravatar for the given email address. | |
local(x = gravatar('[email protected]',-default='mm')) | |
local(x = gravatar('[email protected]',-test=true)) | |
============================================================== */ | |
data | |
private base = 'http://www.gravatar.com/avatar/', | |
private securebase = 'https://secure.gravatar.com/avatar/', | |
public url::string = string, | |
public valid::boolean = false | |
public onCreate( | |
email::string, | |
-rating::string = '', | |
-default::string = '', | |
-size::integer = 0, | |
-secure::boolean = false, | |
-test::boolean = false | |
) => { | |
fail_if( | |
not valid_email(#email), | |
-1, 'Email address supplied is not valid.' | |
) | |
fail_if( | |
#rating->size && array('G','PG','R','X') !>> #rating, | |
-1, 'Rating must be one of G, PG, R, or X.' | |
) | |
fail_if( | |
#size < 0 || #size > 2048, | |
-1, 'Size must be between 1 and 2048.' | |
) | |
#default->size && array('404','mm','identicon','monsterid','wavatar','retro','blank') !>> #default ? | |
fail_if( | |
#default->size && !valid_url(#default), | |
-1, 'Default must be a complete, valid URL.' | |
) | |
local(hash = encrypt_md5(#email->lowercase&)) | |
#secure ? | |
.url = securebase | | |
.url = .base | |
.url->append(#hash) | |
local(params = array) | |
#rating->size ? #params->insert('r=' + #rating) | |
#size > 0 ? #params->insert('s=' + #size) | |
#default->size ? #params->insert('d=' + encode_stricturl(#default)) | |
#params->size ? .url->append('?'+#params->join('&')) | |
if(#test) => { .test ? return .url | return string } | |
return .url | |
} | |
private test() => { | |
.url >> '?' ? local(c = curl(.url+'&d=404')) | local(c = curl(.url+'?d=404')) | |
#c->result | |
return (#c->statusCode == 404 ? false | true) | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment