magento 0.0.1 → 0.0.2

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,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e1e1da5166c184efe64579ac0ecac8d00fd74563ff48afecbc18408e6fda549
4
- data.tar.gz: cab7c16786fb77d757d5700c797426a6e8fcf7894398457b3db288fa950d4915
3
+ metadata.gz: fef777d99770e8377c1a13bec28eafd1b75768ea5a16e0c819c704f35da6bd54
4
+ data.tar.gz: 8598bd0e18b6b2a86dd10dd7f9b28cce14338bc776943d28a4b8ac23da76aa28
5
5
  SHA512:
6
- metadata.gz: 30b5ee0b40741644264242596be14fc4fdc764f1cdcdd11b0b2ad3dba925b620fdb7544e5e7852e3ea7b35d01a28d6e00cd7eec4f2c5521a772889d050525828
7
- data.tar.gz: 0db9425021010206ed3c9c441d0b2f11ec621ce52ef0e884fe5f7c2873f1338fd63d387c285000f22f01ef59b34c805c768534add6ce56a6da309090cbffa9f8
6
+ metadata.gz: 71e8b86fb744f49d1fb9f3899ebe4235b257d54b11231eca25be7368be082120245c279653557be2e0715b4ab4afd1ad5d36afea4685792492299bd4116f7ca1
7
+ data.tar.gz: 045f97e42203bbe2ebc051472e4aae3c741b1ac160cc590cbbf421c66c3ff27ccc193294ff252f63d5cae205e78c13b99030bf085f87389fb9056ddfa60b856c
@@ -0,0 +1,26 @@
1
+ *.gem
2
+ *.rbc
3
+ .DS_Store
4
+ */.DS_Store
5
+ .bundle
6
+ .config
7
+ coverage
8
+ InstalledFiles
9
+ lib/bundler/man
10
+ pkg
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ tmp
16
+ *.swp
17
+ vimsession
18
+ Gemfile.lock
19
+
20
+ # YARD artifacts
21
+ .yardoc
22
+ _yardoc
23
+ doc/
24
+ test/vcr_cassettes
25
+
26
+ .vscode
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pagarme.gemspec
6
+ gemspec
@@ -0,0 +1,85 @@
1
+ # Magento Ruby library
2
+
3
+ ## Install
4
+
5
+ ```rb
6
+ gem 'magento', '~> 0.0.1'
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_sky('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
+ ```
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'time'
4
4
 
5
- require_relative 'magento/error'
5
+ require_relative 'magento/errors'
6
6
  require_relative 'magento/request'
7
7
  require_relative 'magento/model'
8
8
  require_relative 'magento/product'
@@ -0,0 +1,5 @@
1
+ module Magento
2
+ class NotFoundError < StandardError; end
3
+ class UnauthorizedAccessError < StandardError; end
4
+ class UnprocessedRequestError < StandardError; end
5
+ end
@@ -0,0 +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
@@ -0,0 +1,10 @@
1
+ module Magento
2
+ class Product < Model
3
+ class << self
4
+ def find_by_sky(sku)
5
+ product_hash = Request.get("products/#{sku}").parse
6
+ mapHash Product, product_hash
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magento
4
+ class CategoryLink
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class CustomAttribute
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class ExtensionAttribute
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class MediaGalleryEntry
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class Option
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class ProductLink
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class StockItem
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class TierPrice
3
+ end
4
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+ require 'uri'
3
+ require 'http'
4
+
5
+ module Magento
6
+ class Request
7
+ class << self
8
+ def get(resource)
9
+ salva_requisicao(:get, url(resource))
10
+ tratar_erro http_auth.get(url(resource))
11
+ end
12
+
13
+ def put(resource, body)
14
+ salva_requisicao(:put, url(resource), body)
15
+ tratar_erro http_auth.put(url(resource), json: body)
16
+ end
17
+
18
+ def post(resource, body = nil, url_completa = false)
19
+ url = url_completa ? resource : url(resource)
20
+ salva_requisicao(:post, url, body)
21
+ tratar_erro http_auth.post(url, json: body)
22
+ end
23
+
24
+ private
25
+
26
+ def http_auth
27
+ HTTP.auth("Bearer #{Magento.token}")
28
+ end
29
+
30
+ def base_url
31
+ url = Magento.url.to_s.sub(%r{/$}, '')
32
+ "#{url}/rest/all/V1"
33
+ end
34
+
35
+ def url(resource)
36
+ "#{base_url}/#{resource}"
37
+ end
38
+
39
+ def parametros_de_busca(field:, value:, conditionType: :eq)
40
+ criar_parametros(
41
+ filter_groups: {
42
+ '0': {
43
+ filters: {
44
+ '0': {
45
+ field: field,
46
+ conditionType: conditionType,
47
+ value: value
48
+ }
49
+ }
50
+ }
51
+ }
52
+ )
53
+ end
54
+
55
+ def parametros_de_campos(campos:)
56
+ criar_parametros(fields: campos)
57
+ end
58
+
59
+ def criar_parametros(filter_groups: nil, fields: nil, current_page: 1)
60
+ CGI.unescape(
61
+ {
62
+ searchCriteria: {
63
+ currentPage: current_page,
64
+ filterGroups: filter_groups
65
+ }.compact,
66
+ fields: fields
67
+ }.compact.to_query
68
+ )
69
+ end
70
+
71
+ def tratar_erro(resposta)
72
+ unless resposta.status.success?
73
+ begin
74
+ corpo = resposta.parse
75
+ rescue StandardError
76
+ corpo = resposta.to_s
77
+ end
78
+ erro = {
79
+ erro: 'Erro de requisição Magento',
80
+ resposta: { status: resposta.status, corpo: corpo },
81
+ requisicao: @requisicao
82
+ }
83
+
84
+ raise Magento::UnprocessedRequestError, erro.to_json
85
+ end
86
+
87
+ resposta
88
+ end
89
+
90
+ def salva_requisicao(verbo, p_url, p_corpo = nil)
91
+ begin
92
+ corpo = p_corpo[:product].reject { |e| e == :media_gallery_entries }
93
+ rescue StandardError
94
+ corpo = p_corpo
95
+ end
96
+
97
+ @requisicao = {
98
+ verbo: verbo,
99
+ url: p_url,
100
+ corpo: corpo
101
+ }
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'magento'
5
+ s.version = '0.0.2'
6
+ s.date = '2020-07-31'
7
+ s.summary = 'Magento Ruby library'
8
+ s.description = 'Magento Ruby library'
9
+ s.files = `git ls-files`.split($/)
10
+ s.authors = ["Nick Quaranto"]
11
+ s.email = 'nick@quaran.to'
12
+ s.homepage = 'https://github.com/WallasFaria/magento-ruby'
13
+ s.require_paths = ['lib']
14
+
15
+ s.add_dependency 'http', '~> 4.4'
16
+ s.add_dependency 'dry-inflector', '~> 0.2.0'
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magento
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Quaranto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-30 00:00:00.000000000 Z
11
+ date: 2020-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -44,7 +44,23 @@ executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
+ - ".gitignore"
48
+ - Gemfile
49
+ - README.md
47
50
  - lib/magento.rb
51
+ - lib/magento/errors.rb
52
+ - lib/magento/model.rb
53
+ - lib/magento/product.rb
54
+ - lib/magento/product/category_link.rb
55
+ - lib/magento/product/custom_attribute.rb
56
+ - lib/magento/product/extension_attribute.rb
57
+ - lib/magento/product/media_gallery_entry.rb
58
+ - lib/magento/product/option.rb
59
+ - lib/magento/product/product_link.rb
60
+ - lib/magento/product/stock_item.rb
61
+ - lib/magento/product/tier_price.rb
62
+ - lib/magento/request.rb
63
+ - magento.gemspec
48
64
  homepage: https://github.com/WallasFaria/magento-ruby
49
65
  licenses: []
50
66
  metadata: {}