magento 0.3.7 → 0.4.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: 4683ba34529189e0ae2a417065da3514b380463a51ea7b80a95d720e0db5a1e4
4
- data.tar.gz: 5ac940a291de0ad07891cf34958d014c0b82e2a4249bf0b205d1622b066c6fff
3
+ metadata.gz: 10797b41b2292eb765414cf4757cca8e92d6eeaa604ae3c59da76763e11dce9e
4
+ data.tar.gz: 683f76e2c1b7215e279459bd5eca921caba0e58fe18d299a29c256a0a5e11b94
5
5
  SHA512:
6
- metadata.gz: 57a3425ec1c2cc3a110b650569f8a11ff8957092a52095c47b17c3b4aa6efaea586ebf13543ed10968e7e87047058a894a6d64d136764359cd749b37c286237f
7
- data.tar.gz: 966333d6449626d8a929ac6cb6dab27b1e14d18e9d342d599888372d3bd1a2812d2bb4e364f40f38cfdad1e579b38d8f46bfe848c53002fcdeb2c5d63594575e
6
+ metadata.gz: c8c44416f426c6c57bd402cd2d794027ce811bdd1d2e001502d19ffc785e3cb16b26d01fa983c1a3e1582a2d2a6b18f33a3a6f472230069abb33d75263676060
7
+ data.tar.gz: 5175d8d609601f3b207e29f92cfa38fff076c7ec51a225c274cbdfbd5d6a81edc99e3f8069ebeed100b838899add3c875c70054afa2de70601d69148270ad521
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Add in your Gemfile
6
6
 
7
7
  ```rb
8
- gem 'magento', '~> 0.3.7'
8
+ gem 'magento', '~> 0.4.0'
9
9
  ```
10
10
 
11
11
  or run
@@ -109,20 +109,55 @@ Magento::Product.order(status: :desc, name: :asc).all
109
109
 
110
110
  ```rb
111
111
  # Set page and quantity per page
112
- Magento::Product.page(1).per(25) # Default per is 50
112
+ Magento::Product.page(1) # Current page, Default is 1
113
+ .page_size(25) # Default is 50
114
+ .all
115
+
116
+ # per is an alias to page_size
117
+ Magento::Product.per(25).all
113
118
  ```
114
119
 
115
120
  #### Example of several options together:
116
121
  ```rb
117
- Magento::Product.select(:sku, :name, :price)
118
- .where(name_like: 'Tshort%')
119
- .order(price: :desc)
120
- .per(10)
121
- .all
122
+ products = Magento::Product.select(:sku, :name)
123
+ .where(name_like: 'biscoito%')
124
+ .page(1)
125
+ .page_size(5)
126
+ .all
122
127
  ```
123
128
 
124
129
  \* _same pattern to all models_
125
130
 
131
+ ### Response
132
+
133
+ The `all` method retorns a `Magento::RecordCollection` instance
134
+
135
+ ```rb
136
+ products.first
137
+ # #<Magento::Product @sku="2100", @name="Biscoito Piraque Salgadinho 100G">
138
+
139
+ products[0]
140
+ # #<Magento::Product @sku="2100", @name="Biscoito Piraque Salgadinho 100G">
141
+
142
+ products.last
143
+ # #<Magento::Product @sku="964", @name="Biscoito Negresco 140 G Original">
144
+
145
+ products.map(&:sku)
146
+ # ["2100", "792", "836", "913", "964"]
147
+
148
+ products.size
149
+ # 5
150
+
151
+ products.current_page
152
+ # 1
153
+
154
+ products.page_size
155
+ # 5
156
+
157
+ products.total_count
158
+ # 307
159
+ ```
160
+
126
161
  ## Create
127
162
 
128
163
  ```rb
@@ -7,7 +7,9 @@ require_relative 'magento/errors'
7
7
  require_relative 'magento/request'
8
8
  require_relative 'magento/model'
9
9
  require_relative 'magento/model_mapper'
10
+ require_relative 'magento/record_collection'
10
11
  require_relative 'magento/query'
12
+ require_relative 'magento/category'
11
13
  require_relative 'magento/product'
12
14
  require_relative 'magento/country'
13
15
  require_relative 'magento/customer'
@@ -18,6 +20,12 @@ Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
18
20
  module Magento
19
21
  class << self
20
22
  attr_accessor :url, :open_timeout, :timeout, :token, :store
23
+
24
+ def inflector
25
+ @inflector ||= Dry::Inflector.new do |inflections|
26
+ inflections.singular 'children_data', 'category'
27
+ end
28
+ end
21
29
  end
22
30
 
23
31
  self.url = ENV['MAGENTO_URL']
@@ -20,10 +20,14 @@ module Magento
20
20
  self.class.delete(send(self.class.primary_key))
21
21
  end
22
22
 
23
+ def id
24
+ @id || send(self.class.primary_key)
25
+ end
26
+
23
27
  class << self
24
28
  extend Forwardable
25
29
 
26
- def_delegators :query, :all, :page, :per, :order, :select, :where
30
+ def_delegators :query, :all, :page, :per, :page_size, :order, :select, :where
27
31
 
28
32
  def find(id)
29
33
  hash = request.get("#{api_resource}/#{id}").parse
@@ -47,11 +51,11 @@ module Magento
47
51
  end
48
52
 
49
53
  def api_resource
50
- endpoint || inflector.pluralize(entity_name)
54
+ endpoint || Magento.inflector.pluralize(entity_name)
51
55
  end
52
56
 
53
57
  def entity_name
54
- inflector.underscore(name).sub('magento/', '')
58
+ Magento.inflector.underscore(name).sub('magento/', '')
55
59
  end
56
60
 
57
61
  def primary_key
@@ -70,10 +74,6 @@ module Magento
70
74
  def request
71
75
  @request ||= Request.new
72
76
  end
73
-
74
- def inflector
75
- @inflector ||= Dry::Inflector.new
76
- end
77
77
  end
78
78
  end
79
79
  end
@@ -42,7 +42,7 @@ class ModelMapper
42
42
  values.each do |key, value|
43
43
  object.singleton_class.instance_eval { attr_accessor key }
44
44
  if value.is_a?(Hash)
45
- class_name = inflector.camelize(inflector.singularize(key))
45
+ class_name = Magento.inflector.camelize(Magento.inflector.singularize(key))
46
46
  value = map_hash(Object.const_get("Magento::#{class_name}"), value)
47
47
  elsif value.is_a?(Array)
48
48
  value = map_array(key, value)
@@ -56,7 +56,7 @@ class ModelMapper
56
56
  result = []
57
57
  values.each do |value|
58
58
  if value.is_a?(Hash)
59
- class_name = inflector.camelize(inflector.singularize(key))
59
+ class_name = Magento.inflector.camelize(Magento.inflector.singularize(key))
60
60
  result << map_hash(Object.const_get("Magento::#{class_name}"), value)
61
61
  else
62
62
  result << value
@@ -64,8 +64,4 @@ class ModelMapper
64
64
  end
65
65
  result
66
66
  end
67
-
68
- def inflector
69
- @inflector ||= Dry::Inflector.new
70
- end
71
67
  end
@@ -50,14 +50,22 @@ module Magento
50
50
  self
51
51
  end
52
52
 
53
- def per(page_size)
54
- self.page_size = page_size
53
+ def page_size(page_size)
54
+ @page_size = page_size
55
55
  self
56
56
  end
57
57
 
58
+ alias_method :per, :page_size
59
+
58
60
  def select(*fields)
59
61
  fields = fields.map { |field| parse_field(field) }
60
- self.fields = "items[#{fields.join(',')}]"
62
+
63
+ if model == Magento::Category
64
+ self.fields = "children_data[#{fields.join(',')}]"
65
+ else
66
+ self.fields = "items[#{fields.join(',')}],search_criteria,total_count"
67
+ end
68
+
61
69
  self
62
70
  end
63
71
 
@@ -76,13 +84,13 @@ module Magento
76
84
  end
77
85
 
78
86
  def all
79
- items = request.get("#{endpoint}?#{query_params}").parse['items']
80
- items ? items.map { |i| ModelMapper.from_hash(i).to_model(model) } : []
87
+ result = request.get("#{endpoint}?#{query_params}").parse
88
+ RecordCollection.from_magento_response(result, model: model)
81
89
  end
82
90
 
83
91
  private
84
92
 
85
- attr_accessor :current_page, :filter_groups, :page_size, :request, :sort_orders, :model, :fields
93
+ attr_accessor :current_page, :filter_groups, :request, :sort_orders, :model, :fields
86
94
 
87
95
  def endpoint
88
96
  model.api_resource
@@ -100,7 +108,7 @@ module Magento
100
108
  filterGroups: filter_groups,
101
109
  currentPage: current_page,
102
110
  sortOrders: sort_orders,
103
- pageSize: page_size
111
+ pageSize: @page_size
104
112
  }.compact,
105
113
  fields: fields
106
114
  }.compact
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Magento
6
+ class RecordCollection
7
+ attr_accessor :items, :search_criteria, :total_count
8
+ extend Forwardable
9
+
10
+ def initialize
11
+ @items = []
12
+ @search_criteria = Magento::SearchCriterium
13
+ end
14
+
15
+ def_delegators :@search_criteria, :current_page, :filter_groups, :page_size
16
+
17
+ def_delegators :@items, :last, :each_index, :sample, :sort, :count, :[],
18
+ :find_index, :select, :filter, :reject, :collect, :map,
19
+ :first, :all?, :any?, :none?, :one?, :reverse_each, :take,
20
+ :take_while, :drop, :drop_while, :cycle, :index, :sort_by,
21
+ :empty?, :reverse, :length, :size, :each, :find
22
+
23
+ alias_method :per, :page_size
24
+
25
+ class << self
26
+ def from_magento_response(response, model:)
27
+ if model == Magento::Category
28
+ handle_category_response(response, model)
29
+ else
30
+ handle_response(response, model)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def handle_category_response(response, model)
37
+ collection = Magento::RecordCollection.new
38
+
39
+ collection.items = response['children_data']&.map do |item|
40
+ ModelMapper.from_hash(item).to_model(model)
41
+ end || []
42
+
43
+ collection.total_count = response['children_data']&.size || 0
44
+ collection
45
+ end
46
+
47
+ def handle_response(response, model)
48
+ collection = Magento::RecordCollection.new
49
+
50
+ collection.items = response['items']&.map do |item|
51
+ ModelMapper.from_hash(item).to_model(model)
52
+ end || []
53
+
54
+ collection.total_count = response['total_count'] if response['total_count']
55
+
56
+ if response['search_criteria']
57
+ collection.search_criteria = ModelMapper
58
+ .from_hash(response['search_criteria'])
59
+ .to_model(Magento::SearchCriterium)
60
+ end
61
+
62
+ collection
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class BundleProductOption
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class ConfigurableProductOption
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class Filter
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class FilterGroup
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module Magento
2
+ class SearchCriterium
3
+ attr_accessor :current_page, :filter_groups, :page_size
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Magento
2
- VERSION = '0.3.7'
2
+ VERSION = '0.4.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.3.7
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wallas Faria
@@ -58,12 +58,17 @@ files:
58
58
  - lib/magento/order.rb
59
59
  - lib/magento/product.rb
60
60
  - lib/magento/query.rb
61
+ - lib/magento/record_collection.rb
61
62
  - lib/magento/request.rb
62
63
  - lib/magento/shared/address.rb
63
64
  - lib/magento/shared/available_regions.rb
65
+ - lib/magento/shared/bundle_product_option.rb
64
66
  - lib/magento/shared/category_link.rb
67
+ - lib/magento/shared/configurable_product_option.rb
65
68
  - lib/magento/shared/custom_attribute.rb
66
69
  - lib/magento/shared/extension_attribute.rb
70
+ - lib/magento/shared/filter.rb
71
+ - lib/magento/shared/filter_group.rb
67
72
  - lib/magento/shared/item.rb
68
73
  - lib/magento/shared/media_gallery_entry.rb
69
74
  - lib/magento/shared/option.rb
@@ -71,6 +76,7 @@ files:
71
76
  - lib/magento/shared/payment_additional_info.rb
72
77
  - lib/magento/shared/product_link.rb
73
78
  - lib/magento/shared/region.rb
79
+ - lib/magento/shared/search_criterium.rb
74
80
  - lib/magento/shared/shipping.rb
75
81
  - lib/magento/shared/shipping_assignment.rb
76
82
  - lib/magento/shared/stock_item.rb