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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 450a1aaf20e266f101c4a2a0ef107f7d387fe8ae027d9e0751c067831292c078
4
- data.tar.gz: f9700bb931f9128db95c5b3dca904542877512de782e7d50eafd9edf66d9f484
3
+ metadata.gz: 727df9b49e09adb1dcf49a88cb1a6725ac499a2ff87d36934874f5c91a401fcb
4
+ data.tar.gz: a59a7aceeb508d199bd36b9876a339e8a22ccdc1ea26d9bf42338aa1d9fb9a82
5
5
  SHA512:
6
- metadata.gz: 91a4c5bf18c518c9604195363204955eeb8ede7e78c3c803ecf74db06e0bacd66fe04a99c34e09ceda21739e06e6780fc8da4d59ed0b97a8992ff2827260c34f
7
- data.tar.gz: c9d725e344e94ed1874b0ded891a97f59544aa27a54995a9bead0d64ca3c7cac7fa8f2f1ccee98b7ebda0d75538d1e9589f961cda459229740d0116a15116b19
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
- ```rb
6
- gem 'magento', '~> 0.0.2'
7
- ```
8
-
9
- or
10
-
11
- ```sh
12
- gem install 'magento'
13
- ```
14
-
15
- ### Setup
16
-
17
- ```rb
18
- Magento.url = 'https://yourstore.com'
19
- Magento.token = 'MAGENTO_API_KEY'
20
- ```
21
-
22
- ## Product
23
-
24
- ### Get product details by sku
25
-
26
- ```rb
27
- Magento::Product.find_by_sku('sku-test')
28
- ```
29
-
30
- # TODO
31
- ### Get product list
32
-
33
- Get all
34
- ```rb
35
- Magento::Product.all()
36
- ```
37
-
38
- Set page and quantity per page
39
- ```rb
40
- Magento::Product.all(page: 1, page_size: 25) # Default page size is 50
41
- ```
42
-
43
- Filter list by attribute
44
- ```rb
45
- Magento::Product.all(name_like: 'IPhone%')
46
-
47
- Magento::Product.all(price_gt: 100, page: 2)
48
- ```
49
-
50
- ### Search products
51
- ```rb
52
- Magento::Product.search('tshort')
53
- ```
54
-
55
- ## Order
56
-
57
- ### Create Order as admin user
58
-
59
- See the [documentation](https://magento.redoc.ly/2.4-admin/#operation/salesOrderRepositoryV1SavePost) to all attributes
60
-
61
- ```rb
62
- Magento::Order.create(
63
- customer_firstname: '',
64
- customer_lastname: '',
65
- customer_email: '',
66
- # others attrbutes ...,
67
- items: [
68
- {
69
- sku: '',
70
- price: '',
71
- qty_ordered: 1,
72
- # others attrbutes ...,
73
- }
74
- ],
75
- billing_address: {
76
- # attrbutes...
77
- },
78
- payment: {
79
- # attrbutes...
80
- },
81
- extension_attributes: {
82
- # attrbutes...
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
+ ```
@@ -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
- Dir[File.expand_path('magento/product/*.rb', __dir__)].map do |path|
11
- require path
12
- end
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
@@ -0,0 +1,10 @@
1
+ module Magento
2
+ class Country < Model
3
+ class << self
4
+ def find(code)
5
+ country_hash = Request.get("directory/countries/#{code}").parse
6
+ mapHash Country, country_hash
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class AvailableRegion
3
+ end
4
+ end
@@ -1,5 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Magento
2
- class NotFoundError < StandardError; end
3
- class UnauthorizedAccessError < StandardError; end
4
- class UnprocessedRequestError < StandardError; end
5
- end
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
@@ -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
@@ -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
@@ -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
- salva_requisicao(:get, url(resource))
10
- tratar_erro http_auth.get(url(resource))
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
- salva_requisicao(:put, url(resource), body)
15
- tratar_erro http_auth.put(url(resource), json: body)
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
- salva_requisicao(:post, url, body)
21
- tratar_erro http_auth.post(url, json: body)
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 tratar_erro(resposta)
72
+ def handle_error(resposta)
72
73
  unless resposta.status.success?
74
+ errors = []
73
75
  begin
74
- corpo = resposta.parse
76
+ msg = resposta.parse['message']
77
+ errors = resposta.parse['errors']
75
78
  rescue StandardError
76
- corpo = resposta.to_s
79
+ msg = resposta.to_s
77
80
  end
78
- erro = {
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::UnprocessedRequestError, erro.to_json
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 salva_requisicao(verbo, p_url, p_corpo = nil)
88
+ def save_request(method, url, body = nil)
91
89
  begin
92
- corpo = p_corpo[:product].reject { |e| e == :media_gallery_entries }
90
+ body = body[:product].reject { |e| e == :media_gallery_entries }
93
91
  rescue StandardError
94
- corpo = p_corpo
95
92
  end
96
93
 
97
- @requisicao = {
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
@@ -2,13 +2,13 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'magento'
5
- s.version = '0.0.3'
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 = ["Nick Quaranto"]
11
- s.email = 'nick@quaran.to'
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.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Nick Quaranto
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: nick@quaran.to
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