Skip to content

Commit

Permalink
Merge pull request #333 from roomorama/saw_updates
Browse files Browse the repository at this point in the history
Latest changes for SAW integration
  • Loading branch information
Sharipov Ruslan authored Sep 21, 2016
2 parents 0a944f0 + 563c908 commit 5612d2e
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 273 deletions.
20 changes: 11 additions & 9 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_available_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,20 +33,20 @@ 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
else
message = "Failed to perform the `#fetch_rates_for_properties` operation"
message = "Failed to perform the `#fetch_all_unit_rates_for_properties` operation"
announce_error(message, result)
return
end

properties.each do |property|
synchronisation.start(property.internal_id) do
Concierge.context.disable!

unit_rates = find_rates(property.internal_id, all_unit_rates)
fetch_details_and_build_property(property, unit_rates)
end
Expand All @@ -53,7 +55,7 @@ def perform
synchronisation.finish!
end

def fetch_details_and_build_property(property, rates)
def fetch_details_and_build_property(property, unit_rates)
result = importer.fetch_detailed_property(property.internal_id)

if result.success?
Expand All @@ -62,7 +64,7 @@ def fetch_details_and_build_property(property, rates)
roomorama_property = ::SAW::Mappers::RoomoramaProperty.build(
property,
detailed_property,
rates
unit_rates
)

Result.new(roomorama_property)
Expand Down Expand Up @@ -101,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
3 changes: 2 additions & 1 deletion lib/concierge/suppliers/saw/commands/base_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class BaseFetcher
#
# Whitelisted SAW API errors:
# 1007 - No properties are available for the given search parameters
VALID_RESULT_ERROR_CODES = ['1007']
# 3031 - Rates are not available for this property
VALID_RESULT_ERROR_CODES = ['1007', '3031']

attr_reader :credentials

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
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 + 30 * one_day).strftime("%d/%m/%Y")
(today + STAY_OFFSET * one_day).strftime("%d/%m/%Y")
end

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

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

if valid_result?(result_hash)
property_rate = build_property_rate(result_hash)

quotation = if property_rate.has_unit?(params[:unit_id])
SAW::Mappers::Quotation.build(params, property_rate)
else
SAW::Mappers::Quotation.build_unavailable(params)
end
quotation = build_quotation(params, result_hash)

Result.new(quotation)
else
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
43 changes: 3 additions & 40 deletions lib/concierge/suppliers/saw/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,6 @@ def fetch_properties_by_country(country)
properties_fetcher.call(country)
end

# Retrieves the list of available properties in a given country
# Available means those which doesn't have `on_request` attribute
#
# Returns a +Result+ wrapping +Array+ of +SAW::Entities::BasicProperty+
# when operation succeeds
# Returns a +Result+ with +Result::Error+ when operation fails
def fetch_available_properties_by_country(country)
result = fetch_properties_by_country(country)

if result.success?
all_properties = result.value
available_properties = all_properties.reject { |p| p.on_request? }
Result.new(available_properties)
else
result
end
end

# Retrieves the list of properties in given countries
# In case if request to one of the countries fails, method stops its
# execution and returns a result with a failure.
Expand All @@ -93,25 +75,6 @@ def fetch_properties_by_countries(countries)
Result.new(properties)
end

# Retrieves the list of available properties in given countries
# In case if request to one of the countries fails, method stops its
# execution and returns a result with a failure.
#
# Returns a +Result+ wrapping +Array+ of +SAW::Entities::BasicProperty+
# when operation succeeds
# Returns a +Result+ with +Result::Error+ when operation fails
def fetch_available_properties_by_countries(countries)
properties = countries.map do |country|
result = fetch_available_properties_by_country(country)

return result unless result.success?

result.value
end.flatten

Result.new(properties)
end

# Retrieves property with extended information.
#
# Returns a +Result+ wrapping +SAW::Entities::DetailedProperty+ object
Expand All @@ -124,22 +87,22 @@ 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)
bulk_rates_fetcher = Commands::BulkRatesFetcher.new(credentials)
bulk_rates_fetcher.call(ids)
end

# Retrieves rates for properties
# Retrieves rates for units of all given properties.
#
# It groups properties by currency because SAW API doesn't support
# fetching property rates when multiple currencies are given.
#
# 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
65 changes: 0 additions & 65 deletions lib/concierge/suppliers/saw/mappers/property_rate.rb

This file was deleted.

Loading

0 comments on commit 5612d2e

Please sign in to comment.