magento 0.5.1 → 0.7.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: 4034005945e350699f74bfd858941a2e0295e2f8e3b6fd1d19c605d68da703fe
4
- data.tar.gz: 8d6dbbe72502d09f6ea1f437bb52151c767e28ff1ec3e2ab697594b8e59e836e
3
+ metadata.gz: 9c96180295e2e45d66ac2ea4aa956ec516c3c5045d81db431538cd5462b5efcc
4
+ data.tar.gz: b2d13f614c5af4b956e97b87c7e2476d1a40fd1ada11cb94270698630d44f9b2
5
5
  SHA512:
6
- metadata.gz: 84b758c7a69c84f75dc877475d24a61518e609339b3bac0d93a3561241d5a7bb6214d2e909923100ed44626bd1eeead86370af7dc03a6c43c5d5c8bcb688d011
7
- data.tar.gz: ea6359f11056d8defd7fe4b9d8b1a27c0d45e441a56e9f72ca0cd042b52d87d4c6832c3d14b5c6c1bc7ee9401b623ae859dd22dd503f1b209fccdc5f977c0745
6
+ metadata.gz: 512781570485d5af44f8567b31c4988d096682f0e3fe6119803c18385842cecbfc28bdb2a16fb02091d0000cbf2cbae2b5174cf3b0489d398450957c76377069
7
+ data.tar.gz: 51dd50c0b4a2542e6467dff88553eb5a0542f7efff10de0430a6fb3bdba32e5e8232bf4ef4e5f07b886eeebf6835aad8127791c3d84ddc89632db6ed73d35c0d
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Add in your Gemfile
6
6
 
7
7
  ```rb
8
- gem 'magento', '~> 0.5.1'
8
+ gem 'magento', '~> 0.7.0'
9
9
  ```
10
10
 
11
11
  or run
@@ -20,6 +20,10 @@ gem install magento
20
20
  Magento.url = 'https://yourstore.com'
21
21
  Magento.token = 'MAGENTO_API_KEY'
22
22
  Magento.store = :default # optional, Default is :all
23
+
24
+ Magento.with_config(store: :other_store) do # accepts store, url and token parameters
25
+ Magento::Product.find('sku')
26
+ end
23
27
  ```
24
28
 
25
29
  ## Models
@@ -151,6 +155,12 @@ products.size
151
155
  products.current_page
152
156
  >> 1
153
157
 
158
+ products.next_page
159
+ >> 2
160
+
161
+ products.last_page?
162
+ >> false
163
+
154
164
  products.page_size
155
165
  >> 5
156
166
 
@@ -166,6 +176,8 @@ All Methods:
166
176
  ```rb
167
177
  # Information about search criteria
168
178
  :current_page
179
+ :next_page
180
+ :last_page?
169
181
  :page_size
170
182
  :total_count
171
183
  :filter_groups
@@ -276,6 +288,129 @@ cart.payment_information(
276
288
  >> "234575" # return the order id
277
289
  ```
278
290
 
291
+ ## Invoice an Order
292
+
293
+ ```rb
294
+ Magento::Order.invoice(order_id)
295
+ >> 25 # return incoice id
296
+
297
+ # or from instance
298
+
299
+ order = Magento::Order.find(order_id)
300
+
301
+ invoice_id = order.invoice
302
+
303
+ # you can pass parameters too
304
+
305
+ invoice_id = order.invoice(
306
+ capture: false,
307
+ appendComment: true,
308
+ items: [{ order_item_id: 123, qty: 1 }], # pass items to partial invoice
309
+ comment: {
310
+ extension_attributes: { },
311
+ comment: "string",
312
+ is_visible_on_front: 0
313
+ },
314
+ notify: true
315
+ )
316
+ ```
317
+
318
+ [Complete Invoice Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdinvoice#operation/salesInvoiceOrderV1ExecutePost)
319
+
320
+ ## Create refund for invoice
321
+
322
+ ```rb
323
+ Magento::Invoice.invoice(refund_id)
324
+ >> 12 # return refund id
325
+
326
+ # or from instance
327
+
328
+ invoice = Magento::Invoice.find(invoice_id)
329
+
330
+ refund_id = invoice.refund
331
+
332
+ # you can pass parameters too
333
+
334
+ invoice.refund(
335
+ items: [
336
+ {
337
+ extension_attributes: {},
338
+ order_item_id: 0,
339
+ qty: 0
340
+ }
341
+ ],
342
+ isOnline: true,
343
+ notify: true,
344
+ appendComment: true,
345
+ comment: {
346
+ extension_attributes: {},
347
+ comment: string,
348
+ is_visible_on_front: 0
349
+ },
350
+ arguments: {
351
+ shipping_amount: 0,
352
+ adjustment_positive: 0,
353
+ adjustment_negative: 0,
354
+ extension_attributes: {
355
+ return_to_stock_items: [0]
356
+ }
357
+ }
358
+ )
359
+ ```
360
+
361
+ [Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundInvoiceV1ExecutePost)
362
+
363
+
364
+ ## Other Invoice methods
365
+
366
+ ```rb
367
+ invoice = Magento::Invoice.find(invoice_id)
368
+
369
+ invoice.capture # or
370
+ Magento::Invoice.capture(invoice_id)
371
+
372
+ invoice.void # or
373
+ Magento::Invoice.void(invoice_id)
374
+
375
+ invoice.send_email # or
376
+ Magento::Invoice.send_email(invoice_id)
377
+
378
+ Magento::Invoice.comments(invoice_id).all
379
+ Magento::Invoice.comments(invoice_id).where(created_at_gt: Date.today.prev_day).all
380
+ ```
381
+
382
+ ## Creates new Shipment for given Order.
383
+
384
+ ```rb
385
+ Magento::Order.ship(order_id)
386
+ >> 25 # return shipment id
387
+
388
+ # or from instance
389
+
390
+ order = Magento::Order.find(order_id)
391
+
392
+ order.ship
393
+
394
+ # you can pass parameters too
395
+
396
+ order.ship(
397
+ capture: false,
398
+ appendComment: true,
399
+ items: [{ order_item_id: 123, qty: 1 }], # pass items to partial shipment
400
+ tracks: [
401
+ {
402
+ extension_attributes: { },
403
+ track_number: "string",
404
+ title: "string",
405
+ carrier_code: "string"
406
+ }
407
+ ]
408
+ notify: true
409
+ )
410
+ ```
411
+
412
+ [Complete Shipment Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdship#operation/salesShipOrderV1ExecutePost)
413
+
279
414
  ___
280
415
 
281
416
  ##TODO:
@@ -284,3 +419,31 @@ ___
284
419
  ```rb
285
420
  Magento::Product.search('tshort')
286
421
  ```
422
+
423
+ ### First result
424
+ ```rb
425
+ Magento::Product.first
426
+ >> <Magento::Product @sku="some-sku" ...>
427
+
428
+ Magento::Product.where(name_like: 'some name%').first
429
+ >> <Magento::Product @sku="some-sku" ...>
430
+ ```
431
+
432
+ ### Last result
433
+ ```rb
434
+ Magento::Product.last
435
+ >> <Magento::Product @sku="some-sku" ...>
436
+
437
+ Magento::Product.where(name_like: 'some name%').last
438
+ >> <Magento::Product @sku="some-sku" ...>
439
+ ```
440
+
441
+ ### Count result
442
+ ```rb
443
+ Magento::Product.count
444
+ >> 7855
445
+ Magento::Product.where(name_like: 'some name%').count
446
+ >> 15
447
+ ```
448
+
449
+ ### Tests
@@ -0,0 +1,20 @@
1
+ require './lib/magento'
2
+
3
+ Magento.url = 'https://maniadagua.com'
4
+ Magento.token = 'f44251o3xjijz8ou78hoyh8a06kdtkmh'
5
+
6
+ page = 245
7
+
8
+ loop do
9
+ products = Magento::Product.page(page).per(5).all
10
+
11
+ products.each do |product|
12
+ Magento::Product.update(product.sku, extension_attributes: { website_ids: [1, 2] })
13
+ puts "update page #{page}, product: #{product.name}"
14
+ rescue => e
15
+ puts "ERRO ao cadastrar: #{product.name}, #{e.message}"
16
+ end
17
+
18
+ break if products.last_page?
19
+ page = products.next_page
20
+ end
@@ -6,6 +6,7 @@ require 'dry/inflector'
6
6
  require_relative 'magento/errors'
7
7
  require_relative 'magento/request'
8
8
  require_relative 'magento/model_mapper'
9
+ require_relative 'magento/polymorphic_model'
9
10
  require_relative 'magento/model'
10
11
  require_relative 'magento/record_collection'
11
12
  require_relative 'magento/query'
@@ -14,6 +15,7 @@ require_relative 'magento/product'
14
15
  require_relative 'magento/country'
15
16
  require_relative 'magento/customer'
16
17
  require_relative 'magento/order'
18
+ require_relative 'magento/invoice'
17
19
  require_relative 'magento/guest_cart'
18
20
 
19
21
  Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
@@ -35,6 +37,22 @@ module Magento
35
37
  self.token = ENV['MAGENTO_TOKEN']
36
38
  self.store = ENV['MAGENTO_STORE'] || :all
37
39
 
40
+ def self.with_config(utl: Magento.url, token: Magento.token, store: Magento.store)
41
+ @old_url = self.url
42
+ @old_token = self.token
43
+ @old_store = self.store
44
+
45
+ self.url = utl
46
+ self.token = token
47
+ self.store = store
48
+
49
+ yield
50
+ ensure
51
+ self.url = @old_url
52
+ self.token = @old_token
53
+ self.store = @old_store
54
+ end
55
+
38
56
  def self.production?
39
57
  ENV['RACK_ENV'] == 'production' ||
40
58
  ENV['RAILS_ENV'] == 'production' ||
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magento
4
+ class Invoice < Model
5
+ self.primary_key = :entity_id
6
+ self.entity_key = :entity
7
+
8
+ #
9
+ # Sets invoice capture.
10
+ def capture
11
+ self.class.capture(id)
12
+ end
13
+
14
+ #
15
+ # Voids a specified invoice.
16
+ #
17
+ # @return {Boolean}
18
+ def void
19
+ self.class.void(id)
20
+ end
21
+
22
+ #
23
+ # Emails a user a specified invoice.
24
+ #
25
+ # @return {Boolean}
26
+ def send_email
27
+ self.class.send_email(id)
28
+ end
29
+
30
+ #
31
+ # Create refund for invoice
32
+ #
33
+ # invoice = Magento::Invoice.find(invoice_id)
34
+ #
35
+ # invoice.refund # or you can pass parameters
36
+ # invoice.invoice(notify: false) # See the refund class method for more information
37
+ #
38
+ # @return {Integer} return the refund id
39
+ def refund(refund_params = nil)
40
+ self.class.refund(id, refund_params)
41
+ end
42
+
43
+ class << self
44
+ def save
45
+ raise NotImplementedError
46
+ end
47
+
48
+ def update(_attributes)
49
+ raise NotImplementedError
50
+ end
51
+
52
+ def create(_attributes)
53
+ raise NotImplementedError
54
+ end
55
+
56
+ #
57
+ # Sets invoice capture.
58
+ def capture(invoice_id)
59
+ request.post("invoices/#{invoice_id}/capture").parse
60
+ end
61
+
62
+ #
63
+ # Voids a specified invoice.
64
+ #
65
+ # @return {Boolean}
66
+ def void(invoice_id)
67
+ request.post("invoices/#{invoice_id}/avoid").parse
68
+ end
69
+
70
+ #
71
+ # Emails a user a specified invoice.
72
+ #
73
+ # @return {Boolean}
74
+ def send_email(invoice_id)
75
+ request.post("invoices/#{invoice_id}/emails").parse
76
+ end
77
+
78
+ #
79
+ # Lists comments for a specified invoice.
80
+ #
81
+ # Magento::Invoice.comments(invoice_id).all
82
+ # Magento::Invoice.comments(invoice_id).where(created_at_gt: Date.today.prev_day).all
83
+ def comments(invoice_id)
84
+ api_resource = "invoices/#{invoice_id}/comments"
85
+ Query.new(PolymorphicModel.new(Comment, api_resource))
86
+ end
87
+
88
+ #
89
+ # Create refund for invoice
90
+ #
91
+ # Magento::Invoice.refund(invoice_id)
92
+ #
93
+ # or
94
+ #
95
+ # Magento::Order.refund(
96
+ # invoice_id,
97
+ # items: [
98
+ # {
99
+ # extension_attributes: {},
100
+ # order_item_id: 0,
101
+ # qty: 0
102
+ # }
103
+ # ],
104
+ # isOnline: true,
105
+ # notify: true,
106
+ # appendComment: true,
107
+ # comment: {
108
+ # extension_attributes: {},
109
+ # comment: string,
110
+ # is_visible_on_front: 0
111
+ # },
112
+ # arguments: {
113
+ # shipping_amount: 0,
114
+ # adjustment_positive: 0,
115
+ # adjustment_negative: 0,
116
+ # extension_attributes: {
117
+ # return_to_stock_items: [
118
+ # 0
119
+ # ]
120
+ # }
121
+ # }
122
+ # )
123
+ #
124
+ # to complete [documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundInvoiceV1ExecutePost)
125
+ #
126
+ # @return {Integer} return the refund id
127
+ def refund(invoice_id, refund_params=nil)
128
+ request.post("invoice/#{order_id}/refund", invoice_params).parse
129
+ end
130
+ end
131
+ end
132
+ end
@@ -77,7 +77,7 @@ module Magento
77
77
  end
78
78
 
79
79
  def request
80
- @request ||= Request.new
80
+ Request.new
81
81
  end
82
82
  end
83
83
  end
@@ -1,6 +1,109 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Magento
2
4
  class Order < Model
3
5
  self.primary_key = :entity_id
4
6
  self.entity_key = :entity
7
+
8
+ def save
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def update(attrs)
13
+ raise "'entity_id' not found" if @entity_id.nil?
14
+
15
+ attrs[:entity_id] = @entity_id
16
+ self.class.update(attrs)
17
+ end
18
+
19
+ #
20
+ # Invoice current order
21
+ #
22
+ # order = Magento::Order.find(order_id)
23
+ #
24
+ # order.invoice # or you can pass parameters
25
+ # order.invoice(capture: false) # See the invoice class method for more information
26
+ #
27
+ # @return String: return the invoice id
28
+ def invoice(params=nil)
29
+ self.class.invoice(id, params)
30
+ end
31
+
32
+ #
33
+ # Creates new Shipment for given Order.
34
+ #
35
+ # order = Magento::Order.find(order_id)
36
+ #
37
+ # order.ship # or you can pass parameters
38
+ # order.ship(notify: false) # See the shipment class method for more information
39
+ #
40
+ # Return the shipment id
41
+ def ship(params=nil)
42
+ self.class.ship(id, params)
43
+ end
44
+
45
+ class << self
46
+ def update(attributes)
47
+ hash = request.put('orders/create', { entity_key => attributes }).parse
48
+ build(hash)
49
+ end
50
+
51
+ #
52
+ # Invoice an order
53
+ #
54
+ # Magento::Order.invoice(order_id)
55
+ #
56
+ # or
57
+ #
58
+ # Magento::Order.invoice(
59
+ # order_id,
60
+ # capture: false,
61
+ # appendComment: true,
62
+ # items: [{ order_item_id: 123, qty: 1 }], # pass items to partial invoice
63
+ # comment: {
64
+ # extension_attributes: { },
65
+ # comment: "string",
66
+ # is_visible_on_front: 0
67
+ # },
68
+ # notify: true
69
+ # )
70
+ #
71
+ # to complete [documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdinvoice#operation/salesInvoiceOrderV1ExecutePost)
72
+ #
73
+ # @return String: return the invoice id
74
+ def invoice(order_id, invoice_params=nil)
75
+ request.post("order/#{order_id}/invoice", invoice_params).parse
76
+ end
77
+
78
+ #
79
+ # Creates new Shipment for given Order.
80
+ #
81
+ # Magento::Order.ship(order_id)
82
+ #
83
+ # or
84
+ #
85
+ # Magento::Order.ship(
86
+ # order_id,
87
+ # capture: false,
88
+ # appendComment: true,
89
+ # items: [{ order_item_id: 123, qty: 1 }], # pass items to partial shipment
90
+ # tracks: [
91
+ # {
92
+ # extension_attributes: { },
93
+ # track_number: "string",
94
+ # title: "string",
95
+ # carrier_code: "string"
96
+ # }
97
+ # ]
98
+ # notify: true
99
+ # )
100
+ #
101
+ # to complete [documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdship#operation/salesShipOrderV1ExecutePost)
102
+ #
103
+ # @return {String}: return the shipment id
104
+ def ship(order_id, shipment_params = nil)
105
+ request.post("order/#{order_id}/ship", shipment_params).parse
106
+ end
107
+ end
5
108
  end
6
- end
109
+ end
@@ -0,0 +1,18 @@
1
+ module Magento
2
+ class PolymorphicModel
3
+ attr_reader :api_resource
4
+
5
+ def initialize(model, api_resource)
6
+ @model = model
7
+ @api_resource = api_resource
8
+ end
9
+
10
+ def new
11
+ @model.new
12
+ end
13
+
14
+ def build(attributes)
15
+ @model.build(attributes)
16
+ end
17
+ end
18
+ end
@@ -23,6 +23,17 @@ module Magento
23
23
 
24
24
  alias per page_size
25
25
 
26
+ def last_page?
27
+ current_page * page_size >= total_count
28
+ end
29
+
30
+ #
31
+ # Returns the {number} of the next page or {nil}
32
+ # when the current_page is the last page
33
+ def next_page
34
+ current_page + 1 unless last_page?
35
+ end
36
+
26
37
  class << self
27
38
  def from_magento_response(response, model:, iterable_field: 'items')
28
39
  Magento::RecordCollection.new(
@@ -0,0 +1,5 @@
1
+ module Magento
2
+ class Comment
3
+ include Magento::ModelParser
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Magento
2
- VERSION = '0.5.1'
2
+ VERSION = '0.7.0'
3
3
  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.5.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wallas Faria
@@ -48,15 +48,18 @@ files:
48
48
  - ".gitignore"
49
49
  - Gemfile
50
50
  - README.md
51
+ - atrelar_produto_loja.rb
51
52
  - lib/magento.rb
52
53
  - lib/magento/category.rb
53
54
  - lib/magento/country.rb
54
55
  - lib/magento/customer.rb
55
56
  - lib/magento/errors.rb
56
57
  - lib/magento/guest_cart.rb
58
+ - lib/magento/invoice.rb
57
59
  - lib/magento/model.rb
58
60
  - lib/magento/model_mapper.rb
59
61
  - lib/magento/order.rb
62
+ - lib/magento/polymorphic_model.rb
60
63
  - lib/magento/product.rb
61
64
  - lib/magento/query.rb
62
65
  - lib/magento/record_collection.rb
@@ -66,6 +69,7 @@ files:
66
69
  - lib/magento/shared/billing_address.rb
67
70
  - lib/magento/shared/bundle_product_option.rb
68
71
  - lib/magento/shared/category_link.rb
72
+ - lib/magento/shared/comment.rb
69
73
  - lib/magento/shared/configurable_product_option.rb
70
74
  - lib/magento/shared/currency.rb
71
75
  - lib/magento/shared/custom_attribute.rb