Sha256: 385ad734de8a374451caef533ebb0943c88d7672d0db35da04c0d137fc9b8cd8

Contents?: true

Size: 1.9 KB

Versions: 56

Compression:

Stored size: 1.9 KB

Contents

module Sportradar
  module Api
    class Request

      include HTTParty

      # attr_reader :url, :headers, :timeout, :api_key

      def get(path, options={})
        url, headers, options, timeout = base_setup(path, options)
        begin
          # puts url + "?api_key=#{api_key[:api_key]}" # uncomment for debugging
          response = self.class.get(url, headers: headers, query: options, timeout: timeout)
          rescue Net::ReadTimeout, Net::OpenTimeout
            raise Sportradar::Api::Error::Timeout
          rescue EOFError
            raise Sportradar::Api::Error::NoData
        end
        return Sportradar::Api::Error.new(response.code, response.message, response) unless response.success?
        response
      end

      def get_request_info(url)
        base_setup(request_url(url))
      end

      private

      def base_setup(path, options={})
        @url = set_base(path)
        @url += format unless options[:format] == 'none'
        @headers = set_headers unless options[:format] == 'none'
        @timeout = options.delete(:api_timeout) || Sportradar::Api.config.api_timeout
        [@url, @headers, options.merge(api_key), @timeout]
      end

      def set_base(path)
        protocol = !!Sportradar::Api.config.use_ssl ? "https://" : "http://"
        url = "#{protocol}api.sportradar.us"
        url += path
      end
      

      def date_path(date)
        "#{date.year}/#{date.month}/#{date.day}"
      end

      def week_path(year, season, week)
        "#{ year }/#{ season }/#{ week }"
      end


      def format
        ".#{content_format}" if Sportradar::Api.config.format
      end

      def set_headers
        {'Content-Type' => "application/#{content_format}", 'Accept' => "application/#{content_format}"}
      end

      def content_format
        Sportradar::Api.config.format
      end

      def api_key
        raise Sportradar::Api::Error::NoApiKey
      end
    end
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
sportradar-api-0.10.55 lib/sportradar/api/request.rb
sportradar-api-0.10.54 lib/sportradar/api/request.rb
sportradar-api-0.10.53 lib/sportradar/api/request.rb
sportradar-api-0.10.52 lib/sportradar/api/request.rb
sportradar-api-0.10.51 lib/sportradar/api/request.rb
sportradar-api-0.10.50 lib/sportradar/api/request.rb
sportradar-api-0.10.49 lib/sportradar/api/request.rb
sportradar-api-0.10.48 lib/sportradar/api/request.rb
sportradar-api-0.10.47 lib/sportradar/api/request.rb
sportradar-api-0.10.46 lib/sportradar/api/request.rb
sportradar-api-0.10.45 lib/sportradar/api/request.rb
sportradar-api-0.10.44 lib/sportradar/api/request.rb
sportradar-api-0.10.43 lib/sportradar/api/request.rb
sportradar-api-0.10.42 lib/sportradar/api/request.rb
sportradar-api-0.10.41 lib/sportradar/api/request.rb
sportradar-api-0.10.40 lib/sportradar/api/request.rb
sportradar-api-0.10.39 lib/sportradar/api/request.rb
sportradar-api-0.10.38 lib/sportradar/api/request.rb
sportradar-api-0.10.37 lib/sportradar/api/request.rb
sportradar-api-0.10.36 lib/sportradar/api/request.rb