magento 0.9.0 → 0.12.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: 84ae0187402170a5d338e8fe19f7ffa07211be073592c36aff70a5edef16e008
4
- data.tar.gz: 7eed987d9b42e2ec86f9394c088c363658cb27cfcb664a8d2192ada23c0ee5b8
3
+ metadata.gz: 2cac0ea3cb60d8b0a55431e9b404ba17108ae038642e57c7908700915a109818
4
+ data.tar.gz: '098eb32b5b64f50fd1228245ec1b2f431f3512c47ec1b23c8583fc1b4cb18022'
5
5
  SHA512:
6
- metadata.gz: beef9d8552c025b306ad434b33701030e032b8a603545bfcfbc407be1b141f36625075ab4f873e8bac78b6369fd5334239af3da8cff0de04ebc445ed4f1fde3d
7
- data.tar.gz: b8e897978815efe0416471903a3d398b5d4ed8593a59353852cda2d33b3843ae861929f168425072ab69caeb7dec0d360ba5c1345919a2ab81990f368134c744
6
+ metadata.gz: 1125477eea00eafac5824a696777dea28b88dfe3bf9c0ea1161c2c98c286cbfa2198052669d6fbdb1b2041dc3fa987d4120167b1f4708859e1283ed018585d8b
7
+ data.tar.gz: 7db7a194488212a7d5b050d2c0acc5ec5f7f573995d291435a6a5743d39cba78320ea464f1fa2493f7672f59c5775f061f1fb9899be5ddb188b3dc9868a1c80b
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Add in your Gemfile
6
6
 
7
7
  ```rb
8
- gem 'magento', '~> 0.9.0'
8
+ gem 'magento', '~> 0.12.0'
9
9
  ```
10
10
 
11
11
  or run
@@ -32,6 +32,7 @@ Magento::Product
32
32
  Magento::Order
33
33
  Magento::Country
34
34
  Magento::Category
35
+ Magento::Customer
35
36
  ```
36
37
 
37
38
  ## Get details
@@ -68,6 +69,7 @@ Magento::Product.select(:id, :sku, :name, extension_attributes: [:website_ids, {
68
69
  #### Filters:
69
70
 
70
71
  ```rb
72
+ Magento::Product.where(visibility: 4).all
71
73
  Magento::Product.where(name_like: 'IPhone%').all
72
74
  Magento::Product.where(price_gt: 100).all
73
75
 
@@ -130,6 +132,20 @@ products = Magento::Product.select(:sku, :name)
130
132
  .all
131
133
  ```
132
134
 
135
+ ## Get one
136
+
137
+ ```rb
138
+ Magento::Order.where(increment_id: '000013457').first
139
+ # or
140
+ Magento::Order.find_by(increment_id: '000013457')
141
+ ```
142
+
143
+ ## Count
144
+ ```rb
145
+ Magento::Order.count
146
+ Magento::Order.where(status: :pending).count
147
+ ```
148
+
133
149
  \* _same pattern to all models_
134
150
 
135
151
  ### Response
@@ -255,6 +271,10 @@ product.save
255
271
  # or
256
272
 
257
273
  product.update(name: 'Updated name')
274
+
275
+ # or
276
+
277
+ Magento::Product.update('sku-teste', name: 'Updated name')
258
278
  ```
259
279
 
260
280
  ### Delete
@@ -464,14 +484,44 @@ order.cancel # or
464
484
  Magento::Order.cancel(order_id)
465
485
  ```
466
486
 
467
- ___
487
+ ## Generate Sales Rules and Coupons
468
488
 
469
- ##TODO:
489
+ ```rb
490
+ rule = Magento::SalesRule.create(
491
+ name: 'Discount name',
492
+ website_ids: [1],
493
+ customer_group_ids: [0,1,2,3],
494
+ uses_per_customer: 1,
495
+ is_active: true,
496
+ stop_rules_processing: true,
497
+ is_advanced: false,
498
+ sort_order: 0,
499
+ discount_amount: 100,
500
+ discount_step: 1,
501
+ apply_to_shipping: true,
502
+ times_used: 0,
503
+ is_rss: true,
504
+ coupon_type: 'specific',
505
+ use_auto_generation: true,
506
+ uses_per_coupon: 1
507
+ )
470
508
 
471
- ### Search products
509
+ rule.generate_coupon(quantity: 1, length: 10)
510
+ ```
511
+
512
+ Renarate by class method
472
513
  ```rb
473
- Magento::Product.search('tshort')
514
+ Magento::SalesRule.generate_coupon(
515
+ couponSpec: {
516
+ rule_id: 7,
517
+ quantity: 1,
518
+ length: 10
519
+ }
520
+ )
474
521
  ```
522
+ see all params in:
523
+ - [Magento docs Coupon](https://magento.redoc.ly/2.3.5-admin/tag/couponsgenerate#operation/salesRuleCouponManagementV1GeneratePost)
524
+ - [Magento docs SalesRules](https://magento.redoc.ly/2.3.5-admin/tag/salesRules#operation/salesRuleRuleRepositoryV1SavePost)
475
525
 
476
526
  ### First result
477
527
  ```rb
@@ -482,6 +532,23 @@ Magento::Product.where(name_like: 'some name%').first
482
532
  >> <Magento::Product @sku="some-sku" ...>
483
533
  ```
484
534
 
535
+ ### Count result
536
+ ```rb
537
+ Magento::Product.count
538
+ >> 7855
539
+ Magento::Product.where(name_like: 'some name%').count
540
+ >> 15
541
+ ```
542
+
543
+ ___
544
+
545
+ ##TODO:
546
+
547
+ ### Search products
548
+ ```rb
549
+ Magento::Product.search('tshort')
550
+ ```
551
+
485
552
  ### Last result
486
553
  ```rb
487
554
  Magento::Product.last
@@ -491,12 +558,4 @@ Magento::Product.where(name_like: 'some name%').last
491
558
  >> <Magento::Product @sku="some-sku" ...>
492
559
  ```
493
560
 
494
- ### Count result
495
- ```rb
496
- Magento::Product.count
497
- >> 7855
498
- Magento::Product.where(name_like: 'some name%').count
499
- >> 15
500
- ```
501
-
502
561
  ### Tests
@@ -17,6 +17,7 @@ require_relative 'magento/customer'
17
17
  require_relative 'magento/order'
18
18
  require_relative 'magento/invoice'
19
19
  require_relative 'magento/guest_cart'
20
+ require_relative 'magento/sales_rule'
20
21
 
21
22
  Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
22
23
 
@@ -1,17 +1,47 @@
1
1
  module Magento
2
2
  class Customer < Model
3
+ self.endpoint = 'customers/search'
4
+
3
5
  def fullname
4
6
  "#{@firstname} #{@lastname}"
5
7
  end
6
8
 
9
+ def update(attributes)
10
+ raise "id not present" if @id.nil?
11
+
12
+ attributes.each { |key, value| send("#{key}=", value) }
13
+ save
14
+ end
15
+
7
16
  class << self
8
17
  alias_method :find_by_id, :find
9
18
 
19
+ def update(id, attributes)
20
+ hash = request.put("customers/#{id}", { customer: attributes }).parse
21
+
22
+ block_given? ? yield(hash) : build(hash)
23
+ end
24
+
25
+ def create(attributes)
26
+ attributes.transform_keys!(&:to_sym)
27
+ password = attributes.delete :password
28
+ hash = request.post("customers", {
29
+ customer: attributes,
30
+ password: password
31
+ }).parse
32
+ build(hash)
33
+ end
34
+
10
35
  def find_by_token(token)
11
36
  user_request = Request.new(token: token)
12
37
  customer_hash = user_request.get('customers/me').parse
13
38
  build(customer_hash)
14
39
  end
40
+
41
+ def find(id)
42
+ hash = request.get("customers/#{id}").parse
43
+ build(hash)
44
+ end
15
45
  end
16
46
  end
17
47
  end
@@ -7,14 +7,17 @@ module Magento
7
7
  include Magento::ModelParser
8
8
 
9
9
  def save
10
- self.class.update(send(self.class.primary_key), to_h)
10
+ self.class.update(send(self.class.primary_key), to_h) do |hash|
11
+ update_attributes(hash)
12
+ end
11
13
  end
12
-
14
+
13
15
  def update(attrs)
14
16
  raise "#{self.class.name} not saved" if send(self.class.primary_key).nil?
15
17
 
16
- attrs.each { |key, value| send("#{key}=", value) }
17
- save
18
+ self.class.update(send(self.class.primary_key), attrs) do |hash|
19
+ update_attributes(hash)
20
+ end
18
21
  end
19
22
 
20
23
  def delete
@@ -25,10 +28,15 @@ module Magento
25
28
  @id || send(self.class.primary_key)
26
29
  end
27
30
 
31
+ protected def update_attributes(hash)
32
+ ModelMapper.map_hash(self, hash)
33
+ end
34
+
28
35
  class << self
29
36
  extend Forwardable
30
37
 
31
- def_delegators :query, :all, :page, :per, :page_size, :order, :select, :where
38
+ def_delegators :query, :all, :page, :per, :page_size, :order, :select,
39
+ :where, :first, :find_by, :count
32
40
 
33
41
  def find(id)
34
42
  hash = request.get("#{api_resource}/#{id}").parse
@@ -48,7 +56,8 @@ module Magento
48
56
  def update(id, attributes)
49
57
  body = { entity_key => attributes }
50
58
  hash = request.put("#{api_resource}/#{id}", body).parse
51
- build(hash)
59
+
60
+ block_given? ? yield(hash) : build(hash)
52
61
  end
53
62
 
54
63
  def api_resource
@@ -17,9 +17,12 @@ module Magento
17
17
  end
18
18
 
19
19
  def self.map_hash(model, values)
20
- object = model.new
20
+ object = model.is_a?(Class) ? model.new : model
21
21
  values.each do |key, value|
22
- object.singleton_class.instance_eval { attr_accessor key }
22
+ unless object.respond_to?(key) && object.respond_to?("#{key}=")
23
+ object.singleton_class.instance_eval { attr_accessor key }
24
+ end
25
+
23
26
  if value.is_a?(Hash)
24
27
  class_name = Magento.inflector.camelize(Magento.inflector.singularize(key))
25
28
  value = map_hash(Object.const_get("Magento::#{class_name}"), value)
@@ -12,8 +12,9 @@ module Magento
12
12
  def update(attrs)
13
13
  raise "'entity_id' not found" if @entity_id.nil?
14
14
 
15
- attrs[:entity_id] = @entity_id
16
- self.class.update(attrs)
15
+ self.class.update(@entity_id, attrs) do |hash|
16
+ update_attributes(hash)
17
+ end
17
18
  end
18
19
 
19
20
  def cancel
@@ -61,9 +62,10 @@ module Magento
61
62
  end
62
63
 
63
64
  class << self
64
- def update(attributes)
65
+ def update(entity_id, attributes)
66
+ attributes[:entity_id] = entity_id
65
67
  hash = request.put('orders/create', { entity_key => attributes }).parse
66
- build(hash)
68
+ block_given? ? yield(hash) : build(hash)
67
69
  end
68
70
 
69
71
  # @return {Boolean}
@@ -89,6 +89,18 @@ module Magento
89
89
  RecordCollection.from_magento_response(result, model: model, iterable_field: field)
90
90
  end
91
91
 
92
+ def first
93
+ page_size(1).page(1).all.first
94
+ end
95
+
96
+ def find_by(attributes)
97
+ where(attributes).first
98
+ end
99
+
100
+ def count
101
+ select(:id).page_size(1).page(1).all.total_count
102
+ end
103
+
92
104
  private
93
105
 
94
106
  attr_accessor :current_page, :filter_groups, :request, :sort_orders, :model, :fields
@@ -119,10 +131,11 @@ module Magento
119
131
 
120
132
  def parse_filter(key)
121
133
  patter = /(.*)_([a-z]+)$/
122
- raise 'Invalid format' unless key.match(patter)
123
- raise 'Condition not accepted' unless ACCEPTED_CONDITIONS.include?(key.match(patter)[2])
134
+ match = key.match(patter)
135
+
136
+ return match.to_a[1..2] if match && ACCEPTED_CONDITIONS.include?(match[2])
124
137
 
125
- key.match(patter).to_a[1..2]
138
+ [key, 'eq']
126
139
  end
127
140
 
128
141
  def parse_value_filter(condition, value)
@@ -0,0 +1,32 @@
1
+ module Magento
2
+ class SalesRule < Model
3
+ self.primary_key = :rule_id
4
+ self.entity_key = :rule
5
+ self.endpoint = 'salesRules'
6
+
7
+ # Example
8
+ # rule = Magento::SalesRule.find(5)
9
+ # rule.generate_coupon(quantity: 1, length: 10)
10
+ #
11
+ # @return {String[]}
12
+ def generate_coupon(attributes)
13
+ body = { couponSpec: { rule_id: id }.merge(attributes) }
14
+ self.class.generate_coupon(body)
15
+ end
16
+
17
+ class << self
18
+ # Example
19
+ # Magento::SalesRule.generate_coupon(
20
+ # couponSpec: {
21
+ # rule_id: 5,
22
+ # quantity: 1,
23
+ # length: 10
24
+ # }
25
+ # )
26
+ # @return {String[]}
27
+ def generate_coupon(attributes)
28
+ request.post('coupons/generate', attributes).parse
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class ActionCondition
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class Condition
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module Magento
2
- VERSION = '0.9.0'
2
+ VERSION = '0.12.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.9.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wallas Faria
@@ -63,12 +63,15 @@ files:
63
63
  - lib/magento/query.rb
64
64
  - lib/magento/record_collection.rb
65
65
  - lib/magento/request.rb
66
+ - lib/magento/sales_rule.rb
67
+ - lib/magento/shared/action_condition.rb
66
68
  - lib/magento/shared/address.rb
67
69
  - lib/magento/shared/available_regions.rb
68
70
  - lib/magento/shared/billing_address.rb
69
71
  - lib/magento/shared/bundle_product_option.rb
70
72
  - lib/magento/shared/category_link.rb
71
73
  - lib/magento/shared/comment.rb
74
+ - lib/magento/shared/condition.rb
72
75
  - lib/magento/shared/configurable_product_option.rb
73
76
  - lib/magento/shared/currency.rb
74
77
  - lib/magento/shared/custom_attribute.rb