Skip to content

Commit

Permalink
Change default value of the use_ssl parameter (#9)
Browse files Browse the repository at this point in the history
* Default to use https.
* Add https support to profile.
* Use www.gravatar.com for all urls, even https - api docs have changed
* Fix documentation to remove references so secure.gravatar.com
* Change docstrings to reflect https return
  • Loading branch information
pabluk authored Apr 9, 2021
2 parents 50b864e + 7d9fe7b commit 16d7bda
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ Getting the user profile image::
from libgravatar import Gravatar
g = Gravatar('myemailaddress@example.com')
g.get_image()
'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346'
'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346'

Getting the profile URL::

from libgravatar import Gravatar
g = Gravatar('myemailaddress@example.com')
g.get_profile()
'http://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346'
'https://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346'


Gravatar XML-RPC API
Expand Down Expand Up @@ -70,3 +70,4 @@ Author and contributors
* Caleb FANGMEIER (`@cfangmeier <https://github.com/cfangmeier>`_)
* Rarm NAGALINGAM (`@snowjet <https://github.com/snowjet/>`_)
* Manan (`@mentix02 <https://github.com/mentix02/>`_)
* Gareth Simpson (`@xurble <https://github.com/xurble/>`_)
26 changes: 14 additions & 12 deletions libgravatar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ def __init__(self, email):
self.email = sanitize_email(email)
self.email_hash = md5_hash(self.email)

def get_image(self, size=DEFAULT_IMAGE_SIZE, default="", force_default=False, rating="", filetype_extension=False, use_ssl=False):
def get_image(self, size=DEFAULT_IMAGE_SIZE, default="", force_default=False, rating="", filetype_extension=False, use_ssl=True):
"""
Returns an URL to the user profile image.
>>> g = Gravatar('myemailaddress@example.com')
>>> g.get_image()
'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346'
'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346'
With *size* you can request a specific image size, by default, images are presented at 80px by 80px.
You may request image anywhere from 1px up to 2048px.
Expand All @@ -92,7 +92,7 @@ def get_image(self, size=DEFAULT_IMAGE_SIZE, default="", force_default=False, ra
See more details on `Gravatar Image Requests <http://en.gravatar.com/site/implement/images/>`_.
"""
base_url = '{protocol}://{domain}/avatar/' \
base_url = '{protocol}://www.gravatar.com/avatar/' \
'{hash}{extension}{params}'

params_dict = {
Expand Down Expand Up @@ -125,40 +125,42 @@ def get_image(self, size=DEFAULT_IMAGE_SIZE, default="", force_default=False, ra

params = urlencode(params_dict)

protocol = 'http'
domain = 'www.gravatar.com'
if use_ssl:
protocol = 'https'
domain = 'secure.gravatar.com'
protocol = 'https'
if not use_ssl:
protocol = 'http'

extension = '.jpg' if filetype_extension else ''
params = '?%s' % params if params else ''
data = {
'protocol': protocol,
'domain': domain,
'hash': self.email_hash,
'extension': extension,
'params': params,
}
return base_url.format(**data)

def get_profile(self, data_format=''):
def get_profile(self, data_format='', use_ssl=True):
"""
Returns an URL to the profile information associated with the Gravatar account.
>>> g = Gravatar('myemailaddress@example.com')
>>> g.get_profile()
'http://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346'
'https://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346'
See more details on `Gravatar Profile Requests <http://en.gravatar.com/site/implement/profiles/>`_.
"""
base_url = 'http://www.gravatar.com/{hash}{data_format}'
protocol = 'https'
if not use_ssl:
protocol = 'http'

base_url = '{protocol}://www.gravatar.com/{hash}{data_format}'

if data_format and data_format in self.PROFILE_FORMATS:
data_format = '.%s' % data_format

data = {
'protocol': protocol,
'hash': self.email_hash,
'data_format': data_format,
}
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

33 changes: 20 additions & 13 deletions tests/test_gravatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ def setUp(self):

def test_without_params(self):
"""Test a get_image with default parameters."""
image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346'
result = self.g.get_image()
self.assertEqual(image_url, result)

def test_image_size(self):
"""Test image size parameter."""
image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?size=24'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?size=24'
result = self.g.get_image(size=24)
self.assertEqual(image_url, result)

Expand All @@ -28,17 +28,17 @@ def test_invalid_image_size(self):

def test_filetype_extension(self):
"""Test filetype_extension parameter."""
image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346.jpg'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346.jpg'
result = self.g.get_image(filetype_extension=True)
self.assertEqual(image_url, result)

def test_default(self):
"""Test default parameter."""
image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?default=monsterid'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?default=monsterid'
result = self.g.get_image(default='monsterid')
self.assertEqual(image_url, result)

image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?default=http%3A%2F%2Fexample.com%2Fimages%2Favatar.jpg'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?default=http%3A%2F%2Fexample.com%2Fimages%2Favatar.jpg'
result = self.g.get_image(default='http://example.com/images/avatar.jpg')
self.assertEqual(image_url, result)

Expand All @@ -47,31 +47,31 @@ def test_default(self):

def test_forcedefault(self):
"""Test forcedefault parameter."""
image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?forcedefault=y'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?forcedefault=y'
result = self.g.get_image(force_default=True)
self.assertEqual(image_url, result)

def test_use_ssl(self):
"""Test use_ssl parameter."""
image_url = 'https://secure.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346'
result = self.g.get_image(use_ssl=True)
image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346'
result = self.g.get_image(use_ssl=False)
self.assertEqual(image_url, result)

def test_rating(self):
"""Test the rating parameter."""
image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=g'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=g'
result = self.g.get_image(rating='g')
self.assertEqual(image_url, result)

image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=pg'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=pg'
result = self.g.get_image(rating='pg')
self.assertEqual(image_url, result)

image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=r'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=r'
result = self.g.get_image(rating='r')
self.assertEqual(image_url, result)

image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=x'
image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=x'
result = self.g.get_image(rating='x')
self.assertEqual(image_url, result)

Expand All @@ -86,8 +86,15 @@ def setUp(self):

def test_without_params(self):
"""Test a get_profile with default parameters."""
profile_url = 'http://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346'
profile_url = 'https://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346'
result = self.g.get_profile()
self.assertEqual(profile_url, result)

def test_use_ssl(self):
"""Test use_ssl parameter."""
image_url = 'http://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346'
result = self.g.get_profile(use_ssl=False)
self.assertEqual(image_url, result)



0 comments on commit 16d7bda

Please sign in to comment.