3a5f66f6c3feb6092d375c3e145de0d4

I'm wrapping an API in ruby and I'm going back and forth on what would be the best way to do this. Should I offer several methods for the user to call to get flights by a certain airline (ex: flights_by_airline_on(airline_id, flight_number, date), flights_by_airline(airline_id, flight_number), and flights_by_airline(airline_id)) or just one method with optional parameters. If I go this route, is there a better way to parse out what parameters are used?

Thanks.

def flights_by_airline(airline_id, flight_number=nil, date=nil, params={})
      if flight_number && date
        t = convert_date(date)
        perform_get("/airlines/#{airline_id}/flights/#{flight_number}/#{t}.xml", params)
      elsif flight_number
        perform_get("/airlines/#{airline_id}/flights/#{flight_number}.xml", params)
      else
        perform_get("/airlines/#{airline_id}/flights.xml", params)
      end
    end

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

steenslag

March 1, 2010, March 01, 2010 13:18, permalink

1 rating. Login to rate!
def flights_by_airline(airline_id, flight_number=nil, date=nil, params={})
   perform_get( "/#{[airline_id, flight_number,convert_date(date)].compact.join('/')}.xml", params)
end
D41d8cd98f00b204e9800998ecf8427e

steenslag

March 1, 2010, March 01, 2010 13:32, permalink

2 ratings. Login to rate!

hmm... forgot to check for a date.

def flights_by_airline(airline_id, flight_number=nil, date=nil, params={})
   puts( "/#{[airline_id, flight_number, (convert_date(date)if date)].compact.join('/')}.xml", params)
end
3a5f66f6c3feb6092d375c3e145de0d4

jarodluebbert.myopenid.com

March 1, 2010, March 01, 2010 23:55, permalink

No rating. Login to rate!

I ended up with this, thanks guys!

def flights_by_airline(airline_id, flight_number=nil, date=nil, params={})     
      args = [airline_id, 'flights', flight_number, format(date)].compact.join('/')
      perform_get("/airlines/#{args}.xml", params)
end

Your refactoring





Format Copy from initial code

or Cancel