Skip to content

Commit

Permalink
Merge pull request #365 from roomorama/bugfix/saw_price_fetching
Browse files Browse the repository at this point in the history
Bugfix/saw price fetching
  • Loading branch information
Sharipov Ruslan authored Sep 20, 2016
2 parents 37e3376 + 78336bb commit 563c908
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 147 deletions.
12 changes: 8 additions & 4 deletions apps/workers/suppliers/saw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(host)
end

def perform
result = importer.fetch_countries
result = synchronisation.new_context { importer.fetch_countries }

if result.success?
countries = result.value
Expand All @@ -21,7 +21,9 @@ def perform
return
end

result = importer.fetch_properties_by_countries(countries)
result = synchronisation.new_context do
importer.fetch_properties_by_countries(countries)
end

if result.success?
properties = result.value
Expand All @@ -31,7 +33,9 @@ def perform
return
end

result = importer.fetch_all_unit_rates_for_properties(properties)
result = synchronisation.new_context do
importer.fetch_all_unit_rates_for_properties(properties)
end

if result.success?
all_unit_rates = result.value
Expand Down Expand Up @@ -99,7 +103,7 @@ def announce_error(message, result)
end

def find_rates(property_id, all_unit_rates)
all_unit_rates.find { |rate| rate.id == property_id.to_s }
all_unit_rates.find { |rate| rate.property_id == property_id.to_s }
end
end
end
Expand Down
19 changes: 11 additions & 8 deletions lib/concierge/suppliers/saw/commands/bulk_rates_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ module Commands
# command = SAW::Commands::BulkRatesFetcher.new(credentials)
# result = command.call(property_id)
class BulkRatesFetcher < BaseFetcher
# How many days in future skip to fetch rates.
STAY_OFFSET = 90

# SAW returns rates for more properties if chosen STAY_LENGTH is 2 days
STAY_LENGTH = 2

# Calls the SAW API method using the HTTP client.
#
# Arguments
Expand Down Expand Up @@ -39,14 +45,11 @@ def call(ids)

private
def build_rates(rates_hash)
rates = rates_hash.get("response.property")
rates_array = Array(rates_hash.get("response.property"))

return [] unless rates
return rates_array unless rates_array.any?

Array(rates).map do |rate|
safe_hash = Concierge::SafeAccessHash.new(rate)
SAW::Mappers::PropertyRate.build(safe_hash)
end.compact
SAW::Mappers::UnitsPricing.build(rates_array, STAY_LENGTH)
end

def build_payload(ids)
Expand All @@ -59,11 +62,11 @@ def build_payload(ids)
end

def check_in
(today + 90 * one_day).strftime("%d/%m/%Y")
(today + STAY_OFFSET * one_day).strftime("%d/%m/%Y")
end

def check_out
(today + 92 * one_day).strftime("%d/%m/%Y")
(today + (STAY_OFFSET+STAY_LENGTH) * one_day).strftime("%d/%m/%Y")
end

def one_day
Expand Down
24 changes: 9 additions & 15 deletions lib/concierge/suppliers/saw/commands/price_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,9 @@ def call(params)
result_hash = response_parser.to_hash(result.value.body)

if valid_result?(result_hash)
property_rate_res = build_property_rate(result_hash)
quotation = build_quotation(params, result_hash)

if property_rate_res.success?
Result.new SAW::Mappers::Quotation.build(params, property_rate_res.value)
elsif property_rate_res.error.code == :empty_unit_rates
Result.new SAW::Mappers::Quotation.build_unavailable(params)
else
property_rate_res
end
Result.new(quotation)
else
error_result(result_hash)
end
Expand All @@ -62,13 +56,6 @@ def call(params)
end

private
def build_property_rate(hash)
rates_hash = hash.get("response.property")
safe_hash = Concierge::SafeAccessHash.new(rates_hash)

SAW::Mappers::PropertyRate.build(safe_hash)
end

def build_payload(params)
payload_builder.build_compute_pricing(
property_id: params[:property_id],
Expand All @@ -78,6 +65,13 @@ def build_payload(params)
num_guests: params[:guests]
)
end

def build_quotation(params, result_hash)
rates_hash = result_hash.get("response.property")
safe_hash = Concierge::SafeAccessHash.new(rates_hash)

SAW::Mappers::Quotation.build(params, safe_hash)
end
end
end
end
45 changes: 0 additions & 45 deletions lib/concierge/suppliers/saw/entities/property_rate.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/concierge/suppliers/saw/entities/unit_rate.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SAW
module Entities
# +SAW::Entities::PropertyRate+
# +SAW::Entities::UnitRate+
#
# This entity represents an object with available rates for units in
# property.
Expand Down
24 changes: 24 additions & 0 deletions lib/concierge/suppliers/saw/entities/units_pricing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module SAW
module Entities
# +SAW::Entities::UnitsPricing+
#
# This entity represents an object with available rates for property
# units: entity includes +units+ array which has unit rates and the
# currency used for the current property
#
# Attributes
#
# +property_id+ - property id
# +currency+ - currency code
# +units+ - array of +SAW::Entities::UnitRate+ objects
class UnitsPricing
attr_reader :property_id, :units, :currency

def initialize(property_id:, units:, currency:)
@property_id = property_id
@units = units
@currency = currency
end
end
end
end
4 changes: 2 additions & 2 deletions lib/concierge/suppliers/saw/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def fetch_detailed_property(property_id)

# Retrieves rates for units by given property ids
#
# Returns a +Result+ wrapping an array of +SAW::Entities::PropertyRate+
# Returns a +Result+ wrapping an array of +SAW::Entities::UnitsPricing+
# objects when operation succeeds
# Returns a +Result+ with +Result::Error+ when operation fails
def fetch_unit_rates_by_property_ids(ids)
Expand All @@ -102,7 +102,7 @@ def fetch_unit_rates_by_property_ids(ids)
#
# Limits to send maximum 20 property ids (API limitation)
#
# Returns a +Result+ wrapping an array of +SAW::Entities::PropertyRate+
# Returns a +Result+ wrapping an array of +SAW::Entities::UnitsPricing+
# objects when operation succeeds
# Returns a +Result+ with +Result::Error+ when operation fails
def fetch_all_unit_rates_for_properties(properties)
Expand Down
100 changes: 65 additions & 35 deletions lib/concierge/suppliers/saw/mappers/quotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,80 @@ module Mappers
#
# This class is responsible for building a +Quotation+ object
class Quotation
# Builds a quotation
#
# Arguments:
#
# * +params+ [Hash] property parameters
# * +property_rate+ [SAW::Entities::PropertyRate] property rate
#
# Returns [Quotation]
def self.build(params, property_rate)
requested_unit = property_rate.find_unit(params[:unit_id])

if requested_unit
class << self
# Builds a quotation
#
# Arguments:
#
# * +params+ [Hash] quotation request parameters
# * +safe_hash+ [Concierge::SafeAccessHash] result hash with price
#
# Returns [Quotation]
def build(params, safe_hash)
requested_unit = find_requested_unit(safe_hash, params[:unit_id])

if requested_unit
::Quotation.new(
property_id: params[:property_id],
unit_id: params[:unit_id],
check_in: params[:check_in].to_s,
check_out: params[:check_out].to_s,
guests: params[:guests],
currency: parse_currency(safe_hash),
total: parse_price(requested_unit),
available: parse_availability(requested_unit)
)
else
build_unavailable(params)
end
end

private
# Builds unavailable quotation.
# Used in cases when rates information for unit is not available.
#
# Arguments:
#
# * +params+ [Hash] parameters
#
# Returns [Quotation]
def build_unavailable(params)
::Quotation.new(
property_id: params[:property_id],
unit_id: params[:unit_id],
check_in: params[:check_in].to_s,
check_out: params[:check_out].to_s,
guests: params[:guests],
currency: property_rate.currency,
total: requested_unit.price,
available: requested_unit.available
available: false
)
else
self.build_unavailable(params)
end
end

# Builds unavailable quotation.
# Used in cases when rates information for unit is not available.
#
# Arguments:
#
# * +params+ [Hash] parameters
#
# Returns [Quotation]
def self.build_unavailable(params)
::Quotation.new(
property_id: params[:property_id],
unit_id: params[:unit_id],
check_in: params[:check_in].to_s,
check_out: params[:check_out].to_s,
guests: params[:guests],
available: false
)
def units_hash(hash)
hash.get("apartments.accommodation_type.property_accommodation")
end

def find_requested_unit(safe_hash, id)
units = units_hash(safe_hash)
requested_unit_hash = Array(units).find { |u| u["@id"] == id }

return nil unless requested_unit_hash
Concierge::SafeAccessHash.new(requested_unit_hash)
end

def parse_currency(hash)
hash.get("currency_code")
end

def parse_price(hash)
price = hash.get("price_detail.net.total_price.price")
BigDecimal.new(price)
end

def parse_availability(hash)
flag = hash.get("flag_bookable_property_accommodation")

flag == "Y"
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/concierge/suppliers/saw/mappers/roomorama_unit_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RoomoramaUnitSet
#
# * +basic_property+ [SAW::Entities::BasicProperty]
# * +detailed_property+ [SAW::Entities::DetailedProperty]
# * +rates+ [SAW::Entities::UnitsPricing]
#
# +detailed_property+ includes two attributes-hashes:
#
Expand Down
Loading

0 comments on commit 563c908

Please sign in to comment.