diff --git a/app/helpers/searches_helper.rb b/app/helpers/searches_helper.rb index 110834a..250da58 100644 --- a/app/helpers/searches_helper.rb +++ b/app/helpers/searches_helper.rb @@ -1,8 +1,9 @@ module SearchesHelper def trip_classes_map { - t('nano_api.searches.helpers.trip_classes.economy_upcase') => 0, - t('nano_api.searches.helpers.trip_classes.business_upcase') => 1 + t('nano_api.searches.helpers.trip_classes.economy_upcase') => 'Y', + t('nano_api.searches.helpers.trip_classes.premium_economy_upcase') => 'W', + t('nano_api.searches.helpers.trip_classes.business_upcase') => 'C' } end diff --git a/app/models/nano_api/search.rb b/app/models/nano_api/search.rb index 591fd1c..e2317a9 100644 --- a/app/models/nano_api/search.rb +++ b/app/models/nano_api/search.rb @@ -16,10 +16,17 @@ class Search :'en-CA' => :en } - LOCALES_TO_HOSTS = Settings.hosts.respond_to?(:to_hash) ? + TRIP_CLASS_MAPPING = { + '0' => 'Y', + '1' => 'C' + } + + TRIP_CLASSES = %w(Y C W F) + + LOCALES_TO_HOSTS = defined?(Settings) && Settings.hosts.respond_to?(:to_hash) ? Settings.hosts.to_hash.stringify_keys.invert.symbolize_keys : {} - attribute :trip_class, type: Integer, in: [0, 1], default: 0 + attribute :trip_class, type: String, in: TRIP_CLASSES, default: 'Y' attribute :with_request, type: Boolean, default: false attribute :open_jaw, type: Boolean, default: false attribute :internal, type: Boolean, default: false @@ -43,6 +50,10 @@ def locale MAPPING[value] || value end + def trip_class= value + TRIP_CLASS_MAPPING[value.to_s] || super + end + def one_way= value if value.present? && value != '0' self.segments = [segments.first] @@ -139,7 +150,7 @@ def params def search_params result = params.merge( - trip_class: params[:trip_class] == 0 ? 'Y' : 'C', + trip_class: params[:trip_class], host: host ) result.delete(:open_jaw) diff --git a/lib/nano_api/search_id.rb b/lib/nano_api/search_id.rb index 39da804..8c24d6c 100644 --- a/lib/nano_api/search_id.rb +++ b/lib/nano_api/search_id.rb @@ -17,8 +17,7 @@ class SearchId REGEX = /(? # For routing constraints compatibility (?#{PATH_PART_PATTERN}{2,}) - (?f)? - (?b)? + (?[b#{NanoApi::Search::TRIP_CLASSES.join}])? (?\d) (?\d)? (?\d)? @@ -32,8 +31,7 @@ class << self def parse(search_id) if match = search_id.match(/^#{REGEX}$/) search = NanoApi::Search.new( - range: match[:range].present?, - trip_class: match[:trip_class].present? ? 1 : 0, + trip_class: match[:trip_class].try(:upcase) == 'B' ? 'C' : match[:trip_class].presence.try(:upcase) || 'Y', passengers: %w(adults children infants).each_with_object({}) { |key, obj| obj[key] = match[key] if match[key] }, segments: [], with_request: true @@ -73,7 +71,7 @@ def parse(search_id) def compose search search = NanoApi::Search.new(search) unless search.is_a?(NanoApi::Search) data = { - trip_class: search.trip_class == 1 ? 'b' : nil, + trip_class: search.trip_class, passengers: %w(adults children infants).map { |key| search.passengers.send(key).to_s }.join.sub(/0+$/, ''), } segments = search.segments diff --git a/spec/lib/search_id_spec.rb b/spec/lib/search_id_spec.rb index d71bdd9..a0abd84 100644 --- a/spec/lib/search_id_spec.rb +++ b/spec/lib/search_id_spec.rb @@ -5,10 +5,18 @@ describe '.parse' do specify { subject.parse('MOW1005LONb1').params[:with_request].should be_true } - specify { subject.parse('MOW1005LONf1').params[:trip_class].should == 0 } - specify { subject.parse('MOW1005LONfb1').params[:trip_class].should == 1 } - - specify { subject.parse('MOW1005LONfb2').params[:passengers].should == {adults: 2, children: 0, infants: 0} } + specify { subject.parse('MOW1005LON1').params[:trip_class].should == 'Y' } + specify { subject.parse('MOW1005LONb1').params[:trip_class].should == 'C' } + specify { subject.parse('MOW1005LONY1').params[:trip_class].should == 'Y' } + specify { subject.parse('MOW1005LONC1').params[:trip_class].should == 'C' } + specify { subject.parse('MOW1005LONW1').params[:trip_class].should == 'W' } + specify { subject.parse('MOW1005LONF1').params[:trip_class].should == 'F' } + specify { subject.parse('MOW1005LONy1').params[:trip_class].should == 'Y' } + specify { subject.parse('MOW1005LONc1').params[:trip_class].should == 'C' } + specify { subject.parse('MOW1005LONw1').params[:trip_class].should == 'W' } + specify { subject.parse('MOW1005LONf1').params[:trip_class].should == 'F' } + + specify { subject.parse('MOW1005LONb2').params[:passengers].should == {adults: 2, children: 0, infants: 0} } specify { subject.parse('MOW1005LON200532').params[:passengers].should == {adults: 3, children: 2, infants: 0} } specify { subject.parse('MOW1005LON2005321').params[:passengers].should == {adults: 3, children: 2, infants: 1} } @@ -60,9 +68,9 @@ end it 'is case insensitive' do - subject.parse('amow1005lonFB2').params.should include( + subject.parse('amow1005lonB2').params.should include( passengers: {adults: 2, children: 0, infants: 0}, - trip_class: 1, + trip_class: 'C', segments: [{ origin: { iata: 'MOW', type: 'airport'}, destination: { iata: 'LON' }, @@ -90,13 +98,13 @@ { date: '2015-01-01', origin: { iata: 'DDD', type: 'city' }, destination: { iata: 'AAA', type: 'city' } }, { date: '2015-02-15', origin: { iata: 'AAA', type: 'airport' }, destination: { iata: 'BBB', type: 'city' } } ], - trip_class: '1', + trip_class: 'C', passengers: { adults: 3, children: 2, infants: 1 } - ).should == 'CAAA0110CBBB0310ACCC-CDDD0101CAAA-AAAA1502CBBBb321' + ).should == 'CAAA0110CBBB0310ACCC-CDDD0101CAAA-AAAA1502CBBBC321' end specify do @@ -105,13 +113,13 @@ { date: '2014-10-01', origin: { iata: 'AAA', type: 'city' }, destination: { iata: 'BBB', type: 'city' } }, { date: '2014-10-03', origin: { iata: 'BBB', type: 'city' }, destination: { iata: 'AAA', type: 'city' } } ], - class: '1', + trip_class: 'Y', passengers: { adults: 4, children: 0, infants: 3 } - ).should == 'CAAA0110CBBB0310403' + ).should == 'CAAA0110CBBB0310Y403' end specify do @@ -119,13 +127,13 @@ segments: [ { date: '2014-10-01', origin: { iata: 'AAA', type: 'city' }, destination: { iata: 'BBB', type: 'city' } } ], - class: '1', + trip_class: 'W', passengers: { adults: 4, children: 3, infants: 0 } - ).should == 'CAAA0110CBBB43' + ).should == 'CAAA0110CBBBW43' end specify do @@ -133,13 +141,13 @@ segments: [ { date: '2014-10-01', origin: { iata: 'AAA', type: 'city' }, destination: { iata: 'BBB', type: 'city' } } ], - class: '1', + trip_class: 'Y', passengers: { adults: 4, children: 0, infants: 0 } - ).should == 'CAAA0110CBBB4' + ).should == 'CAAA0110CBBBY4' end end end