Class: CardDB::Resources::Games
- Defined in:
- lib/carddb/resources/games.rb
Overview
Games resource for searching and fetching games
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#create(input:) ⇒ Object
Create a publisher-managed game.
-
#fetch(id, cache: nil) ⇒ Game?
Fetch a game by ID.
-
#fetch_many(ids) ⇒ Array<Game>
Fetch multiple games by IDs.
-
#get(game_key:, publisher_slug: nil, cache: nil) ⇒ Game?
Get a game by publisher slug and game key.
-
#get_by_key(game_key:, publisher_id: nil, publisher_slug: nil, cache: nil) ⇒ Object
Get a publisher-managed game by stable key.
-
#get_by_slug(game_slug:, publisher_id: nil, publisher_slug: nil, cache: nil) ⇒ Object
Get a publisher-managed game by slug.
-
#get_regions(publisher_slug: nil, game_key: nil, include_inactive: nil, cache: nil) ⇒ Object
Get publisher-defined game region metadata for scan-capable clients.
-
#list(publisher_id:, first: nil, after: nil, include_archived: nil, cache: nil) ⇒ Object
List games for a publisher-management context.
-
#search(publisher_slug: nil, search: nil, first: nil, after: nil) ⇒ Collection<Game>
Search for games.
-
#update(id:, input:) ⇒ Object
Update a publisher-managed game.
Methods inherited from Base
Constructor Details
This class inherits a constructor from CardDB::Resources::Base
Instance Method Details
#create(input:) ⇒ Object
Create a publisher-managed game. Requires a server-side secret credential.
62 63 64 65 66 67 |
# File 'lib/carddb/resources/games.rb', line 62 def create(input:) config.require_secret_credential!('games.create') data = connection.execute(QueryBuilder.create_game, { input: input }) Game.new(data['gameCreate'], client: client) end |
#fetch(id, cache: nil) ⇒ Game?
Fetch a game by ID
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/carddb/resources/games.rb', line 146 def fetch(id, cache: nil) key = cache_key('games', 'fetch', id: id) with_cache(key, resource: :games, cache: cache) do query = QueryBuilder.fetch_game_by_id data = connection.execute(query, { id: id }) return nil unless data['fetchGame'] Game.new(data['fetchGame'], client: client) end end |
#fetch_many(ids) ⇒ Array<Game>
Fetch multiple games by IDs
190 191 192 193 194 195 196 197 |
# File 'lib/carddb/resources/games.rb', line 190 def fetch_many(ids) raise ArgumentError, 'Maximum 100 IDs allowed' if ids.length > 100 query = QueryBuilder.fetch_games data = connection.execute(query, { ids: ids }) (data['fetchGames'] || []).map { |g| Game.new(g, client: client) } end |
#get(game_key:, publisher_slug: nil, cache: nil) ⇒ Game?
Get a game by publisher slug and game key
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/carddb/resources/games.rb', line 164 def get(game_key:, publisher_slug: nil, cache: nil) resolved_publisher = resolve_publisher(publisher_slug) validate_access!(resolved_publisher, game_key) key = cache_key('games', 'get', publisher_slug: resolved_publisher, game_key: game_key) with_cache(key, resource: :games, cache: cache) do query = QueryBuilder.fetch_game_by_keys variables = { publisherSlug: resolved_publisher, gameKey: game_key } data = connection.execute(query, variables) return nil unless data['fetchGame'] Game.new(data['fetchGame'], client: client) end end |
#get_by_key(game_key:, publisher_id: nil, publisher_slug: nil, cache: nil) ⇒ Object
Get a publisher-managed game by stable key.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/carddb/resources/games.rb', line 36 def get_by_key(game_key:, publisher_id: nil, publisher_slug: nil, cache: nil) key = cache_key('games', 'get_by_key', publisher_id: publisher_id, publisher_slug: publisher_slug, game_key: game_key) with_cache(key, resource: :games, cache: cache) do query = QueryBuilder.game(publisher_id: publisher_id, publisher_slug: publisher_slug, game_key: game_key) variables = build_variables(publisherId: publisher_id, publisherSlug: publisher_slug, gameKey: game_key) data = connection.execute(query, variables) data['game'] ? Game.new(data['game'], client: client) : nil end end |
#get_by_slug(game_slug:, publisher_id: nil, publisher_slug: nil, cache: nil) ⇒ Object
Get a publisher-managed game by slug.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/carddb/resources/games.rb', line 49 def get_by_slug(game_slug:, publisher_id: nil, publisher_slug: nil, cache: nil) key = cache_key('games', 'get_by_slug', publisher_id: publisher_id, publisher_slug: publisher_slug, game_slug: game_slug) with_cache(key, resource: :games, cache: cache) do query = QueryBuilder.game(publisher_id: publisher_id, publisher_slug: publisher_slug, game_slug: game_slug) variables = build_variables(publisherId: publisher_id, publisherSlug: publisher_slug, gameSlug: game_slug) data = connection.execute(query, variables) data['game'] ? Game.new(data['game'], client: client) : nil end end |
#get_regions(publisher_slug: nil, game_key: nil, include_inactive: nil, cache: nil) ⇒ Object
Get publisher-defined game region metadata for scan-capable clients.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/carddb/resources/games.rb', line 78 def get_regions(publisher_slug: nil, game_key: nil, include_inactive: nil, cache: nil) resolved_publisher = resolve_publisher(publisher_slug) resolved_game = resolve_game(game_key) validate_access!(resolved_publisher, resolved_game) variables = build_variables( publisherSlug: resolved_publisher, gameKey: resolved_game, includeInactive: include_inactive ) key = cache_key('games', 'get_regions', **variables) with_cache(key, resource: :games, cache: cache) do data = connection.execute(QueryBuilder.game_scan_regions, variables) (data['gameScanRegions'] || []).map { |region| GameScanRegion.new(region, client: client) } end end |
#list(publisher_id:, first: nil, after: nil, include_archived: nil, cache: nil) ⇒ Object
List games 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 |
# File 'lib/carddb/resources/games.rb', line 8 def list(publisher_id:, first: nil, after: nil, include_archived: nil, cache: nil) query = QueryBuilder.list_games( publisher_id: publisher_id, first: first, after: after, include_archived: include_archived ) variables = build_variables( publisherId: publisher_id, first: first, after: after, includeArchived: include_archived ) key = cache_key('games', 'list', **variables) data = with_cache(key, resource: :games, cache: cache) { connection.execute(query, variables) } Collection.new( data['games'], item_class: Game, next_page_loader: lambda { |cursor| list(publisher_id: publisher_id, first: first, after: cursor, include_archived: include_archived, cache: cache) }, client: client ) end |
#search(publisher_slug: nil, search: nil, first: nil, after: nil) ⇒ Collection<Game>
Search for games
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/carddb/resources/games.rb', line 103 def search(publisher_slug: nil, search: nil, first: nil, after: nil) resolved_publisher = config.resolve_publisher(publisher_slug) validate_access!(resolved_publisher, nil) if resolved_publisher query = QueryBuilder.search_games( publisher_slug: resolved_publisher, search: search, first: first, after: after ) variables = build_variables( publisherSlug: resolved_publisher, search: search, first: first, after: after ) data = connection.execute(query, variables) # Create next page loader next_page_loader = lambda do |cursor| search( publisher_slug: publisher_slug, search: search, first: first, after: cursor ) end Collection.new( data['searchGames'], item_class: Game, next_page_loader: next_page_loader, client: client ) end |
#update(id:, input:) ⇒ Object
Update a publisher-managed game. Requires a server-side secret credential.
70 71 72 73 74 75 |
# File 'lib/carddb/resources/games.rb', line 70 def update(id:, input:) config.require_secret_credential!('games.update') data = connection.execute(QueryBuilder.update_game, { id: id, input: input }) Game.new(data['gameUpdate'], client: client) end |