Skip to content

Commit

Permalink
seed tweets by migrating back thru the timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Schairbaum committed Apr 20, 2012
1 parent 9b6e228 commit ea996a2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
/tmp

*#*
*~*
*~*
credentials.sh
6 changes: 0 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ source 'https://rubygems.org'

gem 'rails', '3.2.3'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platform => :ruby

gem 'uglifier', '>= 1.0.3'
end

Expand Down
24 changes: 24 additions & 0 deletions app/models/tweet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Tweet

KARL = 'dadboner'

include Mongoid::Document
field :created_at, :type => Time
field :twitter_id, :type => String, :unique => true
field :text, :type => String

def self.seed_tweets
tweets = Twitter.user_timeline(KARL, :trim_user => true, :count => 200)
max_id = tweets.map{|t| t.id}.sort.first

while tweets.any?
tweets = Twitter.user_timeline(KARL, :trim_user => true, :count => 200, :max_id => max_id)
max_id = tweets.map{|t| t.id}.sort.first

tweets.each do |twoot|
tweet = create(:twitter_id => twoot.id.to_s, :created_at => twoot.created_at, :text => twoot.text)
end
end
end

end
6 changes: 6 additions & 0 deletions config/initializers/twitter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Twitter.configure do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUME_SECRET']
config.oauth_token = ENV['TWITTER_OAUTH_TOKEN']
config.oauth_token_secret = ENV['TWITTER_OAUTH_TOKEN_SECRET']
end
24 changes: 24 additions & 0 deletions spec/models/tweet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

describe Tweet do
after(:each) do
Tweet.destroy_all
end

describe ".seed_tweets" do
it "creates tweets from the feed" do
tweets = [mock("tweet", { :created_at => Time.now, :text => "tweets", :id_str => "12345678910" }),
mock("tweet", { :created_at => Time.now, :text => "tweets", :id_str => "23456789101"}) ]

Twitter.stub!(:user_timeline).and_return(tweets)

Tweet.seed_tweets
Tweet.count.should == 2
end

it "grabs tweets from Twitter" do
Twitter.should_receive(:user_timeline).and_return([])
Tweet.seed_tweets
end
end
end
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
# config.mock_with :rr

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# config.use_transactional_fixtures = true

# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
Expand Down

0 comments on commit ea996a2

Please sign in to comment.