Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4169de
commit 1dd0a02
Showing
9 changed files
with
231 additions
and
12 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
friendlypins.pin module | ||
======================= | ||
|
||
.. automodule:: friendlypins.pin | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Primitives for operating on Pinterest pins""" | ||
import logging | ||
import json | ||
|
||
|
||
class Pin(object): | ||
"""Abstraction around a Pinterest pin | ||
:param dict data: Raw Pinterest API data describing a pin""" | ||
|
||
def __init__(self, data): | ||
self._log = logging.getLogger(__name__) | ||
self._data = data | ||
|
||
def __str__(self): | ||
"""String representation of this pin, for debugging purposes | ||
:rtype: :class:`str` | ||
""" | ||
return json.dumps(dict(self._data), sort_keys=True, indent=4) | ||
|
||
def __repr__(self): | ||
"""Pin representation in string format | ||
:rtype: :class:`str` | ||
""" | ||
return "<{0} ({1})>".format(self.__class__.__name__, self.note) | ||
|
||
@property | ||
def url(self): | ||
"""Web address for the UI associated with the pin | ||
:rtype: :class:`str` | ||
""" | ||
return self._data['url'] | ||
|
||
@property | ||
def note(self): | ||
"""Descriptive text associated with pin | ||
:rtype: :class:`str` | ||
""" | ||
return self._data['note'] | ||
|
||
@property | ||
def link(self): | ||
"""Source URL containing the original data for the pin | ||
:rtype: :class:`str` | ||
""" | ||
return self._data['link'] | ||
|
||
@property | ||
def unique_id(self): | ||
"""The unique identifier associated with this pin | ||
:rtype: :class:`int` | ||
""" | ||
return int(self._data['id']) | ||
|
||
@property | ||
def media_type(self): | ||
"""Gets descriptor for the type of data stored in the pin's link | ||
:rtype: :class:`str` | ||
""" | ||
if 'media' not in self._data: | ||
return None | ||
|
||
return self._data['media']['type'] | ||
|
||
if __name__ == "__main__": | ||
pass |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
import mock | ||
from friendlypins.pin import Pin | ||
|
||
def test_pin_properties(): | ||
expected_id = 1234 | ||
expected_note = "Here's my note" | ||
expected_url = "https://www.pinterest.ca/MyName/MyPin/" | ||
expected_link = "http://www.google.ca" | ||
expected_media_type = "video" | ||
sample_data = { | ||
"id": str(expected_id), | ||
"note": expected_note, | ||
"link": expected_link, | ||
"url": expected_url, | ||
"media": { | ||
"type": expected_media_type | ||
} | ||
} | ||
|
||
obj = Pin(sample_data) | ||
|
||
assert obj.unique_id == expected_id | ||
assert obj.note == expected_note | ||
assert obj.url == expected_url | ||
assert obj.link == expected_link | ||
assert obj.media_type == expected_media_type | ||
|
||
def test_pin_missing_media_type(): | ||
expected_id = 1234 | ||
expected_note = "Here's my note" | ||
expected_url = "https://www.pinterest.ca/MyName/MyPin/" | ||
expected_link = "http://www.google.ca" | ||
expected_media_type = "video" | ||
sample_data = { | ||
"id": str(expected_id), | ||
"note": expected_note, | ||
"link": expected_link, | ||
"url": expected_url, | ||
} | ||
|
||
obj = Pin(sample_data) | ||
|
||
assert obj.unique_id == expected_id | ||
assert obj.note == expected_note | ||
assert obj.url == expected_url | ||
assert obj.link == expected_link | ||
assert obj.media_type is None | ||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__, "-v", "-s"]) |