Skip to content

Commit

Permalink
Create separate Twitter::Streaming::DeletedTweet object
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Nov 29, 2013
1 parent 1a38e5e commit 084025b
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 39 deletions.
2 changes: 2 additions & 0 deletions etc/erd.dot
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ digraph classes {
Twitter__SourceUser [label="Twitter::SourceUser"]
Twitter__Streaming__Client [label="Twitter::Streaming::Client"]
Twitter__Streaming__Connection [label="Twitter::Streaming::Connection"]
Twitter__Streaming__DeletedTweet [label="Twitter::Streaming::DeletedTweet"]
Twitter__Streaming__Event [label="Twitter::Streaming::Event"]
Twitter__Streaming__FriendList [label="Twitter::Streaming::FriendList"]
Twitter__Streaming__MessageParser [label="Twitter::Streaming::MessageParser"]
Expand Down Expand Up @@ -142,6 +143,7 @@ digraph classes {
Twitter__SourceUser -> Twitter__BasicUser
Twitter__Streaming__Client -> Twitter__Client
Twitter__Streaming__Connection -> Object
Twitter__Streaming__DeletedTweet -> Twitter__Identity
Twitter__Streaming__Event -> Object
Twitter__Streaming__FriendList -> Array
Twitter__Streaming__MessageParser -> Object
Expand Down
Binary file modified etc/erd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions lib/twitter/streaming/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
require 'twitter/streaming/connection'
require 'twitter/streaming/response'
require 'twitter/streaming/message_parser'
require 'twitter/streaming/event'
require 'twitter/streaming/friend_list'

module Twitter
module Streaming
Expand Down
8 changes: 8 additions & 0 deletions lib/twitter/streaming/deleted_tweet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Twitter
module Streaming
class DeletedTweet < Twitter::Identity
attr_reader :user_id
end
DeletedStatus = DeletedTweet
end
end
16 changes: 9 additions & 7 deletions lib/twitter/streaming/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@ module Streaming
class Event

LIST_EVENTS = [
:list_created, :list_destroyed, :list_updated, :list_member_added,
:list_member_added, :list_member_removed, :list_user_subscribed,
:list_user_subscribed, :list_user_unsubscribed, :list_user_unsubscribed
:list_created, :list_destroyed, :list_updated, :list_member_added,
:list_member_added, :list_member_removed, :list_user_subscribed,
:list_user_subscribed, :list_user_unsubscribed, :list_user_unsubscribed,
]

TWEET_EVENTS = [
:favorite, :unfavorite
:favorite, :unfavorite,
]

attr_reader :name, :source, :target, :target_object

# @param data [Hash]
def initialize(data)
@name = data[:event].intern
@name = data[:event].to_sym
@source = Twitter::User.new(data[:source])
@target = Twitter::User.new(data[:target])
@target_object = target_object_factory(@name, data[:target_object])
end

private

def target_object_factory(event_name, data)
if LIST_EVENTS.include?(event_name)
Twitter::List.new(data)
elsif TWEET_EVENTS.include?(event_name)
Twitter::Tweet.new(data)
end
end

end
end
end
end
11 changes: 9 additions & 2 deletions lib/twitter/streaming/message_parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
require 'twitter/direct_message'
require 'twitter/streaming/deleted_tweet'
require 'twitter/streaming/event'
require 'twitter/streaming/friend_list'
require 'twitter/tweet'

module Twitter
module Streaming
class MessageParser
Expand All @@ -13,10 +19,11 @@ def self.parse(data)
FriendList.new(data[:friends])
elsif data[:delete]
if data[:delete][:status]
Tweet.new(data[:delete][:status].merge(:deleted => true))
DeletedTweet.new(data[:delete][:status])
end
end
end

end
end
end
end
7 changes: 0 additions & 7 deletions lib/twitter/tweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Tweet < Twitter::Identity
alias favoriters_count favorite_count
alias in_reply_to_tweet_id in_reply_to_status_id
alias retweeters_count retweet_count
attr_reader :deleted
object_attr_reader :GeoFactory, :geo
object_attr_reader :Metadata, :metadata
object_attr_reader :Place, :place
Expand All @@ -31,12 +30,6 @@ def filter_level
@attrs[:filter_level] || "none"
end
memoize :filter_level

# @note Not a real entity
# @return [Boolean]
def deleted?
deleted == true
end

# @return [String]
# @note May be > 140 characters.
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/track_streaming_user.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{"friends":[488736931,311444249]}
{"created_at":"Wed Apr 06 19:13:37 +0000 2011","id":55709764298092545,"id_str":"55709764298092545","text":"The problem with your code is that it's doing exactly what you told it to do.","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":7505382,"id_str":"7505382","name":"Erik Michaels-Ober","screen_name":"sferik","location":"San Francisco","description":"Write code. Not too much. Mostly Ruby.","url":"https:\/\/github.com\/sferik","entities":{"url":{"urls":[{"url":"https:\/\/github.com\/sferik","expanded_url":null,"indices":[0,25]}]},"description":{"urls":[]}},"protected":false,"followers_count":2479,"friends_count":200,"listed_count":132,"created_at":"Mon Jul 16 12:59:01 +0000 2007","favourites_count":4421,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":8730,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/677717672\/bb0b3653dcf0644e344823e0a2eb3382.png","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/677717672\/bb0b3653dcf0644e344823e0a2eb3382.png","profile_background_tile":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1759857427\/image1326743606_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1759857427\/image1326743606_normal.png","profile_banner_url":"https:\/\/si0.twimg.com\/profile_banners\/7505382\/1349499693","profile_link_color":"0084B4","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":{"type":"Point","coordinates":[37.78349999,-122.39362884]},"coordinates":{"type":"Point","coordinates":[-122.39362884,37.78349999]},"place":{"id":"5c92ab5379de3839","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/5c92ab5379de3839.json","place_type":"neighborhood","name":"South Beach","full_name":"South Beach, San Francisco","country_code":"US","country":"United States","bounding_box":{"type":"Polygon","coordinates":[[[-122.403482,37.777529],[-122.387436,37.777529],[-122.387436,37.794486],[-122.403482,37.794486]]]},"attributes":{}},"contributors":null,"retweet_count":316,"entities":{"hashtags":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false}
{"created_at":"Wed Apr 06 19:13:37 +0000 2011","id":55709764298092545,"id_str":"55709764298092545","text":"The problem with your code is that it's doing exactly what you told it to do.","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":7505382,"id_str":"7505382","name":"Erik Michaels-Ober","screen_name":"sferik","location":"San Francisco","description":"Write code. Not too much. Mostly Ruby.","url":"https:\/\/github.com\/sferik","entities":{"url":{"urls":[{"url":"https:\/\/github.com\/sferik","expanded_url":null,"indices":[0,25]}]},"description":{"urls":[]}},"protected":false,"followers_count":2479,"friends_count":200,"listed_count":132,"created_at":"Mon Jul 16 12:59:01 +0000 2007","favourites_count":4421,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":8730,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/677717672\/bb0b3653dcf0644e344823e0a2eb3382.png","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/677717672\/bb0b3653dcf0644e344823e0a2eb3382.png","profile_background_tile":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1759857427\/image1326743606_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1759857427\/image1326743606_normal.png","profile_banner_url":"https:\/\/si0.twimg.com\/profile_banners\/7505382\/1349499693","profile_link_color":"0084B4","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":{"type":"Point","coordinates":[37.78349999,-122.39362884]},"coordinates":{"type":"Point","coordinates":[-122.39362884,37.78349999]},"place":{"id":"5c92ab5379de3839","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/5c92ab5379de3839.json","place_type":"neighborhood","name":"South Beach","full_name":"South Beach, San Francisco","country_code":"US","country":"United States","bounding_box":{"type":"Polygon","coordinates":[[[-122.403482,37.777529],[-122.387436,37.777529],[-122.387436,37.794486],[-122.403482,37.794486]]]},"attributes":{}},"contributors":null,"retweet_count":316,"entities":{"hashtags":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false}
{"direct_message":{"id":389327745617891328,"id_str":"389327745617891328","text":"hello bot","sender":{"id":10083602,"id_str":"10083602","name":"Adam Bird","screen_name":"adambird","location":"Nottingham, UK","url":"http:\/\/about.me\/adambird", "description":"CEO @onediaryapp, ex @esendex CTO and co-founder, still cycling","protected":false,"followers_count":1286,"friends_count":850,"listed_count":51,"created_at":"Fri Nov 09 00:35:12 +0000 2007","favourites_count":93,"utc_offset":3600,"time_zone":"London","geo_enabled":true,"verified":false,"statuses_count":13480,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/378800000477232297\/23d85bb78f71534eea1e1133fb771f86_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/378800000477232297\/23d85bb78f71534eea1e1133fb771f86_normal.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"sender_id":10083602,"sender_id_str":"10083602","sender_screen_name":"adambird","recipient":{"id":1292911088,"id_str":"1292911088","name":"One Diary Bot","screen_name":"onediarybot","location":"","url":"http:\/\/www.onediary.com","description":"I'm the One Diary bot, here to help you with your life including personal cycling weather forecasts.","protected":false,"followers_count":113,"friends_count":148,"listed_count":1,"created_at":"Sat Mar 23 23:52:18 +0000 2013","favourites_count":0,"utc_offset":"","time_zone":"","geo_enabled":false,"verified":false,"statuses_count":7919,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3651575369\/090551d8dd92080198f707769239ff43_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3651575369\/090551d8dd92080198f707769239ff43_normal.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"recipient_id":1292911088,"recipient_id_str":"1292911088","recipient_screen_name":"onediarybot","created_at":"Sun Oct 13 09:52:22 +0000 2013","entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]}}}
{"event":"follow","source":{"id":10083602,"id_str":"10083602","name":"Adam Bird","screen_name":"adambird","location":"Nottingham, UK","url":"http://t.co/M1eaIKDQyz","description":"CEO @onediaryapp, ex @esendex CTO and co-founder, still cycling","protected":false,"followers_count":1295,"friends_count":850,"listed_count":53,"created_at":"Fri Nov 09 00:35:12 +0000 2007","favourites_count":93,"utc_offset":0,"time_zone":"London","geo_enabled":true,"verified":false,"statuses_count":13507,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/378800000477232297/23d85bb78f71534eea1e1133fb771f86_normal.jpeg","profile_image_url_https":"https://pbs.twimg.com/profile_images/378800000477232297/23d85bb78f71534eea1e1133fb771f86_normal.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"target":{"id":1292911088,"id_str":"1292911088","name":"One Diary Bot","screen_name":"onediarybot","location":null,"url":"http://t.co/etHGc0xHX4","description":"I'm the One Diary bot, here to help you with your life including personal cycling weather forecasts.","protected":false,"followers_count":123,"friends_count":157,"listed_count":1,"created_at":"Sat Mar 23 23:52:18 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":9637,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/3651575369/090551d8dd92080198f707769239ff43_normal.jpeg","profile_image_url_https":"https://pbs.twimg.com/profile_images/3651575369/090551d8dd92080198f707769239ff43_normal.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"created_at":"Sun Oct 27 20:35:25 +0000 2013"}
{"delete":{"status":{"id":272691609211117568,"user_id":478569062,"id_str":"272691609211117568","user_id_str":"478569062","deleted":true}}}
15 changes: 8 additions & 7 deletions spec/twitter/streaming/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class FakeConnection
def initialize(body)
@body = body
end

def stream(request, response)
@body.each_line do |line|
response.on_body(line)
Expand Down Expand Up @@ -80,13 +79,15 @@ def stream(request, response)
end
expect(items.size).to eq(5)
expect(items[0]).to be_a Twitter::Streaming::FriendList
expect(items[0].friend_ids).to eq([488736931,311444249])
expect(items[0]).to eq([488736931,311444249])
expect(items[1]).to be_a Twitter::Tweet
expect(items[1].text).to eq "The problem with your code is that it's doing exactly what you told it to do."
expect(items[3]).to be_a Twitter::DirectMessage
expect(items[3].text).to eq "hello bot"
expect(items[4]).to be_a Twitter::Streaming::Event
expect(items[4].name).to be(:follow)
expect(items[1].text).to eq("The problem with your code is that it's doing exactly what you told it to do.")
expect(items[2]).to be_a Twitter::DirectMessage
expect(items[2].text).to eq("hello bot")
expect(items[3]).to be_a Twitter::Streaming::Event
expect(items[3].name).to eq(:follow)
expect(items[4]).to be_a Twitter::Streaming::DeletedTweet
expect(items[4].id).to eq(272691609211117568)
end

end
30 changes: 30 additions & 0 deletions spec/twitter/streaming/deleted_tweet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'helper'

describe Twitter::Streaming::DeletedTweet do

describe "#==" do
it "returns true when objects IDs are the same" do
deleted_tweet = Twitter::Streaming::DeletedTweet.new(:id => 1)
other = Twitter::Streaming::DeletedTweet.new(:id => 1)
expect(deleted_tweet == other).to be true
end
it "returns false when objects IDs are different" do
deleted_tweet = Twitter::Streaming::DeletedTweet.new(:id => 1)
other = Twitter::Streaming::DeletedTweet.new(:id => 2)
expect(deleted_tweet == other).to be false
end
it "returns false when classes are different" do
deleted_tweet = Twitter::Streaming::DeletedTweet.new(:id => 1)
other = Twitter::Identity.new(:id => 1)
expect(deleted_tweet == other).to be false
end
end

describe "#user_id" do
it "returns the user ID" do
deleted_tweet = Twitter::Streaming::DeletedTweet.new(:id => 1, :user_id => 1)
expect(deleted_tweet.user_id).to eq(1)
end
end

end
Loading

0 comments on commit 084025b

Please sign in to comment.