auchandirect-scrAPI 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # spec/lib/auchandirect/scrAPI/cart_spec.rb
4
+ #
5
+ # Copyright (C) 2011-2014 by Philippe Bourgau. All rights reserved.
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 3.0 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301 USA
21
+
22
+ require 'spec_helper'
23
+ require_relative 'cart_shared_examples'
24
+ require_relative 'client_cart_shared_examples'
25
+
26
+ when_online("AuchanDirectApi remote spec") do
27
+
28
+ module Auchandirect
29
+ module ScrAPI
30
+
31
+ module AuchanDirectApiCredentials
32
+ def valid_email
33
+ "mes.courses.fr.test@gmail.com"
34
+ end
35
+ def valid_password
36
+ # gmail password : "mes*courses"
37
+ "mescourses"
38
+ end
39
+ end
40
+ # force autoload of AuchanDirectApi
41
+ Auchandirect::ScrAPI::Cart.send(:extend, AuchanDirectApiCredentials)
42
+
43
+ describe Auchandirect::ScrAPI::Cart, slow: true, remote: true do
44
+ it_should_behave_like "Any Cart", Auchandirect::ScrAPI::Cart, Auchandirect::ScrAPI::Cart.url
45
+ it_should_behave_like "Any Client Cart", Auchandirect::ScrAPI::Cart, "Identifiez-vous"
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+
@@ -0,0 +1,60 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # spec/lib/auchandirect/scrAPI/client_cart_shared_examples.rb
4
+ #
5
+ # Copyright (C) 2011-2014 by Philippe Bourgau. All rights reserved.
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 3.0 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301 USA
21
+
22
+ require 'spec_helper'
23
+
24
+ shared_examples_for "Any Client Cart" do |store_cart_class, please_login_text|
25
+
26
+ before :all do
27
+ @store_cart_class = store_cart_class
28
+ @please_login_text = please_login_text
29
+ end
30
+
31
+ it "logs in and out through HTTP" do
32
+ @client = Mechanize.new
33
+ expect(logged_in?).to(be_false, "should not be logged in at the begining")
34
+
35
+ login
36
+ expect(logged_in?).to(be_true, "should be logged in after submitting the login form")
37
+
38
+ logout
39
+ expect(logged_in?).to(be_false, "should he logged out after clicking the logout link")
40
+ end
41
+
42
+ private
43
+
44
+ def logged_in?
45
+ page = @client.get(@store_cart_class.url)
46
+ !page.body.include?(@please_login_text)
47
+ end
48
+
49
+ def login
50
+ params = @store_cart_class.login_parameters(@store_cart_class.valid_email, @store_cart_class.valid_password)
51
+ post_params = params.map {|param| {param['name'] => param['value']}}.inject &:merge
52
+
53
+ @client.post(@store_cart_class.login_url, post_params)
54
+ end
55
+
56
+ def logout
57
+ @client.get(@store_cart_class.logout_url)
58
+ end
59
+
60
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # spec/lib/auchandirect/scrAPI/dummy_cart_spec.rb
4
+ #
5
+ # Copyright (C) 2011-2014 by Philippe Bourgau. All rights reserved.
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 3.0 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301 USA
21
+
22
+ require 'spec_helper'
23
+ require_relative 'cart_shared_examples'
24
+ require 'storexplore/testing'
25
+
26
+ module Auchandirect
27
+ module ScrAPI
28
+
29
+ describe Auchandirect::ScrAPI::DummyCart do
30
+ STORE_NAME = "www.cart-dummy-api-spec.com"
31
+
32
+ it_should_behave_like "Any Cart", Auchandirect::ScrAPI::DummyCart, Storexplore::Testing::DummyStore.uri(STORE_NAME)
33
+
34
+ before(:all) do
35
+ Storexplore::Testing::DummyStore.wipe_out_store(STORE_NAME)
36
+ Storexplore::Testing::DummyStore.open(STORE_NAME).generate(3).categories.and(3).categories.and(3).items
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,44 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # spec/lib/auchandirect/scrAPI/items_spec.rb
4
+ #
5
+ # Copyright (C) 2010-2014 by Philippe Bourgau. All rights reserved.
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 3.0 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301 USA
21
+
22
+ require 'spec_helper'
23
+
24
+ when_online "AuchanDirectStoreItemsAPI remote spec" do
25
+
26
+ module Auchandirect
27
+ module ScrAPI
28
+
29
+ describe "AuchanDirectAPI", slow: true, remote: true do
30
+ include_context "a scrapped store"
31
+ it_should_behave_like "an API"
32
+
33
+ def generate_store
34
+ Storexplore::Api.browse("http://www.auchandirect.fr")
35
+ end
36
+
37
+ it "should have absolute urls for images" do
38
+ expect(sample_items_attributes.map {|attr| attr[:image]}).to all_ {include("auchandirect.fr")}
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,114 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # spec/lib/auchandirect/scrAPI/offline_items_spec.rb
4
+ #
5
+ # Copyright (C) 2010-2014 by Philippe Bourgau. All rights reserved.
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 3.0 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301 USA
21
+
22
+ require 'spec_helper'
23
+
24
+ module Auchandirect
25
+ module ScrAPI
26
+
27
+ describe "OfflineAuchanDirectApi", slow: true do
28
+ include_context "a scrapped store"
29
+ it_should_behave_like "an API"
30
+
31
+ def generate_store
32
+ @offline_store_dir = File.join(AUCHANDIRECT_SCRAPI_ROOT_DIR,'offline_sites','www.auchandirect.fr')
33
+ auchan_direct_offline = "file://" + File.join(@offline_store_dir, 'index.html')
34
+ Storexplore::Api.browse(auchan_direct_offline)
35
+ end
36
+
37
+ it "collects names of item categories" do
38
+ parseable_categories_attributes.each do |attributes|
39
+ expect(attributes).to have_key(:name)
40
+ end
41
+ end
42
+
43
+ it "parses promotions prices" do
44
+ prices = items_with('prix-promo', "#produit-infos .bloc-prix-promo > span.prix-promo").
45
+ map {|item_info| item_info[:item].attributes[:price] }
46
+
47
+ expect(prices.first).to be_instance_of(Float)
48
+ end
49
+
50
+ it "collects secondary titles" do
51
+ item_infos = items_with('titre-secondaire', '#produit-infos .titre-secondaire').first
52
+
53
+ expect(item_infos[:item].attributes[:name]).to include(::Auchandirect::ScrAPI::Items::NAMES_SEPARATOR + item_infos[:elements].first.text)
54
+ end
55
+
56
+ private
57
+
58
+ def items_with(grep_hint, selector)
59
+ search_through_files_for(grep_hint, selector).
60
+ map {|item_info| add_corresponding_item_to(item_info)}.
61
+ select {|item_info| !item_info.nil? }
62
+ end
63
+
64
+ def search_through_files_for(grep_hint, selector)
65
+ `find #{@offline_store_dir} -name *.html -exec grep -l "#{grep_hint}" {} \\;`.split("\n").lazy.
66
+
67
+ map do |file|
68
+ doc = Nokogiri::HTML(open(file))
69
+ [file, doc.search(selector)]
70
+ end.
71
+
72
+ select do |file, elements|
73
+ !elements.empty?
74
+ end.
75
+
76
+ map do |file, elements|
77
+ # example : http://www.auchandirect.fr/petit-dejeuner-epicerie-sucree/chocolats,-confiseries/barres-biscuitees-muesli---cereales/id1/485/53869
78
+
79
+ file = URI.unescape(file)
80
+ file = file.gsub(/^.*\/www\.auchandirect\.fr\//, "")
81
+ pieces = file.split("/").take(3)
82
+ pieces[2] = pieces[2].split(",")[0]
83
+ pieces = pieces.map do |piece|
84
+ piece.gsub(/lv_/,'').split(/[\-_, \(\)']+/).select {|p| not (p.size <= 2 or p =~ /^[0-9]/)}
85
+ end
86
+
87
+ {cat_tokens: pieces[0], sub_cat_tokens: pieces[1], item_tokens: pieces[2], elements: elements}
88
+ end
89
+ end
90
+
91
+
92
+ def add_corresponding_item_to(item_info)
93
+ filter(store.categories, item_info[:cat_tokens]).each do |cat|
94
+ filter(cat.categories, item_info[:sub_cat_tokens]).each do |sub_cat|
95
+ sub_cat.categories.each do |sub_sub_cat|
96
+ filter(sub_sub_cat.items, item_info[:item_tokens]).each do |item|
97
+ return item_info.merge(item: item)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ nil
103
+ end
104
+
105
+ def filter(sub_elements, tokens)
106
+ sub_elements.select do |element|
107
+ tokens.all? do |token|
108
+ element.attributes[:name].downcase.include?(token.downcase)
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # spec_helper.rb
4
+ #
5
+ # Copyright (c) 2013-2014 by Philippe Bourgau. All rights reserved.
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 3.0 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301 USA
21
+
22
+ require 'auchandirect/scrAPI'
23
+ require 'spec_combos'
24
+
25
+ # Offline testing
26
+ require_relative 'support/offline_test_helper'
27
+ require_relative 'support/enumerator_lazy'
28
+ include OfflineTestHelper
29
+
30
+ # Gem root dir
31
+ AUCHANDIRECT_SCRAPI_ROOT_DIR = File.join(File.dirname(__FILE__), '..')
32
+
33
+
34
+ # Dummy store generation
35
+ require 'storexplore/testing'
36
+ Storexplore::Testing.config do |config|
37
+ config.dummy_store_generation_dir= File.join(AUCHANDIRECT_SCRAPI_ROOT_DIR, 'tmp')
38
+ end
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # spec/support/enumerator_lazy.rb
4
+ #
5
+ # Copyright (C) 2013-2014 by Philippe Bourgau. All rights reserved.
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 3.0 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301 USA
21
+
22
+ class Enumerator::Lazy
23
+
24
+ def flatten(depth = Float::INFINITY)
25
+ return self if depth <= 0
26
+
27
+ Enumerator::Lazy.new(self) do |yielder, item|
28
+ if item.is_a? Enumerable
29
+ item.lazy.flatten(depth - 1).each do |e|
30
+ yielder.yield(e)
31
+ end
32
+ else
33
+ yielder.yield(item)
34
+ end
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # spec/support/offline_test_helper.rb
4
+ #
5
+ # Copyright (C) 2011-2014 by Philippe Bourgau. All rights reserved.
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 3.0 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301 USA
21
+
22
+ require 'net/ping'
23
+
24
+ # Something to ask if we are online or not
25
+ module OfflineTestHelper
26
+
27
+ # offline and onine predicates
28
+ def offline?
29
+ !online?
30
+ end
31
+ def online?
32
+ @online ||= Net::Ping::TCP.new('www.google.com', 'http').ping?
33
+ end
34
+
35
+ # puts a colored warning if offline, otherwise
36
+ def when_online(description)
37
+ if offline?
38
+ puts yellow("WARNING: skipping #{description} because running offline")
39
+ else
40
+ yield
41
+ end
42
+ end
43
+
44
+ # something to color text
45
+ def yellow(text)
46
+ "\x1B[33m#{text}\x1B[0m"
47
+ end
48
+
49
+ end
@@ -0,0 +1,42 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # spec/support_spec/enumerator_lazy_spec.rb
4
+ #
5
+ # Copyright (C) 2013, 2014 by Philippe Bourgau. All rights reserved.
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 3.0 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301 USA
21
+
22
+ describe Enumerator::Lazy, '#flatten' do
23
+
24
+ it "returns the same collection as stock flatten" do
25
+ tree = [[1], [1,2], [1,[2,3]]]
26
+ expect(tree.lazy.flatten.to_a).to eq tree.flatten
27
+ expect(tree.lazy.flatten(1).to_a).to eq tree.flatten(1)
28
+ expect(tree.lazy.flatten(0).to_a).to eq tree.flatten(0)
29
+ end
30
+
31
+ it "processes items on demand" do
32
+ processed_items = 0
33
+ enums = (1..3).lazy.map {|i| (1..i).lazy.map {|j| processed_items += 1; j}}
34
+
35
+ expect(enums.flatten.first(3)).to eq [1, 1,2]
36
+ expect(processed_items).to eq 3
37
+ end
38
+
39
+ it "returns empty list for empty list" do
40
+ expect([].lazy.flatten.to_a).to eq []
41
+ end
42
+ end