Class: CardDB::Resources::Datasets
- Defined in:
- lib/carddb/resources/datasets.rb
Overview
Datasets resource for searching, fetching, and getting schema
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#fetch(id, cache: nil) ⇒ Dataset?
Fetch a dataset by ID (includes schema).
-
#fetch_many(ids) ⇒ Array<Dataset>
Fetch multiple datasets by IDs.
-
#get(dataset_key:, publisher_slug: nil, game_key: nil, cache: nil) ⇒ Dataset?
Get a dataset by publisher/game/dataset keys (includes schema).
-
#schema(dataset_key:, publisher_slug: nil, game_key: nil) ⇒ DatasetSchema?
Get the schema for a dataset.
-
#search(publisher_slug: nil, game_key: nil, search: nil, purpose: nil, first: nil, after: nil) ⇒ Collection<Dataset>
Search for datasets.
Methods inherited from Base
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)
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
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)
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
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
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 |