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



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/carddb/resources/datasets.rb', line 132

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



180
181
182
183
184
185
186
187
# File 'lib/carddb/resources/datasets.rb', line 180

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

#filter_values(fields:, id: nil, dataset_key: nil, publisher_slug: nil, game_key: nil, search: nil, include_counts: nil, first: nil, after: nil, cache: nil) ⇒ Array<DatasetFilterValueConnection>

Get distinct values for one or more filterable fields on a dataset.

Parameters:

  • fields (Array<String, Symbol>)

    Field keys to fetch values for

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

    Dataset UUID. Provide either id or dataset_key.

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

    Dataset key when looking up by publisher/game keys

  • 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)

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

    Optional case-insensitive search within values

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

    Include per-value counts

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

    Maximum values per field (max 500)

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

    Cursor for pagination. Applies to each field.

Returns:



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/carddb/resources/datasets.rb', line 201

def filter_values(
  fields:,
  id: nil,
  dataset_key: nil,
  publisher_slug: nil,
  game_key: nil,
  search: nil,
  include_counts: nil,
  first: nil,
  after: nil,
  cache: nil
)
  field_keys = fields.map(&:to_s)
  resolved_publisher = id ? nil : resolve_publisher(publisher_slug)
  resolved_game = id ? nil : resolve_game(game_key)

  validate_access!(resolved_publisher, resolved_game) unless id

  query = QueryBuilder.dataset_filter_values(
    id: id,
    publisher_slug: resolved_publisher,
    game_key: resolved_game,
    dataset_key: dataset_key,
    fields: field_keys,
    search: search,
    include_counts: include_counts,
    first: first,
    after: after
  )
  variables = build_variables(
    id: id,
    publisherSlug: resolved_publisher,
    gameKey: resolved_game,
    datasetKey: dataset_key,
    fields: field_keys,
    search: search,
    includeCounts: include_counts,
    first: first,
    after: after
  )
  key = cache_key('datasets', 'filter_values', **variables)

  data = with_cache(key, resource: :datasets, cache: cache) { connection.execute(query, variables) }
  filter_values = data.dig('fetchDataset', 'filterValues') || []
  filter_values.map { |value| DatasetFilterValueConnection.new(value) }
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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/carddb/resources/datasets.rb', line 151

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

#get_by_key(game_id:, dataset_key:, publisher_id: nil, cache: nil) ⇒ Object

Get a dataset by game ID and stable dataset key.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/carddb/resources/datasets.rb', line 48

def get_by_key(game_id:, dataset_key:, publisher_id: nil, cache: nil)
  key = cache_key(
    'datasets',
    'get_by_key',
    publisher_id: publisher_id,
    game_id: game_id,
    dataset_key: dataset_key
  )
  with_cache(key, resource: :datasets, cache: cache) do
    query = QueryBuilder.dataset(publisher_id: publisher_id, game_id: game_id, dataset_key: dataset_key)
    variables = build_variables(publisherId: publisher_id, gameId: game_id, datasetKey: dataset_key)
    data = connection.execute(query, variables)

    data['dataset'] ? Dataset.new(data['dataset'], client: client) : nil
  end
end

#get_schema(id: nil, publisher_id: nil, game_id: nil, dataset_key: nil, cache: nil) ⇒ Object

Fetch only the schema portion for a dataset.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/carddb/resources/datasets.rb', line 66

def get_schema(id: nil, publisher_id: nil, game_id: nil, dataset_key: nil, cache: nil)
  dataset = if id
              get_by_id_for_management(id, cache: cache)
            else
              get_by_key(
                publisher_id: publisher_id,
                game_id: game_id,
                dataset_key: dataset_key,
                cache: cache
              )
            end
  dataset&.schema
end

#list(publisher_id:, game_id: nil, purpose: nil, first: nil, after: nil, include_archived: nil, cache: nil) ⇒ Object

List datasets for a publisher-management context.



8
9
10
11
12
13
14
15
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
# File 'lib/carddb/resources/datasets.rb', line 8

def list(publisher_id:, game_id: nil, purpose: nil, first: nil, after: nil, include_archived: nil, cache: nil)
  query = QueryBuilder.list_datasets(
    publisher_id: publisher_id,
    game_id: game_id,
    purpose: purpose,
    first: first,
    after: after,
    include_archived: include_archived
  )
  variables = build_variables(
    publisherId: publisher_id,
    gameId: game_id,
    purpose: purpose,
    first: first,
    after: after,
    includeArchived: include_archived
  )
  key = cache_key('datasets', 'list', **variables)

  data = with_cache(key, resource: :datasets, cache: cache) { connection.execute(query, variables) }

  Collection.new(
    data['datasets'],
    item_class: Dataset,
    next_page_loader: lambda { |cursor|
      list(
        publisher_id: publisher_id,
        game_id: game_id,
        purpose: purpose,
        first: first,
        after: cursor,
        include_archived: include_archived,
        cache: cache
      )
    },
    client: client
  )
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:



254
255
256
257
258
259
260
261
# File 'lib/carddb/resources/datasets.rb', line 254

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:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/carddb/resources/datasets.rb', line 89

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