magento_remote 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjcxMjU3NmFhYWRhZDEzOTI3MjdkZmQzZDkyYzBkODUwOWMyMjdjYw==
5
+ data.tar.gz: !binary |-
6
+ MDVjOTk5ZjViYmQ1ZDY2ZjEwNDBiMjAxZjEyNWM2YzE5ZjFhYzVjNg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OTZiMGJhZjUwYzkyNmM5ZDczYjU3M2E0OTIyOGUzODFlNTdmZDFkNjE1MmMx
10
+ OGFmNjhhOGQ4NzMyYTg1MDI3ZjRiZDRkNzRhZTY2OTJkZDgzYzIyMmUzYzNi
11
+ YjUzYzg0ZDhhZTU4ZWFjYWE1OTQ3YzZkMTcxY2Y0NmYwMjAzNzk=
12
+ data.tar.gz: !binary |-
13
+ YWRmYTMyMWRhMmUxMjA5YWQ3NTU3NDE3OTMzNTU0NmIyMzFhMTQ0NDgwODky
14
+ NDlkODM4OTcxOTUzNzczMDg2ODY2OWI0YmZlODJjMTdjYzA3NmVlOGI2YzA5
15
+ ZTRkNTNmOTczOGUxMWMyYzMyNWE2MWQyNDg0ZTk2YjFiMTQ5OGI=
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ **/*.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.rvmrc ADDED
@@ -0,0 +1,62 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in magento_remote.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Felix Wolfsteller
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # MagentoRemote
2
+
3
+ Interact with a specific but defaultish Magento shop via its webpage.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'magento_remote'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install magento_remote
18
+
19
+ ## Usage
20
+
21
+ Call
22
+
23
+ magento_add_to_cart -u customerlogin -p customerpassword -b https://theshop -w productid -q quantity -c
24
+
25
+ to put *quantity* of the product with *productid* in your shopping cart.
26
+
27
+ Note that you should work with
28
+
29
+ bundle exec
30
+
31
+ and
32
+
33
+ bundle console
34
+
35
+ while developing.
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( https://github.com/[my-github-username]/magento_remote/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'magento_remote'
5
+
6
+ # Sweet, sweet options.
7
+ options = {}
8
+
9
+ optparse = OptionParser.new do |opts|
10
+ opts.banner = "Usage: magento_add_to_cart [OPTIONS]"
11
+
12
+ opts.separator ""
13
+ opts.separator "Magento shop options"
14
+
15
+ opts.on('-u', '--customer USER', 'customer/username of shop.') do |u|
16
+ options[:user] = u
17
+ end
18
+ opts.on('-p', '--password PASSWORD', 'password of customer account.') do |p|
19
+ options[:pass] = p
20
+ end
21
+ opts.on('-b', '--base-uri URI', 'base URI of shop.') do |b|
22
+ options[:base_uri] = b
23
+ end
24
+
25
+ opts.separator ""
26
+ opts.separator "Product options"
27
+
28
+ opts.on('-w', '--product-id PRODUCTID', 'product id of product to put in cart.') do |p|
29
+ options[:product_id] = p
30
+ end
31
+
32
+ opts.on('-q', '--quantity QUANTITY', 'quantity of product to put in cart.') do |q|
33
+ options[:quantity] = q
34
+ end
35
+
36
+ opts.separator ""
37
+ opts.separator "Output options"
38
+
39
+ opts.on('-c', '--show-cart', 'show cart contents afterwards') do |c|
40
+ options[:show_cart] = true
41
+ end
42
+
43
+ opts.on('-d', '--debug FILE', 'enable debugging output, STDOUT, or FILE if given') do |d|
44
+ if d
45
+ options[:debug] = d
46
+ else
47
+ options[:debug] = true
48
+ end
49
+ end
50
+
51
+ opts.separator ""
52
+ opts.separator "General options"
53
+
54
+ opts.on_tail('--version', 'Show version.') do
55
+ puts "magento_add_to_cart #{MagentoRemote::VERSION}"
56
+ exit 0
57
+ end
58
+ opts.on('-h', '--help', 'Show help.') do
59
+ puts opts
60
+ exit 0
61
+ end
62
+ end
63
+
64
+ optparse.parse!
65
+
66
+ if !options[:user] || !options[:pass] || !options[:base_uri]
67
+ STDERR.puts "Error: You have to define user, pass and base_uri"
68
+ exit 1
69
+ end
70
+
71
+ mech = MagentoMech.from_config options
72
+ if options[:debug] == true
73
+ mech.log_to! STDOUT
74
+ elsif options[:debug]
75
+ mech.log_to! options[:debug]
76
+ end
77
+
78
+ return_code = 0
79
+
80
+ mech.login
81
+
82
+ if mech.add_to_cart options[:product_id], options[:quantity]
83
+ puts "INFO: Succeeded in adding to cart"
84
+ else
85
+ STDERR.puts "ERROR: Something went wrong."
86
+ return_code = 3
87
+ end
88
+
89
+ if options[:show_cart]
90
+ puts "Current Cart Content"
91
+ puts mech.get_cart_content.map {|c| "#{c[1]} #{c[0]}"}
92
+ end
93
+
94
+ exit return_code
@@ -0,0 +1,6 @@
1
+ require "magento_remote/version"
2
+ require 'magento_remote/magento_mech'
3
+
4
+ module MagentoRemote
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,83 @@
1
+ require 'mechanize'
2
+ require 'logger'
3
+
4
+ # The Mech Driver, interacting with a Magento shop page.
5
+ # Note that the Mech does not keep too much state, you have to
6
+ # care about login etc yourself.
7
+ class MagentoMech
8
+ attr_accessor :user
9
+ attr_accessor :pass
10
+
11
+ # Create Mech from hash
12
+ # Argument conf
13
+ # values :base_uri, :user, :pass.
14
+ def self.from_config(conf)
15
+ client = MagentoMech.new(conf[:base_uri])
16
+ client.user = conf[:user]
17
+ client.pass = conf[:pass]
18
+ client
19
+ end
20
+
21
+ # Create Mech with base_uri
22
+ def initialize base_uri
23
+ @mech = Mechanize.new
24
+ @mech.user_agent = 'RawBot, Felix sends the Mech.'
25
+ @base_uri = base_uri
26
+ end
27
+
28
+ # Log to given file (-like object).
29
+ def log_to! file
30
+ puts @mech.log
31
+ @mech.log = Logger.new file
32
+ end
33
+
34
+ # Login to webpage
35
+ def login
36
+ login_with @user, @pass
37
+ end
38
+
39
+ # Put stuff in the cart.
40
+ # Returns true if succeeded, false otherwise.
41
+ def add_to_cart product_id, qty
42
+ fail "Empty obligatory parameter" if product_id.nil? || qty.to_i <= 0
43
+ url = "#{@base_uri}/checkout/cart/add?product=#{product_id}&qty=#{qty}"
44
+
45
+ # Check the returned page name
46
+ result_page = @mech.get url
47
+
48
+ # There are multiple reasons of failure:
49
+ # * product_id unknown
50
+ # * product out of stock
51
+
52
+ # There are multiple ways to detect failure:
53
+ # * contains a form with post action ending on product/#{product_id}/
54
+ # * title is different (stays at product page when failing)
55
+ # * no success msg div is shown.
56
+ # * body has a different class.
57
+ #
58
+ # Using the last of these options:
59
+ # return result_page.search('.catalog-product-view').empty?
60
+
61
+ return !result_page.search('.success-msg span').empty?
62
+ end
63
+
64
+ # Get the current carts contents
65
+ def get_cart_content
66
+ cart_page = @mech.get("#{@base_uri}/checkout/cart/")
67
+ name_links = cart_page.search('td h2 a')
68
+ names = name_links.map &:text
69
+ quantities_inputs = cart_page.search('.qty')
70
+ quantities = quantities_inputs.map {|n| n[:value]}
71
+ names.zip quantities
72
+ end
73
+
74
+ # Login with given credentials
75
+ def login_with username, password
76
+ login_page = @mech.get("#{@base_uri}/customer/account/login/")
77
+
78
+ form = login_page.form_with(:action => "#{@base_uri}/customer/account/loginPost/")
79
+ form.fields.find{|f| f.name == 'login[username]'}.value = username
80
+ form.fields.find{|f| f.name == 'login[password]'}.value = password
81
+ @mech.submit(form)
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module MagentoRemote
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'magento_remote/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "magento_remote"
8
+ spec.version = MagentoRemote::VERSION
9
+ spec.authors = ["Felix Wolfsteller"]
10
+ spec.email = ["felix.wolfsteller@gmail.com"]
11
+ spec.summary = %q{Login to a magento shop and do stuff.}
12
+ spec.description = %q{Allows ordering through a (defaultish) magento web page.}
13
+ spec.homepage = 'https://github.com/fwolfst/magento_remote'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'mechanize'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magento_remote
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Felix Wolfsteller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mechanize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Allows ordering through a (defaultish) magento web page.
70
+ email:
71
+ - felix.wolfsteller@gmail.com
72
+ executables:
73
+ - magento_add_to_cart
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - .rvmrc
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/magento_add_to_cart
85
+ - lib/magento_remote.rb
86
+ - lib/magento_remote/magento_mech.rb
87
+ - lib/magento_remote/version.rb
88
+ - magento_remote.gemspec
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/fwolfst/magento_remote
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Login to a magento shop and do stuff.
114
+ test_files:
115
+ - spec/spec_helper.rb