From 5a1b225f741e582ff89cbcc33643bffa0f74ec5a Mon Sep 17 00:00:00 2001 From: "K.Kolotyuk" Date: Mon, 21 Nov 2016 17:51:11 +0300 Subject: [PATCH 1/2] Add simple description builder --- lib/concierge/property_description.rb | 57 +++++++++++++++ .../poplidays/mappers/roomorama_property.rb | 9 ++- .../concierge/property_description_spec.rb | 70 +++++++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 lib/concierge/property_description.rb create mode 100644 spec/lib/concierge/property_description_spec.rb diff --git a/lib/concierge/property_description.rb b/lib/concierge/property_description.rb new file mode 100644 index 000000000..afbcfe2ef --- /dev/null +++ b/lib/concierge/property_description.rb @@ -0,0 +1,57 @@ +module Concierge + + # +Concierge::PropertyDescription+ + # + # If property does not have description this class helps to build + # it from property attributes. If some of required attributes is missed empty string will be returned. + # + # Usage: + # + # PropertyDescription.new(property).build # => String + class PropertyDescription + attr_reader :property + + def initialize(property) + @property = property + end + + def build + result = '' + if valid? + result = "This is a charming #{property.type} in #{property.city}, which can accommodate "\ + "#{pluralize(property.max_guests, 'guest', 'guests')}." + + rooms_part_start = if property.surface + "The #{property.surface} square metres apartment" + else + "It" + end + + result += "#{rooms_part_start} has #{pluralize(property.number_of_bedrooms, 'bedroom', 'bedrooms')} and "\ + "#{pluralize(property.number_of_bathrooms, 'bathroom', 'bathrooms')}." + end + + result + end + + private + + def valid? + property.type && + property.city && + property.max_guests.to_i > 0 && + property.number_of_bedrooms.to_i > 0 && + property.number_of_bathrooms.to_i > 0 + end + + def pluralize(n, singular, plural=nil) + if n == 1 + "1 #{singular}" + elsif plural + "#{n} #{plural}" + else + "#{n} #{singular}s" + end + end + end +end \ No newline at end of file diff --git a/lib/concierge/suppliers/poplidays/mappers/roomorama_property.rb b/lib/concierge/suppliers/poplidays/mappers/roomorama_property.rb index 7ef88b57d..30dc24c6c 100644 --- a/lib/concierge/suppliers/poplidays/mappers/roomorama_property.rb +++ b/lib/concierge/suppliers/poplidays/mappers/roomorama_property.rb @@ -55,6 +55,9 @@ def build(property, details, availabilities, extras) set_security_deposit!(roomorama_property, details) set_cleaning_info!(roomorama_property, extras) + if roomorama_property.description.to_s.empty? + roomorama_property.description = description_builder(roomorama_property).build + end Result.new(roomorama_property) end @@ -98,7 +101,7 @@ def set_security_deposit!(roomorama_property, details) end def build_descriptions(details) - [details.get('description.indoor'), details.get('description.outdoor')].compact.join("\n\n") + [details.get('description.indoor'), details.get('description.outdoor')].select { |d| !d.to_s.empty? }.join("\n\n") end def set_images!(result, details) @@ -225,6 +228,10 @@ def calc_cleaning_extra_price(cleaning_extra) price['value'] if price end + def description_builder(property) + Concierge::PropertyDescription.new(property) + end + def to_stay(availability, mandatory_services) Roomorama::Calendar::Stay.new( { diff --git a/spec/lib/concierge/property_description_spec.rb b/spec/lib/concierge/property_description_spec.rb new file mode 100644 index 000000000..4020b9109 --- /dev/null +++ b/spec/lib/concierge/property_description_spec.rb @@ -0,0 +1,70 @@ +require "spec_helper" + +RSpec.describe Concierge::PropertyDescription do + + describe "#build" do + context 'when property does not have required fields' do + it "returns empty string if property does not have required fields" do + p = Roomorama::Property.new('15') + + subject = described_class.new(p) + expect(subject.build).to eq '' + end + + it "returns empty string if property does not have required fields" do + p = Roomorama::Property.load( + { + identifier: '15', + disabled: true, # to make property valid + type: 'apartment', + max_guests: 4, + number_of_bedrooms: 3, + number_of_bathrooms: 2 + } + ).value + + subject = described_class.new(p) + expect(subject.build).to eq '' + end + end + + context 'when property does not have surface' do + it "returns appropriate description" do + p = Roomorama::Property.load( + { + identifier: '15', + disabled: true, # to make property valid + type: 'apartment', + max_guests: 4, + city: 'Omsk', + number_of_bedrooms: 3, + number_of_bathrooms: 2 + } + ).value + + subject = described_class.new(p) + expect(subject.build).to eq 'This is a charming apartment in Omsk, which can accommodate 4 guests.It has 3 bedrooms and 2 bathrooms.' + end + end + + context 'when property has all required fields' do + it "returns appropriate description" do + p = Roomorama::Property.load( + { + identifier: '15', + disabled: true, # to make property valid + type: 'apartment', + max_guests: 4, + city: 'Omsk', + number_of_bedrooms: 3, + number_of_bathrooms: 2, + surface: 56 + } + ).value + + subject = described_class.new(p) + expect(subject.build).to eq 'This is a charming apartment in Omsk, which can accommodate 4 guests.The 56 square metres apartment has 3 bedrooms and 2 bathrooms.' + end + end + end +end From 05ded5dd7b557f288a928f0e81459fb1de1aaaff Mon Sep 17 00:00:00 2001 From: "K.Kolotyuk" Date: Fri, 2 Dec 2016 17:28:39 +0600 Subject: [PATCH 2/2] Add space --- lib/concierge/property_description.rb | 2 +- spec/lib/concierge/property_description_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/concierge/property_description.rb b/lib/concierge/property_description.rb index afbcfe2ef..2806c6295 100644 --- a/lib/concierge/property_description.rb +++ b/lib/concierge/property_description.rb @@ -19,7 +19,7 @@ def build result = '' if valid? result = "This is a charming #{property.type} in #{property.city}, which can accommodate "\ - "#{pluralize(property.max_guests, 'guest', 'guests')}." + "#{pluralize(property.max_guests, 'guest', 'guests')}. " rooms_part_start = if property.surface "The #{property.surface} square metres apartment" diff --git a/spec/lib/concierge/property_description_spec.rb b/spec/lib/concierge/property_description_spec.rb index 4020b9109..24ff6dbef 100644 --- a/spec/lib/concierge/property_description_spec.rb +++ b/spec/lib/concierge/property_description_spec.rb @@ -43,7 +43,7 @@ ).value subject = described_class.new(p) - expect(subject.build).to eq 'This is a charming apartment in Omsk, which can accommodate 4 guests.It has 3 bedrooms and 2 bathrooms.' + expect(subject.build).to eq 'This is a charming apartment in Omsk, which can accommodate 4 guests. It has 3 bedrooms and 2 bathrooms.' end end @@ -63,7 +63,7 @@ ).value subject = described_class.new(p) - expect(subject.build).to eq 'This is a charming apartment in Omsk, which can accommodate 4 guests.The 56 square metres apartment has 3 bedrooms and 2 bathrooms.' + expect(subject.build).to eq 'This is a charming apartment in Omsk, which can accommodate 4 guests. The 56 square metres apartment has 3 bedrooms and 2 bathrooms.' end end end