Repository: TheFriendlyCoder/friendlypins

Issue:
Add method for deleting pins
To facilitate deletion of pins, we need to add a new `delete()` method to the Pins class to serve this purpose.

Discussion:

Patch:
diff --git a/src/friendlypins/board.py b/src/friendlypins/board.py
index 7cfd3c2..f541582 100644
--- a/src/friendlypins/board.py
+++ b/src/friendlypins/board.py
@@ -82,7 +82,7 @@ class Board(object):
             assert 'data' in raw
 
             for cur_item in raw['data']:
-                retval.append(Pin(cur_item))
+                retval.append(Pin(cur_item, self._root_url, self._token))
 
             self._log.debug("Raw keys are %s", raw.keys())
             self._log.debug("Paged info is %s", raw['page'])
diff --git a/src/friendlypins/pin.py b/src/friendlypins/pin.py
index 9c89f66..9394aaa 100644
--- a/src/friendlypins/pin.py
+++ b/src/friendlypins/pin.py
@@ -1,15 +1,22 @@
 """Primitives for operating on Pinterest pins"""
 import logging
 import json
+import requests
+from friendlypins.headers import Headers
 
 
 class Pin(object):
     """Abstraction around a Pinterest pin
 
-    :param dict data: Raw Pinterest API data describing a pin"""
+    :param dict data: Raw Pinterest API data describing a pin
+    :param str root_url: URL of the Pinterest REST API
+    :param str token: Authentication token for interacting with the API
+    """
 
-    def __init__(self, data):
+    def __init__(self, data, root_url, token):
         self._log = logging.getLogger(__name__)
+        self._root_url = root_url
+        self._token = token
         self._data = data
 
     def __str__(self):
@@ -69,5 +76,19 @@ class Pin(object):
 
         return self._data['media']['type']
 
+    def delete(self):
+        """Removes this pin from it's respective board"""
+        temp_url = '{0}/pins/{1}/'.format(
+            self._root_url,
+            self.unique_id)
+        temp_url += "?access_token={0}".format(self._token)
+
+        response = requests.delete(temp_url)
+
+        header = Headers(response.headers)
+        self._log.debug("Boards query response header %s", header)
+
+        response.raise_for_status()
+
 if __name__ == "__main__":
     pass


Test patch:
diff --git a/unit_tests/test_api.py b/unit_tests/test_api.py
index 8a0cc08..0a6e5c1 100644
--- a/unit_tests/test_api.py
+++ b/unit_tests/test_api.py
@@ -27,6 +27,8 @@ def test_get_user():
         assert expected_lastname == result.last_name
         assert expected_id == result.unique_id
 
+        mock_response.raise_for_status.assert_called_once()
+
 
 if __name__ == "__main__":
     pytest.main([__file__, "-v", "-s"])
diff --git a/unit_tests/test_board.py b/unit_tests/test_board.py
index eac8bbc..41eef02 100644
--- a/unit_tests/test_board.py
+++ b/unit_tests/test_board.py
@@ -56,5 +56,7 @@ def test_get_all_pins():
         assert expected_note == result[0].note
         assert expected_id == result[0].unique_id
         assert expected_mediatype == result[0].media_type
+
+        mock_response.raise_for_status.assert_called_once()
 if __name__ == "__main__":
     pytest.main([__file__, "-v", "-s"])
diff --git a/unit_tests/test_pin.py b/unit_tests/test_pin.py
index f18318b..1bbc348 100644
--- a/unit_tests/test_pin.py
+++ b/unit_tests/test_pin.py
@@ -18,7 +18,7 @@ def test_pin_properties():
         }
     }
 
-    obj = Pin(sample_data)
+    obj = Pin(sample_data, "http://www.pinterest.com", "1234abcd")
 
     assert obj.unique_id == expected_id
     assert obj.note == expected_note
@@ -31,7 +31,6 @@ def test_pin_missing_media_type():
     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,
@@ -39,7 +38,7 @@ def test_pin_missing_media_type():
         "url": expected_url,
     }
 
-    obj = Pin(sample_data)
+    obj = Pin(sample_data, "http://www.pinterest.com", "1234abcd")
 
     assert obj.unique_id == expected_id
     assert obj.note == expected_note
@@ -47,5 +46,22 @@ def test_pin_missing_media_type():
     assert obj.link == expected_link
     assert obj.media_type is None
 
+def test_delete():
+    api_url = "https://pinterest_url/v1"
+    token = "1234abcd"
+
+    data = {
+        "id": "12345678"
+    }
+
+    obj = Pin(data, api_url, token)
+
+    with mock.patch("friendlypins.pin.requests") as mock_requests:
+        mock_response = mock.MagicMock()
+        mock_requests.delete.return_value = mock_response
+
+        obj.delete()
+        mock_requests.delete.assert_called_once()
+        mock_response.raise_for_status.assert_called_once()
 if __name__ == "__main__":
     pytest.main([__file__, "-v", "-s"])
\ No newline at end of file
diff --git a/unit_tests/test_user.py b/unit_tests/test_user.py
index 04c0809..596f239 100644
--- a/unit_tests/test_user.py
+++ b/unit_tests/test_user.py
@@ -30,5 +30,7 @@ def test_get_boards():
         assert expected_name == result[0].name
         assert expected_id == result[0].unique_id
 
+        mock_response.raise_for_status.assert_called_once()
+
 if __name__ == "__main__":
     pytest.main([__file__, "-v", "-s"])