如何将以下 ruby 代码转换为 Rails 插件?
#!/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单地创建一个插件骨架
然后在 RAILS_ROOT/vendor/plugins/fb_tweet/lib/fb_tweet.rb 文件中输入代码内容:
现在,打开控制台并简单地输入:
FbTweet.tweet("www.example.com ”)
Simply create a plugin skeleton
then in the RAILS_ROOT/vendor/plugins/fb_tweet/lib/fb_tweet.rb file put the contents of your code:
Now, open your console and simply enter:
FbTweet.tweet("www.example.com")