Skip to content
Browse files

Initial tweet engine models

  • Loading branch information...
1 parent 69224f2 commit 5919c263dcc6d8cd1031af8d64d1e0fbd017c096 @chewbranca chewbranca committed Aug 12, 2011
View
14 app/models/tweet.rb
@@ -0,0 +1,14 @@
+class Tweet < ActiveRecord::Base
+
+ has_many :tweet_urls
+ has_many :urls, :through => :tweet_urls
+
+ def raw_urls
+ @raw_urls ||= text.scan(%r{http://[^\s]+})
+ end
+
+ def raw_json
+ @raw_json ||= JSON.parse(raw_tweet)
+ end
+
+end
View
4 app/models/tweet_url.rb
@@ -0,0 +1,4 @@
+class TweetUrl < ActiveRecord::Base
+ belongs_to :tweet
+ belongs_to :url
+end
View
4 app/models/url.rb
@@ -0,0 +1,4 @@
+class Url < ActiveRecord::Base
+ has_many :tweet_urls
+ has_many :urls, :through => :tweet_urls
+end
View
19 db/migrate/20110811204937_create_tweets.rb
@@ -0,0 +1,19 @@
+class CreateTweets < ActiveRecord::Migration
+ def self.up
+ create_table :tweets do |t|
+ t.integer :twitter_stream_id
+ t.string :twitter_id_str
+ t.string :text
+ t.text :raw_tweet
+
+ t.timestamps
+ end
+
+ add_index :tweets, :twitter_stream_id
+ add_index :tweets, :twitter_id_str, :unique => true
+ end
+
+ def self.down
+ drop_table :tweets
+ end
+end
View
17 db/migrate/20110811232456_create_tweet_urls.rb
@@ -0,0 +1,17 @@
+class CreateTweetUrls < ActiveRecord::Migration
+ def self.up
+ create_table :tweet_urls do |t|
+ t.integer :tweet_id
+ t.integer :url_id
+ t.timestamps
+ end
+
+ add_index :tweet_urls, :tweet_id
+ add_index :tweet_urls, :url_id
+ add_index :tweet_urls, [:tweet_id, :url_id]
+ end
+
+ def self.down
+ drop_table :tweet_urls
+ end
+end
View
17 db/migrate/20110812002233_create_urls.rb
@@ -0,0 +1,17 @@
+class CreateUrls < ActiveRecord::Migration
+ def self.up
+ create_table :urls do |t|
+ t.integer :source_id
+ t.string :url
+
+ t.timestamps
+ end
+
+ add_index :urls, :source_id
+ add_index :urls, :url
+ end
+
+ def self.down
+ drop_table :urls
+ end
+end
View
35 db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110627220941) do
+ActiveRecord::Schema.define(:version => 20110812002233) do
create_table "announcements", :force => true do |t|
t.string "prefix"
@@ -807,13 +807,46 @@
add_index "translations", ["locale_id", "key", "pluralization_index"], :name => "index_translations_on_locale_id_and_key_and_pluralization_index"
+ create_table "tweet_urls", :force => true do |t|
+ t.integer "tweet_id"
+ t.integer "url_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "tweet_urls", ["tweet_id", "url_id"], :name => "index_tweet_urls_on_tweet_id_and_url_id"
+ add_index "tweet_urls", ["tweet_id"], :name => "index_tweet_urls_on_tweet_id"
+ add_index "tweet_urls", ["url_id"], :name => "index_tweet_urls_on_url_id"
+
create_table "tweeted_items", :force => true do |t|
t.string "item_type"
t.integer "item_id"
t.datetime "created_at"
t.datetime "updated_at"
end
+ create_table "tweets", :force => true do |t|
+ t.integer "twitter_stream_id"
+ t.string "twitter_id_str"
+ t.string "text"
+ t.text "raw_tweet"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "tweets", ["twitter_id_str"], :name => "index_tweets_on_twitter_id_str", :unique => true
+ add_index "tweets", ["twitter_stream_id"], :name => "index_tweets_on_twitter_stream_id"
+
+ create_table "urls", :force => true do |t|
+ t.integer "source_id"
+ t.string "url"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "urls", ["source_id"], :name => "index_urls_on_source_id"
+ add_index "urls", ["url"], :name => "index_urls_on_url"
+
create_table "user_profiles", :force => true do |t|
t.integer "user_id", :limit => 8, :null => false
t.integer "facebook_user_id", :limit => 8, :default => 0
View
19 lib/tweeter.rb
@@ -35,10 +35,29 @@ def consumer
end
def fetch_list user, name
+ tweets = fetch_raw_list user, name
+ urls = tweets.map {|t| t["text"].scan(%r{http://[^\s]+}) }.flatten
+ self.class.fetch_real_urls urls
+ end
+ def save_list user, name
tweets = fetch_raw_list user, name
urls = tweets.map {|t| t["text"].scan(%r{http://[^\s]+}) }.flatten
self.class.fetch_real_urls urls
+ tweets.each do |raw_tweet|
+ tweet = Tweet.create!({
+ :twitter_id_str => raw_tweet["id_str"],
+ :text => raw_tweet["text"],
+ :raw_tweet => raw_tweet.to_json
+ })
+ self.class.fetch_real_urls(tweet.raw_urls).each do |url_str|
+ url = Url.find_or_create_by_url(url_str)
+ TweetUrl.create!({
+ :tweet => tweet,
+ :url => url
+ })
+ end
+ end
end
def fetch_raw_list user, name
View
11 test/fixtures/tweet_urls.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
View
11 test/fixtures/tweets.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
View
11 test/fixtures/urls.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
View
8 test/unit/tweet_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class TweetTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
8 test/unit/tweet_url_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class TweetUrlTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
8 test/unit/url_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class UrlTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit 5919c26

Please sign in to comment.
Something went wrong with that request. Please try again.