Sha256: 4a6d77bde6463a7bbc9b72e0ce496c85d18ec118c6659b6273293f0811f0777c

Contents?: true

Size: 1.63 KB

Versions: 24

Compression:

Stored size: 1.63 KB

Contents

module Sportradar
  module Api
    class Request

      include HTTParty

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

      def get(path, options={})
        base_setup(path, options)
        begin
          response = self.class.get(url, headers: headers, query: options.merge(api_key), 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

      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
      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
        ".#{Sportradar::Api.config.format}" if Sportradar::Api.config.format
      end

      def set_headers
        {'Content-Type' => "application/#{Sportradar::Api.config.format}", 'Accept' => "application/#{Sportradar::Api.config.format}"}
      end

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

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
sportradar-api-0.9.24 lib/sportradar/api/request.rb
sportradar-api-0.9.23 lib/sportradar/api/request.rb
sportradar-api-0.9.22 lib/sportradar/api/request.rb
sportradar-api-0.9.21 lib/sportradar/api/request.rb
sportradar-api-0.9.20 lib/sportradar/api/request.rb
sportradar-api-0.9.19 lib/sportradar/api/request.rb
sportradar-api-0.9.18 lib/sportradar/api/request.rb
sportradar-api-0.9.17 lib/sportradar/api/request.rb
sportradar-api-0.9.16 lib/sportradar/api/request.rb
sportradar-api-0.9.15 lib/sportradar/api/request.rb
sportradar-api-0.9.14 lib/sportradar/api/request.rb
sportradar-api-0.9.13 lib/sportradar/api/request.rb
sportradar-api-0.9.12 lib/sportradar/api/request.rb
sportradar-api-0.9.11 lib/sportradar/api/request.rb
sportradar-api-0.9.10 lib/sportradar/api/request.rb
sportradar-api-0.9.9 lib/sportradar/api/request.rb
sportradar-api-0.9.8 lib/sportradar/api/request.rb
sportradar-api-0.9.7 lib/sportradar/api/request.rb
sportradar-api-0.9.6 lib/sportradar/api/request.rb
sportradar-api-0.9.5 lib/sportradar/api/request.rb