加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何用httparty切换base_uri

发布时间:2020-12-16 19:44:40 所属栏目:百科 来源:网络整理
导读:我试图将一个参数传递给一个登录方法,我想基于该参数来切换基本的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"
我试图将一个参数传递给一个登录方法,我想基于该参数来切换基本的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"
...

希望它有帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读