无法在Ruby模块方法中使用Curl URL

时间:2015-05-01 13:44:31

标签: ruby curl curb

我遇到问题,无法使用以下任何一种方法(1,2和3)。

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   来自的网址   /Users/home/.rvm/gems/ruby-2.0.0-p598/gems/curb-0.8.8/lib/curl/easy.rb:72:in   `执行'

当我运行 call_method(@ method_3_url)时,它似乎确实正常运行。

我也可以使用原始的POST网址并将其粘贴到Chrome中,然后就可以了。

我花了几个小时在网上寻找解决方案,而且我似乎无法使其正常工作......使用HTTParty时我也遇到了类似的错误。请帮忙:-)

1 个答案:

答案 0 :(得分:0)

您的实例变量不在模块中,因此超出了范围。

而不是:

@foo = 'bar'

module Foo
  ...
end

您正在寻找:

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