magento_remote 0.0.1 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjcxMjU3NmFhYWRhZDEzOTI3MjdkZmQzZDkyYzBkODUwOWMyMjdjYw==
4
+ NjYyOTdkMDFhMjU5ZGRjMDQzMzA1NTIxZDJiNjI1MmIyM2RlOTA4NA==
5
5
  data.tar.gz: !binary |-
6
- MDVjOTk5ZjViYmQ1ZDY2ZjEwNDBiMjAxZjEyNWM2YzE5ZjFhYzVjNg==
6
+ YWZhYjkyZmZmM2QzZGVmNjhiMWViYTA5YjYzZDc1N2QxZDVlZDQ2YQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTZiMGJhZjUwYzkyNmM5ZDczYjU3M2E0OTIyOGUzODFlNTdmZDFkNjE1MmMx
10
- OGFmNjhhOGQ4NzMyYTg1MDI3ZjRiZDRkNzRhZTY2OTJkZDgzYzIyMmUzYzNi
11
- YjUzYzg0ZDhhZTU4ZWFjYWE1OTQ3YzZkMTcxY2Y0NmYwMjAzNzk=
9
+ ZjFlMWZkYWMyZTIwMzViZTE4MTY1OGViNzZjMTc1NDk4MWQ4ZmFkN2I4NjBh
10
+ ZjgxNzg5YTA0M2E4NmFmZWNlYzQ4ZTU5YjE1ZWNiNjFkMTNmMzQ1NDEyYzA2
11
+ MWJhZTE1MzllMWE4NzNlMGM1ZDViY2U5ZTM4YTQxMzIyOGNiYzE=
12
12
  data.tar.gz: !binary |-
13
- YWRmYTMyMWRhMmUxMjA5YWQ3NTU3NDE3OTMzNTU0NmIyMzFhMTQ0NDgwODky
14
- NDlkODM4OTcxOTUzNzczMDg2ODY2OWI0YmZlODJjMTdjYzA3NmVlOGI2YzA5
15
- ZTRkNTNmOTczOGUxMWMyYzMyNWE2MWQyNDg0ZTk2YjFiMTQ5OGI=
13
+ MzVmMzQzMDI3MTJlNDZhOTI4YTM5ZjUxNTE5ZDg3ZmNjNWM1ZmM3YTJjZGM0
14
+ ZTg4ZjVmNmQyYTNiYzE3MDI0NmU0M2M3NzI3MzEwOGUyM2VmZDdjYzcxZjBi
15
+ Yzk0Yjc2MDQ0MTQ0NzZjMDA4MWRmZTAyMzRkYzhhMTYwYWI0MDk=
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ magento_remote
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p545
data/README.md CHANGED
@@ -24,6 +24,12 @@ Call
24
24
 
25
25
  to put *quantity* of the product with *productid* in your shopping cart.
26
26
 
27
+ magento_find_product -b https://theshop -s isearchforthisword
28
+
29
+ to display a table of matching products.
30
+
31
+ Note that both scripts show you information about possible parameters when invoked with `--help`.
32
+
27
33
  Note that you should work with
28
34
 
29
35
  bundle exec
@@ -33,6 +33,10 @@ optparse = OptionParser.new do |opts|
33
33
  options[:quantity] = q
34
34
  end
35
35
 
36
+ opts.on('-a', '--as-many-as-possible', 'try to order as many as possible.') do
37
+ options[:amap] = true
38
+ end
39
+
36
40
  opts.separator ""
37
41
  opts.separator "Output options"
38
42
 
@@ -68,6 +72,16 @@ if !options[:user] || !options[:pass] || !options[:base_uri]
68
72
  exit 1
69
73
  end
70
74
 
75
+ if !options[:product_id] || !options[:quantity]
76
+ STDERR.puts "Error: You have to specify product_id and quantity."
77
+ exit 1
78
+ end
79
+
80
+ if options[:quantity].to_i < 1
81
+ STDERR.puts "Error: Quantity to put in cart has to be > 0."
82
+ exit 1
83
+ end
84
+
71
85
  mech = MagentoMech.from_config options
72
86
  if options[:debug] == true
73
87
  mech.log_to! STDOUT
@@ -79,11 +93,23 @@ return_code = 0
79
93
 
80
94
  mech.login
81
95
 
82
- if mech.add_to_cart options[:product_id], options[:quantity]
83
- puts "INFO: Succeeded in adding to cart"
96
+ if options[:amap]
97
+ qty = mech.add_to_cart! options[:product_id], options[:quantity]
98
+ if qty == options[:quantity]
99
+ puts "INFO: Succeeded in adding to cart"
100
+ elsif qty > 0
101
+ puts "INFO: #{options[:quantity]} items not available, added #{qty}."
102
+ else
103
+ STDERR.puts "ERROR: Something went wrong, probably product not in stock."
104
+ return_code = 4
105
+ end
84
106
  else
85
- STDERR.puts "ERROR: Something went wrong."
86
- return_code = 3
107
+ if mech.add_to_cart options[:product_id], options[:quantity]
108
+ puts "INFO: Succeeded in adding to cart"
109
+ else
110
+ STDERR.puts "ERROR: Something went wrong."
111
+ return_code = 3
112
+ end
87
113
  end
88
114
 
89
115
  if options[:show_cart]
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'terminal-table'
5
+ require 'magento_remote'
6
+
7
+ # Sweet, sweet options.
8
+ options = {}
9
+
10
+ optparse = OptionParser.new do |opts|
11
+ opts.banner = "Usage: magento_find_product [OPTIONS]\n Find product(s) in shop."
12
+
13
+ opts.separator ""
14
+ opts.separator "Magento shop options"
15
+
16
+ opts.on('-b', '--base-uri URI', 'base URI of shop.') do |b|
17
+ options[:base_uri] = b
18
+ end
19
+
20
+ opts.separator ""
21
+ opts.separator "Product options"
22
+
23
+ # TODO Make this an argument.
24
+ opts.on('-s', '--search-term SEARCHTERM', 'term to search for (sku, name, description...)') do |s|
25
+ options[:search_term] = s
26
+ end
27
+
28
+ opts.separator ""
29
+ opts.separator "Output options"
30
+
31
+ opts.on('-d', '--debug FILE', 'enable debugging output, STDOUT, or FILE if given') do |d|
32
+ if d
33
+ options[:debug] = d
34
+ else
35
+ options[:debug] = true
36
+ end
37
+ end
38
+
39
+ opts.separator ""
40
+ opts.separator "General options"
41
+
42
+ opts.on_tail('--version', 'Show version.') do
43
+ puts "magento_find_product #{MagentoRemote::VERSION}"
44
+ exit 0
45
+ end
46
+ opts.on('-h', '--help', 'Show help.') do
47
+ puts opts
48
+ exit 0
49
+ end
50
+ end
51
+
52
+ optparse.parse!
53
+
54
+ if !options[:base_uri] || !options[:search_term]
55
+ STDERR.puts "Error: You have to define search_term and base_uri"
56
+ exit 1
57
+ end
58
+
59
+ mech = MagentoMech.from_config options
60
+ if options[:debug] == true
61
+ mech.log_to! STDOUT
62
+ elsif options[:debug]
63
+ mech.log_to! options[:debug]
64
+ end
65
+
66
+ return_code = 0
67
+
68
+ matches = mech.find_product options[:search_term]
69
+ if matches.nil?
70
+ puts "Nothing found"
71
+ else
72
+ puts Terminal::Table.new :headings => ['Name', 'Product ID', 'In stock?'], :rows => matches
73
+ end
74
+
75
+ exit return_code
@@ -61,6 +61,15 @@ class MagentoMech
61
61
  return !result_page.search('.success-msg span').empty?
62
62
  end
63
63
 
64
+ # Puts as many items of given product to cart as possible
65
+ # Returns number of items put to cart.
66
+ def add_to_cart! product_id, qty
67
+ while qty.to_i > 0 && !add_to_cart(product_id, qty)
68
+ qty = qty.to_i - 1
69
+ end
70
+ qty
71
+ end
72
+
64
73
  # Get the current carts contents
65
74
  def get_cart_content
66
75
  cart_page = @mech.get("#{@base_uri}/checkout/cart/")
@@ -80,4 +89,49 @@ class MagentoMech
80
89
  form.fields.find{|f| f.name == 'login[password]'}.value = password
81
90
  @mech.submit(form)
82
91
  end
92
+
93
+ # Search products.
94
+ # Arguments
95
+ # search_string: sku, name or title, urlencoded for get request.
96
+ # returns [[name1, product_id1, instock?1],[name2, p_id2...]...]
97
+ # or nil if not found.
98
+ def find_product search_string
99
+ url = relative_url("/catalogsearch/result/index/?limit=all&q=#{search_string}")
100
+ @mech.get url
101
+
102
+ product_li = @mech.page.search('.equal-height .item')
103
+
104
+ return nil if product_li.empty?
105
+
106
+ products = product_li.map do |product|
107
+ # Add to cart button is missing if out of stock.
108
+ buttons = product.search("button")
109
+ stock = buttons && !buttons[0].nil?
110
+
111
+ # Find product ID from wishlist link.
112
+ wishlist_link = product.search("ul li a")[0]
113
+ wishlist_link.attributes['href'].value[/product\/(\d+)/]
114
+ pid = $1
115
+
116
+ # Find name from heading.
117
+ name = product.search('h2')[0].text
118
+ [name, pid, stock]
119
+ end
120
+
121
+ return products
122
+ end
123
+
124
+ private
125
+
126
+ # Construct path relative to base uri.
127
+ # Example:
128
+ # base uri is http://zentimental.net
129
+ # relative_url 'index.html' # => http://zentimental.net/index.html
130
+ def relative_url path
131
+ if @base_uri.end_with?('/') && !path.start_with?('/')
132
+ @base_uri + '/' + path
133
+ else
134
+ @base_uri + path
135
+ end
136
+ end
83
137
  end
@@ -1,3 +1,3 @@
1
1
  module MagentoRemote
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0".freeze
3
3
  end
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency 'mechanize'
22
+ spec.add_dependency 'terminal-table'
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.6"
24
25
  spec.add_development_dependency "rake"
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.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Wolfsteller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-04 00:00:00.000000000 Z
11
+ date: 2014-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: terminal-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -71,17 +85,20 @@ email:
71
85
  - felix.wolfsteller@gmail.com
72
86
  executables:
73
87
  - magento_add_to_cart
88
+ - magento_find_product
74
89
  extensions: []
75
90
  extra_rdoc_files: []
76
91
  files:
77
92
  - .gitignore
78
93
  - .rspec
79
- - .rvmrc
94
+ - .ruby-gemset
95
+ - .ruby-version
80
96
  - Gemfile
81
97
  - LICENSE.txt
82
98
  - README.md
83
99
  - Rakefile
84
100
  - bin/magento_add_to_cart
101
+ - bin/magento_find_product
85
102
  - lib/magento_remote.rb
86
103
  - lib/magento_remote/magento_mech.rb
87
104
  - lib/magento_remote/version.rb
data/.rvmrc DELETED
@@ -1,62 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
- # development environment upon cd'ing into the directory
5
-
6
- # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
- # Only full ruby name is supported here, for short names use:
8
- # echo "rvm use 1.9.3" > .rvmrc
9
- environment_id="ruby-1.9.3-p545@magento_remote"
10
-
11
- # Uncomment the following lines if you want to verify rvm version per project
12
- # rvmrc_rvm_version="1.25.19 (stable)" # 1.10.1 seems like a safe start
13
- # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
- # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
- # return 1
16
- # }
17
-
18
- # First we attempt to load the desired environment directly from the environment
19
- # file. This is very fast and efficient compared to running through the entire
20
- # CLI and selector. If you want feedback on which environment was used then
21
- # insert the word 'use' after --create as this triggers verbose mode.
22
- if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
- && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
- then
25
- \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
- for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
27
- do
28
- if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
29
- then \. "${__hook}" || true
30
- fi
31
- done
32
- unset __hook
33
- if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
34
- then
35
- if [[ $- == *i* ]] # check for interactive shells
36
- then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
37
- " # show the user the ruby and gemset they are using in green
38
- else printf "%b" "Using: $GEM_HOME
39
- " # don't use colors in non-interactive shells
40
- fi
41
- fi
42
- else
43
- # If the environment file has not yet been created, use the RVM CLI to select.
44
- rvm --create "$environment_id" || {
45
- echo "Failed to create RVM environment '${environment_id}'."
46
- return 1
47
- }
48
- fi
49
-
50
- # If you use bundler, this might be useful to you:
51
- # if [[ -s Gemfile ]] && {
52
- # ! builtin command -v bundle >/dev/null ||
53
- # builtin command -v bundle | GREP_OPTIONS="" \grep $rvm_path/bin/bundle >/dev/null
54
- # }
55
- # then
56
- # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
57
- # gem install bundler
58
- # fi
59
- # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
60
- # then
61
- # bundle install | GREP_OPTIONS="" \grep -vE '^Using|Your bundle is complete'
62
- # fi