rubycurlcurb

Can't get Curl URL to work inside a Ruby Module Method


I am having a problem where I can't get any of the following methods, (1, 2 and 3) to work.

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

I get the following error:

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'

When I run call_method(@method_3_url) it does seem to work correctly.

I can also take the original POST URL and paste it into Chrome and it'll work..

I have spent hours looking for a solution online for this and I can't seem to make it work.. I also get a similar error when using HTTParty. Please help :-)


Solution

  • Your instance variables aren't in the module, and are therefore out of scope.

    Instead of:

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

    You're looking for:

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