Integrating the Twitter Gem to Pull in Tweets in Rails 5
This guide examines how to integrate the Twitter gem, configure a connection to the Twitter API, and how to query tweets so that they can be utilized by the Rails application.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Nice work in the last section that was quite a bit of formwork. Before I close this out I'm going to finish and make sure we close out each of the tasks inside a pivotal tracker. And now I'm going to drag over working with the lib directory and this is what we're going to get started in this section.

What we are going to do here is we're going to update these tasks because what I want to do and I've researched a few different variations of what I wanted to implement. Originally I was going to integrate the http gem for outside data calls but after I started playing around with it a little bit I thought it'd be fun to bring in news, tech news, or whatever kind of news that you want to focus on in from Twitter, so I thought I'd be kind of a cool way to integrate the Twitter gems. So I'm going to say integrate Twitter gem for tech news. And then we are going to call the data from the module. And we're also going to parse the data for views. We'll walk through what that actually means when we get to that section.

Before we do anything else, part of the reason why this section exists in the course is because I want to show you how you can leverage the lib directory and how you can utilize that in your applications. The lib directory is a great one for building out functionality that you may someday want to export into its own gem. It's a very good testing ground for that because you're able to essentially isolate your code without having to have it connected to any other part of the application and you can call it whenever you need it.

One of the most important parts about this is it speaks directly into the ability to write code that is loosely coupled, which means that we're writing code that is not so tightly connected to the rest of the app that if we make one change we have to go and it creates this kind of domino effect where we have to go and make changes to the rest of the application. So I thought this would be a good type of feature to build in and we can walk through how to build things in with the lib directory. Like I've mentioned earlier on the course, one trend I've noticed without it even being on purpose is the more advanced kinds of applications that I build, the more I find myself spending additional time in the lib directory because my focus is really high on being able to isolate code components and spend less time in the actual app itself and spend more time building out functionality that could be used across multiple services and then simply calling those from the app. That's kind of the practice that we're going to work on right here.

I'm going to switch into the terminal and right now we're on the master branch. I'm going to type git status just to make sure that we don't have any pending changes. I'm going to type git checkout -b lib. Now we're in the lib directory. I'm going to open up Sublime Text and let's start with talking about what we will need in order to implement the lib directory and to call from it.

In earlier versions of rails you could simply start throwing modules into the lib directory and they would all be loaded. The process of that was called eager loading of the lib. Now in Rails 5 we have to manually call this. The first thing we need to do is open up our application rb file. Go in your config directory and it's going to be this one, config/application.rb. Inside of this application class we are going to add a new line of code and it's going to be config dot eager underscore load underscore paths and we are going to pipe in a new path. Because you can see that we're using this operator that kind of tells us that our eager load paths is an array and we are adding a new element to it. So what we're passing in is the rule that says, hey rails when you start up the application I want you to load any of the modules we have inside of the lib directory and we can do that by doing some string interpellation and saying rails dot root which is going to say the root of this particular application and say slash lib.

config.eager_load_ paths << "#{Rails.root}/lib"

If that looks confusing all it's saying here is Rails root which means our dev dev camp portfolio route I want you to pass in this slash lib path which is right here and it's saying simply make that available to the rest of the application. So with that in place what we can do is we can come into the lib directory and create a new file. Much like our concerns and much like many of the other files in rails the naming is very important. If we have a module, and I'm going to call this module our social tool, that means that we have to name the file social_tool.rb.

medium

And now we can have a module called socialTool. Make sure that that is named and also because we put an underscore there the t in tool has to be capitalized so we're going to use this camel case. Now inside of this, we're going to have just one method and it's going to be a self-method so it's going to be a class method that can be called directly on the module. And I'm going to call it Twitter_search and hit save and obviously, we don't have a connection to Twitter yet.

That's the next thing we're going to do. Let's open up rubygems.org and I'll include this in the show notes. This is the official Twitter gem. And when I say official I mean it's the one that has been used by far the most as nearly three point nine million downloads so this is a good one to pick. Hit copy to clipboard and let's come in a sublime text. Open up the gem file on the very bottom add Twitter.

Now inside the terminal run bundle install.

And let's go take a look at the Twitter documentation. Click on home page and open this up in a new tab. That is the actual documentation. I want the source code and that's something that you will see sometimes is the home page may at times can link to a different page like you saw when we created our own gem. These are not automated, whoever the developer is passes these in and sometimes they think of the home page being the GitHub page and other times they think it means something else. If we come here to the GitHub page this has all of the instructions that we are going to need. So this has everything from being able to pull in the Twitter client all the way through the usage examples and we're going to implement the one based on search.

large

That's right here. This is one we're going to pull in. So we're going to run a search and it is going to run some type of search query and then it's going to run a recent one and we can pick the latest ones we want. We're going to collect them, this as a Ruby method which means we're going to have an array and then inside of it we're going to have the user's screen name followed by the text of the tweet. So that is what we are going to implement but we're not going to do that quite yet because we're not ready to yet. In order to communicate with Twitter we have to go and we have to create a connection to Twitter which means that we need to get a consumer key a consumer secret an access token and an access token secret.

Now they give a link right here. So if you go to the page there's a little link here that says register your application with Twitter. I have this page open right here just with a couple Twitter apps that I have made including one that I made for this guide.

What you're going to want to do is one if you don't have a Twitter account you can just sign up for a free Twitter account if you're following along. And once you've done that go to apps.twitter.com and you're going to click on Create a new app.

medium

Now because there's going to be a lot of API credentials in this. What I am going to do is I'm going to pause the video right now. I'm going to go through the process of creating a new application and I'm not going to video it. I am going to screenshot it so that I can show you. But I also can cover up my own secret key and credentials and those things just for security purposes. To reiterate I'm a little bit paranoid about sharing any kinds of credentials. You definitely should be as well because if those get in the wrong hands then you could have things happen like imagine this scenario, if a hacker got my credentials then they could go create a bunch of spammy apps or something like that and I could have my entire Twitter account closed down which would not be a good thing.

I'm going to hit pause and I'm going to take some screenshots and then show those here on the screen so you can see exactly where to get the credentials. Remember we're looking for a consumer key a consumer secret an access token and access token secret. I will see you after that filming has been done.

large

In the screenshot right here. I clicked on the keys and access tokens at the very top of the page. And that is going to have only the information we need all on one page. You have your consumer key and your consumer secret key. These are both of your API keys and those are the first two ones that I highlighted in the application settings box.

Moving down you're going to need access tokens. By default if you have never used Twitter before as specifically you've never used it for this purpose you're going to have to generate them. So in the bottom yours is going to look a little bit different. It's going to ask you do you want to generate keys just hit yes that you do or hit generate keys, the button that's there, and then it's going to generate it and you're going to see access tokens so you're going to see exactly what I have here. You're going to have two of them. One is going to be the access token. The other is going to say access token secret. After you have that take all of those those four keys and then copy them all over into your .env file. So the same spot that we put all of our carrier wave I should say all of our AWS keys, put them all in there and they're going to need to be mapped to what we're going to go through next.

Switching back to the Twitter documentation page right here. You can see this client call in the middle of the page, this has the full set of items.

large

In terms of a mapping you have the ability to call these whatever you want. I'm going to actually grab all of this code right here. Let's come into our social tool paste it in and indent it. Here we will pick out exactly the .env keys that we're going to use. Here I'm going to say API or I should say Twitter consumer key. I'm going to say Twitter consumer secret. And then Twitter access token and Twitter access secret. Now this by itself is not going to work. Remember we have to do Env.fetch and then wrap this entire call right here. And let's come and do it each one of these and then add the end of the parentheses.

module SocialTool
  def self.twitter_search
    client = Twitter::REST::Client.new do |config|
      config.consumer_key.       = ENV.fetch("TWITTER_CONSUMER_KEY")
      config.consumer_secret.    = ENV.fetch("TWITTER_CONSUMER_SECRET")
      config.access_token.       = ENV.fetch("TWITTER_ACCESS_TOKEN")
      config.access_token_secret = ENV.fetch("TWITTER_ACCESS_SECRET")
    end
  end
end

What this is going to allow us to do is go into our .env file and our .env should look something like this. Obviously I'm not going to show you my .env file but this is going to be the next best thing. I'll show you exactly what it should look like. So you come right here select all these items. So it's going to look exactly like this followed by whatever those values were inside of your Twitter dashboard.

medium

So fill those out and as soon as you do when you run this then all of it is going to be available to you. So feel free to pause the video right now, add those into your .env file and then when we come back I will test it out to make sure that those are working.

And we're back and assuming that you followed along you now have your .env filled with these items. I added one more entry called fake Twitter consumer key just to test this out. And now if I come hit rails c and paste this in. Uninitialized constant Env. And that's because I just made a mistake. This should be ENV with all capitals.

large

So now if I type in reload! and, I guess technically I didn't have to hit reload. All I'm doing is checking to make sure that this works. So if I do fake Twitter consumer key now you can see that it prints out not my real key.

medium

If I did my Twitter consumer key, my full one then it would've printed it out. It's a good thing they actually have that misspelling.

Now that we know that that is working we can be confident that when this runs it's going to go and it's going to grab each of those keys and it's going to make it available to the Twitter rest client. So everything there is good.

Let's move down. Our Twitter search functionality needs to actually search. So we're going to take the client that we just created and we're going to call search on it and inside of this we can put anything that we want. I'm going to do hash tag Rails

client.search("#rails", result_type: 'recent')

because I want to actually show Rails tweets. So this is going to be rails and the result type is going to be recent. And these are things that are just specific to the Twitter rest client. So this is something that is specific to the gem not to rails or anything like that. All I'm doing is I'm essentially going through the case study that they gave right here. Their example was how to see all of the Justin Bieber marriage proposals. You totally are fine if you want to show that on your portfolio. Not my thing though so I'm going to instead switch it up and use any tweets that say hashtag rails.

So now if I want to say, take six of these. This is going to grab the latest six and then I want to call collect.

client.search("#rails", result_type: 'recent').take(6).collect do

end

Now this is a Ruby method so this is going to grab them and convert it into an array. Now collect takes a block. So inside of this what we want, and we need to pass in a block variable that we can say tweet. And now we can have access to this and do whatever we want with it. So we want the user's screen name. Whoever made the tweet and so we'll grab this block variable here. Tweet dot user dot screen name. Then just right here we can put in the text of the tweet. Feel free to explore the documentation if you want to have anything different than that.

client.search("#rails", result_type: 'recent').take(6).collect do |tweet|
  "#{tweet.user.screen_name}: #{tweet.text}"
end

So with all of this in place technically this should work. So let's keep our fingers crossed and let's open up the rails console and see. This is going to be SocialTool.twitter_search and running that, look there. These are the rails tweets coming in stored in an array for us.

large

So everything here is working. We have all kinds of tweets. Even ones coming in internationally. And like I said, feel free to put in any kinds of search parameters you want. I'm just putting in rails because I think it's a nice simple one. You could do something like change it up and say Ruby on Rails. I definitely recommend for you to explore searching on Twitter itself for these items and then you can have them brought down into your own system. I'm going to type reload! which reloads the code into the rails console. Run this again. And now it's going to bring up a whole set of different items so you can see that now it's bringing in any tags that say Ruby on Rails along with all of the various text options and who's posting and all that kind of cool stuff.

large

So that is pretty neat. Within a pretty short amount of code we are actively searching on Twitter and we're bringing back content. Real content, real tweets that we can use in our own application.

So I think this is a good stopping point for us. I'm going to say implemented initial connection to Twitter API. Now we can push this up to the lib branch.

medium

Let's check pivotal tracker to see if we can cross anything off here. Build module and integrate into the app, app pipeline, integrate Twitter gem for tech news. Look at that we are running right through these tasks.

As cool as it is to call things from the rails console that really isn't going to impress anyone coming to your portfolio. So in the next guide we're going to see what we need to do to implement this and call it from the application.

Resources