0

I am using FOOD2FORK api to extract data using HTTParty in ruby.

My code :

require 'httparty'  #Note : This file is in the models >> filename.rb
#Don't need to require when using bundler
#Restful Web Services 
#1. Base URI 2. Support XML or JSON 3. Support HTTP operations (GET, POST)
#Think of web at MVC : website you can get resources in any format 
#HTTParty parses XML or JSON for you (like your browser - it's a client).  Parses into a Ruby hash or array

class Recipe
    include HTTParty
    ENV["FOOD2FORK_KEY"] = "5b6b74c6cc0fa9dc23871a7ae753f6c7"

    base_uri "https://food2fork.com/api" #Same across most requests 
    default_params key: ENV["FOOD2FORK_KEY"], fields: "image_url" #defaults, like API developer key #fields: "image_url, source_url, title",
    format :json #Tell it which format data comes in
    #q:"search" request parameter 

    def self.for (keyword) 
        #class method called for which takes in a term 
        get("/search", query: {q: keyword})["recipes"]  #get is method provided by HTTParty 


        #returns array where each element in the array is a hash 
    end 
    pp Recipe.for "chocolate"
end

It's returning me

[
  {
    "publisher"=>"BBC Good Food", 
    "f2f_url"=>"http://food2fork.com/view/9089e3", 
    "title"=>"Cookie Monster cupcakes", 
    "source_url"=>"http://www.bbcgoodfood.com/recipes/873655/cookie-monster-cupcakes",
    "recipe_id"=>"9089e3", 
    "image_url"=>"http://static.food2fork.com/604133_mediumd392.jpg", 
    "social_rank"=>100.0, 
    "publisher_url"=>"http://www.bbcgoodfood.com"
  }
]

But I only want to extract it's image_url even on using field it's extracting the whole data set any idea how to extract only image_url ?

P.S You can check the format of JSON here - http://food2fork.com/api/search?key=65987bf1f4b22007c365c243f5670f35&q=shredded%20chicken

2
  • 2
    Assuming, you are storing the response response.map{|res| res["image_url"]} should do the trick for you. Commented Aug 30, 2018 at 5:19
  • If I understand your problem correctly, you want to return only the image_urls as response. This I think is not possible since it is an external API. You can fetch the image_urls from the response though with this response[:recipes].map{|x| x['image_url']} Commented Aug 30, 2018 at 5:35

1 Answer 1

1

Below code should work

response = your response from API

response_data = JSON.parse(response.body).with_indifferent_access

response_data['recipes'].each do |recipe|
  puts recipe['image_url'] # image_url of block
end
Sign up to request clarification or add additional context in comments.

2 Comments

1. with_indifferent_access makes no sense; 2. this code produces several local variables inside a block, immediately discarded and returns nothing useful. The comment by @KedarnagMukanahallipatna above is correct and should be considered the valid answer.
@mudasobwa 1) it make sense if developer want to fetch with symbol instead of string 2) Yes. i know it will produce lots local variables and its not useful but its only for understanding of OP

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.