URI::InvalidURIError 使用 HTTParty 时

URI::InvalidURIError When Using HTTParty

我按照 HTTParty github page 中的一个例子得出了这个:

class MatchHistory
    include HTTParty
    base_uri = "api.steampowered.com/IDOTA2Match_570"

    def initialize
        @options = { query: { key: STEAM_API_KEY } }
    end

    def latest
        self.class.get("/GetMatchHistory/V001", @options)
    end
end

get '/' do 
    history = MatchHistory.new

    history.latest.body
end

我收到以下错误:

URI::InvalidURIError at /
the scheme http does not accept registry part: :80 (or bad hostname?)

但是,当我使用如下更简单的解决方案时,returns 结果很好:

class MatchHistory
    def initialize
        @base_uri = "http://api.steampowered.com/IDOTA2Match_570"
    end

    def latest
        HTTParty.get(@base_uri + "/GetMatchHistory/V001/?key=" + STEAM_API_KEY)
    end
end

base_uri 是一个 class 方法,因此您应该在 class 中定义它,而不是在您的初始化程序中。您可以在您提供的 link 中的第一个示例中看到它。

class MatchHistory
    include HTTParty

    base_uri "api.steampowered.com/IDOTA2Match_570"

    def initialize
        @options = { query: { key: STEAM_API_KEY } }
    end

    def latest
        self.class.get("/GetMatchHistory/V001", @options)
    end
end