0

I am trying to create an instance variable that contains the list of URLs in the given JSON Return from that call (https://en.wikipedia.org/w/api.php?action=query&titles=World&generator=images&gimlimit=10&prop=imageinfo&iiprop=url|dimensions|mime&format=json)

In the first instance when i accessing the object individually using i get the correct value. If however iterate to get multiple values i am getting.

undefined method `[]' for nil:NilClass (NoMethodError) error

 @links=[] 
@description= @data_hash["query"]["pages"]["-1"]["imageinfo"][0]["url"] #Works ok
url = ("https://en.wikipedia.org/w/api.php?action=query&titles=World&generator=images&gimlimit=10&prop=imageinfo&iiprop=url|dimensions|mime&format=json")
  uri = URI(url)
  response = Net::HTTP.get(uri)
  @data_hash= JSON.parse(response)
 ["url"]
 @data_hash["query"]["pages"].map { |i|

    i.map { |e|
       @links<< e['imageinfo'][0]
      } 
    }

1 Answer 1

1

This should work:

@links = @data_hash['query']['pages']
           .values
           .map { |page| page['imageinfo'].first['url'] }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.