# Path: logic/notification_request.py
# class NotificationRequest(object):
# 
#     def __init__(self, subscription, payload):
#         self.url = subscription.request_url
#         self.method = subscription.request_method
#         self.format = subscription.request_format
#         self.secret = subscription.secret
#         self.event = subscription.event
#         self.content = self._content(self.format, payload)
# 
#     def send(self):
#         try:
#             return pool.urlopen(
#                 self.method,
#                 self.url,
#                 body=self.content.payload,
#                 headers=self.headers,
#             )
#         except Exception:
#             logging.info('Could not send request to %s', self.url)
#             return False
# 
#     @property
#     def headers(self):
#         # hmac does not like unicode
#         digest = hmac.new(str(self.secret), str(self.content.payload), hashlib.sha1)
#         return {
#             'Content-type': self.content.content_type,
#             'X-YelpLove-Event': self.event,
#             'User-Agent': '{} ({})'.format(config.APP_NAME, config.APP_BASE_URL),
#             'X-Hub-Signature': digest.hexdigest(),
#         }
# 
#     def _content(self, format, payload):
#         if CONTENT_TYPE_JSON == format:
#             return JSONContent(payload)
#         else:
#             raise RuntimeError('Unsupported format %s' % format)
# 
# Path: models/love.py
# class Love(ndb.Model):
#     """Models an instance of sent love."""
#     message = ndb.TextProperty()
#     recipient_key = ndb.KeyProperty(kind=Employee)
#     secret = ndb.BooleanProperty(default=False)
#     sender_key = ndb.KeyProperty(kind=Employee)
#     timestamp = ndb.DateTimeProperty(auto_now_add=True)
#     company_values = ndb.StringProperty(repeated=True)
# 
#     @property
#     def seconds_since_epoch(self):
#         return int(mktime(self.timestamp.timetuple()))
# 
# Path: models/subscription.py
# class Subscription(ndb.Model):
#     """Models a webhook subscription."""
#     request_method = ndb.StringProperty(required=True, default='post')
#     request_format = ndb.StringProperty(required=True, default=CONTENT_TYPE_JSON)
#     request_url = ndb.StringProperty(required=True)
#     active = ndb.BooleanProperty(required=True, default=False)
#     event = ndb.StringProperty(required=True)
#     secret = ndb.StringProperty(required=True)
#     timestamp = ndb.DateTimeProperty(auto_now_add=True)
#     owner_key = ndb.KeyProperty(kind=Employee)
# 
#     @classmethod
#     def create_from_dict(cls, d, persist=True):
#         new_subscription = cls()
#         new_subscription.owner_key = Employee.get_current_employee().key
#         new_subscription.request_url = d['request_url']
#         new_subscription.active = d['active']
#         new_subscription.event = d['event']
#         new_subscription.secret = d['secret']
# 
#         if persist is True:
#             new_subscription.put()
# 
#         return new_subscription
# 
#     @classmethod
#     def all_active_for_event(cls, event):
#         return cls.query(
#             cls.active == True,  # noqa
#             cls.event == event,
#         )


from google.appengine.ext import ndb
from logic.notification_request import NotificationRequest
from models.love import Love
from models.subscription import Subscription
import logic.event
# -*- coding: utf-8 -*-




class LovesentNotifier(object):

    event = logic.event.LOVESENT

    def __init__(self, *args, **kwargs):
        self.love = ndb.Key(Love, kwargs.get('love_id')).get()

    def notify(self):
        subscriptions = self._subscriptions()
        for subscription in subscriptions:

# Path: logic/notification_request.py
# class NotificationRequest(object):
# 
#     def __init__(self, subscription, payload):
#         self.url = subscription.request_url
#         self.method = subscription.request_method
#         self.format = subscription.request_format
#         self.secret = subscription.secret
#         self.event = subscription.event
#         self.content = self._content(self.format, payload)
# 
#     def send(self):
#         try:
#             return pool.urlopen(
#                 self.method,
#                 self.url,
#                 body=self.content.payload,
#                 headers=self.headers,
#             )
#         except Exception:
#             logging.info('Could not send request to %s', self.url)
#             return False
# 
#     @property
#     def headers(self):
#         # hmac does not like unicode
#         digest = hmac.new(str(self.secret), str(self.content.payload), hashlib.sha1)
#         return {
#             'Content-type': self.content.content_type,
#             'X-YelpLove-Event': self.event,
#             'User-Agent': '{} ({})'.format(config.APP_NAME, config.APP_BASE_URL),
#             'X-Hub-Signature': digest.hexdigest(),
#         }
# 
#     def _content(self, format, payload):
#         if CONTENT_TYPE_JSON == format:
#             return JSONContent(payload)
#         else:
#             raise RuntimeError('Unsupported format %s' % format)
# 
# Path: models/love.py
# class Love(ndb.Model):
#     """Models an instance of sent love."""
#     message = ndb.TextProperty()
#     recipient_key = ndb.KeyProperty(kind=Employee)
#     secret = ndb.BooleanProperty(default=False)
#     sender_key = ndb.KeyProperty(kind=Employee)
#     timestamp = ndb.DateTimeProperty(auto_now_add=True)
#     company_values = ndb.StringProperty(repeated=True)
# 
#     @property
#     def seconds_since_epoch(self):
#         return int(mktime(self.timestamp.timetuple()))
# 
# Path: models/subscription.py
# class Subscription(ndb.Model):
#     """Models a webhook subscription."""
#     request_method = ndb.StringProperty(required=True, default='post')
#     request_format = ndb.StringProperty(required=True, default=CONTENT_TYPE_JSON)
#     request_url = ndb.StringProperty(required=True)
#     active = ndb.BooleanProperty(required=True, default=False)
#     event = ndb.StringProperty(required=True)
#     secret = ndb.StringProperty(required=True)
#     timestamp = ndb.DateTimeProperty(auto_now_add=True)
#     owner_key = ndb.KeyProperty(kind=Employee)
# 
#     @classmethod
#     def create_from_dict(cls, d, persist=True):
#         new_subscription = cls()
#         new_subscription.owner_key = Employee.get_current_employee().key
#         new_subscription.request_url = d['request_url']
#         new_subscription.active = d['active']
#         new_subscription.event = d['event']
#         new_subscription.secret = d['secret']
# 
#         if persist is True:
#             new_subscription.put()
# 
#         return new_subscription
# 
#     @classmethod
#     def all_active_for_event(cls, event):
#         return cls.query(
#             cls.active == True,  # noqa
#             cls.event == event,
#         )


# Path: logic/notifier/lovesent_notifier.py
from google.appengine.ext import ndb
from logic.notification_request import NotificationRequest
from models.love import Love
from models.subscription import Subscription
import logic.event

# -*- coding: utf-8 -*-




class LovesentNotifier(object):

    event = logic.event.LOVESENT

    def __init__(self, *args, **kwargs):
        self.love = ndb.Key(Love, kwargs.get('love_id')).get()

    def notify(self):
        subscriptions = self._subscriptions()
        for subscription in subscriptions:

            NotificationRequest(subscription, self.payload()).send()