Class: YFantasy::Api::Client
- Inherits:
-
Object
- Object
- YFantasy::Api::Client
- Defined in:
- lib/y_fantasy/api/client.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- @@retry =
true
Class Method Summary collapse
Instance Method Summary collapse
- #authenticate ⇒ Object
-
#get(resource, keys: [], game_codes: [], subresources: [], **options) ⇒ Object
NOTE: URL construction needs to be sophisticated enough to know when it should use “out” params vs nested subresources.
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #refresh_access_token_if_needed ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
24 25 26 |
# File 'lib/y_fantasy/api/client.rb', line 24 def initialize authenticate end |
Class Method Details
.get(resource, keys: [], game_codes: [], subresources: [], **options) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/y_fantasy/api/client.rb', line 13 def self.get(resource, keys: [], game_codes: [], subresources: [], **) new.get(resource, keys: keys, game_codes: game_codes, subresources: subresources, **) rescue Client::Error, Authentication::Error if @@retry @@retry = false retry end raise end |
Instance Method Details
#authenticate ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/y_fantasy/api/client.rb', line 61 def authenticate if YFantasy::Api::Authentication.authenticate @access_token = YFantasy::Api::Authentication.access_token @refresh_token = YFantasy::Api::Authentication.refresh_token else @error_type = YFantasy::Api::Authentication.error_type @error_desc = YFantasy::Api::Authentication.error_desc end end |
#get(resource, keys: [], game_codes: [], subresources: [], **options) ⇒ Object
NOTE: URL construction needs to be sophisticated enough to know when it should use “out” params vs nested subresources. E.g. /league/380.l.190823/teams/stats this is how you get 1 league with all teams, and all stats for each team teams is a subresource of league stats is a subresource of teams
This fails because “stats” is not a subresource of league: /league/380.l.190823;out=teams/stats
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/y_fantasy/api/client.rb', line 38 def get(resource, keys: [], game_codes: [], subresources: [], **) refresh_access_token_if_needed url = UrlBuilder.new(resource, keys: keys, game_codes: game_codes, subresources: subresources, **).build # puts "\n Client#get #{url} \n" # TODO: remove response = Net::HTTP.get_response(URI(url), "Authorization" => "Bearer #{@access_token}") body = response.body case response when Net::HTTPSuccess Ox.load(body, mode: :hash_no_attrs).fetch(:fantasy_content) when Net::HTTPClientError error = Ox.load(body, mode: :hash_no_attrs) error_msg = error.dig(:"yahoo:error", :"yahoo:description") || error.dig(:error, :description) msg = "#{response.code}: #{error_msg}" raise YFantasy::Api::Client::Error.new(msg) else raise YFantasy::Api::Client::Error.new("An unknown error occurred") end end |
#refresh_access_token_if_needed ⇒ Object
71 72 73 74 75 |
# File 'lib/y_fantasy/api/client.rb', line 71 def refresh_access_token_if_needed return if YFantasy::Api::Authentication.access_token_valid? authenticate end |