Rails 3 provides a very simple way to connect your application with TWITTER.
Here are the steps which you might need to post a tweet in your twitter account from your application :
1) First of all go to https://dev.twitter.com/apps and register your application. Make sure that you have configured "OAuth settings" and "Your access token" section in your Twitter application.
[Note : 'Access level' must have the 'read and write' privileges ]
2) Inside your gemfile add the Twitter gem:
gem 'twitter'
3) Next to install the gem simply open the console and do:
$ bundle install
or
$ bundle
1) First of all go to https://dev.twitter.com/apps and register your application. Make sure that you have configured "OAuth settings" and "Your access token" section in your Twitter application.
[Note : 'Access level' must have the 'read and write' privileges ]
2) Inside your gemfile add the Twitter gem:
gem 'twitter'
3) Next to install the gem simply open the console and do:
$ bundle install
or
$ bundle
4) Put the following code inside the view file ( eg, user_page.html.erb ) :
<%= form_for (@tweet = Tweet.new, :url => user_tweet_path) do |tweet_form| %>
<%= tweet_form.text_area :tweet_content, :id => "tweet" %>
<%= tweet_form.submit "Tweet" %>
<% end %>
5) Now go to the respective controller and just add an action to handle the form submission :
def user_tweet
require "rubygems"
require "twitter"
# Certain methods require authentication. To get your Twitter OAuth credentials,
# register an app at http://dev.twitter.com/apps
Twitter.configure do |config|
config.consumer_key = ' << your consumer key >>'
config.consumer_secret = ' << your consumer secret >>'
config.oauth_token = ' << your access token >> '
config.oauth_token_secret = '<< your access token secret >>'
end
# Initialize your Twitter client
client = Twitter::Client.new
# Post a status update
client.update("I just posted a status update via the Twitter Ruby Gem !")
redirect_to request.referer, :notice => 'Tweet successfully posted'
end
6) TADA!!! your application is now connected to the twitter and you can post tweets directly from your application without going to twitter.com
Here are some more examples of functions to integrate with twitter API. Use the following codes inside any action to get the respective results :
<%= form_for (@tweet = Tweet.new, :url => user_tweet_path) do |tweet_form| %>
<%= tweet_form.text_area :tweet_content, :id => "tweet" %>
<%= tweet_form.submit "Tweet" %>
<% end %>
5) Now go to the respective controller and just add an action to handle the form submission :
def user_tweet
require "rubygems"
require "twitter"
# Certain methods require authentication. To get your Twitter OAuth credentials,
# register an app at http://dev.twitter.com/apps
Twitter.configure do |config|
config.consumer_key = ' << your consumer key >>'
config.consumer_secret = ' << your consumer secret >>'
config.oauth_token = ' << your access token >> '
config.oauth_token_secret = '<< your access token secret >>'
end
# Initialize your Twitter client
client = Twitter::Client.new
# Post a status update
client.update("I just posted a status update via the Twitter Ruby Gem !")
redirect_to request.referer, :notice => 'Tweet successfully posted'
end
6) TADA!!! your application is now connected to the twitter and you can post tweets directly from your application without going to twitter.com
Here are some more examples of functions to integrate with twitter API. Use the following codes inside any action to get the respective results :
require "rubygems"
require "twitter"
# Get a user's location
puts Twitter.user("sferik").location
# Get a user's most recent status update
puts Twitter.user_timeline("sferik").first.text
# Get a status update by id
puts Twitter.status(27558893223).text
# Initialize a Twitter search
search = Twitter::Search.new
# Find the 3 most recent marriage proposals to @justinbieber
search.containing("marry me").to("justinbieber").result_type("recent").per_page(3).each do |r|
puts "#{r.from_user}: #{r.text}"
end
# Enough about Justin Bieber
search.clear
# Let's find a Japanese-language status update tagged #ruby
puts search.hashtag("ruby").language("ja").no_retweets.per_page(1).fetch.first.text
# And another
puts search.fetch_next_page.first.text
# Certain methods require authentication. To get your Twitter OAuth credentials,
# register an app at http://dev.twitter.com/apps
Twitter.configure do |config|
config.consumer_key = YOUR_CONSUMER_KEY
config.consumer_secret = YOUR_CONSUMER_SECRET
config.oauth_token = YOUR_OAUTH_TOKEN
config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET
end
# Initialize your Twitter client
client = Twitter::Client.new
# Post a status update
client.update("I just posted a status update via the Twitter Ruby Gem!")
# Read the most recent status update in your home timeline
puts client.home_timeline.first.text
# Who's your most popular friend?
puts client.friends.sort{|a, b| a.followers_count <=> b.followers_count}.reverse.first.name
# Who's your most popular follower?
puts client.followers.sort{|a, b| a.followers_count <=> b.followers_count}.reverse.first.name
# Get your rate limit status
puts client.rate_limit_status.remaining_hits.to_s + " Twitter API request(s) remaining this hour"