carddb 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardDB
4
+ # Main client class for interacting with the CardDB API.
5
+ #
6
+ # @example Basic usage
7
+ # client = CardDB::Client.new(api_key: "carddb_xxx")
8
+ # games = client.games.search(publisher_slug: "pokemon-company")
9
+ #
10
+ # @example With defaults
11
+ # client = CardDB::Client.new(
12
+ # api_key: "carddb_xxx",
13
+ # default_publisher: "pokemon-company",
14
+ # default_game: "pokemon-tcg"
15
+ # )
16
+ # records = client.records.search(dataset_key: "cards")
17
+ class Client
18
+ # @return [Configuration] The client configuration
19
+ attr_reader :config
20
+
21
+ # @return [Connection] The HTTP connection
22
+ attr_reader :connection
23
+
24
+ # Create a new CardDB client.
25
+ #
26
+ # @param api_key [String, nil] API key for authentication
27
+ # @param endpoint [String, nil] API endpoint URL
28
+ # @param timeout [Integer, nil] Request timeout in seconds
29
+ # @param open_timeout [Integer, nil] Connection timeout in seconds
30
+ # @param default_publisher [String, nil] Default publisher slug
31
+ # @param default_game [String, nil] Default game key
32
+ # @param allowed_publishers [Array<String>, nil] Allowed publisher slugs
33
+ # @param allowed_games [Hash<String, Array<String>>, nil] Allowed games per publisher
34
+ # @param config [Configuration, nil] Configuration object (overrides other params)
35
+ def initialize(
36
+ api_key: nil,
37
+ endpoint: nil,
38
+ timeout: nil,
39
+ open_timeout: nil,
40
+ default_publisher: nil,
41
+ default_game: nil,
42
+ allowed_publishers: nil,
43
+ allowed_games: nil,
44
+ config: nil
45
+ )
46
+ @config = build_config(
47
+ config: config,
48
+ api_key: api_key,
49
+ endpoint: endpoint,
50
+ timeout: timeout,
51
+ open_timeout: open_timeout,
52
+ default_publisher: default_publisher,
53
+ default_game: default_game,
54
+ allowed_publishers: allowed_publishers,
55
+ allowed_games: allowed_games
56
+ )
57
+ @connection = Connection.new(@config)
58
+ end
59
+
60
+ # Access the Publishers resource
61
+ #
62
+ # @return [Resources::Publishers]
63
+ def publishers
64
+ @publishers ||= Resources::Publishers.new(self, connection, config)
65
+ end
66
+
67
+ # Access the Games resource
68
+ #
69
+ # @return [Resources::Games]
70
+ def games
71
+ @games ||= Resources::Games.new(self, connection, config)
72
+ end
73
+
74
+ # Access the Datasets resource
75
+ #
76
+ # @return [Resources::Datasets]
77
+ def datasets
78
+ @datasets ||= Resources::Datasets.new(self, connection, config)
79
+ end
80
+
81
+ # Access the Records resource
82
+ #
83
+ # @return [Resources::Records]
84
+ def records
85
+ @records ||= Resources::Records.new(self, connection, config)
86
+ end
87
+
88
+ # Access the Decks resource
89
+ #
90
+ # @return [Resources::Decks]
91
+ def decks
92
+ @decks ||= Resources::Decks.new(self, connection, config)
93
+ end
94
+
95
+ # Access the Rules resource
96
+ #
97
+ # @return [Resources::Rules]
98
+ def rules
99
+ @rules ||= Resources::Rules.new(self, connection, config)
100
+ end
101
+
102
+ # Get information about the current API key
103
+ #
104
+ # @return [Hash, nil] API key info or nil if no API key
105
+ def me
106
+ return nil unless config.api_key
107
+
108
+ query = QueryBuilder.fetch_me
109
+ data = connection.execute(query, {})
110
+ data['fetchMe']
111
+ end
112
+
113
+ # Execute multiple queries in a single batch request.
114
+ #
115
+ # @yield [Batch] The batch builder
116
+ # @return [Array] Results in the same order as operations were added
117
+ #
118
+ # @example
119
+ # results = client.batch do |b|
120
+ # b.games.fetch("uuid-1")
121
+ # b.publishers.fetch(slug: "pokemon-company")
122
+ # end
123
+ def batch
124
+ batch = Batch.new(self)
125
+ yield batch
126
+ batch.execute
127
+ end
128
+
129
+ private
130
+
131
+ def build_config(config:, **options)
132
+ # Use provided config or global config as base
133
+ base_config = config || CardDB.configuration
134
+
135
+ # Merge in any provided options
136
+ base_config.merge(options.compact)
137
+ end
138
+ end
139
+ end