magento 0.20.1 → 0.24.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/gem-push.yml +1 -0
- data/.rspec +3 -0
- data/.rspec_status +5 -0
- data/Gemfile +4 -0
- data/README.md +59 -1
- data/Rakefile +7 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/magento.rb +2 -0
- data/lib/magento/inventory.rb +40 -0
- data/lib/magento/params/create_product.rb +26 -26
- data/lib/magento/product.rb +68 -0
- data/lib/magento/shared/custom_attribute.rb +1 -0
- data/lib/magento/version.rb +1 -1
- data/spec/magento_spec.rb +5 -0
- data/spec/product_spec.rb +31 -0
- data/spec/spec_helper.rb +15 -0
- metadata +10 -2
- data/magento-ruby.gemspec +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cc3c4705cb31c6aab0ee3e39b0513bd9e21fa7f98fa6c0564e832a01c488b68
|
4
|
+
data.tar.gz: 20354ed49b5e998940775d8e3f07aea5ca0a0e2be5d482634c77528f3209e330
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e927a070c8b469cc0b3019ca27c9d74c51cb270b4165e3fb2343fd3c54d406d0d33507ad2aba9a53250d4c513e70eeb3c92adea579c0b2c775e23667e4fd1c8
|
7
|
+
data.tar.gz: f91fd2fcaca2777d599eac5eb665553adf5054eaa8a42593c2bad95226b953c43e14e1e7687e4c64c4351cc9f6e05fc35b3edd1e181fcd99976f481706e2d791
|
@@ -22,6 +22,7 @@ jobs:
|
|
22
22
|
touch $HOME/.gem/credentials
|
23
23
|
chmod 0600 $HOME/.gem/credentials
|
24
24
|
printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
25
|
+
sed "s/'magento'/'magento-ruby'/" magento.gemspec > magento-ruby.gemspec
|
25
26
|
gem build magento-ruby.gemspec
|
26
27
|
gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
|
27
28
|
env:
|
data/.rspec
ADDED
data/.rspec_status
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
example_id | status | run_time |
|
2
|
+
------------------------------- | ------ | --------------- |
|
3
|
+
./spec/magento_spec.rb[1:1] | passed | 0.00057 seconds |
|
4
|
+
./spec/product_spec.rb[1:1:1:1] | passed | 0.00102 seconds |
|
5
|
+
./spec/product_spec.rb[1:1:2:1] | passed | 0.00111 seconds |
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
Add in your Gemfile
|
6
6
|
|
7
7
|
```rb
|
8
|
-
gem 'magento', '~> 0.
|
8
|
+
gem 'magento', '~> 0.24.0'
|
9
9
|
```
|
10
10
|
|
11
11
|
or run
|
@@ -614,6 +614,53 @@ Magento::Product.where(name_like: 'some name%').count
|
|
614
614
|
>> 15
|
615
615
|
```
|
616
616
|
|
617
|
+
## Inventory
|
618
|
+
|
619
|
+
### Check whether a product is salable
|
620
|
+
|
621
|
+
```rb
|
622
|
+
Inventory.get_product_salable_quantity(sku: '4321', stock_id: 1)
|
623
|
+
>> 1
|
624
|
+
```
|
625
|
+
|
626
|
+
### Check whether a product is salable for a specified quantity
|
627
|
+
|
628
|
+
```rb
|
629
|
+
Inventory.is_product_salable_for_requested_qty(
|
630
|
+
sku: '4321',
|
631
|
+
stock_id: 1,
|
632
|
+
requested_qty: 2
|
633
|
+
)
|
634
|
+
>> OpenStruct {
|
635
|
+
:salable => false,
|
636
|
+
:errors => [
|
637
|
+
[0] {
|
638
|
+
"code" => "back_order-disabled",
|
639
|
+
"message" => "Backorders are disabled"
|
640
|
+
},
|
641
|
+
...
|
642
|
+
]
|
643
|
+
}
|
644
|
+
```
|
645
|
+
|
646
|
+
## Update product stock
|
647
|
+
|
648
|
+
```rb
|
649
|
+
product = Magento::Product.find('sku')
|
650
|
+
product.update_stock(qty: 12, is_in_stock: true)
|
651
|
+
```
|
652
|
+
|
653
|
+
or by class method
|
654
|
+
|
655
|
+
```rb
|
656
|
+
Magento::Product.update_stock(sku, id, {
|
657
|
+
qty: 12,
|
658
|
+
is_in_stock: true
|
659
|
+
})
|
660
|
+
```
|
661
|
+
|
662
|
+
see all available attributes in: https://magento.redoc.ly/2.4.1-admin/tag/productsproductSkustockItemsitemId
|
663
|
+
|
617
664
|
___
|
618
665
|
|
619
666
|
##TODO:
|
@@ -633,3 +680,14 @@ Magento::Product.where(name_like: 'some name%').last
|
|
633
680
|
```
|
634
681
|
|
635
682
|
### Tests
|
683
|
+
|
684
|
+
|
685
|
+
## Development
|
686
|
+
|
687
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
688
|
+
|
689
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
690
|
+
|
691
|
+
## Contributing
|
692
|
+
|
693
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/WallasFaria/nfce_crawler.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "magento"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/magento.rb
CHANGED
@@ -4,6 +4,7 @@ require 'time'
|
|
4
4
|
require 'dry/inflector'
|
5
5
|
require 'active_support/core_ext/string/inflections'
|
6
6
|
require 'active_support/core_ext/hash/keys'
|
7
|
+
require 'active_support/core_ext/module/delegation'
|
7
8
|
|
8
9
|
require_relative 'magento/configuration'
|
9
10
|
require_relative 'magento/errors'
|
@@ -22,6 +23,7 @@ require_relative 'magento/order'
|
|
22
23
|
require_relative 'magento/invoice'
|
23
24
|
require_relative 'magento/guest_cart'
|
24
25
|
require_relative 'magento/sales_rule'
|
26
|
+
require_relative 'magento/inventory'
|
25
27
|
require_relative 'magento/import'
|
26
28
|
|
27
29
|
Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Magento
|
2
|
+
class Inventory
|
3
|
+
class << self
|
4
|
+
#
|
5
|
+
# ==== Example
|
6
|
+
#
|
7
|
+
# Inventory.is_product_salable_for_requested_qty(
|
8
|
+
# sku: '4321',
|
9
|
+
# stock_id: 1,
|
10
|
+
# requested_qty: 2
|
11
|
+
# )
|
12
|
+
# # =>
|
13
|
+
# OpenStruct {
|
14
|
+
# :salable => false,
|
15
|
+
# :errors => [
|
16
|
+
# [0] {
|
17
|
+
# "code" => "back_order-disabled",
|
18
|
+
# "message" => "Backorders are disabled"
|
19
|
+
# },
|
20
|
+
# ...
|
21
|
+
# ]
|
22
|
+
# }
|
23
|
+
#
|
24
|
+
# @return OpenStruct
|
25
|
+
def is_product_salable_for_requested_qty(sku:, stock_id:, requested_qty:)
|
26
|
+
result = Request.new.get(
|
27
|
+
"inventory/is-product-salable-for-requested-qty/#{sku}/#{stock_id}/#{requested_qty}"
|
28
|
+
).parse
|
29
|
+
|
30
|
+
OpenStruct.new(result)
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_product_salable_quantity(sku:, stock_id:)
|
34
|
+
Request.new.get(
|
35
|
+
"inventory/get-product-salable-quantity/#{sku}/#{stock_id}"
|
36
|
+
).parse
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -4,33 +4,33 @@ require_relative 'create_custom_attribute'
|
|
4
4
|
module Magento
|
5
5
|
module Params
|
6
6
|
# Example
|
7
|
-
#
|
8
|
-
# params = Magento::Params::CreateProduct.new(
|
9
|
-
# sku: '556-teste-builder',
|
10
|
-
# name: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
|
11
|
-
# description: 'Descrição do produto',
|
12
|
-
# brand: 'Coca-Cola',
|
13
|
-
# price: 4.99,
|
14
|
-
# special_price: 3.49,
|
15
|
-
# quantity: 2,
|
16
|
-
# weight: 0.3,
|
17
|
-
# attribute_set_id: 4,
|
18
|
-
# images: [
|
19
|
-
# *Magento::Params::CreateImage.new(
|
20
|
-
# path: 'https://urltoimage.com/image.jpg',
|
21
|
-
# title: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
|
22
|
-
# position: 0,
|
23
|
-
# main: true
|
24
|
-
# ).variants, # it's generate all variants thumbnail => '100x100', small_image => '300x300' and image => '800x800'
|
25
|
-
# Magento::Params::CreateImage.new(
|
26
|
-
# path: '/path/to/image.jpg',
|
27
|
-
# title: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
|
28
|
-
# position: 1
|
29
|
-
# )
|
30
|
-
# ]
|
31
|
-
# )
|
32
7
|
#
|
33
|
-
# Magento::
|
8
|
+
# params = Magento::Params::CreateProduct.new(
|
9
|
+
# sku: '556-teste-builder',
|
10
|
+
# name: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
|
11
|
+
# description: 'Descrição do produto',
|
12
|
+
# brand: 'Coca-Cola',
|
13
|
+
# price: 4.99,
|
14
|
+
# special_price: 3.49,
|
15
|
+
# quantity: 2,
|
16
|
+
# weight: 0.3,
|
17
|
+
# attribute_set_id: 4,
|
18
|
+
# images: [
|
19
|
+
# *Magento::Params::CreateImage.new(
|
20
|
+
# path: 'https://urltoimage.com/image.jpg',
|
21
|
+
# title: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
|
22
|
+
# position: 0,
|
23
|
+
# main: true
|
24
|
+
# ).variants, # it's generate all variants thumbnail => '100x100', small_image => '300x300' and image => '800x800'
|
25
|
+
# Magento::Params::CreateImage.new(
|
26
|
+
# path: '/path/to/image.jpg',
|
27
|
+
# title: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
|
28
|
+
# position: 1
|
29
|
+
# )
|
30
|
+
# ]
|
31
|
+
# )
|
32
|
+
#
|
33
|
+
# Magento::Product.create(params.to_h)
|
34
34
|
#
|
35
35
|
class CreateProduct < Dry::Struct
|
36
36
|
ProductTypes = Type::String.default('simple'.freeze).enum(
|
data/lib/magento/product.rb
CHANGED
@@ -6,12 +6,34 @@ module Magento
|
|
6
6
|
attr(m) || super(m, *params, &block)
|
7
7
|
end
|
8
8
|
|
9
|
+
def stock
|
10
|
+
extension_attributes&.stock_item
|
11
|
+
end
|
12
|
+
|
13
|
+
def stock_quantity
|
14
|
+
stock&.qty
|
15
|
+
end
|
16
|
+
|
9
17
|
# returns custom_attribute value by custom_attribute code
|
10
18
|
# return nil if custom_attribute is not present
|
11
19
|
def attr(attribute_code)
|
12
20
|
@custom_attributes&.find { |a| a.attribute_code == attribute_code.to_s }&.value
|
13
21
|
end
|
14
22
|
|
23
|
+
def set_custom_attribute(code, value)
|
24
|
+
@custom_attributes ||= []
|
25
|
+
attribute = @custom_attributes.find { |a| a.attribute_code == code.to_s }
|
26
|
+
|
27
|
+
if attribute
|
28
|
+
attribute.value = value
|
29
|
+
else
|
30
|
+
@custom_attributes << Magento::CustomAttribute.build(
|
31
|
+
attribute_code: code.to_s,
|
32
|
+
value: value
|
33
|
+
)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
15
37
|
def respond_to?(attribute_code)
|
16
38
|
super || @custom_attributes&.any? { |a| a.attribute_code == attribute_code.to_s }
|
17
39
|
end
|
@@ -44,6 +66,29 @@ module Magento
|
|
44
66
|
)
|
45
67
|
end
|
46
68
|
|
69
|
+
#
|
70
|
+
# Remove tier price
|
71
|
+
#
|
72
|
+
# product = Magento::Product.find(1)
|
73
|
+
# product.remove_tier_price(quantity: 1, customer_group_id: :all)
|
74
|
+
#
|
75
|
+
# @return {Boolean}
|
76
|
+
def remove_tier_price(quantity:, customer_group_id: :all)
|
77
|
+
self.class.remove_tier_price(
|
78
|
+
sku, quantity: quantity, customer_group_id: customer_group_id
|
79
|
+
)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Update product stock
|
83
|
+
#
|
84
|
+
# product = Magento::Product.find('sku')
|
85
|
+
# product.update_stock(qty: 12, is_in_stock: true)
|
86
|
+
#
|
87
|
+
# see all available attributes in: https://magento.redoc.ly/2.4.1-admin/tag/productsproductSkustockItemsitemId
|
88
|
+
def update_stock(attributes)
|
89
|
+
self.class.update_stock(sku, id, attributes)
|
90
|
+
end
|
91
|
+
|
47
92
|
class << self
|
48
93
|
alias_method :find_by_sku, :find
|
49
94
|
|
@@ -66,6 +111,29 @@ module Magento
|
|
66
111
|
"products/#{sku}/group-prices/#{customer_group_id}/tiers/#{quantity}/price/#{price}"
|
67
112
|
).parse
|
68
113
|
end
|
114
|
+
|
115
|
+
# Remove tier price
|
116
|
+
#
|
117
|
+
# Product.remove_tier_price('sku', quantity: 1, customer_group_id: :all)
|
118
|
+
#
|
119
|
+
# @return {Boolean}
|
120
|
+
def remove_tier_price(sku, quantity:, customer_group_id: :all)
|
121
|
+
request.delete(
|
122
|
+
"products/#{sku}/group-prices/#{customer_group_id}/tiers/#{quantity}"
|
123
|
+
).parse
|
124
|
+
end
|
125
|
+
|
126
|
+
# Update product stock
|
127
|
+
#
|
128
|
+
# Magento::Product.update_stock(sku, id, {
|
129
|
+
# qty: 12,
|
130
|
+
# is_in_stock: true
|
131
|
+
# })
|
132
|
+
#
|
133
|
+
# see all available attributes in: https://magento.redoc.ly/2.4.1-admin/tag/productsproductSkustockItemsitemId
|
134
|
+
def update_stock(sku, id, attributes)
|
135
|
+
request.put("products/#{sku}/stockItems/#{id}", stockItem: attributes).parse
|
136
|
+
end
|
69
137
|
end
|
70
138
|
end
|
71
139
|
end
|
data/lib/magento/version.rb
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
RSpec.describe Magento::Product do
|
2
|
+
describe '#set_custom_attribute' do
|
3
|
+
let(:product) { Magento::Product.build(
|
4
|
+
sku: 25,
|
5
|
+
custom_attributes: [
|
6
|
+
{ attribute_code: 'description', value: 'Some description' }
|
7
|
+
]
|
8
|
+
) }
|
9
|
+
|
10
|
+
context 'when the custom attribute already exists' do
|
11
|
+
it 'must change the attribute value' do
|
12
|
+
expect(product.description).to eql('Some description')
|
13
|
+
product.set_custom_attribute(:description, 'description updated')
|
14
|
+
expect(product.attr(:description)).to eql('description updated')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when the custom attribute does not exists' do
|
19
|
+
it 'must add a new attribute' do
|
20
|
+
expect(product.attr(:new_attribute)).to be_nil
|
21
|
+
expect(product.attr(:other_new_attribute)).to be_nil
|
22
|
+
|
23
|
+
product.set_custom_attribute(:new_attribute, 'value')
|
24
|
+
product.set_custom_attribute('other_new_attribute', [1, 2])
|
25
|
+
|
26
|
+
expect(product.attr(:new_attribute)).to eql('value')
|
27
|
+
expect(product.attr(:other_new_attribute)).to eql([1, 2])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require 'byebug'
|
3
|
+
require "magento"
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
# Enable flags like --only-failures and --next-failure
|
7
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
8
|
+
|
9
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
10
|
+
config.disable_monkey_patching!
|
11
|
+
|
12
|
+
config.expect_with :rspec do |c|
|
13
|
+
c.syntax = :expect
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magento
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.24.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wallas Faria
|
@@ -88,8 +88,13 @@ extra_rdoc_files: []
|
|
88
88
|
files:
|
89
89
|
- ".github/workflows/gem-push.yml"
|
90
90
|
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rspec_status"
|
91
93
|
- Gemfile
|
92
94
|
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
93
98
|
- lib/magento.rb
|
94
99
|
- lib/magento/category.rb
|
95
100
|
- lib/magento/configuration.rb
|
@@ -103,6 +108,7 @@ files:
|
|
103
108
|
- lib/magento/import/image_finder.rb
|
104
109
|
- lib/magento/import/product.rb
|
105
110
|
- lib/magento/import/template/products.csv
|
111
|
+
- lib/magento/inventory.rb
|
106
112
|
- lib/magento/invoice.rb
|
107
113
|
- lib/magento/model.rb
|
108
114
|
- lib/magento/model_mapper.rb
|
@@ -149,8 +155,10 @@ files:
|
|
149
155
|
- lib/magento/shared/total.rb
|
150
156
|
- lib/magento/shared/value.rb
|
151
157
|
- lib/magento/version.rb
|
152
|
-
- magento-ruby.gemspec
|
153
158
|
- magento.gemspec
|
159
|
+
- spec/magento_spec.rb
|
160
|
+
- spec/product_spec.rb
|
161
|
+
- spec/spec_helper.rb
|
154
162
|
homepage: https://github.com/WallasFaria/magento-ruby
|
155
163
|
licenses: []
|
156
164
|
metadata: {}
|
data/magento-ruby.gemspec
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
lib = File.expand_path('lib', __dir__)
|
4
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
-
|
6
|
-
require 'magento/version'
|
7
|
-
|
8
|
-
Gem::Specification.new do |s|
|
9
|
-
s.name = 'magento-ruby'
|
10
|
-
s.version = Magento::VERSION
|
11
|
-
s.date = '2020-07-31'
|
12
|
-
s.summary = 'Magento Ruby library'
|
13
|
-
s.description = 'Magento Ruby library'
|
14
|
-
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
15
|
-
s.authors = ['Wallas Faria']
|
16
|
-
s.email = 'wallasfaria@hotmail.com'
|
17
|
-
s.homepage = 'https://github.com/WallasFaria/magento-ruby'
|
18
|
-
s.require_paths = ['lib']
|
19
|
-
|
20
|
-
s.add_dependency 'dry-inflector', '~> 0.2.0'
|
21
|
-
s.add_dependency 'http', '~> 4.4'
|
22
|
-
s.add_dependency 'dry-struct'
|
23
|
-
s.add_dependency 'activesupport'
|
24
|
-
s.add_dependency 'mini_magick'
|
25
|
-
end
|