class Sportradar::Client
Attributes
base_uri[R]
endpoint[R]
league[R]
path[R]
query[R]
sport[R]
Public Class Methods
new(league:, endpoint:, path:, query: nil)
click to toggle source
# File lib/sportradar/client/client.rb, line 3 def initialize(league:, endpoint:, path:, query: nil) @league = league @endpoint = endpoint @base_uri = Sportradar.configuration.base_uri.freeze @path = path @query = query end
Public Instance Methods
access_level()
click to toggle source
# File lib/sportradar/client/client.rb, line 83 def access_level Sportradar.configuration. access_levels[league]. freeze end
api_key()
click to toggle source
# File lib/sportradar/client/client.rb, line 89 def api_key Sportradar.configuration. api_keys[league]. freeze end
api_version()
click to toggle source
# File lib/sportradar/client/client.rb, line 95 def api_version Sportradar.configuration. api_version[league]. freeze end
fetch()
click to toggle source
# File lib/sportradar/client/client.rb, line 32 def fetch Oj.load(response.body) rescue StandardError => e puts "Parsing Error (#{e.message})" raise e end
filename()
click to toggle source
# File lib/sportradar/client/client.rb, line 58 def filename "#{sport}_#{league}_#{path.gsub('/', '_')}" end
filepath()
click to toggle source
# File lib/sportradar/client/client.rb, line 62 def filepath "#{Sportradar.configuration.filepath.freeze}/#{sport}/#{league}/#{endpoint}".tap do |path| FileUtils.mkpath(path) end end
headers()
click to toggle source
# File lib/sportradar/client/client.rb, line 50 def headers {} end
output_file()
click to toggle source
# File lib/sportradar/client/client.rb, line 54 def output_file File.join filepath, filename end
request()
click to toggle source
# File lib/sportradar/client/client.rb, line 79 def request Net::HTTP::Get.new(url, headers) end
response()
click to toggle source
# File lib/sportradar/client/client.rb, line 14 def response Sportradar.configuration.http.request(request) do |res| case res.code when '200' res when '401' raise 'Unauthorized' when '403' raise 'Forbidden' else raise "#{res.code} Error in HTTP request for #{path}" end end rescue StandardError => e puts "HTTP Request for #{path} failed (#{e.message})" raise e end
save()
click to toggle source
# File lib/sportradar/client/client.rb, line 39 def save if result = File.write(output_file, response.body) if result == 0 puts "File #{filename} saved with no content." end end rescue StandardError => e puts "File #{filename} saving failed (#{e.message})" raise e end
url()
click to toggle source
# File lib/sportradar/client/client.rb, line 68 def url @url ||= begin if query template = Addressable::Template.new("#{base_uri}/#{league}-#{access_level}#{api_version}/#{path}{?query*}&api_key=#{api_key}") template.partial_expand(query).pattern else "#{base_uri}/#{league}-#{access_level}#{api_version}/#{path}?api_key=#{api_key}" end end end