class Corraios::Delivery

Attributes

amount_of_posts[R]
code[R]
deadline[R]
error_message[R]
name[R]
value[R]

Public Class Methods

new(correios_response, service_name, service_code) click to toggle source
# File lib/corraios/delivery.rb, line 9
def initialize(correios_response, service_name, service_code)
  @name = service_name
  @code = service_code
  @amount_of_posts = 1
  parse_response(correios_response)
end

Public Instance Methods

==(other) click to toggle source
# File lib/corraios/delivery.rb, line 33
def ==(other)
  @code == other.code
end
error?() click to toggle source
# File lib/corraios/delivery.rb, line 16
def error?
  !!@error_message
end
merge!(other) click to toggle source
# File lib/corraios/delivery.rb, line 24
def merge!(other)
  raise CorraiosError unless self == other
  if success? && other.success?
    merge_when_success!(other)
  else
    merge_when_error!(other)
  end
end
success?() click to toggle source
# File lib/corraios/delivery.rb, line 20
def success?
  !error?
end

Private Instance Methods

merge_when_error!(other) click to toggle source
# File lib/corraios/delivery.rb, line 44
def merge_when_error!(other)
  @value = 0.0
  @deadline = 0
  @error_message ||= other.error_message
end
merge_when_success!(other) click to toggle source
# File lib/corraios/delivery.rb, line 39
def merge_when_success!(other)
  @value += other.value
  @deadline = [@deadline, other.deadline].max
end
parse_response(response) click to toggle source
# File lib/corraios/delivery.rb, line 50
def parse_response(response)
  service = validate_response(response)
  value = to_float(service['Valor'])
  if value > 0.0
    @value = value
    @deadline = service['PrazoEntrega'].to_i
  else
    @error_message = service['MsgErro'].strip
  end
end
to_float(string) click to toggle source
# File lib/corraios/delivery.rb, line 61
def to_float(string)
  string.to_s.gsub(',', '.').to_f
end
validate_response(response) click to toggle source
# File lib/corraios/delivery.rb, line 65
def validate_response(response)
  if response['Servicos'] && response['Servicos']['cServico']
    response['Servicos']['cServico']
  else
    raise InvalidResponse
  end
end