Class: YFantasy::BaseResource Abstract

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Subresourceable
Defined in:
lib/y_fantasy/resources/base_resource.rb

Overview

This class is abstract.

Base class for all “primary” Yahoo Fantasy Sports API resources.

Note:

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.

Direct Known Subclasses

Game, Group, League, PickemTeam, Player, Team

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Subresourceable

included

Class Method Details

.collection_nameSymbol?

Returns the collection name (aka pluralized resource name)

Returns:

  • (Symbol, nil)

    The collection name or nil if this is the BaseResource



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)

Returns:

  • (Boolean)

    False by default.



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

Parameters:

  • key (String, Symbol, Integer)

    The key to find the resource by

  • with (Array<Symbol>, Symbol) (defaults to: [])

    Subresources to include with the response

  • options (Hash)

    Additional options for the request

Returns:

Raises:

  • (Error)

    If invalid options are provided



40
41
42
43
44
45
46
47
48
# File 'lib/y_fantasy/resources/base_resource.rb', line 40

def find(key, with: [], **options)
  validate_options(options)
  subresources = Transformations::T.wrap_in_array(with)
  SubresourceValidator.validate!(self, subresources)
  data = YFantasy::Api::Client.get(resource_name, keys: key, subresources: subresources, **options)
  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

Parameters:

  • keys (Array<String, Symbol, Integer>) (defaults to: [])

    Keys to find resources by

  • with (Array<Symbol>, Symbol) (defaults to: [])

    Subresources to include with the response

  • scope_to_user (Boolean) (defaults to: false)

    Whether to scope the request to the authenticated user

Returns:



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_nameSymbol?

Returns the resource name as a symbol

Returns:

  • (Symbol, nil)

    The resource name or nil if this is the BaseResource



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

#keyObject?

Returns the key of the resource

Returns:

  • (Object, nil)

    The resource key or nil if not applicable



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