2015-05-01 35 views
0

我遇到問題,無法獲取以下任何方法(1,2和3)。無法讓Curl URL在Ruby模塊內部工作

require "curb" 

@username = 'user' 
@api_key = 'key' 
@base_uri = 'https://url.com' 
@offer_id = 999 
@login_method = "login=#{@username}&api_key=#{@api_key}" 
@method_3_url ="#{@base_uri}/3/?#{@login_method}" 

module My_script 
    def self.call_method(url) 
    Curl::Easy.http_get(url){|curl| curl.follow_location = true; curl.max_redirects=10;} 
    end 

    def self.method1 
    call_method("#{@base_uri}/1/#{@login_method}") 
    end 

    def self.method2 
    call_method("#{@base_uri}/2/?#{@login_method}") 
    end 

    def self.method3 
    call_method("#{@base_uri}/3/?#{@login_method}") 
    end 
end 

我得到以下錯誤:

Curl::Err::MalformedURLError: URL using bad/illegal format or missing URL from /Users/home/.rvm/gems/ruby-2.0.0-p598/gems/curb-0.8.8/lib/curl/easy.rb:72:in `perform'

當我運行call_method(@ method_3_url)它似乎正常工作。

我也可以把原帖網址,並將其粘貼到瀏覽器,它會工作..

我花了幾個小時這個尋找一個解決方案在線和我似乎無法使它工作。當使用HTTParty時,我也會遇到類似的錯誤。請幫助:-)

回答

0

您的實例變量不在模塊中,因此超出了範圍。

相反的:

@foo = 'bar' 

module Foo 
    ... 
end 

您正在尋找:

module Foo 
    @foo = 'bar' 
    ... 
end