Ruby URI::使用HttpParty时发生InvalidURIError
我遵循了来自的一个示例,并得出以下结论:Ruby URI::使用HttpParty时发生InvalidURIError,ruby,sinatra,httparty,Ruby,Sinatra,Httparty,我遵循了来自的一个示例,并得出以下结论: 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("/GetMatchHisto
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?)
但是,当我使用以下更简单的解决方案时,它返回的结果非常好:
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\u uri
是一个类方法,因此您应该在类中定义它,而不是在初始值设定项中。您可以在您提供的链接的第一个示例中看到它
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
对不起,那是我的错。我已经编辑了这个问题,但它没有解决问题。等号是不必要的!由于缺少括号,我不知何故认为base_uri是一个可值的方法,而不是一个方法。只是我的粗心。