Class: CardDB::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/carddb/client.rb

Overview

Main client class for interacting with the CardDB API.

Examples:

Basic usage

client = CardDB::Client.new(api_key: "carddb_xxx")
games = client.games.search(publisher_slug: "pokemon-company")

With defaults

client = CardDB::Client.new(
  api_key: "carddb_xxx",
  default_publisher: "pokemon-company",
  default_game: "pokemon-tcg"
)
records = client.records.search(dataset_key: "cards")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, publishable_key: nil, secret_key: nil, access_token: nil, environment: nil, endpoint: nil, timeout: nil, open_timeout: nil, default_publisher: nil, default_game: nil, allowed_publishers: nil, allowed_games: nil, config: nil) ⇒ Client

Create a new CardDB client.

rubocop:disable Metrics/ParameterLists

Parameters:

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

    API key for authentication

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

    Browser-safe publishable API key

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

    Server-side secret API key for trusted workflows

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

    OAuth bearer token for user-authorized requests

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

    API endpoint URL

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

    Request timeout in seconds

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

    Connection timeout in seconds

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

    Default publisher slug

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

    Default game key

  • allowed_publishers (Array<String>, nil) (defaults to: nil)

    Allowed publisher slugs

  • allowed_games (Hash<String, Array<String>>, nil) (defaults to: nil)

    Allowed games per publisher

  • config (Configuration, nil) (defaults to: nil)

    Configuration object (overrides other params)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/carddb/client.rb', line 39

def initialize(
  api_key: nil,
  publishable_key: nil,
  secret_key: nil,
  access_token: nil,
  environment: nil,
  endpoint: nil,
  timeout: nil,
  open_timeout: nil,
  default_publisher: nil,
  default_game: nil,
  allowed_publishers: nil,
  allowed_games: nil,
  config: nil
)
  @config = build_config(
    config: config,
    api_key: api_key,
    publishable_key: publishable_key,
    secret_key: secret_key,
    access_token: access_token,
    environment: environment,
    endpoint: endpoint,
    timeout: timeout,
    open_timeout: open_timeout,
    default_publisher: default_publisher,
    default_game: default_game,
    allowed_publishers: allowed_publishers,
    allowed_games: allowed_games
  )
  @connection = Connection.new(@config)
end

Instance Attribute Details

#configConfiguration (readonly)

Returns The client configuration.

Returns:



19
20
21
# File 'lib/carddb/client.rb', line 19

def config
  @config
end

#connectionConnection (readonly)

Returns The HTTP connection.

Returns:



22
23
24
# File 'lib/carddb/client.rb', line 22

def connection
  @connection
end

Instance Method Details

#batch {|Batch| ... } ⇒ Array

Execute multiple queries in a single batch request.

Examples:

results = client.batch do |b|
  b.games.fetch("uuid-1")
  b.publishers.fetch(slug: "pokemon-company")
end

Yields:

  • (Batch)

    The batch builder

Returns:

  • (Array)

    Results in the same order as operations were added



136
137
138
139
140
# File 'lib/carddb/client.rb', line 136

def batch
  batch = Batch.new(self)
  yield batch
  batch.execute
end

#datasetsResources::Datasets

Access the Datasets resource

Returns:



90
91
92
# File 'lib/carddb/client.rb', line 90

def datasets
  @datasets ||= Resources::Datasets.new(self, connection, config)
end

#decksResources::Decks

Access the Decks resource

Returns:



104
105
106
# File 'lib/carddb/client.rb', line 104

def decks
  @decks ||= Resources::Decks.new(self, connection, config)
end

#gamesResources::Games

Access the Games resource

Returns:



83
84
85
# File 'lib/carddb/client.rb', line 83

def games
  @games ||= Resources::Games.new(self, connection, config)
end

#meHash?

Get information about the current API key

Returns:

  • (Hash, nil)

    API key info or nil if no API key



118
119
120
121
122
123
124
# File 'lib/carddb/client.rb', line 118

def me
  return nil unless config.effective_api_key || config.access_token

  query = QueryBuilder.fetch_me
  data = connection.execute(query, {})
  data['fetchMe']
end

#publishersResources::Publishers

Access the Publishers resource



76
77
78
# File 'lib/carddb/client.rb', line 76

def publishers
  @publishers ||= Resources::Publishers.new(self, connection, config)
end

#recordsResources::Records

Access the Records resource

Returns:



97
98
99
# File 'lib/carddb/client.rb', line 97

def records
  @records ||= Resources::Records.new(self, connection, config)
end

#rulesResources::Rules

Access the Rules resource

Returns:



111
112
113
# File 'lib/carddb/client.rb', line 111

def rules
  @rules ||= Resources::Rules.new(self, connection, config)
end