Database Reference
In-Depth Information
3.3
Building a simple application
We'll build a simple application for archiving and displaying tweets. You can imagine
this being a component in a larger application that allows users to keep tabs on search
terms relevant to their businesses. This example will demonstrate how easy it is to con-
sume JSON from an API like Twitter's and convert that to MongoDB documents. If you
were doing this with a relational database, you'd have to devise a schema in advance,
probably consisting of multiple tables, and then declare those tables. Here, none of
that is required, yet you'll still preserve the rich structure of the tweet documents, and
you'll be able to query them effectively.
Let's call the app TweetArchiver. TweetArchiver will consist of two components:
the archiver and the viewer. The archiver will call the Twitter search API and store the
relevant tweets, and the viewer will display the results in a web browser.
3.3.1
Setting up
This application requires three Ruby libraries. You can install them like so:
gem install mongo
gem install twitter
gem install sinatra
It'll be useful to have a configuration file that you can share between the archiver and
viewer scripts. Create a file called config.rb, and initialize the following constants:
DATABASE_NAME = "twitter-archive"
COLLECTION_NAME = "tweets"
TAGS = ["mongodb", "ruby"]
First you specify the names of the database and collection you'll be using for your
application. Then you define an array of search terms, which you'll send to the Twit-
ter API .
The next step is to write the archiver script. You start with a TweetArchiver class.
You'll instantiate the class with a search term. Then you'll call the update method on
the TweetArchiver instance, which issues a Twitter API call, and save the results to a
MongoDB collection.
Let's start with the class's constructor:
def initialize(tag)
connection = Mongo::Connection.new
db
= connection[DATABASE_NAME]
@tweets
= db[COLLECTION_NAME]
@tweets.create_index([['id', 1]], :unique => true)
@tweets.create_index([['tags', 1], ['id', -1]])
@tag = tag
@tweets_found = 0
end
The initialize method instantiates a connection, a database object, and the collec-
tion object you'll use to store the tweets. This method also creates a couple of indexes.
Search WWH ::




Custom Search