0

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

1 Answers1

1

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.

mahemoff
  • 44,526
  • 36
  • 160
  • 222
  • Will try this out thanks, my only question is when you do params[:sectionid] isn't that referring to a query param in the request not a body Param in the response? – Sammy Smolin Mar 06 '17 at 10:48
  • I don't understand, you are trying to determine the response from the response? The response should depend on what's in the request. I think you maybe mean that the request has a JSON payload .. if so, yes, if the client sends it with the appropriate request header - `content-type: application/json`, Rails will automatically set params from that. – mahemoff Mar 06 '17 at 12:58
  • yes i think so thanks, I tried to add this code, but i'm not getting the result I want does this make sense? `@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` `@events.each do |listing| if listing[0]["sectionId"].present? render 'show.html.erb'` `else render 'index.html.erb'` `end` `end` ` end` – Sammy Smolin Mar 08 '17 at 00:40
  • The condition looks better but you shouldn't be putting render inside the loop. First construct a string by looping, them use render to output it. Or better still, do that in your view template. I would start by making a separate class like `StubHub` and unit testing the data structure it receives, and whatever functions your template needs to render that. – mahemoff Mar 08 '17 at 08:09