Friday 18 November 2011

Mongrel gem installation problem in Rails 3 Solved

Sometimes I have seen developers complaining that the 'Mongrel' gem version '1.1.5' has some installation problem when they try it out in Rails 3.

It shows an error something like this:

$ gem install mongrel
Building native extensions.  This could take a while...
ERROR:  Error installing mongrel:
ERROR: Failed to build gem native extension.

        /home/souvikd/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
checking for main() in -lc... yes
creating Makefile
make
gcc -I. -I/home/souvikd/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/i686-linux -I/home/souvikd/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/ruby/backward -I/home/souvikd/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1 -I. -D_FILE_OFFSET_BITS=64  -fPIC -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long  -fPIC  -o http11_parser.o -c http11_parser.c
http11_parser.rl: In function ‘http_parser_execute’:
http11_parser.rl:105:3: warning: comparison between signed and unsigned integer expressions
gcc -I. -I/home/souvikd/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/i686-linux -I/home/souvikd/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/ruby/backward -I/home/souvikd/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1 -I. -D_FILE_OFFSET_BITS=64  -fPIC -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long  -fPIC  -o http11.o -c http11.c
http11.c: In function ‘http_field’:
http11.c:70:3: warning: format not a string literal and no format arguments
http11.c:71:3: warning: format not a string literal and no format arguments
http11.c:77:22: error: ‘struct RString’ has no member named ‘ptr’
http11.c:77:50: error: ‘struct RString’ has no member named ‘len’
http11.c: In function ‘request_uri’:
http11.c:102:3: warning: format not a string literal and no format arguments
http11.c: In function ‘fragment’:
http11.c:113:3: warning: format not a string literal and no format arguments
http11.c: In function ‘request_path’:
http11.c:124:3: warning: format not a string literal and no format arguments
http11.c: In function ‘query_string’:
http11.c:135:3: warning: format not a string literal and no format arguments
http11.c: In function ‘header_done’:
http11.c:172:13: error: ‘struct RString’ has no member named ‘ptr’
http11.c:172:13: error: ‘struct RString’ has no member named ‘ptr’
http11.c:172:13: error: ‘struct RString’ has no member named ‘ptr’
http11.c:174:89: error: ‘struct RString’ has no member named ‘ptr’
http11.c:176:52: error: ‘struct RString’ has no member named ‘ptr’
http11.c:177:26: error: ‘struct RString’ has no member named ‘len’
http11.c: In function ‘HttpParser_execute’:
http11.c:298:23: error: ‘struct RString’ has no member named ‘ptr’
http11.c:299:23: error: ‘struct RString’ has no member named ‘len’
http11.c:307:5: warning: format not a string literal and no format arguments
make: *** [http11.o] Error 1

Gem files will remain installed in /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/mongrel-1.1.5 for inspection.
Results logged to /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/mongrel-1.1.5/ext/http11/gem_make.out

Here's what you can do to fix this issue:

1) Go to gemfile.lock and change daemons (1.1.4) to daemons (1.0.10)

[ This previous version of daemons is required to install the Mongrel gem version 1.2.0.pre2 ]

2) Next to update the gem configuration , we run
$ bundle

3) Go to gemfile and add the lines
gem "mongrel", "1.2.0.pre2"

[ This version of the mongrel gem is compatible with Rails 3 ]

4) Again we give bundle command to install the gem
$ bundle

Done!! now you run the rails server and it will boot Mongrel rather than the default WEBrick server.



Tuesday 15 November 2011

Rails 3 and Twitter API

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

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 :


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"