magento 0.21.1 → 0.26.0

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: 15c67c62a1922aae194eab45cae0684451a565ff8d7897fd914e8f4c7f2b9934
4
- data.tar.gz: 0a75750bf4e0976d81b72d69398d05ca5e7d63ea3588fe161ff6dd46d4223f7f
3
+ metadata.gz: 05b964b40491a1dc85eab1440137ab38089116468c2bd1d4d3fc86cd63a2c106
4
+ data.tar.gz: 38f1348057912147922411e91494621e3fc8ad7047b6c275f5ce9be9d67b3eb7
5
5
  SHA512:
6
- metadata.gz: 02e95ff4336f3312a98acd8158decb1f66241f1183bb75de941f75c2cedaa7705ffcc732a9d8cb73d925cbd5f22a4404f42462bc8be46bfe86d219e880d45949
7
- data.tar.gz: 4f70b1965fcaeaf3f67bafee6a2a3f4d2f38296f11eb591f0e42302d796f0b4c810851facfd3d9c757d9fb97d1c4d4e996d653b6c46e4b8184205522560a655b
6
+ metadata.gz: ea2acea7866041af1c49d8d453fc52c483aa4d0d9e99bc3620447e50630f6e0aec0724a29536fc1f3e9081c88684b6f4a83a9c1b3c9ac6ef1e71fdf662e4478c
7
+ data.tar.gz: 69593620dfafef7a3e2290471afc6278e4de583cb3762b23a0c891d63680df3d694e2bdbfda05caff0651b907cd6648938e37ca7a7d6f2ce5cecbb8635e49da1
@@ -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/.gitignore CHANGED
@@ -7,7 +7,6 @@
7
7
  coverage
8
8
  InstalledFiles
9
9
  lib/bundler/man
10
- pkg
11
10
  rdoc
12
11
  spec/reports
13
12
  test/tmp
@@ -25,4 +24,7 @@ test/vcr_cassettes
25
24
 
26
25
  .vscode
27
26
  .byebug_history
28
- /manual_test
27
+ /manual_test
28
+
29
+ # rspec failure tracking
30
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile CHANGED
@@ -4,3 +4,7 @@ source "https://rubygems.org"
4
4
 
5
5
  # Specify your gem's dependencies in magento.gemspec
6
6
  gemspec
7
+
8
+ gem "rake", "~> 12.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem 'byebug', '~> 11.1', '>= 11.1.3'
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Add in your Gemfile
6
6
 
7
7
  ```rb
8
- gem 'magento', '~> 0.21.1'
8
+ gem 'magento', '~> 0.26.0'
9
9
  ```
10
10
 
11
11
  or run
@@ -643,6 +643,24 @@ Inventory.is_product_salable_for_requested_qty(
643
643
  }
644
644
  ```
645
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
+
646
664
  ___
647
665
 
648
666
  ##TODO:
@@ -662,3 +680,14 @@ Magento::Product.where(name_like: 'some name%').last
662
680
  ```
663
681
 
664
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.
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ Bundler::GemHelper.install_tasks name: 'magento'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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'
@@ -61,6 +61,10 @@ module Magento
61
61
  self.class.ship(id, params)
62
62
  end
63
63
 
64
+ def send_email
65
+ self.class.send_email(id)
66
+ end
67
+
64
68
  class << self
65
69
  def update(entity_id, attributes)
66
70
  attributes[:entity_id] = entity_id
@@ -172,6 +176,10 @@ module Magento
172
176
  def ship(order_id, shipment_params = nil)
173
177
  request.post("order/#{order_id}/ship", shipment_params).parse
174
178
  end
179
+
180
+ def send_email(order_id)
181
+ request.post("orders/#{order_id}/emails").parse
182
+ end
175
183
  end
176
184
  end
177
185
  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::Product.create(params.to_h)
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(
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magento
4
+ module Params
5
+ class CreateProductLink < Dry::Struct
6
+ LinkType = Type::String.enum(
7
+ 'related',
8
+ 'upsell',
9
+ 'crosssell',
10
+ 'associated'
11
+ )
12
+
13
+ attribute :link_type, LinkType
14
+ attribute :linked_product_sku, Type::String
15
+ attribute :linked_product_type, Magento::Params::CreateProduct::ProductTypes
16
+ attribute :position, Type::Integer
17
+ attribute :sku, Type::String
18
+
19
+ def to_h
20
+ {
21
+ link_type: link_type,
22
+ linked_product_sku: linked_product_sku,
23
+ linked_product_type: linked_product_type,
24
+ position: position,
25
+ sku: sku
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -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
@@ -25,7 +47,6 @@ module Magento
25
47
  self.class.remove_media(sku, media_id)
26
48
  end
27
49
 
28
- #
29
50
  # Add {price} on product {sku} for specified {customer_group_id}
30
51
  #
31
52
  # Param {quantity} is the minimun amount to apply the price
@@ -44,6 +65,29 @@ module Magento
44
65
  )
45
66
  end
46
67
 
68
+ #
69
+ # Remove tier price
70
+ #
71
+ # product = Magento::Product.find(1)
72
+ # product.remove_tier_price(quantity: 1, customer_group_id: :all)
73
+ #
74
+ # @return {Boolean}
75
+ def remove_tier_price(quantity:, customer_group_id: :all)
76
+ self.class.remove_tier_price(
77
+ sku, quantity: quantity, customer_group_id: customer_group_id
78
+ )
79
+ end
80
+
81
+ # Update product stock
82
+ #
83
+ # product = Magento::Product.find('sku')
84
+ # product.update_stock(qty: 12, is_in_stock: true)
85
+ #
86
+ # see all available attributes in: https://magento.redoc.ly/2.4.1-admin/tag/productsproductSkustockItemsitemId
87
+ def update_stock(attributes)
88
+ self.class.update_stock(sku, id, attributes)
89
+ end
90
+
47
91
  class << self
48
92
  alias_method :find_by_sku, :find
49
93
 
@@ -66,6 +110,37 @@ module Magento
66
110
  "products/#{sku}/group-prices/#{customer_group_id}/tiers/#{quantity}/price/#{price}"
67
111
  ).parse
68
112
  end
113
+
114
+ # Remove tier price
115
+ #
116
+ # Product.remove_tier_price('sku', quantity: 1, customer_group_id: :all)
117
+ #
118
+ # @return {Boolean}
119
+ def remove_tier_price(sku, quantity:, customer_group_id: :all)
120
+ request.delete(
121
+ "products/#{sku}/group-prices/#{customer_group_id}/tiers/#{quantity}"
122
+ ).parse
123
+ end
124
+
125
+ # Update product stock
126
+ #
127
+ # Magento::Product.update_stock(sku, id, {
128
+ # qty: 12,
129
+ # is_in_stock: true
130
+ # })
131
+ #
132
+ # see all available attributes in: https://magento.redoc.ly/2.4.1-admin/tag/productsproductSkustockItemsitemId
133
+ def update_stock(sku, id, attributes)
134
+ request.put("products/#{sku}/stockItems/#{id}", stockItem: attributes).parse
135
+ end
136
+
137
+ def create_links(sku, product_links)
138
+ request.post("products/#{sku}/links", { items: product_links })
139
+ end
140
+
141
+ def remove_link(sku, link_type:, linked_product_sku:)
142
+ request.delete("products/#{sku}/links/#{link_type}/#{linked_product_sku}")
143
+ end
69
144
  end
70
145
  end
71
146
  end
@@ -23,7 +23,7 @@ module Magento
23
23
  'to' # The end of a range. Must be used with from
24
24
  ].freeze
25
25
 
26
- def initialize(model, request: Request.new)
26
+ def initialize(model, request: Request.new, api_resource: nil)
27
27
  @model = model
28
28
  @request = request
29
29
  @filter_groups = nil
@@ -31,6 +31,7 @@ module Magento
31
31
  @page_size = 50
32
32
  @sort_orders = nil
33
33
  @fields = nil
34
+ @endpoint = api_resource || model.api_resource
34
35
  end
35
36
 
36
37
  def where(attributes)
@@ -131,7 +132,7 @@ module Magento
131
132
  attr_accessor :current_page, :filter_groups, :request, :sort_orders, :model, :fields
132
133
 
133
134
  def endpoint
134
- model.api_resource
135
+ @endpoint
135
136
  end
136
137
 
137
138
  def verify_id(field)
@@ -27,6 +27,10 @@ module Magento
27
27
  def generate_coupon(attributes)
28
28
  request.post('coupons/generate', attributes).parse
29
29
  end
30
+
31
+ protected def query
32
+ Query.new(self, api_resource: 'salesRules/search')
33
+ end
30
34
  end
31
35
  end
32
36
  end
@@ -1,4 +1,5 @@
1
1
  module Magento
2
2
  class CustomAttribute
3
+ include Magento::ModelParser
3
4
  end
4
5
  end
@@ -1,3 +1,3 @@
1
1
  module Magento
2
- VERSION = '0.21.1'
2
+ VERSION = '0.26.0'
3
3
  end
@@ -0,0 +1,38 @@
1
+ class RequestMock
2
+ attr_reader :path
3
+
4
+ def post(path)
5
+ @path = path
6
+ OpenStruct.new(success?: true, parse: true)
7
+ end
8
+ end
9
+
10
+ RSpec.describe Magento::Order do
11
+ before { Magento.url = 'https://site.com' }
12
+
13
+ describe '.send_email' do
14
+ it 'shuld request POST /orders/:id/emails' do
15
+ request = RequestMock.new
16
+ allow(Magento::Order).to receive(:request).and_return(request)
17
+
18
+ order_id = 25
19
+ result = Magento::Order.send_email(order_id)
20
+
21
+ expect(request.path).to eql("orders/#{order_id}/emails")
22
+ expect(result).to be true
23
+ end
24
+ end
25
+
26
+ describe '#send_email' do
27
+ it 'shuld request POST /orders/:id/emails' do
28
+ request = RequestMock.new
29
+ allow(Magento::Order).to receive(:request).and_return(request)
30
+
31
+ order = Magento::Order.build(id: 25)
32
+ result = order.send_email
33
+
34
+ expect(request.path).to eql("orders/25/emails")
35
+ expect(result).to be true
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.describe Magento do
2
+ it "has a version number" do
3
+ expect(Magento::VERSION).not_to be nil
4
+ end
5
+ end
@@ -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
@@ -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.21.1
4
+ version: 0.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wallas Faria
@@ -88,8 +88,12 @@ extra_rdoc_files: []
88
88
  files:
89
89
  - ".github/workflows/gem-push.yml"
90
90
  - ".gitignore"
91
+ - ".rspec"
91
92
  - Gemfile
92
93
  - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
93
97
  - lib/magento.rb
94
98
  - lib/magento/category.rb
95
99
  - lib/magento/configuration.rb
@@ -113,6 +117,7 @@ files:
113
117
  - lib/magento/params/create_custom_attribute.rb
114
118
  - lib/magento/params/create_image.rb
115
119
  - lib/magento/params/create_product.rb
120
+ - lib/magento/params/create_product_link.rb
116
121
  - lib/magento/polymorphic_model.rb
117
122
  - lib/magento/product.rb
118
123
  - lib/magento/query.rb
@@ -150,8 +155,11 @@ files:
150
155
  - lib/magento/shared/total.rb
151
156
  - lib/magento/shared/value.rb
152
157
  - lib/magento/version.rb
153
- - magento-ruby.gemspec
154
158
  - magento.gemspec
159
+ - spec/magento/order_spec.rb
160
+ - spec/magento_spec.rb
161
+ - spec/product_spec.rb
162
+ - spec/spec_helper.rb
155
163
  homepage: https://github.com/WallasFaria/magento-ruby
156
164
  licenses: []
157
165
  metadata: {}
@@ -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