Class: Sportradar::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sportradar/client/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(league:, endpoint:, path:, query: nil) ⇒ Client

Returns a new instance of Client.



3
4
5
6
7
8
9
10
11
12
# 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

Instance Method Details

#access_levelObject



83
84
85
86
87
# File 'lib/sportradar/client/client.rb', line 83

def access_level
  Sportradar.configuration.
    access_levels[league].
    freeze
end

#api_keyObject



89
90
91
92
93
# File 'lib/sportradar/client/client.rb', line 89

def api_key
  Sportradar.configuration.
    api_keys[league].
    freeze
end

#api_versionObject



95
96
97
98
99
# File 'lib/sportradar/client/client.rb', line 95

def api_version
  Sportradar.configuration.
    api_version[league].
    freeze
end

#fetchObject



32
33
34
35
36
37
# 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

#filenameObject



58
59
60
# File 'lib/sportradar/client/client.rb', line 58

def filename
  "#{sport}_#{league}_#{path.gsub('/', '_')}"
end

#filepathObject



62
63
64
65
66
# 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

#headersObject



50
51
52
# File 'lib/sportradar/client/client.rb', line 50

def headers
  {}
end

#output_fileObject



54
55
56
# File 'lib/sportradar/client/client.rb', line 54

def output_file
  File.join filepath, filename
end

#requestObject



79
80
81
# File 'lib/sportradar/client/client.rb', line 79

def request
  Net::HTTP::Get.new(url, headers)
end

#responseObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

#saveObject



39
40
41
42
43
44
45
46
47
48
# 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

#urlObject



68
69
70
71
72
73
74
75
76
77
# 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