Class: EspnPub::Client
- Inherits:
-
Object
- Object
- EspnPub::Client
- Defined in:
- lib/espn_pub/client.rb
Overview
Client for making requests to the ESPN public API.
Defined Under Namespace
Classes: UnexpectedResponseCodeError
Constant Summary collapse
- BASE_URI =
'https://site.api.espn.com/'- API_VERSION =
'v2'
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize(base_uri:, version:) ⇒ Client
constructor
Initialize a new Client.
-
#send_request(path) ⇒ Hash
Send a GET request to the specified API path.
Constructor Details
#initialize(base_uri:, version:) ⇒ Client
Initialize a new Client.
21 22 23 24 |
# File 'lib/espn_pub/client.rb', line 21 def initialize(base_uri:, version:) @uri = get_uri(base_uri) @version = version end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
15 16 17 |
# File 'lib/espn_pub/client.rb', line 15 def uri @uri end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
15 16 17 |
# File 'lib/espn_pub/client.rb', line 15 def version @version end |
Instance Method Details
#send_request(path) ⇒ Hash
Send a GET request to the specified API path.
31 32 33 34 35 36 37 38 39 |
# File 'lib/espn_pub/client.rb', line 31 def send_request(path) response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| http.request_get path end raise UnexpectedResponseCodeError, "Unexpected response code: #{response.code}" unless response.code.to_i == 200 JSON.parse response.body end |