API 集成错误 HTTParty

API integration error HTTParty

我正在学习如何使用 HTTParty 和 API,但我的代码有问题。

Users/admin/.rbenv/versions/2.0.0-p481/lib/ruby/2.0.0/uri/generic.rb:214:in `initialize': the scheme http does not accept registry part: :80 (or bad hostname?) 

我尝试使用 debug_output STDOUT 作为我的方法的参数,并在包含 HTTParty 之后获得线索但没有成功。未显示任何内容:

     require 'httparty'

      class LolObserver
        include HTTParty
        default_timeout(1) #timeout after 1 second

        attr_reader :api_key, :playerid
        attr_accessor :region
        def initialize(region,playerid,apikey)
          @region = region_server(region)
          @playerid = playerid
          @api_key = apikey
        end

        def region_server(region)
          case region
          when "euw"
            self.class.base_uri "https://euw.api.pvp.net"
            self.region = "EUW1"
          when "na"
            self.class.base_uri "https://na.api.pvp.net"
            self.region = "NA1"
          end
        end

        def handle_timeouts
          begin
            yield
          #Timeout::Error, is raised if a chunk of the response cannot be read within the read_timeout.
          #Timeout::Error, is raised if a connection cannot be created within the open_timeout.
          rescue Net::OpenTimeout, Net::ReadTimeout
            #todo
          end
        end

        def base_path
          "/observer-mode/rest/consumer/getSpectatorGameInfo"
        end

        def current_game_info
          handle_timeouts do
            url = "#{ base_path }/#{region}/#{playerid}?api_key=#{api_key}"
            puts '------------------------------'
            puts url
            HTTParty.get(url,:debug_output => $stdout)
          end
        end

      end

我验证了我的 URL 没问题,所以我不知道问题出在哪里。

我用静态 base_uri 进行了测试,它没有任何改变。

奇怪的是当我这样做的时候:

 HTTParty.get("https://euw.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/EUW1/randomid?api_key=myapikey")

一切正常,我正在收到回复。

HTTPParty 似乎不喜欢您设置 base_uri 的方式。

除非您需要这样,否则只需添加另一个名为 domainattr_reader 即可。

require 'httparty'

class LolObserver
 include HTTParty
 default_timeout(1) #timeout after 1 second

 attr_reader :api_key, :playerid, :domain
 attr_accessor :region
 def initialize(region,playerid,apikey)
   @region = region_server(region)
   @playerid = playerid
   @api_key = apikey
 end

 def region_server(region)
   case region
   when "euw"
     @domain = "https://euw.api.pvp.net"
     self.region = "EUW1"
   when "na"
     @domain = "https://na.api.pvp.net"
     self.region = "NA1"
   end
 end

 def handle_timeouts
   begin
     yield
   #Timeout::Error, is raised if a chunk of the response cannot be read within the read_timeout.
   #Timeout::Error, is raised if a connection cannot be created within the open_timeout.
   rescue Net::OpenTimeout, Net::ReadTimeout
     #todo
   end
 end

 def base_path
   "/observer-mode/rest/consumer/getSpectatorGameInfo"
 end

 def current_game_info
   handle_timeouts do
     url = "#{domain}/#{ base_path }/#{region}/#{playerid}?api_key=#{api_key}"
     puts '------------------------------'
     puts url
     HTTParty.get(url,:debug_output => $stdout)
   end
 end

 end