Class: AMarmita::Cart::Base

Inherits:
AMarmita::ClientModule show all
Defined in:
lib/a_marmita/cart/base.rb

Instance Attribute Summary

Attributes inherited from AMarmita::ClientModule

#client

Instance Method Summary collapse

Methods inherited from AMarmita::ClientModule

#initialize

Constructor Details

This class inherits a constructor from AMarmita::ClientModule

Instance Method Details

#add_item(id, date = nil, quantity = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/a_marmita/cart/base.rb', line 24

def add_item(id, date = nil, quantity = nil)
  return :bad_login if cant_log_in?
  
  quantity = 1 if Helpers.blank?(quantity)
  date = Helpers.format_date(date)

  server_response = get_page_body("http://www.amarmita.com/enc.php?id=#{id}&qtd=#{quantity}&data=#{date}&op=1")

  { "1" => :ok, "-2" => :bad_date }[server_response] || :error
end

#checkout(payment_method) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/a_marmita/cart/base.rb', line 51

def checkout(payment_method)
  return :bad_login if cant_log_in?

  case payment_method.to_s
  when "through_mb"
    client.payment.through_mb
  when "end_of_month"
    client.payment.end_of_month
  else
    :unknown_payment_method
  end
end

#itemsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/a_marmita/cart/base.rb', line 8

def items
  return :bad_login if cant_log_in?

  page, last_date = get_page("http://www.amarmita.com/enc.php?op=4"), ''

  content_rows = page.parser.css('table > tr')
  
  content_rows.map do |row|
    new_date = row.css('.em_marmitalist_data')

    last_date = Helpers.cart_date_parser(new_date.first.text) unless new_date.empty?

    Cart::Scrapper.new(row).scrap(date: last_date)
  end.compact
end

#remove_item(id) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/a_marmita/cart/base.rb', line 35

def remove_item(id)
  return :bad_login if cant_log_in?
  
  server_response = get_page_body("http://www.amarmita.com/enc.php?id=#{id}&op=2")

  server_response == "1" ? :ok : :error
end

#totalObject



43
44
45
46
47
48
49
# File 'lib/a_marmita/cart/base.rb', line 43

def total
  return :bad_login if cant_log_in?
  
  total = get_page_body("http://www.amarmita.com/enc.php?op=5")

  Helpers.to_float(total).first
end