ruby – 如何用httparty切换base_uri

前端之家收集整理的这篇文章主要介绍了ruby – 如何用httparty切换base_uri前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将一个参数传递给一个登录方法,我想基于该参数来切换基本的uri.

像这样:

class Managementdb
  include HTTParty

  def self.login(game_name)
        case game_name
        when "game1"
            self.base_uri = "http://game1"
        when "game2"
            self.base_uri = "http://game2"
        when "game3"
            self.base_uri = "http://game3"
        end

    response = self.get("/login")

        if response.success?
      @authToken = response["authToken"]
    else
      # this just raises the net/http response that was raised
      raise response.response    
    end
  end

  ...

当我从一个方法中称之为基本的uri时,我该怎么办?

解决方法

在HTTParty中,base_uri是一个设置内部选项哈希的类方法.要从您的自定义方法登录中动态更改它,您可以将其称为方法(不将其分配为变量).

例如,要更改上面的代码,应该按照您的期望设置base_uri:

...
case game_name
  when "game1"
    # call it as a method
    self.base_uri "http://game1"
...

希望它有帮助.

原文链接:https://www.f2er.com/ruby/271828.html

猜你在找的Ruby相关文章