auchandirect-scrAPI 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d467fe59e8fbdaec06eac26018e63a1f35144ee6
4
+ data.tar.gz: 0e6fa119d92864dda09e498dbcee4f388c70b1b7
5
+ SHA512:
6
+ metadata.gz: 6d5dcb57103f62abc269e1ba4c73e09d3db88c5df6e654eb590dbff2fc3d2d600b06d30a429b9a85ee71b05dca6bd945d066621856cfdf6ee520945540221f95
7
+ data.tar.gz: 866b9b4bff3e1819ddb3a63b816e466a381ab37469f60825d54ec91929914c7df4a1968a0a590553830d13116030d1be0af0acd81963199783dad02c7bc0254e
Binary file
Binary file
@@ -0,0 +1,26 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .ruby-version
6
+ Gemfile.lock
7
+ coverage
8
+ InstalledFiles
9
+ lib/bundler/man
10
+ pkg
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ tmp
16
+ vendor
17
+
18
+ # YARD artifacts
19
+ .yardoc
20
+ _yardoc
21
+ doc/
22
+
23
+ # emacs files
24
+ *~
25
+ *#
26
+ .#*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ rvm:
3
+ - ruby-head
4
+ - 2.1.0
5
+ - 2.0.0
6
+ - jruby-head
7
+ - jruby-21mode
8
+ - jruby-20mode
9
+ script: bundle exec rake
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: ruby-head
13
+ # no lazy for enumerators in jruby
14
+ - rvm: jruby-head
15
+ - rvm: jruby-21mode
16
+ - rvm: jruby-20mode
17
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in auchandirect-scrAPI.gemspec
4
+ gemspec
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,29 @@
1
+ # Auchandirect::ScrAPI
2
+
3
+ A ruby gem providing, through scrapping, an API to the french www.auchandirect.com online grocery
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'auchandirect-scrAPI'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install auchandirect-scrAPI
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Run specs'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'auchandirect/scrAPI/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "auchandirect-scrAPI"
8
+ spec.version = Auchandirect::ScrAPI::VERSION
9
+ spec.authors = ["Philou"]
10
+ spec.email = ["philippe.bourgau@gmail.com"]
11
+ spec.description = %q{Through scrapping, an API to the french www.auchandirect.com online grocery}
12
+ spec.summary = %q{auchandirect.com grocery API}
13
+ spec.homepage = "https://github.com/philou/auchandirect-scrAPI"
14
+ spec.license = "LGPL v3"
15
+
16
+ spec.files = `git ls-files`.split($/).reject {|f| f.match(/^offline_sites/)}
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency 'storexplore', '~> 0.2'
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake", "~> 10.1"
23
+ spec.add_development_dependency "guard-rspec", "~> 4.0"
24
+ spec.add_development_dependency "net-ping", "~> 1.7"
25
+ spec.add_development_dependency "spec_combos", "~> 0.2"
26
+
27
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # auchandirect/scrAPI.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 'storexplore'
23
+ require 'auchandirect/scrAPI/version'
24
+ require 'auchandirect/scrAPI/items'
25
+ require 'auchandirect/scrAPI/base_cart'
26
+ require 'auchandirect/scrAPI/dummy_cart'
27
+ require 'auchandirect/scrAPI/cart'
28
+ require 'auchandirect/scrAPI/invalid_account_error'
29
+ require 'auchandirect/scrAPI/webrick_uri_escape_monkey_patch'
@@ -0,0 +1,68 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # auchandirect/scrAPI/base_cart.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
+ module Auchandirect
23
+ module ScrAPI
24
+
25
+ # Objects providing an api like access to third party online stores
26
+ class BaseCart
27
+
28
+ class << self
29
+ alias :login :new
30
+ end
31
+
32
+ # main url of the store
33
+ # def self.url
34
+ def url
35
+ self.class.url
36
+ end
37
+
38
+ # url at which a client browser can login
39
+ # def self.login_url
40
+
41
+ # parameters for a client side login
42
+ # def self.login_parameters(login, password)
43
+
44
+ # login and password parameters names
45
+ # def self.login_parameter
46
+ # def self.password_parameter
47
+
48
+ # url at which a client browser can logout
49
+ # def self.logout_url
50
+
51
+ # logs in to the remote store
52
+ # def initialize(login, password)
53
+
54
+ # logs out from the store
55
+ # def logout
56
+
57
+ # total value of the remote cart
58
+ # def cart_value
59
+
60
+ # empties the cart of the current user
61
+ # def empty_the_cart
62
+
63
+ # adds items to the cart of the current user
64
+ # def add_to_cart_cart(quantity, item_remote_id)
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,173 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # auchandirect/scrAPI/cart.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 'json'
23
+
24
+ module Auchandirect
25
+ module ScrAPI
26
+
27
+ # Store API for AuchanDirect store
28
+ class Cart < BaseCart
29
+
30
+ # main url of the store
31
+ def self.url
32
+ "http://www.auchandirect.fr"
33
+ end
34
+
35
+ # Logins to auchan direct store
36
+ def initialize(login, password)
37
+ @agent = Mechanize.new
38
+ do_login(login, password)
39
+ raise InvalidAccountError unless logged_in?
40
+ end
41
+
42
+ # url at which a client browser can login
43
+ def self.login_url
44
+ url + login_path
45
+ end
46
+ # parameters for a client side login
47
+ def self.login_parameters(login, password)
48
+ default_params = post_parameters.map {|name, value| {'name' => name, 'value' => value, 'type' => 'hidden'}}
49
+ user_params = [{'name' => FORMDATA_PARAMETER, 'value' => login_form_data(Mechanize.new), 'type' => 'hidden'},
50
+ {'name' => LOGIN_PARAMETER, 'value' => login, 'type' => 'text'},
51
+ {'name' => PASSWORD_PARAMETER, 'value' => password, 'type' => 'password'}]
52
+
53
+ default_params + user_params
54
+ end
55
+
56
+ # login and password parameters names
57
+ def self.login_parameter
58
+ LOGIN_PARAMETER
59
+ end
60
+ def self.password_parameter
61
+ PASSWORD_PARAMETER
62
+ end
63
+
64
+ # url at which a client browser can logout
65
+ def self.logout_url
66
+ url + logout_path
67
+ end
68
+
69
+ # logs out from the store
70
+ def logout
71
+ get(self.class.logout_path)
72
+ end
73
+
74
+ # total value of the remote cart
75
+ def cart_value
76
+ cart_page = get("/monpanier")
77
+ cart_page.search("span.prix-total").first.content.gsub(/€$/,"").to_f
78
+ end
79
+
80
+ # empties the cart of the current user
81
+ def empty_the_cart
82
+ post("/boutiques.blockzones.popuphandler.cleanbasketpopup.cleanbasket")
83
+ end
84
+
85
+ # adds items to the cart of the current user
86
+ def add_to_cart(quantity, item_remote_id)
87
+ quantity.times do
88
+ post("/boutiques.mozaique.thumbnailproduct.addproducttobasket/#{item_remote_id}")
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ def do_login(login,password)
95
+ formdata = login_form_data(@agent)
96
+
97
+ post(login_path,
98
+ FORMDATA_PARAMETER => formdata,
99
+ LOGIN_PARAMETER => login,
100
+ PASSWORD_PARAMETER => password)
101
+ end
102
+
103
+ def self.login_path
104
+ "/boutiques.blockzones.popuphandler.authenticatepopup.authenticateform"
105
+ end
106
+
107
+ def self.login_form_data(agent)
108
+ home_page = agent.get(Cart.url)
109
+
110
+ login_form_json = post(agent, "/boutiques.paniervolant.customerinfos:showsigninpopup", {}, {'Referer' => home_page.uri})
111
+
112
+ html_body = JSON.parse(login_form_json.body)["zones"]["secondPopupZone"]
113
+ doc = Nokogiri::HTML("<html><body>#{html_body}</body></html>")
114
+ doc.xpath("//input[@name='#{FORMDATA_PARAMETER}']/@value").first.content
115
+ end
116
+
117
+ def logged_in?
118
+ main_page = get("/Accueil")
119
+ !main_page.body.include?("Identifiez-vous")
120
+ end
121
+
122
+ def get(path)
123
+ @agent.get(url + path)
124
+ end
125
+
126
+ def post(path, parameters = {}, headers = {})
127
+ self.class.post(@agent, path, parameters, headers)
128
+ end
129
+
130
+ def self.post(agent, path, parameters = {}, headers = {})
131
+ agent.post(url + path, post_parameters.merge(parameters), fast_header.merge(headers))
132
+ end
133
+
134
+ def self.fast_header
135
+ {'X-Requested-With' => 'XMLHttpRequest'}
136
+ end
137
+
138
+ def self.logout_path
139
+ parametrized_path("/boutiques.paniervolant.customerinfos:totallogout", post_parameters)
140
+ end
141
+
142
+ def self.parametrized_path(path, parameters)
143
+ string_parameters = parameters.map do |key,value|
144
+ "#{key}=#{value}"
145
+ end
146
+ "#{path}?#{string_parameters.join('&')}"
147
+ end
148
+
149
+ def self.post_parameters
150
+ {'t:ac' => "Accueil", 't:cp' => 'gabarit/generated'}
151
+ end
152
+
153
+ def method_missing(method_sym, *arguments, &block)
154
+ if delegate_to_class?(method_sym)
155
+ self.class.send(method_sym, *arguments, &block)
156
+ else
157
+ super
158
+ end
159
+ end
160
+
161
+ def respond_to?(method_sym)
162
+ super or delegate_to_class?(method_sym)
163
+ end
164
+ def delegate_to_class?(method_sym)
165
+ self.class.respond_to?(method_sym)
166
+ end
167
+
168
+ FORMDATA_PARAMETER = 't:formdata'
169
+ LOGIN_PARAMETER = 'inputLogin'
170
+ PASSWORD_PARAMETER = 'inputPwd'
171
+ end
172
+ end
173
+ end