Class: CardDB::Resources::Datasets

Inherits:
Base
  • Object
show all
Defined in:
lib/carddb/resources/datasets.rb

Overview

Datasets resource for searching, fetching, and getting schema

Instance Attribute Summary

Attributes inherited from Base

#client, #config, #connection

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from CardDB::Resources::Base

Instance Method Details

#fetch(id, cache: nil) ⇒ Dataset?

Fetch a dataset by ID (includes schema)

Parameters:

  • id (String)

    The dataset UUID

  • cache (Boolean, nil) (defaults to: nil)

    Whether to cache (nil = use config setting)

Returns:

  • (Dataset, nil)

    The dataset or nil if not found



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/carddb/resources/datasets.rb', line 59

def fetch(id, cache: nil)
  key = cache_key('datasets', 'fetch', id: id)
  with_cache(key, resource: :datasets, cache: cache) do
    query = QueryBuilder.fetch_dataset_by_id
    data = connection.execute(query, { id: id })

    return nil unless data['fetchDataset']

    Dataset.new(data['fetchDataset'], client: client)
  end
end

#fetch_many(ids) ⇒ Array<Dataset>

Fetch multiple datasets by IDs

Parameters:

  • ids (Array<String>)

    Array of dataset UUIDs (max 100)

Returns:

  • (Array<Dataset>)

    Array of datasets

Raises:

  • (ArgumentError)

    If more than 100 IDs provided



107
108
109
110
111
112
113
114
# File 'lib/carddb/resources/datasets.rb', line 107

def fetch_many(ids)
  raise ArgumentError, 'Maximum 100 IDs allowed' if ids.length > 100

  query = QueryBuilder.fetch_datasets
  data = connection.execute(query, { ids: ids })

  (data['fetchDatasets'] || []).map { |d| Dataset.new(d, client: client) }
end

#get(dataset_key:, publisher_slug: nil, game_key: nil, cache: nil) ⇒ Dataset?

Get a dataset by publisher/game/dataset keys (includes schema)

Parameters:

  • publisher_slug (String, nil) (defaults to: nil)

    Publisher slug (uses default if not provided)

  • game_key (String, nil) (defaults to: nil)

    Game key (uses default if not provided)

  • dataset_key (String)

    Dataset key (required)

  • cache (Boolean, nil) (defaults to: nil)

    Whether to cache (nil = use config setting)

Returns:

  • (Dataset, nil)

    The dataset or nil if not found



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/carddb/resources/datasets.rb', line 78

def get(dataset_key:, publisher_slug: nil, game_key: nil, cache: nil)
  resolved_publisher = resolve_publisher(publisher_slug)
  resolved_game = resolve_game(game_key)

  validate_access!(resolved_publisher, resolved_game)

  key = cache_key('datasets', 'get', publisher_slug: resolved_publisher, game_key: resolved_game,
                                     dataset_key: dataset_key)
  with_cache(key, resource: :datasets, cache: cache) do
    query = QueryBuilder.fetch_dataset_by_keys
    variables = {
      publisherSlug: resolved_publisher,
      gameKey: resolved_game,
      datasetKey: dataset_key
    }

    data = connection.execute(query, variables)

    return nil unless data['fetchDataset']

    Dataset.new(data['fetchDataset'], client: client)
  end
end

#schema(dataset_key:, publisher_slug: nil, game_key: nil) ⇒ DatasetSchema?

Get the schema for a dataset

Parameters:

  • dataset_key (String)

    Dataset key

  • publisher_slug (String, nil) (defaults to: nil)

    Publisher slug (uses default if not provided)

  • game_key (String, nil) (defaults to: nil)

    Game key (uses default if not provided)

Returns:



122
123
124
125
126
127
128
129
# File 'lib/carddb/resources/datasets.rb', line 122

def schema(dataset_key:, publisher_slug: nil, game_key: nil)
  dataset = get(
    dataset_key: dataset_key,
    publisher_slug: publisher_slug,
    game_key: game_key
  )
  dataset&.schema
end

#search(publisher_slug: nil, game_key: nil, search: nil, purpose: nil, first: nil, after: nil) ⇒ Collection<Dataset>

Search for datasets

Parameters:

  • publisher_slug (String, nil) (defaults to: nil)

    Filter by publisher slug

  • game_key (String, nil) (defaults to: nil)

    Filter by game key

  • search (String, nil) (defaults to: nil)

    Search by name

  • purpose (String, nil) (defaults to: nil)

    Filter by dataset purpose (DATA or RULES)

  • first (Integer, nil) (defaults to: nil)

    Maximum number of results

  • after (String, nil) (defaults to: nil)

    Cursor for pagination

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/carddb/resources/datasets.rb', line 16

def search(publisher_slug: nil, game_key: nil, search: nil, purpose: nil, first: nil, after: nil)
  resolved_publisher = config.resolve_publisher(publisher_slug)
  resolved_game = config.resolve_game(game_key)

  validate_access!(resolved_publisher, resolved_game) if resolved_publisher

  query = QueryBuilder.search_datasets(
    publisher_slug: resolved_publisher,
    game_key: resolved_game,
    search: search,
    purpose: purpose,
    first: first,
    after: after
  )

  variables = build_variables(
    publisherSlug: resolved_publisher,
    gameKey: resolved_game,
    search: search,
    purpose: purpose,
    first: first,
    after: after
  )

  data = connection.execute(query, variables)

  # Create next page loader
  search_params = { publisher_slug: publisher_slug, game_key: game_key, search: search, purpose: purpose, first: first }
  next_page_loader = ->(cursor) { search(**search_params, after: cursor) }

  Collection.new(
    data['searchDatasets'],
    item_class: Dataset,
    next_page_loader: next_page_loader,
    client: client
  )
end