views:

292

answers:

1
#!/opt/ruby_ee/bin
 require 'rubygems'
 require 'twitter'
 require 'net/http'
 require 'uri'
 require 'httparty'
 module FbTweet

  def FbTweet.tweet(url)
   bitly_url = Bitly.shorten(url)
   httpauth = Twitter::HTTPAuth.new('username', 'password')
   base = Twitter::Base.new(httpauth)
   base.update("Testing to port this into rails plugin #{bitly_url}")
   base.friends_timeline.each { |tweet| puts tweet.text }
  end

  class Bitly
   include HTTParty
   base_uri 'api.bit.ly'
   basic_auth 'millisami', 'R_sadfdsfwefsdf2323fafasd'
   format :json
   def self.shorten(url)
    response = get('/shorten', :query => required_params.merge(:longUrl => url))
    response['results'][url]['shortUrl']
   end
   def self.required_params
    {:version => "2.0.1"}
   end
  end

 end
 puts "Calling twitter to test.."
 puts FbTweet.tweet("http://google.com")
+2  A: 

Simply create a plugin skeleton

./script/generate plugin FbTweet

then in the RAILS_ROOT/vendor/plugins/fb_tweet/lib/fb_tweet.rb file put the contents of your code:

  require 'rubygems'
    require 'twitter'
    require 'net/http'
    require 'uri'
    require 'httparty'
    module FbTweet

            def FbTweet.tweet(url)
                    bitly_url = Bitly.shorten(url)
                    httpauth = Twitter::HTTPAuth.new('newnepal', 'wnn_tweet_pwd')
                    base = Twitter::Base.new(httpauth)
                    base.update("Testing to port this into rails plugin #{bitly_url}")
                    base.friends_timeline.each { |tweet| puts tweet.text }
            end

            class Bitly
                    include HTTParty
                    base_uri 'api.bit.ly'
                    basic_auth 'millisami', 'R_fca41718b34e48a06daf0260c5919cf0'
                    format :json
                    def self.shorten(url)
                            response = get('/shorten', :query => required_params.merge(:longUrl => url))
                            response['results'][url]['shortUrl']
                    end
                    def self.required_params
                            {:version => "2.0.1"}
                    end
            end

    end

Now, open your console and simply enter:

FbTweet.tweet("www.example.com")

astropanic
s/vedor/vendor/
obvio171