From f0d39757b6e3faba90094432840fe07f0d60d517 Mon Sep 17 00:00:00 2001 From: Chetan Date: Tue, 31 Oct 2017 08:08:45 +0530 Subject: [PATCH] Fix method_complexity issue in sendgrid/helpers/mail/ganalytics.py #498 --- sendgrid/helpers/mail/ganalytics.py | 50 +++++++++++++++-------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/sendgrid/helpers/mail/ganalytics.py b/sendgrid/helpers/mail/ganalytics.py index 666c2852e..b955b7241 100644 --- a/sendgrid/helpers/mail/ganalytics.py +++ b/sendgrid/helpers/mail/ganalytics.py @@ -30,18 +30,23 @@ def __init__(self, self._utm_content = None self._utm_campaign = None - if enable is not None: - self.enable = enable - if utm_source is not None: - self.utm_source = utm_source - if utm_medium is not None: - self.utm_medium = utm_medium - if utm_term is not None: - self.utm_term = utm_term - if utm_content is not None: - self.utm_content = utm_content - if utm_campaign is not None: - self.utm_campaign = utm_campaign + self.__set_field("enable", enable) + self.__set_field("utm_source", utm_source) + self.__set_field("utm_medium", utm_medium) + self.__set_field("utm_term", utm_term) + self.__set_field("utm_content", utm_content) + self.__set_field("utm_campaign", utm_campaign) + + def __set_field(self, field, value): + """ Sets a field to the provided value if value is not None + + :param field: Name of the field + :type field: String + :param value: value to be set, ignored if None + :type value: Any + """ + if value is not None: + setattr(self, field, value) @property def enable(self): @@ -123,17 +128,14 @@ def get(self): :returns: This Ganalytics, ready for use in a request body. :rtype: dict """ + keys = ["enable", "utm_source", "utm_medium", "utm_term", + "utm_content", "utm_campaign"] + ganalytics = {} - if self.enable is not None: - ganalytics["enable"] = self.enable - if self.utm_source is not None: - ganalytics["utm_source"] = self.utm_source - if self.utm_medium is not None: - ganalytics["utm_medium"] = self.utm_medium - if self.utm_term is not None: - ganalytics["utm_term"] = self.utm_term - if self.utm_content is not None: - ganalytics["utm_content"] = self.utm_content - if self.utm_campaign is not None: - ganalytics["utm_campaign"] = self.utm_campaign + + for key in keys: + value = getattr(self, key, None) + if value is not None: + ganalytics[key] = value + return ganalytics