Twitter gem

At Craft Academy, we always have practical assessment that aims to assess the skills of the students to realize what we need to enhance or work on to become a better junior developer. A coach will be assigned to each one of us that will give us the problem to solve for 20-30 minutes. Honestly, this is one of the most frightening part for all the students because your coach is looking at you closely while coding that makes you feel conscious or stupid especially if you cannot solve the problem.

The Twitter Challenge

Twitter challenge is the problem that was assigned to us this week where we were asked to write method in an editor and paste them into irb or pry. For this week, they want to assess our ability to read documentation, research and make use of gems and use third party services. We were asked to use a gem called “twitter” that will allow us to access the TwitterAPI in order to get the 5 last tweets from Amber.

Resources

  • The twitter gem can be found on https://github.com/sferik/twitter
     — The user name you want to search for is @heyamberwilkie , twitter account of our supportive coach Amber Wilkie.
     — you need to register an application with Twitter here: https://apps.twitter.com/
     If you are facing a choice what method to use you should go with the “REST Client” (you’ll understand when you’ve read the gem documentation)

This is how I solve the challenge:

  1. I checked the resources given about twitter gem: https://github.com/sferik/twitter
  2. Then I set up the directory

$ mkdir Twitter

$ cd Twitter

3.The I created a file where I wrote my code.

$ touch twitter.rb

4. Afterwards, I installed the Twitter gem.

$ gem install twitter

5. Next step is to make sure that I have a Twitter account and register the application at https://apps.twitter.com/.

Figure 1.0 Form to create an application

It requires the app Name, Description, and URL. Fill out the info. You can use something like “http://www.webpage.com” to get started with and update it later. Fill out the required info and create your new app.

Once it is done, we will be taken to the Application details page. Click the the “Key and Access Tokens” tab to see the details we need for our app. . This is where you’ll find your Consumer Key and Consumer Secret Keys. Well, there’s one more step that we need to do before we’re going to be ready to go. Scroll down to the bottom to “Token Actions” and create a new access token. This will create the new “Access Token” and “Access Token Secret” needed to connect to Twitter’s API.

Figure 2.0 Application details page

After getting all the keys and tokens needed, I started my program by typing at the first line.

require twitter

Then I wrote my own details in the following block of code:

client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end

In this code, I created a client object to communicate with the Twitter API. Don’t forget to fill in the config variables with your information from the Twitter app settings page.

Now that I have everything connected I can view the five (5) recent tweets of Amber by adding the code below:

client.search("from:heyamberwilkie", result_type: "recent").take(5).each do |tweet|
puts tweet.text
end

Result in irb:

16:51 $ irb
2.3.3 :001 > load 'twitter.rb'
@victoriawagman Re: photo booking websites! https://t.co/WN6bxza6BA
I just published "My Favorite Things Right Now" https://t.co/D4sOrBSDTE
I just published "'Angular2 — The Complete Guide' readalong: Part Two, Data Binding" https://t.co/gF3HMtTUn8
RT @ACLU: BREAKING: Congress fast-tracks resolution to allow internet providers to sell consumer data without permission https://t.co/2wVpc
Sunday Lasagna: Go "I can't believe how much fucking lasagna this is" or go home.
=> true

I also added one more feature, to view the 10 recent tweets about ruby and here it goes:

puts 'Viewing 10 recent tweets about ruby'
client.search("#ruby").take(10).each do |tweet|
puts tweet.text
end

Result in irb:

Viewing 10 recent tweets about ruby
#Capricorn Star Sign Gemstone Bead Bracelet - #Handmade - #ruby gem beads https://t.co/VzSsWE3kzd
Don't forget the Learn #Ruby in DC! meetup tomorrow. RSVP while you still can: https://t.co/4TWDX4T64F
RT @hashrockettil: Chaining Multiple RSpec Change Matchers https://t.co/1zI15Cy4HT via @jbrancha #til #ruby
HNews: From Ruby to Crystal: A Quick Look https://t.co/Ir9Gz28jDy #ruby
RT @RoRNinjas: [Brasilian] Pratical Guide: Spaceship-operator https://t.co/8h6MV5ILer #ruby #rubyonrails
[Brasilian] Pratical Guide: Spaceship-operator https://t.co/8h6MV5ILer #ruby #rubyonrails
RT @microcite: L'équipe s'échauffe pour le dossier FCG de @PaulBohec ce sera physique, ce sera brûlant ! @Les_Mammouths38 #Ruby 🏉 https://t
#womens #yellowgold #rounddiamond #ruby #ring https://t.co/J6zFXTsjsE
Torenc Roberts: https://t.co/I5FCre6IM7; is For Sale #Interact #C #html #Java #Ruby #Python #CSS #Nodejs #Clojure … https://t.co/wzEouZeCJP
RT @inj3ct0r: #0daytoday #Ruby on Rails 4.0.x / 4.1.x / 4.2.x (Web Console v2) - Whitelist Bypass Code Execution [#0day #Exploit] https://t

Well, I can say that failing to complete the assessment in the required duration might be frustrating but as a student we must take it positively. We can think of it like we are discovering our strength and weaknesses. Therefore, we can maintain the strength and work hard to turn our weaknesses into strength. There will always be room for improvement as long as we are determined to enhance our coding skills.