Home > Back-end >  I am trying to retrieve data from a Ruby cdn of the Pokemon API but I am struggling
I am trying to retrieve data from a Ruby cdn of the Pokemon API but I am struggling

Time:12-05

Below is my Pokemon TCG API key

I have read the documentation but I cannot find a way to retrieve the data I need which is the images of each card in a set. If anyone can help with or give me any pointers on how to retrieve this I would greatly appreciate it.

https://github.com/PokemonTCG/pokemon-tcg-sdk-ruby/blob/master/README.md

This above is the link to the documentation on Github.

Pokemon.configure do |config|
  config.api_key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
end

sets = Pokemon::Set.where(q: 'legalities.standard:legal').first
p sets

I am expecting to retrieve specific data from the Pokemon TCG API which from above should be within these Ruby classes.

CodePudding user response:

p sets.images.symbol will print out the link to:

"https://images.pokemontcg.io/swshp/symbol.png"

p sets.images.logo will print out the link to:

"https://images.pokemontcg.io/swshp/logo.png"

When you are unsure what is contained within the class you're getting, in this case a Pokemon::Set class, you may see all the methods of this resulting variable with sets.methods.sort.each{|m| p m}, there you'll find the images method and can dig deeper to symbol and logo.

In your example you are searching for sets. In case of cards:

cards = Pokemon::Card.where(page: 5, pageSize: 100)
cards.each{|card| p card.images.large}

This will print out all the links to large cards.


Update

Clarification based on comments

How to print out methods and how to know which class you are referring to.
Let's start with you code snippet:

sets = Pokemon::Set.where(q: 'legalities.standard:legal').first
puts sets.class
Output:
Pokemon::Set

It would be more clear if you name your variable as set because you're selecting the .first element of an array.

Back to my cards example:

cards = Pokemon::Card.where(page: 5, pageSize: 100)
puts cards.class
Output:
Array

As I did not select any item from the array cards naming makes perfect sense, and this array will contain a card:

card = cards.first
puts card.class
Output
Pokemon::Card

Printing methods

So, given we are now well aware of class each variable contains and what we are referring to, we are ready to print out the methods for both:

sets.methods.sort.each{|method| p method}
cards[0].methods.sort.each{|method| p method}
Output:
…
:hash
:hp
:hp=
:id
:id=
:images       <== Here!
:images=
:inspect
:instance_eval
:instance_exec
:instance_of?
…

Once again, mind the [0] selection of the first item. When we want to know methods, we want to know it either for Pokemon::Set or Pokemon::Card.

  • Related