Class: Report::ShippingRates::ShippingRatesCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
app/services/report/shipping_rates/shipping_rates_command.rb

Overview

Service object: shipping rates command.

Instance Method Summary collapse

Instance Method Details

#executeObject

Run the report with the given filters and store the result.

Returns:

  • (Object)


10
11
12
13
14
# File 'app/services/report/shipping_rates/shipping_rates_command.rb', line 10

def execute
  return unless valid?

  @result = Report::ShippingRates::ShippingRates.result(attributes)
end

#success?Boolean

Whether the command validated and ran successfully.

Returns:

  • (Boolean)


35
36
37
# File 'app/services/report/shipping_rates/shipping_rates_command.rb', line 35

def success?
  valid? && @result.success?
end

#to_csvString

Render the report rows as CSV.

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/report/shipping_rates/shipping_rates_command.rb', line 18

def to_csv
  return unless valid?

  @result = Report::ShippingRates::ShippingRates.result(attributes)
  return if @result.shipping_rates.blank?

  attributes = @result.shipping_rates.first.keys
  CSV.generate(headers: true) do |csv|
    csv << attributes
    @result.shipping_rates.each do |r|
      csv << r
    end
  end
end