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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: abce27b2504c667c77150db6dde7d872f1406a8f
4
- data.tar.gz: a0ba4c28ca36edfd8ad349f94fa50ef8c89e45e0
3
+ metadata.gz: 25e48a3c4c2d32aa637c541ef48cd02a9f649b96
4
+ data.tar.gz: 54fb6b0a3365a1ad50c5a881e9ba062d64b51f95
5
5
  SHA512:
6
- metadata.gz: 565e069bca28c75ae6179a843a2ef8220b3524c8a952775da24fd5543e7b5d97d6b4de37780a3aca342cd78c5da8c9b8dfd0539aa1dfd89cc548c3e58a038573
7
- data.tar.gz: f459b1e4bdb19df64d9a9a37563f0ab8228f868ade07f80daecc752e66f9e3cf5f984016d1c5d8eac203aba085c309e00d904f2664e8662dd8706483941f4ea3
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
@@ -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
- form = login_page.form_with(:action => '#', :method => 'POST')
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
@@ -1,3 +1,3 @@
1
1
  module MagentoRemote
2
- VERSION = "0.2.3".freeze
2
+ VERSION = "0.2.4".freeze
3
3
  end
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.3
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-01-25 00:00:00.000000000 Z
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.1
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.