Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Premium economy #26

Open
wants to merge 8 commits into
base: form_tabs
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/helpers/searches_helper.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
17 changes: 14 additions & 3 deletions app/models/nano_api/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions lib/nano_api/search_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class SearchId

REGEX = /(?<match> # For routing constraints compatibility
(?<path_parts>#{PATH_PART_PATTERN}{2,})
(?<range>f)?
(?<trip_class>b)?
(?<trip_class>[b#{NanoApi::Search::TRIP_CLASSES.join}])?
(?<adults>\d)
(?<children>\d)?
(?<infants>\d)?
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 22 additions & 14 deletions spec/lib/search_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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} }

Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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
Expand All @@ -105,41 +113,41 @@
{ 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
subject.compose(
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
subject.compose(
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