Class: CardDB::Resources::Publishers

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

Overview

Publishers resource for searching and fetching publishers

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

#fetch(id: nil, slug: nil, cache: nil) ⇒ Publisher?

Fetch a publisher by ID or slug

Parameters:

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

    The publisher UUID

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

    The publisher slug

  • cache (Boolean, nil) (defaults to: nil)

    Whether to cache (nil = use config setting)

Returns:

  • (Publisher, nil)

    The publisher or nil if not found

Raises:

  • (ArgumentError)

    If neither id nor slug is provided



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/carddb/resources/publishers.rb', line 52

def fetch(id: nil, slug: nil, cache: nil)
  raise ArgumentError, 'Must provide either id or slug' if id.nil? && slug.nil?

  key = cache_key('publishers', 'fetch', id: id, slug: slug)
  with_cache(key, resource: :publishers, cache: cache) do
    if id
      query = QueryBuilder.fetch_publisher_by_id
      data = connection.execute(query, { id: id })
    else
      query = QueryBuilder.fetch_publisher_by_slug
      data = connection.execute(query, { slug: slug })
    end

    return nil unless data['fetchPublisher']

    Publisher.new(data['fetchPublisher'], client: client)
  end
end

#fetch_many(slugs) ⇒ Array<Publisher>

Fetch multiple publishers by slugs

Parameters:

  • slugs (Array<String>)

    Array of publisher slugs (max 100)

Returns:

Raises:

  • (ArgumentError)

    If more than 100 slugs provided



76
77
78
79
80
81
82
83
# File 'lib/carddb/resources/publishers.rb', line 76

def fetch_many(slugs)
  raise ArgumentError, 'Maximum 100 slugs allowed' if slugs.length > 100

  query = QueryBuilder.fetch_publishers
  data = connection.execute(query, { slugs: slugs })

  (data['fetchPublishers'] || []).map { |p| Publisher.new(p, client: client) }
end

#search(search: nil, first: nil, after: nil) ⇒ Collection<Publisher>

Search for publishers

Parameters:

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

    Search by name

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

    Maximum number of results

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

    Cursor for pagination

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/carddb/resources/publishers.rb', line 13

def search(search: nil, first: nil, after: nil)
  query = QueryBuilder.search_publishers(
    search: search,
    first: first,
    after: after
  )

  variables = build_variables(
    search: search,
    first: first,
    after: after
  )

  data = connection.execute(query, variables)

  # Create next page loader
  next_page_loader = lambda do |cursor|
    search(
      search: search,
      first: first,
      after: cursor
    )
  end

  Collection.new(
    data['searchPublishers'],
    item_class: Publisher,
    next_page_loader: next_page_loader,
    client: client
  )
end