Class: NBA::LiveConnection
- Inherits:
-
Object
- Object
- NBA::LiveConnection
- Defined in:
- lib/nba/live_connection.rb
Overview
Handles HTTP connections to the NBA Live Data API
Constant Summary collapse
- BASE_URL =
Default base URL for the NBA Live Data API
"https://cdn.nba.com/static/json/liveData/".freeze
Instance Attribute Summary collapse
-
#base_url ⇒ String
readonly
private
Returns the base URL for API requests.
Instance Method Summary collapse
-
#get(path) ⇒ String
Makes a GET request to the specified path.
-
#initialize(base_url: BASE_URL) ⇒ NBA::LiveConnection
constructor
Initializes a new LiveConnection object.
Constructor Details
#initialize(base_url: BASE_URL) ⇒ NBA::LiveConnection
Initializes a new LiveConnection object
27 28 29 |
# File 'lib/nba/live_connection.rb', line 27 def initialize(base_url: BASE_URL) @base_url = base_url end |
Instance Attribute Details
#base_url ⇒ String (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the base URL for API requests
18 19 20 |
# File 'lib/nba/live_connection.rb', line 18 def base_url @base_url end |
Instance Method Details
#get(path) ⇒ String
Makes a GET request to the specified path
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/nba/live_connection.rb', line 38 def get(path) uri = URI.join(base_url, path) hostname = uri.hostname or raise ArgumentError, "Invalid URI: #{uri}" request = Net::HTTP::Get.new(uri) apply_headers(request) Net::HTTP.start(hostname, uri.port, use_ssl: uri.scheme.eql?("https")) do |http| response = http.request(request) decode_body(response) if response.is_a?(Net::HTTPSuccess) end end |