gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:36:44 25 4
gpt4 key购买 nike

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

最佳答案

简单地创建一个插件框架

./script/generate plugin 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")

关于ruby-on-rails - 如何将以下 ruby​​ 代码转换为 Rails 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1250954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com