Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclasses for different Event types #46

Closed
banksJeremy opened this issue May 3, 2014 · 1 comment
Closed

Subclasses for different Event types #46

banksJeremy opened this issue May 3, 2014 · 1 comment
Assignees

Comments

@banksJeremy
Copy link
Collaborator

Instead of having a single Event type, with a long conditional chain that decides what properties to add based on the type of event, we could have a bunch of subclasses of Event for different types, with a constructor that would take even JSON data and return an Event instance of the appropriate subtype (or just an instance of Event of the type is unknown).

@Event.register_type
class MessagePosted(Event):
    type_id = 1

    def __init__(self, data, wrapper=None):
        super(MessagePosted, self).__init__(data, wrapper)
        self.content = data['content']
        self.text_content = _utils.html_to_text(self.content)
        self.user_name = data['user_name']
        self.user_id = data['user_id']
        self.message_id = data['message_id']
from chatexchange import events

event = events.make(data)
if isinstance(event, events.MessagePosted):
    print "Got message:", event.text_content

    assert event.type_id == 1

Because of the number of classes this would add, it might be appropriate to put them in their own chatexchange.events module. But maybe not.

This would be particularly valuable if we ended up implementing an interface as discussed in #43, where different event types would not just have different static attributes but would have different methods as well.

@banksJeremy banksJeremy reopened this May 3, 2014
@banksJeremy banksJeremy self-assigned this May 3, 2014
banksJeremy pushed a commit to jeremyBanks/stack.chat that referenced this issue May 3, 2014
Adds tests for MessageEdited and MessageStarred event initialization.

ref Manishearth#46
@banksJeremy
Copy link
Collaborator Author

53b156f passed my Travis and your Travis. Merging into master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant