开发者

I don't want to make 2 API calls for this

The following code does two things:

  1. Checks to see if the item has the key "checkins"
  2. Gets the checkins value if it has the key

I really don't like making two API calls for this. Its really slowed my code down but I'm forced to because if I make the API call for checkins and the key doesn't exist, then my code blows up. There must be a bet开发者_开发技巧ter way to do this.

if graph.get_object(self.place_id).has_key?("checkins") 
  checkins =  graph.get_object(self.place_id)["checkins"] - self.checkins
else 
  checkins = self.checkins
end


graph_checkins = graph.get_object(self.place_id)["checkins"]
checkins = if graph_checkins
             graph_checkins - self.checkins
           else
             self.checkins
           end


Here is what I meant by my comment:

graph_object = graph.get_object(self.place_id) # assign return to variable
if graph_object.has_key?("checkins")
  # reference returned object, no need to request it again
  checkins = graph_object["checkins"] - self.checkins
else
  checkins = self.checkins
end

*I'd use Wayne Conrad's example, simpler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜