我这里有一个名叫GmailGcalendar的模型助手
我在两个模型中有多个动作,即gmail.rb和pivotaltracker.rb
它们执行相同的功能,但唯一的区别是连接url。
在我的lib助手中:
def connection_url
if API::Pivotaltracker
'https://www.pivotaltracker.com'
else
'https://accounts.google.com'
end
end
def my_connections
connection_name ||= Faraday.new(:url => "#{connection_url}" , ssl: {verify: false}) do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
puts "@@@@"
puts connection_url
end在我的gmail.rb里
def connection
my_connections
end还有我的pivotaltracker.rb
def connection
my_connections
end现在他们有了不同的连接网址。
枢轴转到https://www.pivotaltracker.com,Gmail转到https://accounts.google.com
但我似乎无法让条件在connection_url动作中发挥作用。
任何解决办法都将不胜感激。
编辑:
我在这里使用connection_url:(在法拉第街区)
def my_connections
connection_name ||= Faraday.new(:url => "#{connection_url}" , ssl: {verify: false}) do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
puts "@@@@"
puts connection_url
end发布于 2013-08-14 07:23:15
您可以将lib助手重写为类:
class Connection
def initialize(url)
@url = url
end
def setup
connection_name ||= Faraday.new(:url => "@url" , ssl: {verify: false}) do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
end
end现在,在模型中,您可以用您想要的url初始化这个类,例如:
def connection
Connection.new("https://accounts.google.com").setup
end我没有测试这段代码,但它应该能工作。
https://stackoverflow.com/questions/18225268
复制相似问题