Sha256: 298a8f357fa36e47c5220b56d2a02c5526a8b0a25dd175b6b86eb72b583eb0bb

Contents?: true

Size: 1.67 KB

Versions: 34

Compression:

Stored size: 1.67 KB

Contents

module Sportradar
  module Api
    class Request

      include HTTParty

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

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

      def get(path, options={})
        base_setup(path, options)
        puts url
        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
        if response.success?
          response
        else
          Sportradar::Api::Error.new(response.code, response.message, response)
        end
      end

      private

      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

34 entries across 34 versions & 1 rubygems

Version Path
sportradar-api-0.1.38 lib/sportradar/api/request.rb
sportradar-api-0.1.37 lib/sportradar/api/request.rb
sportradar-api-0.1.36 lib/sportradar/api/request.rb
sportradar-api-0.1.35 lib/sportradar/api/request.rb
sportradar-api-0.1.34 lib/sportradar/api/request.rb
sportradar-api-0.1.33 lib/sportradar/api/request.rb
sportradar-api-0.1.32 lib/sportradar/api/request.rb
sportradar-api-0.1.31 lib/sportradar/api/request.rb
sportradar-api-0.1.30 lib/sportradar/api/request.rb
sportradar-api-0.1.29 lib/sportradar/api/request.rb
sportradar-api-0.1.28 lib/sportradar/api/request.rb
sportradar-api-0.1.27 lib/sportradar/api/request.rb
sportradar-api-0.1.26 lib/sportradar/api/request.rb
sportradar-api-0.1.25 lib/sportradar/api/request.rb
sportradar-api-0.1.24 lib/sportradar/api/request.rb
sportradar-api-0.1.23 lib/sportradar/api/request.rb
sportradar-api-0.1.22 lib/sportradar/api/request.rb
sportradar-api-0.1.21 lib/sportradar/api/request.rb
sportradar-api-0.1.19 lib/sportradar/api/request.rb
sportradar-api-0.1.18 lib/sportradar/api/request.rb