magenthor 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0ff08ef8e893c262882d2d49a92e8a8a58e09e9
4
+ data.tar.gz: 8dcb51d57f9594665447bbad208fdb97fc0a433f
5
+ SHA512:
6
+ metadata.gz: f83acf1b1b252bec528ff5030d4e259a1fcb0fdb4611ca520f6a57ca68864259607ad2cab44a0f6459d3ae4b27230d7db7e233ad16d6e95ee4c6f99ad6eb98be
7
+ data.tar.gz: dc64072c5834e32d4415eeee4fcfb99a3897e2fcd92dcb03079169f1a1d7cf779922fb5ddf0db1786980bffe62fe87dfe87fb2f0305c3266713d501be9b2fd29
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.gem
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in magenthor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Daniele Lenares
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,65 @@
1
+ # Magenthor (ALPHA)
2
+
3
+ A Rubygem wrapper for the XMLRPC Magento API.
4
+
5
+ This code is inspired by, but not based on, [magentor gem](https://github.com/pstuteville/magentor)
6
+
7
+ ## Installation
8
+
9
+ Since this is still in an early stage of development, I discourage the installation except for debugging (or curiosity) purposes
10
+
11
+ ## Usage
12
+
13
+ Initialize the connection
14
+ ```ruby
15
+ Magenthor::Base.new({
16
+ :port => 80,
17
+ :host => 'magentohost.tld',
18
+ :api_user => 'apiuser',
19
+ :api_key => 'apikey'
20
+ })
21
+ ```
22
+ Get the list of customers
23
+ ```ruby
24
+ Magnethor::Customer.list
25
+ ```
26
+ Find a customer by ID
27
+ ```ruby
28
+ Magenthor::Customer.find 1
29
+ ```
30
+ Find customers by Magento attribute
31
+ ```ruby
32
+ Magenthor::Customer.find_by_email "email@me.tld"
33
+ Magenthor::Customer.find_by_group_id 2
34
+ #Magenthor::Customer.find_by_[magento customer attribute]
35
+ ```
36
+ Update a customer attributes
37
+ ```ruby
38
+ customer = Magenthor::Customer.find 1
39
+ customer.firstname = "John"
40
+ customer.update
41
+ #=> true
42
+ ```
43
+ Create a new customer
44
+ ```ruby
45
+ customer = Magenthor::Customer.new
46
+ customer.email = "email@me.tld"
47
+ customer.password = "p4ssw0rd"
48
+ customer.website_id = 1
49
+ customer.create
50
+ #=> true
51
+ ```
52
+ Delete a customer
53
+ ```ruby
54
+ customer = Magenthor::Customer.find 1
55
+ customer.delete
56
+ #=> true
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it ( https://github.com/Ryuk87/magenthor/fork )
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,65 @@
1
+ module Magenthor
2
+ class Base
3
+ @@client = nil
4
+ @@session_id = nil
5
+ @@api_user = nil
6
+ @@api_key = nil
7
+
8
+ #TODO: better description
9
+ #Intializer for setting up connection. Params required: host/port/api_user/api_key
10
+ def initialize params
11
+ if params.class != Hash
12
+ puts "Parameters Error!"
13
+ return false
14
+ end
15
+
16
+ @@api_user = params[:api_user]
17
+ @@api_key = params[:api_key]
18
+ url = "http://#{params[:host]}:#{params[:port]}/api/xmlrpc"
19
+
20
+ @@client = XMLRPC::Client.new2(url)
21
+ @@client.http_header_extra = { "accept-encoding" => "identity" }
22
+ end
23
+
24
+ private
25
+
26
+ #TODO: better description
27
+ def self.login
28
+ begin
29
+ @@session_id = @@client.call('login', @@api_user, @@api_key)
30
+ return true
31
+ rescue => e
32
+ if e.class == NoMethodError
33
+ puts 'You must first set the connection parameters using Magenthor::Base.new'
34
+ return false
35
+ end
36
+ end
37
+ end
38
+
39
+ #TODO: better description
40
+ def self.logout
41
+ response = @@client.call('endSession', @@session_id)
42
+ @@session_id = nil
43
+ end
44
+
45
+ #TODO: better description
46
+ def self.commit resource_path, params
47
+ if params.class == Hash
48
+ params = [params] #Magento wants an Array, always!
49
+ end
50
+
51
+ if login
52
+ begin
53
+ @@client.call('call', @@session_id, resource_path, params)
54
+ rescue => e
55
+ if e.class == XMLRPC::FaultException
56
+ puts "Magento says: #{e.message}"
57
+ end
58
+ return false
59
+ ensure
60
+ logout
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,143 @@
1
+ module Magenthor
2
+ class Customer < Base
3
+
4
+ attr_accessor :firstname, :lastname, :middlename, :increment_id, :store_id,
5
+ :website_id, :created_in, :email, :group_id, :prefix, :suffix, :dob,
6
+ :taxvat, :confirmation, :gender, :password
7
+ attr_reader :customer_id, :increment_id, :created_at, :updated_at, :password_hash
8
+
9
+ private
10
+ attr_writer :customer_id, :increment_id, :created_at, :updated_at, :password_hash
11
+
12
+ public
13
+
14
+ #TODO: better description
15
+ #Initialize a new customer entity
16
+ def initialize params = {}
17
+ methods.grep(/\w=$/).each do |m|
18
+ send(m, nil)
19
+ end
20
+ params.each do |k, v|
21
+ send("#{k}=", v) if respond_to? "#{k}="
22
+ end
23
+ self.customer_id = params["customer_id"]
24
+ self.increment_id = params["increment_id"]
25
+ self.created_at = params["created_at"]
26
+ self.updated_at = params["updated_at"]
27
+ self.password_hash = params["password_hash"]
28
+
29
+ end
30
+
31
+ #TODO: better description
32
+ #Update an existing customer
33
+ def update
34
+ attributes = {}
35
+ methods.grep(/\w=$/).each do |m|
36
+ attributes[m.to_s.gsub('=','')] = send(m.to_s.gsub('=',''))
37
+ end
38
+ self.class.commit('customer.update', [self.customer_id, attributes])
39
+ end
40
+
41
+ #TODO: better description
42
+ #Create a new customer
43
+ def create
44
+ attributes = {}
45
+ methods.grep(/\w=$/).each do |m|
46
+ attributes[m.to_s.gsub('=','')] = send(m.to_s.gsub('=',''))
47
+ end
48
+ response = self.class.commit('customer.create', [attributes])
49
+ return false if response == false
50
+
51
+ obj = self.class.find(response)
52
+ methods.grep(/\w=$/).each do |m|
53
+ send(m, obj.send(m.to_s.gsub('=','')))
54
+ end
55
+ self.customer_id = obj.customer_id
56
+ self.increment_id = obj.increment_id
57
+ self.created_at = obj.created_at
58
+ self.updated_at = obj.updated_at
59
+ self.password_hash = obj.password_hash
60
+
61
+ return true
62
+ end
63
+
64
+ #TODO: better description
65
+ #Delete an existing customer
66
+ def delete
67
+ response = self.class.commit('customer.delete', [self.customer_id])
68
+ return false if response == false
69
+
70
+ methods.grep(/\w=$/).each do |m|
71
+ send(m, nil)
72
+ end
73
+ self.customer_id = nil
74
+ self.increment_id = nil
75
+ self.created_at = nil
76
+ self.updated_at = nil
77
+ self.password_hash = nil
78
+
79
+ return true
80
+ end
81
+
82
+ class << self
83
+ #TODO: better description
84
+ #List al customers with all info
85
+ def list filters = []
86
+ response = commit('customer.list', filters)
87
+ customers = []
88
+ response.each do |r|
89
+ customers << find(r["customer_id"])
90
+ end
91
+ return customers
92
+ end
93
+
94
+ #TODO: better description
95
+ #Find a customer by id
96
+ def find customer_id
97
+ response = commit('customer.info', [customer_id])
98
+ new(response) unless response == false
99
+ end
100
+
101
+ #TODO: better description
102
+ #Dynamic methods to find customers based on Magento attributes
103
+ customer_attributes = [
104
+ "increment_id",
105
+ "created_in",
106
+ "store_id",
107
+ "website_id",
108
+ "email",
109
+ "firstname",
110
+ "middlename",
111
+ "lastname",
112
+ "group_id",
113
+ "prefix",
114
+ "suffix",
115
+ "dob",
116
+ "taxvat",
117
+ "confirmation"]
118
+ customer_attributes.each do |a|
119
+ define_method("find_by_#{a}") do |arg|
120
+ find_by a, arg
121
+ end
122
+ end
123
+
124
+
125
+ private
126
+
127
+ #TODO: better description
128
+ #Method to find customers based on a specific Magento attribute
129
+ def find_by (attribute, value)
130
+ response = commit('customer.list', [attribute => value])
131
+ if response.count > 1
132
+ customers = []
133
+ response.each do |r|
134
+ customers << find(r["customer_id"])
135
+ end
136
+ return customers
137
+ else
138
+ return new(response[0])
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,3 @@
1
+ module Magenthor
2
+ VERSION = "0.0.1"
3
+ end
data/lib/magenthor.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "magenthor/version"
2
+ require "magenthor/base"
3
+ require "xmlrpc/client"
4
+
5
+ XMLRPC::Config.send(:remove_const, :ENABLE_NIL_PARSER)
6
+ XMLRPC::Config.send(:const_set, :ENABLE_NIL_PARSER, true)
7
+ XMLRPC::Config.send(:remove_const, :ENABLE_NIL_CREATE)
8
+ XMLRPC::Config.send(:const_set, :ENABLE_NIL_CREATE, true)
9
+
10
+ module Magenthor
11
+ autoload :Customer, 'magenthor/customer'
12
+ end
data/magenthor.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'magenthor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "magenthor"
8
+ spec.version = Magenthor::VERSION
9
+ spec.authors = ["Daniele Lenares"]
10
+ spec.email = ["daniele.lenares@gmail.com"]
11
+ spec.summary = %q{A Rubygem wrapper for the XMLRPC Magento API.}
12
+ spec.description = %q{A Rubygem wrapper for the XMLRPC Magento API.}
13
+ spec.homepage = "https://github.com/Ryuk87/magenthor"
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_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "pry"
24
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magenthor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniele Lenares
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
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
+ description: A Rubygem wrapper for the XMLRPC Magento API.
56
+ email:
57
+ - daniele.lenares@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/magenthor.rb
68
+ - lib/magenthor/base.rb
69
+ - lib/magenthor/customer.rb
70
+ - lib/magenthor/version.rb
71
+ - magenthor.gemspec
72
+ homepage: https://github.com/Ryuk87/magenthor
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.14
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: A Rubygem wrapper for the XMLRPC Magento API.
96
+ test_files: []