jayD
jayD

Reputation: 819

API integration error HTTParty

I'm learning how to work with HTTParty and API and I'm having an issue with my code.

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?) 

I've tried using debug_output STDOUT both as an argument to my method and after including HTTParty to have a clue but with no success. Nothing gets displayed:

     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

I verified my URL which is fine so I'm lost as to where the problem is coming from.

I tested with a static base_uri and it doesn't change anything.

The odd thing is when I do:

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

Everything is working fine and I'm getting a response.

Upvotes: 2

Views: 511

Answers (1)

blnc
blnc

Reputation: 4404

HTTParty doesn't seem to like the way you set your base_uri.

Unless you need it to be like that just add another attr_reader called domain and it will work.

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

Upvotes: 1

Related Questions