0

I' writing a piece of code that will integrate with TripIt. In my TripIt plans, I have an example trip with ONE item (Activity). I want to sort by date. The sort fails. When I add another Activity the sort works. I believe it's because of when it's a single item, it's a hash and when it's a collection of items, it's an array of hashes.

Here is my sort method:

  @tripit_trip[k].sort_by { |h| h["StartDateTime"]["date"] }.each do |o|
    o["ObjectType"] = "ActivityObject"
    tmp[ o["StartDateTime"]["date"] ] = [] if tmp[ o["StartDateTime"]["date"] ].nil?
    tmp[ o["StartDateTime"]["date"] ] << o
  end

It fails on the first line.

Here is what the data looks like coming from TripIt:

 // One item
 --- 
 id: "20392856"
 trip_id: "11086745"
 is_client_traveler: "true"
 relative_url: /reservation/show/id/20392856
 display_name: Programming
 is_purchased: "true"
 StartDateTime: 
   date: "2011-01-09"
   timezone: Asia/Ulaanbaatar

This is the TripIt data when there are two items:

 // Two items

    --- 
    - id: "20631958"
      trip_id: "11086745"
      is_client_traveler: "true"
      relative_url: /reservation/show/id/20631958
      display_name: Programming 2
      is_purchased: "true"
      StartDateTime: 
        date: "2011-01-09"
        timezone: Asia/Ulaanbaatar
    - id: "20392856"
      trip_id: "11086745"
      is_client_traveler: "true"
      relative_url: /reservation/show/id/20392856
      display_name: Programming
      is_purchased: "true"
      StartDateTime: 
        date: "2011-01-09"
        timezone: Asia/Ulaanbaatar

Any insight on what I'm doing wrong would be wonderful.

Thanks!

1 Answer 1

1

With a single item in the list, you don't exactly need to sort. Why not just put an if in the code that skips the sort if there's only one returned item?

Sign up to request clarification or add additional context in comments.

3 Comments

That's certainly a reasonable suggestion. Looking at my code, I have quite a few if statements for each type of object coming from TripIt. So I would have to put that in there too. I'm already considering refactoring the code because it's starting to look sloppy. But that's certainly a good idea.
Given that TripIt is already returning different data structures for one result versus many (which is ugly, but nothing you can control), it wouldn't seem unreasonable to me to see special-case handling for one element in your code.
Thanks for the suggestion. I actually just checked to see if the data returned is a hash or array. Really sucks that TripIt returns two completely data structures like that. Anyway, this appears to work: gist.github.com/758019

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.