Class: CardDB::Resources::Decks

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

Overview

Decks resource for hosted decks and external deck hydration.

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

#collaborators(deck_id:, cache: nil) ⇒ Object

List collaborators for a deck.



105
106
107
108
109
110
111
# File 'lib/carddb/resources/decks.rb', line 105

def collaborators(deck_id:, cache: nil)
  key = cache_key('decks', 'collaborators', deck_id: deck_id)
  with_cache(key, resource: :decks, cache: cache) do
    data = connection.execute(QueryBuilder.deck_collaborators, { deckId: deck_id })
    (data['deckCollaborators'] || []).map { |collaborator| DeckCollaborator.new(collaborator, client: client) }
  end
end

#create(input:) ⇒ Object

Create a hosted deck.



123
124
125
126
# File 'lib/carddb/resources/decks.rb', line 123

def create(input:)
  data = connection.execute(QueryBuilder.create_deck, { input: input })
  Deck.new(data['deckCreate'], client: client)
end

#create_preview_token(input:) ⇒ Object

Create a revocable draft preview token.



161
162
163
164
# File 'lib/carddb/resources/decks.rb', line 161

def create_preview_token(input:)
  data = connection.execute(QueryBuilder.create_deck_preview_token, { input: input })
  DeckPreviewTokenCreatePayload.new(data['deckPreviewTokenCreate'], client: client)
end

#delete(id:) ⇒ Object

Delete a hosted deck. rubocop:disable Naming/PredicateMethod



176
177
178
179
# File 'lib/carddb/resources/decks.rb', line 176

def delete(id:)
  data = connection.execute(QueryBuilder.delete_deck, { id: id })
  !!data['deckDelete']
end

#fetch(id, cache: nil) ⇒ Object

Fetch a hosted deck by CardDB UUID.



26
27
28
29
30
31
32
# File 'lib/carddb/resources/decks.rb', line 26

def fetch(id, cache: nil)
  key = cache_key('decks', 'fetch', id: id)
  with_cache(key, resource: :decks, cache: cache) do
    data = connection.execute(QueryBuilder.fetch_deck, { id: id })
    data['fetchDeck'] ? Deck.new(data['fetchDeck'], client: client) : nil
  end
end

#fetch_by_external_ref(external_ref:, cache: nil) ⇒ Object

Fetch a hosted deck by external reference for the current API application.



60
61
62
63
64
65
66
# File 'lib/carddb/resources/decks.rb', line 60

def fetch_by_external_ref(external_ref:, cache: nil)
  key = cache_key('decks', 'fetch_by_external_ref', external_ref: external_ref)
  with_cache(key, resource: :decks, cache: cache) do
    data = connection.execute(QueryBuilder.fetch_deck_by_external_ref, { externalRef: external_ref })
    data['fetchDeckByExternalRef'] ? Deck.new(data['fetchDeckByExternalRef'], client: client) : nil
  end
end

#fetch_by_slug(slug:, publisher_slug: nil, game_key: nil, cache: nil) ⇒ Object

Fetch a hosted deck by canonical or historical slug.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/carddb/resources/decks.rb', line 44

def fetch_by_slug(slug:, 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('decks', 'fetch_by_slug', publisher_slug: resolved_publisher, game_key: resolved_game, slug: slug)
  with_cache(key, resource: :decks, cache: cache) do
    data = connection.execute(
      QueryBuilder.fetch_deck_by_slug,
      { publisherSlug: resolved_publisher, gameKey: resolved_game, slug: slug }
    )
    data['fetchDeckBySlug'] ? Deck.new(data['fetchDeckBySlug'], client: client) : nil
  end
end

#fetch_mine(id, cache: nil) ⇒ Object

Fetch one deck owned by the current account or API application.



35
36
37
38
39
40
41
# File 'lib/carddb/resources/decks.rb', line 35

def fetch_mine(id, cache: nil)
  key = cache_key('decks', 'fetch_mine', id: id)
  with_cache(key, resource: :decks, cache: cache) do
    data = connection.execute(QueryBuilder.my_deck, { id: id })
    data['myDeck'] ? Deck.new(data['myDeck'], client: client) : nil
  end
end

#fetch_version(id, cache: nil) ⇒ Object

Fetch one immutable published deck version.



69
70
71
72
73
74
75
# File 'lib/carddb/resources/decks.rb', line 69

def fetch_version(id, cache: nil)
  key = cache_key('decks', 'fetch_version', id: id)
  with_cache(key, resource: :decks, cache: cache) do
    data = connection.execute(QueryBuilder.deck_version, { id: id })
    data['deckVersion'] ? DeckVersion.new(data['deckVersion'], client: client) : nil
  end
end

#hydrate_entries(dataset_key:, entries:, publisher_slug: nil, game_key: nil, identifier_field: nil, cache: nil) ⇒ Object

Hydrate third-party-owned deck entries without storing them in CardDB.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/carddb/resources/decks.rb', line 183

def hydrate_entries(dataset_key:, entries:, publisher_slug: nil, game_key: nil, identifier_field: nil, cache: nil)
  return [] if entries.empty?

  resolved_publisher = resolve_publisher(publisher_slug)
  resolved_game = resolve_game(game_key)
  validate_access!(resolved_publisher, resolved_game)

  identifiers = entries.map { |entry| entry_identifier(entry) }
  key = cache_key(
    'decks',
    'hydrate_entries',
    publisher_slug: resolved_publisher,
    game_key: resolved_game,
    dataset_key: dataset_key,
    identifiers: identifiers
  )
  records = with_cache(key, resource: :decks, cache: cache) do
    data = connection.execute(
      QueryBuilder.fetch_records_by_identifier,
      {
        publisherSlug: resolved_publisher,
        gameKey: resolved_game,
        datasetKey: dataset_key,
        identifiers: identifiers
      }
    )
    (data['fetchRecordsByIdentifier'] || []).map { |record| Record.new(record, client: client) }
  end

  records_by_identifier = records_by_deck_identifier(records, identifiers, identifier_field)
  entries.map do |entry|
    entry.merge(record: records_by_identifier[entry_identifier(entry)])
  end
end

#list_mine(first: nil, after: nil, cache: nil) ⇒ Object

List decks owned by the current account or API application.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/carddb/resources/decks.rb', line 8

def list_mine(first: nil, after: nil, cache: nil)
  query = QueryBuilder.list_my_decks(first: first, after: after)
  variables = build_variables(first: first, after: after)
  key = cache_key('decks', 'list_mine', **variables)

  data = with_cache(key, resource: :decks, cache: cache) do
    connection.execute(query, variables)
  end

  Collection.new(
    data['myDecks'],
    item_class: Deck,
    next_page_loader: ->(cursor) { list_mine(first: first, after: cursor, cache: cache) },
    client: client
  )
end

#list_versions(deck_id:, first: nil, after: nil, cache: nil) ⇒ Object

List immutable published versions for a deck.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/carddb/resources/decks.rb', line 78

def list_versions(deck_id:, first: nil, after: nil, cache: nil)
  query = QueryBuilder.deck_versions(first: first, after: after)
  variables = build_variables(deckId: deck_id, first: first, after: after)
  key = cache_key('decks', 'list_versions', **variables)

  data = with_cache(key, resource: :decks, cache: cache) do
    connection.execute(query, variables)
  end

  Collection.new(
    data['deckVersions'],
    item_class: DeckVersion,
    next_page_loader: ->(cursor) { list_versions(deck_id: deck_id, first: first, after: cursor, cache: cache) },
    client: client
  )
end

#preview(token:, cache: nil) ⇒ Object

Fetch current draft data using a revocable preview token.



96
97
98
99
100
101
102
# File 'lib/carddb/resources/decks.rb', line 96

def preview(token:, cache: nil)
  key = cache_key('decks', 'preview', token: token)
  with_cache(key, resource: :decks, cache: cache) do
    data = connection.execute(QueryBuilder.deck_preview, { token: token })
    data['deckPreview'] ? Deck.new(data['deckPreview'], client: client) : nil
  end
end

#preview_tokens(deck_id:, cache: nil) ⇒ Object

List draft preview tokens for a deck.



114
115
116
117
118
119
120
# File 'lib/carddb/resources/decks.rb', line 114

def preview_tokens(deck_id:, cache: nil)
  key = cache_key('decks', 'preview_tokens', deck_id: deck_id)
  with_cache(key, resource: :decks, cache: cache) do
    data = connection.execute(QueryBuilder.deck_preview_tokens, { deckId: deck_id })
    (data['deckPreviewTokens'] || []).map { |token| DeckPreviewToken.new(token, client: client) }
  end
end

#publish(id:, input: {}) ⇒ Object

Publish the current draft as an immutable deck version.



135
136
137
138
# File 'lib/carddb/resources/decks.rb', line 135

def publish(id:, input: {})
  data = connection.execute(QueryBuilder.publish_deck, { id: id, input: input })
  DeckVersion.new(data['deckPublish'], client: client)
end

#remove_collaborator(deck_id:, account_id:) ⇒ Object

Remove a deck collaborator. rubocop:disable Naming/PredicateMethod



151
152
153
154
155
156
157
# File 'lib/carddb/resources/decks.rb', line 151

def remove_collaborator(deck_id:, account_id:)
  data = connection.execute(
    QueryBuilder.remove_deck_collaborator,
    { deckId: deck_id, accountId:  }
  )
  !!data['deckCollaboratorRemove']
end

#revoke_preview_token(id:) ⇒ Object

Revoke a draft preview token. rubocop:disable Naming/PredicateMethod



168
169
170
171
# File 'lib/carddb/resources/decks.rb', line 168

def revoke_preview_token(id:)
  data = connection.execute(QueryBuilder.revoke_deck_preview_token, { id: id })
  !!data['deckPreviewTokenRevoke']
end

#update(id:, input:) ⇒ Object

Update a hosted deck.



129
130
131
132
# File 'lib/carddb/resources/decks.rb', line 129

def update(id:, input:)
  data = connection.execute(QueryBuilder.update_deck, { id: id, input: input })
  Deck.new(data['deckUpdate'], client: client)
end

#upsert_collaborator(deck_id:, account_id:, role:) ⇒ Object

Create or update a deck collaborator.



141
142
143
144
145
146
147
# File 'lib/carddb/resources/decks.rb', line 141

def upsert_collaborator(deck_id:, account_id:, role:)
  data = connection.execute(
    QueryBuilder.upsert_deck_collaborator,
    { deckId: deck_id, accountId: , role: role }
  )
  DeckCollaborator.new(data['deckCollaboratorUpsert'], client: client)
end