SEND TWEETS FROM RAILS APPLICATION

So, we’re continuing from here: https://puneetitengineer.wordpress.com/2009/08/12/send-tweets-fr…ls-application/

So far we’ve covered the basics of it, now its time to create some controllers…. Open your command/terminal and fire this command:

ruby script/generate controller twitt

Once you’re done, open this file and write the below code inside it:

class TwittController < ApplicationController

gem(‘twitter4r’, ‘>=0.2.0′)
require ‘twitter’
require ‘twitter_search’
require ‘rubygems’
before_filter :authorize, :except => [:login, :authenticate, :save_users, :twitter_cred]

def login
@user = User.new
end

def authenticate

@user = User.new(params[:user])
client = Twitter::Client.new
if client.authenticate?(@user.username, @user.password)
@username = User.save_users(params[:user])
session[:user_id] = @user.id
session[:username] = @user.username
session[:password] = @user.password
redirect_to :action =>’post_status’
else
render :action =>’login’
end

end

end

Now, open /app/views/twitt/login.html.erb and paste below lines:

<% form_for :user, :url => { :action => :authenticate } do |form| %>

<%= label :user, :username, “UserName:” %>
<%= form.text_field :username, :size => 30 %>

<%= label :user, :password, “Password:” %>
<%= form.password_field :password, :size => 30 %>

<%= submit_tag “login” , :class => “submit” %>
<% end %>

Advertisements

Send Tweets from rails application

Hey all,

I hope this article will help you if you want to send tweets from your rails application. I have used couple of gems and plugins for that, will describe you those in this tutorial.

AIM: We have a rails application which will allow twitter user to log-in. Once the user logged-in he/she can send tweets from your rails application.

DEPENDENCIES: 1. geokit-rails [Plugin]
2. ym4r-gm [Plugin]
3. twitter4r [Gem]
4. twitter-search [Gem]
5. twitter-console [Gem]
6. google-geocode [Gem] [Optional]

INSTALLATION:
1. Install twitter4r (gem install twitter4r[for windows user], sudo gem install twitter4r[for linux users])
2. Install twitter-search (sudo gem install dancroak-twitter-search -s http://gems.github.com)
3. Install twitter-console *for linux users only
[Follow this: http://www.fsckin.com/2008/03/31/twitter-clients-for-linux/%5D
[and this: http://blog.guillermoamaral.com/2007/03/18/twitter-console-update/%5D
4. Install google-geocode (sudo gem install google-geocode)
5. Install goekit-rails
5.1 ruby script/plugin install git://github.com/andre/geokit-rails.git
5.2 Add this line to your environment.rb: config.gem “geokit”
5.3 Tell Rails to install the gem: rake gems:install
(for more details visit this link: http://github.com/andre/geokit-rails/tree/master)
6. Install ym4r_gm (ruby script/plugin install svn://rubyforge.org/var/svn/ym4r/Plugins/GM/trunk/ym4r_gm)
[if you get trouble in installation follow this: http://ym4r.rubyforge.org/%5D

So we have a base setup for our rails application. Now lets have some code.
First of all lets create few models as well as migrations tables:
1. ruby script/generate model Keyword
2. ruby script/generate model Location
3. ruby script/generate model Post
4. ruby script/generate model User
5. ruby script/generate model Techtwit
6. ruby script/generate migration sessions

After creating all the above steps lets create some columns for our tables:
1. for keywords: timestamps
2. for locations: address:text, timestamps
3. for posts: message: text, timestamps
4. for users: username:string, password:string
5. for techtwits: twitter_id:string, timestamps
6. for sessions: session_id:string, data:text, timestamps
6.1 add_index :sessions, :session_id
6.2 add_index :sessions, :updated_at

Follow this Tutorial to finish-off this application.