Class: MLB::Collection Private

Inherits:
Shale::Mapper
  • Object
show all
Defined in:
lib/mlb/collection.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Base class for simple collection endpoints that only need an .all method. Provides a class macro to define the endpoint, item type, and attribute name, eliminating repetitive boilerplate across 20+ similar collection classes.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.collection_nameSymbol (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 collection attribute name

Returns:

  • (Symbol)

    the collection attribute name



21
22
23
# File 'lib/mlb/collection.rb', line 21

def collection_name
  @collection_name
end

.endpointString (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 API endpoint for this collection

Returns:

  • (String)

    the API endpoint



15
16
17
# File 'lib/mlb/collection.rb', line 15

def endpoint
  @endpoint
end

Class Method Details

.allArray

Retrieves all items from this collection

Examples:

MLB::Sports.all #=> [#<MLB::Sport id=1 name="Major League Baseball">, ...]

Returns:

  • (Array)

    the list of items



42
43
44
45
# File 'lib/mlb/collection.rb', line 42

def all
  response = CLIENT.get(endpoint)
  from_json("{\"#{collection_name}\":#{response}}").public_send(collection_name)
end

.collection(endpoint:, item_type:, collection_name:) ⇒ void

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.

This method returns an undefined value.

Configures the collection with endpoint and item type

Parameters:

  • endpoint (String)

    the API endpoint to fetch from

  • item_type (Class)

    the Shale::Mapper class for individual items

  • collection_name (Symbol)

    the attribute name for the collection



30
31
32
33
34
# File 'lib/mlb/collection.rb', line 30

def collection(endpoint:, item_type:, collection_name:)
  @endpoint = endpoint
  @collection_name = collection_name
  attribute collection_name, item_type, collection: true
end