Class: YFantasy::BaseResource Abstract
- Inherits:
-
Object
- Object
- YFantasy::BaseResource
- Extended by:
- Forwardable
- Includes:
- Subresourceable
- Defined in:
- lib/y_fantasy/resources/base_resource.rb
Overview
Base class for all “primary” Yahoo Fantasy Sports API resources.
Primary resources are those with a Yahoo Key/ID, that can be referenced in a Yahoo Fantasy API URL. This includes Game, League, Team, Player, Group, and PickemTeam.
Defined Under Namespace
Classes: Error
Class Method Summary collapse
-
.collection_name ⇒ Symbol?
Returns the collection name (aka pluralized resource name).
-
.dependent? ⇒ Boolean
Always returns false for primary resources (anything that inherits from BaseResource).
-
.find(key, with: [], **options) ⇒ BaseResource
Finds a single resource by key.
-
.find_all(keys = [], with: [], scope_to_user: false) ⇒ Array<BaseResource>
Finds all resources of the current type.
-
.resource_name ⇒ Symbol?
Returns the resource name as a symbol.
Instance Method Summary collapse
-
#key ⇒ Object?
Returns the key of the resource.
Methods included from Subresourceable
Class Method Details
.collection_name ⇒ Symbol?
Returns the collection name (aka pluralized resource name)
66 67 68 69 70 |
# File 'lib/y_fantasy/resources/base_resource.rb', line 66 def collection_name return if base_resource? :"#{resource_name}s" end |
.dependent? ⇒ Boolean
Always returns false for primary resources (anything that inherits from BaseResource)
52 53 54 |
# File 'lib/y_fantasy/resources/base_resource.rb', line 52 def dependent? false end |
.find(key, with: [], **options) ⇒ BaseResource
Finds a single resource by key
40 41 42 43 44 45 46 47 48 |
# File 'lib/y_fantasy/resources/base_resource.rb', line 40 def find(key, with: [], **) () subresources = Transformations::T.wrap_in_array(with) SubresourceValidator.validate!(self, subresources) data = YFantasy::Api::Client.get(resource_name, keys: key, subresources: subresources, **) resource = Transformations.transformer_for(resource_name).call(data) resource.add_fetched_subresources(subresources) resource end |
.find_all(keys = [], with: [], scope_to_user: false) ⇒ Array<BaseResource>
Finds all resources of the current type
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/y_fantasy/resources/base_resource.rb', line 23 def find_all(keys = [], with: [], scope_to_user: false) keys = Array(keys) subresources = Transformations::T.wrap_in_array(with) data = YFantasy::Api::Client.get( collection_name, keys: keys, subresources: subresources, scope_to_user: scope_to_user ) resources = Transformations::CollectionTransformer.new(collection_name).call(data) resources.each { |resource| resource.add_fetched_subresources(subresources) } resources end |
.resource_name ⇒ Symbol?
Returns the resource name as a symbol
58 59 60 61 62 |
# File 'lib/y_fantasy/resources/base_resource.rb', line 58 def resource_name return if base_resource? to_s.split("::").last.scan(/[A-Z][a-z]+/).join("_").downcase.to_sym end |
Instance Method Details
#key ⇒ Object?
Returns the key of the resource
108 109 110 111 112 |
# File 'lib/y_fantasy/resources/base_resource.rb', line 108 def key if (name = self.class.resource_name) public_send(:"#{name}_key") end end |