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

Parameters:

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

    API key for authentication

  • 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)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/carddb/client.rb', line 35

def initialize(
  api_key: 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,
    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



123
124
125
126
127
# File 'lib/carddb/client.rb', line 123

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

#datasetsResources::Datasets

Access the Datasets resource

Returns:



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

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

#decksResources::Decks

Access the Decks resource

Returns:



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

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

#gamesResources::Games

Access the Games resource

Returns:



70
71
72
# File 'lib/carddb/client.rb', line 70

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



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

def me
  return nil unless config.api_key

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

#publishersResources::Publishers

Access the Publishers resource



63
64
65
# File 'lib/carddb/client.rb', line 63

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

#recordsResources::Records

Access the Records resource

Returns:



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

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

#rulesResources::Rules

Access the Rules resource

Returns:



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

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