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

#create(input:) ⇒ Object

Create a hosted deck.



44
45
46
47
# File 'lib/carddb/resources/decks.rb', line 44

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

#delete(id:) ⇒ Object

Delete a hosted deck. rubocop:disable Naming/PredicateMethod



57
58
59
60
# File 'lib/carddb/resources/decks.rb', line 57

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.



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

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

#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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/carddb/resources/decks.rb', line 64

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

#update(id:, input:) ⇒ Object

Update a hosted deck.



50
51
52
53
# File 'lib/carddb/resources/decks.rb', line 50

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