class Auchandirect::ScrAPI::Cart
Cart
API for AuchanDirect store
Constants
- FORMDATA_PARAMETER
- LOGIN_PARAMETER
- PASSWORD_PARAMETER
Public Class Methods
login_parameter()
click to toggle source
Client side ‘login’ parameter name
# File lib/auchandirect/scrAPI/cart.rb, line 62 def self.login_parameter LOGIN_PARAMETER end
login_parameters(login, password)
click to toggle source
Parameters for a client side login
# File lib/auchandirect/scrAPI/cart.rb, line 52 def self.login_parameters(login, password) default_params = post_parameters.map {|name, value| {'name' => name, 'value' => value, 'type' => 'hidden'}} user_params = [{'name' => FORMDATA_PARAMETER, 'value' => login_form_data(new_agent), 'type' => 'hidden'}, {'name' => LOGIN_PARAMETER, 'value' => login, 'type' => 'text'}, {'name' => PASSWORD_PARAMETER, 'value' => password, 'type' => 'password'}] default_params + user_params end
login_url()
click to toggle source
Url at which a client browser can login
# File lib/auchandirect/scrAPI/cart.rb, line 48 def self.login_url URL + login_path end
logout_url()
click to toggle source
Url at which a client browser can logout
# File lib/auchandirect/scrAPI/cart.rb, line 71 def self.logout_url URL + logout_path end
new(login, password)
click to toggle source
Logins to auchan direct store
# File lib/auchandirect/scrAPI/cart.rb, line 35 def initialize(login, password) @agent = new_agent do_login(login, password) raise InvalidAccountError unless logged_in? end
new_agent()
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 41 def self.new_agent Mechanize.new do |it| Agent.configure(it) end end
password_parameter()
click to toggle source
Client side ‘password’ parameter name
# File lib/auchandirect/scrAPI/cart.rb, line 66 def self.password_parameter PASSWORD_PARAMETER end
url()
click to toggle source
Main url of the store
# File lib/auchandirect/scrAPI/cart.rb, line 30 def self.url HOMEPAGE end
Private Class Methods
fast_header()
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 168 def self.fast_header {'X-Requested-With' => 'XMLHttpRequest'} end
login_form_data(agent)
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 127 def self.login_form_data(agent) home_page = agent.get(Cart.url) with_retry(5) do login_form_json = post(agent, "/boutiques.paniervolant.customerinfos:showsigninpopup", {}, {'Referer' => home_page.uri.to_s}) html_body = JSON.parse(login_form_json.body)["zones"]["popupZone"] doc = Nokogiri::HTML("<html><body>#{html_body}</body></html>") doc.xpath("//input[@name='#{FORMDATA_PARAMETER}']/@value").first.content end end
login_path()
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 123 def self.login_path "/boutiques.blockzones.popuphandler.authenticatepopup.authenticateform" end
logout_path()
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 172 def self.logout_path parametrized_path("/boutiques.paniervolant.customerinfos:totallogout", post_parameters) end
parametrized_path(path, parameters)
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 176 def self.parametrized_path(path, parameters) string_parameters = parameters.map do |key,value| "#{key}=#{value}" end "#{path}?#{string_parameters.join('&')}" end
post(agent, path, parameters = {}, headers = {})
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 164 def self.post(agent, path, parameters = {}, headers = {}) agent.post(URL + path, post_parameters.merge(parameters), fast_header.merge(headers)) end
post_parameters()
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 183 def self.post_parameters {'t:ac' => "Accueil", 't:cp' => 'gabarit/generated'} end
with_retry(attempts) { || ... }
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 139 def self.with_retry(attempts) begin return yield rescue Exception => e attempts -= 1 retry unless attempts == 0 raise e end end
Public Instance Methods
add_to_cart(quantity, item_remote_id)
click to toggle source
Adds items to the cart of the current user
# File lib/auchandirect/scrAPI/cart.rb, line 92 def add_to_cart(quantity, item_remote_id) quantity.times do post("/boutiques.mozaique.thumbnailproduct.addproducttobasket/#{item_remote_id}") end end
cart_value()
click to toggle source
Total value of the remote cart
# File lib/auchandirect/scrAPI/cart.rb, line 81 def cart_value cart_page = get("/monpanier") cart_page.search("span.prix-total").first.content.gsub(/€$/,"").to_f end
empty_the_cart()
click to toggle source
Empties the cart of the current user
# File lib/auchandirect/scrAPI/cart.rb, line 87 def empty_the_cart post("/boutiques.blockzones.popuphandler.cleanbasketpopup.cleanbasket") end
logout()
click to toggle source
Logs out from the store
# File lib/auchandirect/scrAPI/cart.rb, line 76 def logout get(self.class.logout_path) end
method_missing(method_sym, *arguments, &block)
click to toggle source
Missing methods can be forwarded to the class
Calls superclass method
# File lib/auchandirect/scrAPI/cart.rb, line 99 def method_missing(method_sym, *arguments, &block) if delegate_to_class?(method_sym) self.class.send(method_sym, *arguments, &block) else super end end
respond_to?(method_sym)
click to toggle source
It can respond to messages that the class can handle
Calls superclass method
# File lib/auchandirect/scrAPI/cart.rb, line 108 def respond_to?(method_sym) super or delegate_to_class?(method_sym) end
Private Instance Methods
delegate_to_class?(method_sym)
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 187 def delegate_to_class?(method_sym) self.class.respond_to?(method_sym) end
do_login(login,password)
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 114 def do_login(login,password) formdata = login_form_data(@agent) post(login_path, FORMDATA_PARAMETER => formdata, LOGIN_PARAMETER => login, PASSWORD_PARAMETER => password) end
get(path)
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 156 def get(path) @agent.get(URL + path) end
logged_in?()
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 151 def logged_in? main_page = get("/Accueil") !main_page.body.include?("Identifiez-vous") end
post(path, parameters = {}, headers = {})
click to toggle source
# File lib/auchandirect/scrAPI/cart.rb, line 160 def post(path, parameters = {}, headers = {}) self.class.post(@agent, path, parameters, headers) end