如何将以下 ruby​​ 代码转换为 Rails 插件?

发布于 2024-07-30 21:00:04 字数 1091 浏览 4 评论 0原文

#!/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")
#!/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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话里做英雄 2024-08-06 21:00:06

简单地创建一个插件骨架

./script/生成插件FbTweet

然后在 RAILS_ROOT/vendor/plugins/fb_tweet/lib/fb_tweet.rb 文件中输入代码内容:

  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

现在,打开控制台并简单地输入:

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

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")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文