module RussianPost::OperationsFactory

Public Class Methods

build(operations_hash) click to toggle source
# File lib/russianpost/operations_factory.rb, line 8
def build(operations_hash)
  @country_list = Iso3166Ru
  operations_hash.map { |o| build_operation(o) }
end
build_operation(operation_hash) click to toggle source
# File lib/russianpost/operations_factory.rb, line 13
def build_operation(operation_hash)
  operation = RussianPost::Operation.new
  ungroup_parameters(operation_hash).each do |key, value|
    operation[key] = process_param(key, value)
  end

  operation
end

Private Class Methods

process_address(key, value) click to toggle source
# File lib/russianpost/operations_factory.rb, line 46
def process_address(key, value)
  if [:destination_address, :operation_address].include? key
    RussianPost::Address.new(
      value[:index],
      value[:description])
  end
end
process_country(key, value) click to toggle source
# File lib/russianpost/operations_factory.rb, line 54
def process_country(key, value)
  if [:mail_direct, :country_from, :country_oper].include? key
    RussianPost::CountryFactory.build(value, country_list: @country_list)
  end
end
process_fixnum(key, value) click to toggle source
# File lib/russianpost/operations_factory.rb, line 40
def process_fixnum(key, value)
  if [:payment, :value, :mass_rate, :insr_rate, :air_rate, :rate, :mass, :max_mass_ru, :max_mass_en].include? key
    value.to_i
  end
end
process_generic_param(key, value) click to toggle source
# File lib/russianpost/operations_factory.rb, line 60
def process_generic_param(key, value)
  if value.kind_of? Hash
    RussianPost::GenericOperationParameter.new(
      value[:id] ? value[:id].to_i : nil,
      value[:name])
  end
end
process_param(key, value) click to toggle source

Methods below convert certain parameters into proper data structures

# File lib/russianpost/operations_factory.rb, line 32
def process_param(key, value)
  process_fixnum(key, value) ||
  process_address(key, value) ||
  process_country(key, value) ||
  process_generic_param(key, value) ||
  value
end
ungroup_parameters(operation_hash) click to toggle source

Initially all parameters are grouped (address parameters, finance parameters, etc.). This method flattens the structure a bit

# File lib/russianpost/operations_factory.rb, line 27
def ungroup_parameters(operation_hash)
  operation_hash.values.compact.reduce(Hash.new){ |acc, el| acc.merge(el) }
end