Class: CardDB::Resources::Publishers
- Defined in:
- lib/carddb/resources/publishers.rb
Overview
Publishers resource for searching and fetching publishers
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#fetch(id: nil, slug: nil, cache: nil) ⇒ Publisher?
Fetch a publisher by ID or slug.
-
#fetch_many(slugs) ⇒ Array<Publisher>
Fetch multiple publishers by slugs.
-
#search(search: nil, first: nil, after: nil) ⇒ Collection<Publisher>
Search for publishers.
Methods inherited from Base
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
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
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
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 |