a_marmita 0.0.1 → 0.5.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +2 -0
- data/.rspec +4 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +9 -1
- data/lib/a_marmita.rb +40 -21
- data/lib/a_marmita/authentication.rb +29 -0
- data/lib/a_marmita/cart/base.rb +66 -0
- data/lib/a_marmita/cart/scrapper.rb +50 -0
- data/lib/a_marmita/client_module.rb +24 -0
- data/lib/a_marmita/helpers.rb +62 -0
- data/lib/a_marmita/meals/base.rb +64 -0
- data/lib/a_marmita/meals/scrapper.rb +54 -0
- data/lib/a_marmita/orders/base.rb +39 -0
- data/lib/a_marmita/orders/scrapper.rb +76 -0
- data/lib/a_marmita/payment.rb +21 -0
- data/lib/a_marmita/scrapper.rb +46 -0
- data/lib/a_marmita/version.rb +1 -1
- data/lib/a_marmita/web_agent.rb +24 -0
- data/run.rb +23 -0
- data/spec/a_marmita/client_spec.rb +68 -0
- data/spec/a_marmita/meals_spec.rb +18 -0
- data/spec/spec_helper.rb +47 -0
- data/spec/support/environment.rb +9 -0
- data/spec/support/meals_parsed_response.rb +5 -0
- data/spec/support/meals_server_response.rb +3498 -0
- data/spec/support/web_mock.rb +9 -0
- metadata +31 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YjhiMzBiYzFmNTAxZmRkOWUzOWRiMmE3MGUxOTY2NDM3NTE5NGIxNw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ODc0ZjQ3MjgzNDVlODlhOTkzNTVjYTdlMDY3YzVjMzUzMDBiMDZmNw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YzEwYzFmOGM2MjRkNzVmNTUwNmZjNDNiY2FkZDEzZDFiY2RjNjBjMzU4ODg5
|
10
|
+
ZmM0ZGZhODU5NmZhZmFkNmQxMzMwMTE0ZTlmZTk3NGUyZmEwNmY4ZWE4MGYx
|
11
|
+
Mzc3ODEzMjcwNDk5ZGRkZTQwNjdmMGEwNTFlOWU0NTg1MDk0YzQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Y2NjOWM1ZGEyZmM4ZTFjMmJmOTNiNzYwNDNhYzBiYzJlYjUzMWI2NmQxYWZi
|
14
|
+
NjA5ZDhmYjA1YTcxYTFhZmU5YjJjMDBkNDFjY2Q4ZWY1MGY4YzFiNTUyYjlm
|
15
|
+
ODg4YjdhNDNhOWU2Y2YxNWIxMGU2ZTg4NDJjNDQwZmE3ZWM5OWI=
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
a_marmita (0.
|
4
|
+
a_marmita (0.3.5)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
addressable (2.3.6)
|
9
10
|
coderay (1.1.0)
|
11
|
+
crack (0.4.2)
|
12
|
+
safe_yaml (~> 1.0.0)
|
10
13
|
diff-lcs (1.2.5)
|
11
14
|
domain_name (0.5.18)
|
12
15
|
unf (>= 0.0.5, < 1.0.0)
|
@@ -41,10 +44,14 @@ GEM
|
|
41
44
|
rspec-expectations (2.14.5)
|
42
45
|
diff-lcs (>= 1.1.3, < 2.0)
|
43
46
|
rspec-mocks (2.14.6)
|
47
|
+
safe_yaml (1.0.1)
|
44
48
|
slop (3.5.0)
|
45
49
|
unf (0.1.3)
|
46
50
|
unf_ext
|
47
51
|
unf_ext (0.0.6)
|
52
|
+
webmock (1.17.4)
|
53
|
+
addressable (>= 2.2.7)
|
54
|
+
crack (>= 0.3.2)
|
48
55
|
webrobots (0.1.1)
|
49
56
|
|
50
57
|
PLATFORMS
|
@@ -56,3 +63,4 @@ DEPENDENCIES
|
|
56
63
|
mechanize
|
57
64
|
pry
|
58
65
|
rspec (~> 2.11)
|
66
|
+
webmock
|
data/lib/a_marmita.rb
CHANGED
@@ -1,40 +1,59 @@
|
|
1
|
+
require 'mechanize'
|
2
|
+
|
3
|
+
require "a_marmita/client_module"
|
4
|
+
require "a_marmita/payment"
|
1
5
|
require "a_marmita/version"
|
6
|
+
require "a_marmita/helpers"
|
7
|
+
require "a_marmita/scrapper"
|
8
|
+
require "a_marmita/web_agent"
|
9
|
+
require "a_marmita/cart/base"
|
10
|
+
require "a_marmita/meals/base"
|
11
|
+
require "a_marmita/orders/base"
|
12
|
+
require "a_marmita/authentication"
|
2
13
|
|
3
14
|
module AMarmita
|
4
15
|
|
5
|
-
|
6
|
-
|
7
|
-
def get_meals
|
8
|
-
|
9
|
-
login('joao.goncalves@linkedcare.com', 'yEJHVd')
|
16
|
+
class Client
|
10
17
|
|
11
|
-
|
18
|
+
include WebAgent
|
19
|
+
include Authentication
|
12
20
|
|
13
|
-
|
21
|
+
def cart
|
22
|
+
@cart ||= Cart::Base.new(self)
|
23
|
+
end
|
14
24
|
|
25
|
+
def meals
|
26
|
+
@meals ||= Meals::Base.new(self)
|
27
|
+
end
|
15
28
|
|
16
|
-
|
29
|
+
def orders
|
30
|
+
@orders ||= Orders::Base.new(self)
|
31
|
+
end
|
17
32
|
|
18
|
-
|
19
|
-
|
33
|
+
def payment
|
34
|
+
@payment ||= Payment.new(self)
|
35
|
+
end
|
20
36
|
|
21
|
-
page.parser.css('#conteudo table tr')
|
22
|
-
binding.pry
|
23
37
|
end
|
24
38
|
|
25
|
-
|
26
|
-
page = agent.get "http://www.amarmita.com/login.php?user=#{email}&psw=#{pass}"
|
39
|
+
module ClassMethods
|
27
40
|
|
28
|
-
|
29
|
-
|
41
|
+
def new_client
|
42
|
+
Client.new
|
43
|
+
end
|
30
44
|
|
31
|
-
|
32
|
-
|
45
|
+
def singleton
|
46
|
+
@singleton ||= Client.new
|
47
|
+
end
|
33
48
|
|
34
|
-
|
35
|
-
|
36
|
-
|
49
|
+
def method_missing(method, *args, &block)
|
50
|
+
return super unless singleton.respond_to?(method)
|
51
|
+
|
52
|
+
singleton.send(method, *args)
|
37
53
|
end
|
54
|
+
|
38
55
|
end
|
39
56
|
|
57
|
+
extend ClassMethods
|
58
|
+
|
40
59
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module AMarmita
|
2
|
+
|
3
|
+
module Authentication
|
4
|
+
|
5
|
+
def self.include(base)
|
6
|
+
base.class_eval { attr_reader :email, :pass }
|
7
|
+
end
|
8
|
+
|
9
|
+
def set_credentials(email, pass)
|
10
|
+
@email, @pass = email, pass
|
11
|
+
end
|
12
|
+
|
13
|
+
def login(email = nil, pass = nil)
|
14
|
+
set_credentials(email, pass) unless email.nil?
|
15
|
+
|
16
|
+
@logged = (get_page_body("http://www.amarmita.com/login.php?user=#{@email}&psw=#{@pass}") == "1")
|
17
|
+
end
|
18
|
+
|
19
|
+
def logged?(force = false)
|
20
|
+
@logged = nil if force
|
21
|
+
|
22
|
+
return @logged unless @logged.nil?
|
23
|
+
|
24
|
+
@logged = (get_page_body("http://www.amarmita.com/logged.php") == "1")
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require "a_marmita/cart/scrapper"
|
2
|
+
|
3
|
+
module AMarmita
|
4
|
+
module Cart
|
5
|
+
|
6
|
+
class Base < ClientModule
|
7
|
+
|
8
|
+
def items
|
9
|
+
return :bad_login if cant_log_in?
|
10
|
+
|
11
|
+
page, last_date = get_page("http://www.amarmita.com/enc.php?op=4"), ''
|
12
|
+
|
13
|
+
content_rows = page.parser.css('table > tr')
|
14
|
+
|
15
|
+
content_rows.map do |row|
|
16
|
+
new_date = row.css('.em_marmitalist_data')
|
17
|
+
|
18
|
+
last_date = Helpers.cart_date_parser(new_date.first.text) unless new_date.empty?
|
19
|
+
|
20
|
+
Cart::Scrapper.new(row).scrap(date: last_date)
|
21
|
+
end.compact
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_item(id, date = nil, quantity = 1)
|
25
|
+
return :bad_login if cant_log_in?
|
26
|
+
|
27
|
+
date = Helpers.format_date(date)
|
28
|
+
|
29
|
+
server_response = get_page_body("http://www.amarmita.com/enc.php?id=#{id}&qtd=#{quantity}&data=#{date}&op=1")
|
30
|
+
|
31
|
+
{ "1" => :ok, "-2" => :bad_date }[server_response] || :error
|
32
|
+
end
|
33
|
+
|
34
|
+
def remove_item(id)
|
35
|
+
return :bad_login if cant_log_in?
|
36
|
+
|
37
|
+
server_response = get_page_body("http://www.amarmita.com/enc.php?id=#{id}&op=2")
|
38
|
+
|
39
|
+
server_response == "1" ? :ok : :error
|
40
|
+
end
|
41
|
+
|
42
|
+
def total
|
43
|
+
return :bad_login if cant_log_in?
|
44
|
+
|
45
|
+
total = get_page_body("http://www.amarmita.com/enc.php?op=5")
|
46
|
+
|
47
|
+
Helpers.to_float(total).first
|
48
|
+
end
|
49
|
+
|
50
|
+
def checkout(payment_method)
|
51
|
+
return :bad_login if cant_log_in?
|
52
|
+
|
53
|
+
case payment_method
|
54
|
+
when :through_mb
|
55
|
+
client.payment.through_mb
|
56
|
+
when :end_of_month
|
57
|
+
client.payment.end_of_month
|
58
|
+
else
|
59
|
+
raise 'Unknown payment method!'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module AMarmita
|
2
|
+
module Cart
|
3
|
+
|
4
|
+
class Scrapper < AMarmita::Scrapper
|
5
|
+
|
6
|
+
DESCRIPTION, QUANTITY = 0, 1
|
7
|
+
|
8
|
+
|
9
|
+
def run(options = {})
|
10
|
+
@attributes = {
|
11
|
+
date: options[:date],
|
12
|
+
id: Helpers.to_int(get_id),
|
13
|
+
description: get_description,
|
14
|
+
quantity: Helpers.to_int(get_quantity)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
protected ############## PROTECTED ##################
|
20
|
+
|
21
|
+
def get_id
|
22
|
+
get_xml_value(css_parser.css('a').first) do |xml_object|
|
23
|
+
get_id_from_javascript_function xml_object.attr('href')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_description
|
28
|
+
get_xml_value similar_entries[DESCRIPTION]
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_quantity
|
32
|
+
get_xml_value similar_entries[QUANTITY]
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
private ################# PRIVATE ##################
|
37
|
+
|
38
|
+
def similar_entries
|
39
|
+
@similar_entries ||= css_parser.css('.em_marmitalist_desc')
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_id_from_javascript_function(javascript_function)
|
43
|
+
id = javascript_function[(javascript_function.index('(') + 1)..-1]
|
44
|
+
id[0..(id.index(')') - 1)]
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module AMarmita
|
2
|
+
|
3
|
+
class ClientModule
|
4
|
+
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
attr_reader :client
|
8
|
+
|
9
|
+
def initialize(client)
|
10
|
+
@client = client
|
11
|
+
end
|
12
|
+
|
13
|
+
def_delegators :client, :get_page_body, :get_page, :logged?, :login
|
14
|
+
|
15
|
+
|
16
|
+
protected ################ PROTECTED #################
|
17
|
+
|
18
|
+
def cant_log_in?
|
19
|
+
!logged? && !login
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module AMarmita
|
4
|
+
|
5
|
+
module Helpers
|
6
|
+
|
7
|
+
extend self
|
8
|
+
|
9
|
+
MONTH_NAMES_PT = ["", "janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"]
|
10
|
+
|
11
|
+
|
12
|
+
def try(object, *a, &b)
|
13
|
+
if a.empty? && block_given?
|
14
|
+
yield object
|
15
|
+
else
|
16
|
+
object.respond_to?(a.first) ? object.public_send(*a, &b) : nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_int(string)
|
21
|
+
string.nil? ? 0 : string.gsub(/[^0-9]/, '').to_i
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_float(string)
|
25
|
+
return 0 if string.nil?
|
26
|
+
|
27
|
+
values = string.scan(/\d+[,.]*\d*/).flatten.map(&:to_f)
|
28
|
+
|
29
|
+
values.length > 1 ? values : values.first
|
30
|
+
end
|
31
|
+
|
32
|
+
def cart_date_parser(string)
|
33
|
+
return nil if string.nil?
|
34
|
+
|
35
|
+
day, month = *string.scan(/.* - ([0-9]*) (.*)/).flatten
|
36
|
+
|
37
|
+
format_date "#{Date.today.year}-#{get_month_number(month)}-#{day}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def date_parser(date)
|
41
|
+
if date.is_a?(String)
|
42
|
+
date = Date.parse(date) rescue nil
|
43
|
+
end
|
44
|
+
|
45
|
+
date
|
46
|
+
end
|
47
|
+
|
48
|
+
def format_date(date)
|
49
|
+
date = date_parser(date)
|
50
|
+
|
51
|
+
date = (Date.today + 1) unless date.respond_to?(:strftime)
|
52
|
+
|
53
|
+
date.strftime('%Y-%m-%d')
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_month_number(month)
|
57
|
+
MONTH_NAMES_PT.index(month.downcase)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "a_marmita/meals/scrapper"
|
2
|
+
|
3
|
+
module AMarmita
|
4
|
+
module Meals
|
5
|
+
|
6
|
+
SOUP, MAIN_COURSE, DESSERT = 1, 2, 3
|
7
|
+
|
8
|
+
class Base < ClientModule
|
9
|
+
|
10
|
+
def today_menu
|
11
|
+
page = get_page "http://www.amarmita.com/ementa.php"
|
12
|
+
|
13
|
+
content_rows = page.parser.css('#conteudo > table > tr')
|
14
|
+
|
15
|
+
{ available_days: parse_links(page), list: parse_meals(content_rows) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def soups(date = nil, week_number = nil)
|
19
|
+
get_meals(SOUP, date, week_number)
|
20
|
+
end
|
21
|
+
|
22
|
+
def main_courses(date = nil, week_number = nil)
|
23
|
+
get_meals(MAIN_COURSE, date, week_number)
|
24
|
+
end
|
25
|
+
|
26
|
+
def desserts(date = nil, week_number = nil)
|
27
|
+
get_meals(DESSERT, date, week_number)
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
protected ################### PROTECTED ################
|
32
|
+
|
33
|
+
def get_meals(type, date = nil, week_number = nil)
|
34
|
+
date = Helpers.date_parser(date) || Date.today
|
35
|
+
week_number ||= date.strftime('%U').to_i + 1
|
36
|
+
|
37
|
+
page = get_page "http://www.amarmita.com/encomendamain.php?week=#{week_number}&tipo=#{type}&ano=#{date.year}&mes=#{date.month}&dia=#{date.day}"
|
38
|
+
|
39
|
+
content_rows = page.parser.css('table')[0].css('> tr')
|
40
|
+
|
41
|
+
parse_meals(content_rows)
|
42
|
+
end
|
43
|
+
|
44
|
+
def parse_links(page)
|
45
|
+
page.links.inject([]) do |links, link|
|
46
|
+
links << get_date_and_week_number_from_javascript_function(link.href) if link.href.include?('em_setday')
|
47
|
+
links
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_meals(content_rows)
|
52
|
+
content_rows.map { |row| Meals::Scrapper.new(row).scrap }.compact
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_date_and_week_number_from_javascript_function(javascript_function)
|
56
|
+
args = javascript_function[(javascript_function.index('(') + 1)..-2].split(',')
|
57
|
+
|
58
|
+
{ date: args[0..2].join('-'), week_number: args[3] }
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|