Class: Sportradar::Api::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/sportradar/api/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#qps_allottedObject (readonly)

Returns the value of attribute qps_allotted.



4
5
6
# File 'lib/sportradar/api/request.rb', line 4

def qps_allotted
  @qps_allotted
end

#qps_currentObject (readonly)

Returns the value of attribute qps_current.



4
5
6
# File 'lib/sportradar/api/request.rb', line 4

def qps_current
  @qps_current
end

#quota_allottedObject (readonly)

Returns the value of attribute quota_allotted.



4
5
6
# File 'lib/sportradar/api/request.rb', line 4

def quota_allotted
  @quota_allotted
end

#quota_currentObject (readonly)

Returns the value of attribute quota_current.



4
5
6
# File 'lib/sportradar/api/request.rb', line 4

def quota_current
  @quota_current
end

Instance Method Details

#get(path, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sportradar/api/request.rb', line 43

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
  unless response.success?
    puts url + "?api_key=#{api_key[:api_key]}" # uncomment for debugging
    puts
    puts response.inspect
    Sportradar::Api::Error.new(response.code, response.message, response)
  else
    response
  end
end

#get_data(url) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sportradar/api/request.rb', line 15

def get_data(url)
  data = get request_url(url)
  if data.is_a?(::Sportradar::Api::Error)
    puts request_url(url)
    puts
    puts data.inspect
    raise data
  end
  parse_usage(data.headers)
  data
end

#get_request_info(url) ⇒ Object



63
64
65
# File 'lib/sportradar/api/request.rb', line 63

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

#parse_usage(headers) ⇒ Object



36
37
38
39
40
41
# File 'lib/sportradar/api/request.rb', line 36

def parse_usage(headers)
  @qps_allotted   = (headers['x-plan-qps-allotted']   || headers['x-packagekey-qps-allotted']).to_i
  @qps_current    = (headers['x-plan-qps-current']    || headers['x-packagekey-qps-current']).to_i
  @quota_allotted = (headers['x-plan-quota-allotted'] || headers['x-packagekey-quota-allotted']).to_i
  @quota_current  = (headers['x-plan-quota-current']  || headers['x-packagekey-quota-current']).to_i
end

#sim!Object

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



10
11
12
13
# File 'lib/sportradar/api/request.rb', line 10

def sim!
  @access_level = 'sim'
  self
end

#usageObject



27
28
29
30
31
32
33
34
# File 'lib/sportradar/api/request.rb', line 27

def usage
  {
    qps_allotted:   @qps_allotted,
    qps_current:    @qps_current,
    quota_allotted: @quota_allotted,
    quota_current:  @quota_current,
  }
end