Search code examples
ruby-on-railsjsonapiif-statementunirest

how to make two api requests using a response param and conditional


I'm trying to use this conditional so if the response does not contain a sectionid it will return the first response and if it does contain a sectionid it will return the second response. The problem is, is that sectionid is in the response and I'm not sure how to define the conditional with the response before I make the request, the way I'm currently doing it the first response is returned no matter the circumstance of the request. any help would be much appreciated thanks.

 def show
  if "sectionId" != true 
    @events = Unirest.get("https://api.stubhub.com/search/inventory/v2?eventid=#{params[:id]}&limit=1000&start=0&rows=1000&sectionStats=true", headers:{"Accept" => "application/json", "Authorization" => "Bearer*****"}).body
 else
   @events = Unirest.get("https://api.stubhub.com/search/inventory/v2/sectionsummary?eventid=#{params[:id]}", headers:{"Accept" => "application/json", "Authorization" => "Bearer ******"}).body
end

Solution

  • Your condition will always be true because you are checking if the string value "sectionId" is different to the boolean value true. It's always different.

    Shouldn't it be params[:sectionId] instead of "sectionId"? Look at the logs and check what params are entering into show. Alternatively, add puts params.inspect on the first line of show to directly output them on your console.