Class: SolidusMe::MelhorEnvio

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_me/melhor_envio.rb

Constant Summary collapse

SHIPPING_METHODS =
[
  { name: "Correios PAC", carrier: "Correios", service_level: "PAC", external_id: 1 },
  { name: "Correios SEDEX", carrier: "Correios", service_level: "SEDEX", external_id: 2 },
  { name: "Correios Mini Envios", carrier: "Correios", service_level: "Mini Envios", external_id: 17 },
  { name: "Jadlog .Package", carrier: "Jadlog", service_level: ".Package", external_id: 3 },
  { name: "Jadlog .Com", carrier: "Jadlog", service_level: ".Com", external_id: 4 }
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package, shipping_methods) ⇒ MelhorEnvio

Returns a new instance of MelhorEnvio.



13
14
15
16
# File 'app/models/solidus_me/melhor_envio.rb', line 13

def initialize(package, shipping_methods)
  @package = package
  @shipping_methods = shipping_methods
end

Instance Attribute Details

#packageObject

Returns the value of attribute package.



12
13
14
# File 'app/models/solidus_me/melhor_envio.rb', line 12

def package
  @package
end

#shipping_methodsObject

Returns the value of attribute shipping_methods.



12
13
14
# File 'app/models/solidus_me/melhor_envio.rb', line 12

def shipping_methods
  @shipping_methods
end

Class Method Details

.generate_code(external_id) ⇒ Object



18
19
20
# File 'app/models/solidus_me/melhor_envio.rb', line 18

def self.generate_code external_id
  (self.name.underscore + "_#{external_id}").downcase
end

.installObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/solidus_me/melhor_envio.rb', line 22

def self.install
  SHIPPING_METHODS.each do |provider_method|
    Spree::ShippingMethod.find_or_create_by(code: generate_code(provider_method[:external_id])) do |shipping_method|
      shipping_method.name = provider_method[:name]
      shipping_method.carrier = provider_method[:carrier]
      shipping_method.service_level = provider_method[:service_level]
      shipping_method.code = generate_code(provider_method[:external_id])
      shipping_method.calculator = Spree::Calculator::Shipping::FlatRate.create
      shipping_method.shipping_categories = [Spree::ShippingCategory.first]
      shipping_method.available_to_users = false
      shipping_method.provider_id = SolidusTecEstimator::Provider.find_by(class_name: self.to_s).id
    end
  end
end

Instance Method Details

#shipping_ratesObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/solidus_me/melhor_envio.rb', line 37

def shipping_rates
  @me_account = Account.first
  return [] if @me_account.blank?

  @me_account.check_token
  @me_client = MeApi::Client.new(@me_account.access_token)

  melhor_envio_rates = get_rates_from_melhor_envio(package)
  shipping_rates = melhor_envio_rates.map do |melhor_envio_rate|
    build_shipping_rate(melhor_envio_rate, package)
  end.compact

  shipping_rates
end