magento 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +104 -85
- data/lib/magento.rb +30 -30
- data/lib/magento/country.rb +10 -0
- data/lib/magento/country/available_regions.rb +4 -0
- data/lib/magento/errors.rb +17 -4
- data/lib/magento/model.rb +43 -43
- data/lib/magento/product.rb +9 -9
- data/lib/magento/product/extension_attribute.rb +3 -3
- data/lib/magento/request.rb +17 -24
- data/magento.gemspec +3 -3
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 727df9b49e09adb1dcf49a88cb1a6725ac499a2ff87d36934874f5c91a401fcb
|
4
|
+
data.tar.gz: a59a7aceeb508d199bd36b9876a339e8a22ccdc1ea26d9bf42338aa1d9fb9a82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed4f5da0dc2ce6370803458b809759ca103593a8125d5c87f20258710df176ca75b573147972e8efe390585adb3fd9848be498bef835c82043e001def7cf3168
|
7
|
+
data.tar.gz: 69b1f401af9ff65d0228a8cf4cdf8fa2805637ffe5d71044c03cdf21a36c86e6f20b5a1f7dd86bbe376ebd7cb0aae942a59048e822d11b9e92e7c2b098d2540e
|
data/README.md
CHANGED
@@ -1,85 +1,104 @@
|
|
1
|
-
# Magento Ruby library
|
2
|
-
|
3
|
-
## Install
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
```
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
```
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
```
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
Get
|
34
|
-
|
35
|
-
|
36
|
-
```
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
```
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
Magento::Product.all(
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
```
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
1
|
+
# Magento Ruby library
|
2
|
+
|
3
|
+
## Install
|
4
|
+
|
5
|
+
Add in your Gemfile
|
6
|
+
|
7
|
+
```rb
|
8
|
+
gem 'magento', '~> 0.1.0'
|
9
|
+
```
|
10
|
+
|
11
|
+
or run
|
12
|
+
|
13
|
+
```sh
|
14
|
+
gem install magento
|
15
|
+
```
|
16
|
+
|
17
|
+
### Setup
|
18
|
+
|
19
|
+
```rb
|
20
|
+
Magento.url = 'https://dev.superbomemcasa.com.br'
|
21
|
+
Magento.token = 'pyppkwezopag85x4vd8y7eoiaixzo1z7'
|
22
|
+
```
|
23
|
+
|
24
|
+
## Product
|
25
|
+
|
26
|
+
### Get product details by sku
|
27
|
+
|
28
|
+
```rb
|
29
|
+
Magento::Product.find_by_sku('sku-test')
|
30
|
+
```
|
31
|
+
|
32
|
+
# TODO
|
33
|
+
### Get product list
|
34
|
+
|
35
|
+
Get all
|
36
|
+
```rb
|
37
|
+
Magento::Product.all()
|
38
|
+
```
|
39
|
+
|
40
|
+
Set page and quantity per page
|
41
|
+
```rb
|
42
|
+
Magento::Product.all(page: 1, page_size: 25) # Default page size is 50
|
43
|
+
```
|
44
|
+
|
45
|
+
Filter list by attribute
|
46
|
+
```rb
|
47
|
+
Magento::Product.all(name_like: 'IPhone%')
|
48
|
+
|
49
|
+
Magento::Product.all(price_gt: 100, page: 2)
|
50
|
+
```
|
51
|
+
|
52
|
+
### Search products
|
53
|
+
```rb
|
54
|
+
Magento::Product.search('tshort')
|
55
|
+
```
|
56
|
+
|
57
|
+
## Customer
|
58
|
+
|
59
|
+
### Get current customer
|
60
|
+
```rb
|
61
|
+
Magento.token = 'CUSTOMER_TOKEN'
|
62
|
+
|
63
|
+
Magento::Customer.me
|
64
|
+
```
|
65
|
+
|
66
|
+
### Get available regions for a country
|
67
|
+
|
68
|
+
```rb
|
69
|
+
country = Magento::Country.find('BR')
|
70
|
+
|
71
|
+
country.available_regions
|
72
|
+
```
|
73
|
+
|
74
|
+
## Order
|
75
|
+
|
76
|
+
### Create Order as admin user
|
77
|
+
|
78
|
+
See the [documentation](https://magento.redoc.ly/2.4-admin/#operation/salesOrderRepositoryV1SavePost) to all attributes
|
79
|
+
|
80
|
+
```rb
|
81
|
+
Magento::Order.create(
|
82
|
+
customer_firstname: '',
|
83
|
+
customer_lastname: '',
|
84
|
+
customer_email: '',
|
85
|
+
# others attrbutes ...,
|
86
|
+
items: [
|
87
|
+
{
|
88
|
+
sku: '',
|
89
|
+
price: '',
|
90
|
+
qty_ordered: 1,
|
91
|
+
# others attrbutes ...,
|
92
|
+
}
|
93
|
+
],
|
94
|
+
billing_address: {
|
95
|
+
# attrbutes...
|
96
|
+
},
|
97
|
+
payment: {
|
98
|
+
# attrbutes...
|
99
|
+
},
|
100
|
+
extension_attributes: {
|
101
|
+
# attrbutes...
|
102
|
+
}
|
103
|
+
)
|
104
|
+
```
|
data/lib/magento.rb
CHANGED
@@ -1,30 +1,30 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'time'
|
4
|
-
|
5
|
-
require_relative 'magento/errors'
|
6
|
-
require_relative 'magento/request'
|
7
|
-
require_relative 'magento/model'
|
8
|
-
require_relative 'magento/product'
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
module Magento
|
15
|
-
class << self
|
16
|
-
attr_accessor :url, :open_timeout, :timeout, :token
|
17
|
-
end
|
18
|
-
|
19
|
-
self.url = ENV['MAGENTO_URL']
|
20
|
-
self.open_timeout = 30
|
21
|
-
self.timeout = 90
|
22
|
-
self.token = ENV['MAGENTO_TOKEN']
|
23
|
-
|
24
|
-
def self.production?
|
25
|
-
ENV['RACK_ENV'] == 'production' ||
|
26
|
-
ENV['RAILS_ENV'] == 'production' ||
|
27
|
-
ENV['PRODUCTION'] ||
|
28
|
-
ENV['production']
|
29
|
-
end
|
30
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
require_relative 'magento/errors'
|
6
|
+
require_relative 'magento/request'
|
7
|
+
require_relative 'magento/model'
|
8
|
+
require_relative 'magento/product'
|
9
|
+
require_relative 'magento/country'
|
10
|
+
|
11
|
+
Dir[File.expand_path('magento/product/*.rb', __dir__)].map { |path| require path }
|
12
|
+
Dir[File.expand_path('magento/country/*.rb', __dir__)].map { |path| require path }
|
13
|
+
|
14
|
+
module Magento
|
15
|
+
class << self
|
16
|
+
attr_accessor :url, :open_timeout, :timeout, :token
|
17
|
+
end
|
18
|
+
|
19
|
+
self.url = ENV['MAGENTO_URL']
|
20
|
+
self.open_timeout = 30
|
21
|
+
self.timeout = 90
|
22
|
+
self.token = ENV['MAGENTO_TOKEN']
|
23
|
+
|
24
|
+
def self.production?
|
25
|
+
ENV['RACK_ENV'] == 'production' ||
|
26
|
+
ENV['RAILS_ENV'] == 'production' ||
|
27
|
+
ENV['PRODUCTION'] ||
|
28
|
+
ENV['production']
|
29
|
+
end
|
30
|
+
end
|
data/lib/magento/errors.rb
CHANGED
@@ -1,5 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Magento
|
2
|
-
class
|
3
|
-
|
4
|
-
|
5
|
-
|
4
|
+
class MagentoError < StandardError
|
5
|
+
attr_reader :status
|
6
|
+
attr_reader :errors
|
7
|
+
attr_reader :request
|
8
|
+
|
9
|
+
def initialize(msg = '', status = 400, errors = nil, request = nil)
|
10
|
+
@status = status
|
11
|
+
@errors = errors
|
12
|
+
@request = request
|
13
|
+
super(msg)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class NotFound < MagentoError; end
|
18
|
+
end
|
data/lib/magento/model.rb
CHANGED
@@ -1,43 +1,43 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/inflector'
|
4
|
-
|
5
|
-
module Magento
|
6
|
-
class Model
|
7
|
-
class << self
|
8
|
-
protected
|
9
|
-
|
10
|
-
def mapHash(klass, values)
|
11
|
-
object = klass.new
|
12
|
-
values.each do |key, value|
|
13
|
-
object.singleton_class.instance_eval { attr_accessor key }
|
14
|
-
if value.is_a?(Hash)
|
15
|
-
class_name = inflector.camelize(inflector.singularize(key))
|
16
|
-
value = mapHash(Object.const_get("Magento::#{class_name}"), value)
|
17
|
-
elsif value.is_a?(Array)
|
18
|
-
value = mapArray(key, value)
|
19
|
-
end
|
20
|
-
object.send("#{key}=", value)
|
21
|
-
end
|
22
|
-
object
|
23
|
-
end
|
24
|
-
|
25
|
-
def mapArray(key, values)
|
26
|
-
result = []
|
27
|
-
values.each do |value|
|
28
|
-
if value.is_a?(Hash)
|
29
|
-
class_name = inflector.camelize(inflector.singularize(key))
|
30
|
-
result << mapHash(Object.const_get("Magento::#{class_name}"), value)
|
31
|
-
else
|
32
|
-
result << value
|
33
|
-
end
|
34
|
-
end
|
35
|
-
result
|
36
|
-
end
|
37
|
-
|
38
|
-
def inflector
|
39
|
-
@inflector ||= Dry::Inflector.new
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dry/inflector'
|
4
|
+
|
5
|
+
module Magento
|
6
|
+
class Model
|
7
|
+
class << self
|
8
|
+
protected
|
9
|
+
|
10
|
+
def mapHash(klass, values)
|
11
|
+
object = klass.new
|
12
|
+
values.each do |key, value|
|
13
|
+
object.singleton_class.instance_eval { attr_accessor key }
|
14
|
+
if value.is_a?(Hash)
|
15
|
+
class_name = inflector.camelize(inflector.singularize(key))
|
16
|
+
value = mapHash(Object.const_get("Magento::#{class_name}"), value)
|
17
|
+
elsif value.is_a?(Array)
|
18
|
+
value = mapArray(key, value)
|
19
|
+
end
|
20
|
+
object.send("#{key}=", value)
|
21
|
+
end
|
22
|
+
object
|
23
|
+
end
|
24
|
+
|
25
|
+
def mapArray(key, values)
|
26
|
+
result = []
|
27
|
+
values.each do |value|
|
28
|
+
if value.is_a?(Hash)
|
29
|
+
class_name = inflector.camelize(inflector.singularize(key))
|
30
|
+
result << mapHash(Object.const_get("Magento::#{class_name}"), value)
|
31
|
+
else
|
32
|
+
result << value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
result
|
36
|
+
end
|
37
|
+
|
38
|
+
def inflector
|
39
|
+
@inflector ||= Dry::Inflector.new
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/magento/product.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
module Magento
|
2
|
-
class Product < Model
|
3
|
-
class << self
|
4
|
-
def find_by_sku(sku)
|
5
|
-
product_hash = Request.get("products/#{sku}").parse
|
6
|
-
mapHash Product, product_hash
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
1
|
+
module Magento
|
2
|
+
class Product < Model
|
3
|
+
class << self
|
4
|
+
def find_by_sku(sku)
|
5
|
+
product_hash = Request.get("products/#{sku}").parse
|
6
|
+
mapHash Product, product_hash
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
10
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class ExtensionAttribute
|
3
|
-
end
|
1
|
+
module Magento
|
2
|
+
class ExtensionAttribute
|
3
|
+
end
|
4
4
|
end
|
data/lib/magento/request.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'uri'
|
3
4
|
require 'http'
|
4
5
|
|
@@ -6,19 +7,19 @@ module Magento
|
|
6
7
|
class Request
|
7
8
|
class << self
|
8
9
|
def get(resource)
|
9
|
-
|
10
|
-
|
10
|
+
save_request(:get, url(resource))
|
11
|
+
handle_error http_auth.get(url(resource))
|
11
12
|
end
|
12
13
|
|
13
14
|
def put(resource, body)
|
14
|
-
|
15
|
-
|
15
|
+
save_request(:put, url(resource), body)
|
16
|
+
handle_error http_auth.put(url(resource), json: body)
|
16
17
|
end
|
17
18
|
|
18
19
|
def post(resource, body = nil, url_completa = false)
|
19
20
|
url = url_completa ? resource : url(resource)
|
20
|
-
|
21
|
-
|
21
|
+
save_request(:post, url, body)
|
22
|
+
handle_error http_auth.post(url, json: body)
|
22
23
|
end
|
23
24
|
|
24
25
|
private
|
@@ -68,37 +69,29 @@ module Magento
|
|
68
69
|
)
|
69
70
|
end
|
70
71
|
|
71
|
-
def
|
72
|
+
def handle_error(resposta)
|
72
73
|
unless resposta.status.success?
|
74
|
+
errors = []
|
73
75
|
begin
|
74
|
-
|
76
|
+
msg = resposta.parse['message']
|
77
|
+
errors = resposta.parse['errors']
|
75
78
|
rescue StandardError
|
76
|
-
|
79
|
+
msg = resposta.to_s
|
77
80
|
end
|
78
|
-
|
79
|
-
erro: 'Erro de requisição Magento',
|
80
|
-
resposta: { status: resposta.status, corpo: corpo },
|
81
|
-
requisicao: @requisicao
|
82
|
-
}
|
81
|
+
raise Magento::NotFound.new(msg, resposta.status.code, errors, @request) if resposta.status.not_found?
|
83
82
|
|
84
|
-
raise Magento::
|
83
|
+
raise Magento::MagentoError.new(msg, resposta.status.code, errors, @request)
|
85
84
|
end
|
86
|
-
|
87
85
|
resposta
|
88
86
|
end
|
89
87
|
|
90
|
-
def
|
88
|
+
def save_request(method, url, body = nil)
|
91
89
|
begin
|
92
|
-
|
90
|
+
body = body[:product].reject { |e| e == :media_gallery_entries }
|
93
91
|
rescue StandardError
|
94
|
-
corpo = p_corpo
|
95
92
|
end
|
96
93
|
|
97
|
-
@
|
98
|
-
verbo: verbo,
|
99
|
-
url: p_url,
|
100
|
-
corpo: corpo
|
101
|
-
}
|
94
|
+
@request = { method: method, url: url, body: body }
|
102
95
|
end
|
103
96
|
end
|
104
97
|
end
|
data/magento.gemspec
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'magento'
|
5
|
-
s.version = '0.0
|
5
|
+
s.version = '0.1.0'
|
6
6
|
s.date = '2020-07-31'
|
7
7
|
s.summary = 'Magento Ruby library'
|
8
8
|
s.description = 'Magento Ruby library'
|
9
9
|
s.files = `git ls-files`.split($/)
|
10
|
-
s.authors = ["
|
11
|
-
s.email = '
|
10
|
+
s.authors = ["Wallas Faria"]
|
11
|
+
s.email = 'wallasfaria@hotmail.com'
|
12
12
|
s.homepage = 'https://github.com/WallasFaria/magento-ruby'
|
13
13
|
s.require_paths = ['lib']
|
14
14
|
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magento
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Wallas Faria
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 0.2.0
|
41
41
|
description: Magento Ruby library
|
42
|
-
email:
|
42
|
+
email: wallasfaria@hotmail.com
|
43
43
|
executables: []
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
@@ -48,6 +48,8 @@ files:
|
|
48
48
|
- Gemfile
|
49
49
|
- README.md
|
50
50
|
- lib/magento.rb
|
51
|
+
- lib/magento/country.rb
|
52
|
+
- lib/magento/country/available_regions.rb
|
51
53
|
- lib/magento/errors.rb
|
52
54
|
- lib/magento/model.rb
|
53
55
|
- lib/magento/product.rb
|