Class: CardDB::Resources::Games

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

Overview

Games resource for searching and fetching games

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

#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

Parameters:

  • id (String)

    The game UUID

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

    Whether to cache (nil = use config setting)

Returns:

  • (Game, nil)

    The game or nil if not found



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/carddb/resources/games.rb', line 127

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

Parameters:

  • ids (Array<String>)

    Array of game UUIDs (max 100)

Returns:

  • (Array<Game>)

    Array of games

Raises:

  • (ArgumentError)

    If more than 100 IDs provided



171
172
173
174
175
176
177
178
# File 'lib/carddb/resources/games.rb', line 171

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

Parameters:

  • game_key (String)

    Game key (required)

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

    Publisher slug (uses default if not provided)

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

    Whether to cache (nil = use config setting)

Returns:

  • (Game, nil)

    The game or nil if not found



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/carddb/resources/games.rb', line 145

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

#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

Parameters:

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

    Filter by publisher slug

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

    Search by name

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

    Maximum number of results

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

    Cursor for pagination

Returns:



84
85
86
87
88
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
# File 'lib/carddb/resources/games.rb', line 84

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