magento_remote 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/bin/magento_add_to_cart +7 -2
- data/lib/magento_remote/magento_mech.rb +30 -5
- data/lib/magento_remote/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25e48a3c4c2d32aa637c541ef48cd02a9f649b96
|
4
|
+
data.tar.gz: 54fb6b0a3365a1ad50c5a881e9ba062d64b51f95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11a3b8a1c7949e820df26bc03c056762b3f0debc99ca0914868883393e55fd6a61253cf4a97b16205b69f043817b85812e488bbf9d194245ba0e0a91d781f225
|
7
|
+
data.tar.gz: 80f4368b469dc9455d3a3670f5d7994f3d1cf111e776083502e91122b1330cfc7dab9e892b95a3e5304646e6b4806c769745f6ebeae6d779cf386ebd985e0522
|
data/README.md
CHANGED
@@ -22,7 +22,7 @@ Call
|
|
22
22
|
|
23
23
|
magento_add_to_cart -u customerlogin -p customerpassword -b https://theshop -w productid -q quantity -c
|
24
24
|
|
25
|
-
to put *quantity* of the product with *productid* in your shopping cart.
|
25
|
+
to put *quantity* of the product with *productid* in your shopping cart (read about form token parameter further down, if working with 'hardened' magento versions, or magento >= 1.8).
|
26
26
|
|
27
27
|
magento_find_product -b https://theshop -s isearchforthisword
|
28
28
|
|
@@ -32,6 +32,8 @@ to display a table of matching products.
|
|
32
32
|
|
33
33
|
to display *limit* number of products, starting with product id of *startpid*.
|
34
34
|
|
35
|
+
The form_token for add_to_cart functionality is necessary for specially 'hardened' magento instances, or magento versions >= 1.8 . You find it encoded in the URL of any form action that deals with cart additions. It was only tested for 'ajax' cart actions.
|
36
|
+
|
35
37
|
Note that all scripts show you information about possible parameters when invoked with `--help`.
|
36
38
|
|
37
39
|
Note that you should work with
|
data/bin/magento_add_to_cart
CHANGED
@@ -23,6 +23,9 @@ optparse = OptionParser.new do |opts|
|
|
23
23
|
opts.on('-b', '--base-uri URI', 'base URI of shop.') do |b|
|
24
24
|
options[:base_uri] = b
|
25
25
|
end
|
26
|
+
opts.on('-f', '--form-token FORM_TOKEN', 'base-64 encoded URL to be used as form token (magento >= 1.8).') do |f|
|
27
|
+
options[:form_token] = f
|
28
|
+
end
|
26
29
|
|
27
30
|
opts.separator ""
|
28
31
|
opts.separator "Product options"
|
@@ -71,6 +74,8 @@ optparse.parse!
|
|
71
74
|
|
72
75
|
if !options[:user] || !options[:pass] || !options[:base_uri]
|
73
76
|
STDERR.puts "Error: You have to define user, pass and base_uri"
|
77
|
+
puts
|
78
|
+
puts optparse
|
74
79
|
exit 1
|
75
80
|
end
|
76
81
|
|
@@ -97,7 +102,7 @@ return_code = 0
|
|
97
102
|
mech.login
|
98
103
|
|
99
104
|
if options[:amap]
|
100
|
-
qty = mech.add_to_cart! options[:product_id], options[:quantity]
|
105
|
+
qty = mech.add_to_cart! options[:product_id], options[:quantity], options[:form_token]
|
101
106
|
if qty == options[:quantity].to_i
|
102
107
|
puts "INFO: Succeeded in adding to cart"
|
103
108
|
elsif qty > 0
|
@@ -107,7 +112,7 @@ if options[:amap]
|
|
107
112
|
return_code = 4
|
108
113
|
end
|
109
114
|
else
|
110
|
-
if mech.add_to_cart options[:product_id], options[:quantity]
|
115
|
+
if mech.add_to_cart options[:product_id], options[:quantity], options[:form_token]
|
111
116
|
puts "INFO: Succeeded in adding to cart"
|
112
117
|
else
|
113
118
|
STDERR.puts "ERROR: Something went wrong."
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'mechanize'
|
2
2
|
require 'logger'
|
3
|
+
require 'json'
|
3
4
|
|
4
5
|
# The Mech Driver, interacting with a Magento shop page.
|
5
6
|
# Note that the Mech does not keep too much state, you have to
|
@@ -46,10 +47,32 @@ class MagentoMech
|
|
46
47
|
login_with @user, @pass
|
47
48
|
end
|
48
49
|
|
50
|
+
# See add_to_cart for more notes
|
51
|
+
def ajax_add_to_cart product_id, qty, form_token
|
52
|
+
if product_id.nil? || qty.to_i <= 0 || form_token.nil?
|
53
|
+
fail "Empty obligatory parameter"
|
54
|
+
end
|
55
|
+
|
56
|
+
url = "#{@base_uri}/checkout/cart/add?product=#{product_id}&qty=#{qty}"
|
57
|
+
result_page = @mech.post "#{@base_uri}/checkout/cart/add/uenc/#{form_token}/product/#{product_id}/?isajaxcart=true&groupmessage=1&minicart=1&ajaxlinks=1",
|
58
|
+
{product: product_id,
|
59
|
+
qty: qty}
|
60
|
+
|
61
|
+
result = JSON.parse result_page.content
|
62
|
+
return !result["outStock"]
|
63
|
+
end
|
64
|
+
|
49
65
|
# Put stuff in the cart.
|
66
|
+
# Use the form_token for magento >= 1.8 (or form token enforcing magento
|
67
|
+
# installations), otherwise leave it nil.
|
68
|
+
#
|
50
69
|
# Returns true if succeeded, false otherwise.
|
51
|
-
def add_to_cart product_id, qty
|
70
|
+
def add_to_cart product_id, qty, form_token=nil
|
52
71
|
fail "Empty obligatory parameter" if product_id.nil? || qty.to_i <= 0
|
72
|
+
|
73
|
+
if !form_token.nil?
|
74
|
+
return ajax_add_to_cart(product_id, qty, form_token)
|
75
|
+
end
|
53
76
|
url = "#{@base_uri}/checkout/cart/add?product=#{product_id}&qty=#{qty}"
|
54
77
|
|
55
78
|
# Check the returned page name
|
@@ -73,10 +96,10 @@ class MagentoMech
|
|
73
96
|
|
74
97
|
# Puts as many items of given product to cart as possible
|
75
98
|
# Returns number of items put to cart.
|
76
|
-
def add_to_cart! product_id, qty
|
99
|
+
def add_to_cart! product_id, qty, form_token=nil
|
77
100
|
# Try to be a bit clever and early find out whether article
|
78
101
|
# is out of stock.
|
79
|
-
if add_to_cart(product_id, qty)
|
102
|
+
if add_to_cart(product_id, qty, form_token)
|
80
103
|
# to_i
|
81
104
|
return qty
|
82
105
|
end
|
@@ -84,7 +107,7 @@ class MagentoMech
|
|
84
107
|
# Apparently not enough in stock!
|
85
108
|
|
86
109
|
if qty.to_i > 4
|
87
|
-
if !add_to_cart(product_id, 1)
|
110
|
+
if !add_to_cart(product_id, 1, form_token)
|
88
111
|
# out of stock
|
89
112
|
return 0
|
90
113
|
else
|
@@ -99,6 +122,7 @@ class MagentoMech
|
|
99
122
|
end
|
100
123
|
|
101
124
|
# Get the current carts contents
|
125
|
+
# Returns [[name, qty], [name2, qty2] ... ]
|
102
126
|
def get_cart_content
|
103
127
|
cart_page = @mech.get("#{@base_uri}/checkout/cart/")
|
104
128
|
name_links = cart_page.search('td h2 a')
|
@@ -112,7 +136,8 @@ class MagentoMech
|
|
112
136
|
def login_with username, password
|
113
137
|
login_page = @mech.get("#{@base_uri}/customer/account/login/")
|
114
138
|
|
115
|
-
|
139
|
+
# Probably we could just send the POST directly.
|
140
|
+
form = login_page.form_with(:action => "#{@base_uri}/customer/account/loginPost/", :method => 'POST')
|
116
141
|
form.action = "#{@base_uri}/customer/account/loginPost/"
|
117
142
|
form.fields.find{|f| f.name == 'login[username]'}.value = username
|
118
143
|
form.fields.find{|f| f.name == 'login[password]'}.value = password
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magento_remote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felix Wolfsteller
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mechanize
|
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
145
|
version: '0'
|
146
146
|
requirements: []
|
147
147
|
rubyforge_project:
|
148
|
-
rubygems_version: 2.5
|
148
|
+
rubygems_version: 2.4.5
|
149
149
|
signing_key:
|
150
150
|
specification_version: 4
|
151
151
|
summary: Login to a magento shop and do stuff.
|