magento 0.3.7 → 0.4.4
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 +2 -2
- data/README.md +267 -186
- data/lib/magento.rb +8 -0
- data/lib/magento/model.rb +7 -7
- data/lib/magento/model_mapper.rb +4 -8
- data/lib/magento/query.rb +16 -7
- data/lib/magento/record_collection.rb +44 -0
- data/lib/magento/shared/billing_address.rb +4 -0
- data/lib/magento/shared/bundle_product_option.rb +4 -0
- data/lib/magento/shared/configurable_product_option.rb +4 -0
- data/lib/magento/shared/filter.rb +4 -0
- data/lib/magento/shared/filter_group.rb +4 -0
- data/lib/magento/shared/search_criterium.rb +5 -0
- data/lib/magento/shared/sort_order.rb +4 -0
- data/lib/magento/shared/status_history.rb +4 -0
- data/lib/magento/version.rb +2 -2
- metadata +10 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af61beeba0846cab8e23f29034500fe562898978552a23da76e0024fd7a93fa1
|
4
|
+
data.tar.gz: cdcffc887ee8a9b420b9d8262744b248d8b31330a938a64704a95a392e7f7bc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1643ee9ac6bf85537296ea21b2968c9926d896b59c571fc18807736d98eaa9e4f8fe1578754a34012d56b11304c32d5c8a9f38d8a32ea4ae3d750011f3a1e01
|
7
|
+
data.tar.gz: 44cceb73112da8dcebff5233ffec93a34dac3bc56126871b768a8ab8306044e42395bfd7b5c2fa8a77cfb911403253f60e84c84981ef17398387f4ff89d9a4a8
|
data/README.md
CHANGED
@@ -1,186 +1,267 @@
|
|
1
|
-
# Magento Ruby library
|
2
|
-
|
3
|
-
## Install
|
4
|
-
|
5
|
-
Add in your Gemfile
|
6
|
-
|
7
|
-
```rb
|
8
|
-
gem 'magento', '~> 0.
|
9
|
-
```
|
10
|
-
|
11
|
-
or run
|
12
|
-
|
13
|
-
```sh
|
14
|
-
gem install magento
|
15
|
-
```
|
16
|
-
|
17
|
-
### Setup
|
18
|
-
|
19
|
-
```rb
|
20
|
-
Magento.url = 'https://yourstore.com'
|
21
|
-
Magento.token = 'MAGENTO_API_KEY'
|
22
|
-
Magento.store = :default # optional, Default is :all
|
23
|
-
```
|
24
|
-
|
25
|
-
## Models
|
26
|
-
```rb
|
27
|
-
Magento::Product
|
28
|
-
Magento::Order
|
29
|
-
Magento::Country
|
30
|
-
Magento::Category
|
31
|
-
```
|
32
|
-
|
33
|
-
## Get details
|
34
|
-
|
35
|
-
```rb
|
36
|
-
Magento::Product.find('sku-test')
|
37
|
-
Magento::Order.find(25)
|
38
|
-
Magento::Country.find('BR')
|
39
|
-
```
|
40
|
-
\* _same pattern to all models_
|
41
|
-
|
42
|
-
**Outside pattern**
|
43
|
-
|
44
|
-
Get customer by token
|
45
|
-
|
46
|
-
```rb
|
47
|
-
Magento::Customer.find_by_token('user_token')
|
48
|
-
```
|
49
|
-
|
50
|
-
## Get List
|
51
|
-
|
52
|
-
```rb
|
53
|
-
Magento::Product.all
|
54
|
-
```
|
55
|
-
|
56
|
-
#### Select fields:
|
57
|
-
```rb
|
58
|
-
Magento::Product.select(:id, :sku, :name).all
|
59
|
-
Magento::Product.select(:id, :sku, :name, extension_attributes: :category_links).all
|
60
|
-
Magento::Product.select(:id, :sku, :name, extension_attributes: [:category_links, :website_ids]).all
|
61
|
-
Magento::Product.select(:id, :sku, :name, extension_attributes: [:website_ids, { category_links: :category_id }]).all
|
62
|
-
```
|
63
|
-
|
64
|
-
#### Filters:
|
65
|
-
|
66
|
-
```rb
|
67
|
-
Magento::Product.where(name_like: 'IPhone%').all
|
68
|
-
Magento::Product.where(price_gt: 100).all
|
69
|
-
|
70
|
-
# price > 10 AND price < 20
|
71
|
-
Magento::Product.where(price_gt: 10)
|
72
|
-
.where(price_lt: 20).all
|
73
|
-
|
74
|
-
# price < 1 OR price > 100
|
75
|
-
Magento::Product.where(price_lt: 1, price_gt: 100).all
|
76
|
-
|
77
|
-
Magento::Order.where(status_in: [:canceled, :complete]).all
|
78
|
-
```
|
79
|
-
|
80
|
-
| Condition | Notes |
|
81
|
-
| --------- | ----- |
|
82
|
-
|eq | Equals. |
|
83
|
-
|finset | A value within a set of values |
|
84
|
-
|from | The beginning of a range. Must be used with to |
|
85
|
-
|gt | Greater than |
|
86
|
-
|gteq | Greater than or equal |
|
87
|
-
|in | In. The value is an array |
|
88
|
-
|like | Like. The value can contain the SQL wildcard characters when like is specified. |
|
89
|
-
|lt | Less than |
|
90
|
-
|lteq | Less than or equal |
|
91
|
-
|moreq | More or equal |
|
92
|
-
|neq | Not equal |
|
93
|
-
|nfinset | A value that is not within a set of values |
|
94
|
-
|nin | Not in. The value is an array |
|
95
|
-
|notnull | Not null |
|
96
|
-
|null | Null |
|
97
|
-
|to | The end of a range. Must be used with from |
|
98
|
-
|
99
|
-
|
100
|
-
#### SortOrder:
|
101
|
-
|
102
|
-
```rb
|
103
|
-
Magento::Product.order(:sku).all
|
104
|
-
Magento::Product.order(sku: :desc).all
|
105
|
-
Magento::Product.order(status: :desc, name: :asc).all
|
106
|
-
```
|
107
|
-
|
108
|
-
#### Pagination:
|
109
|
-
|
110
|
-
```rb
|
111
|
-
# Set page and quantity per page
|
112
|
-
Magento::Product.page(1)
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
Magento::Product.
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
1
|
+
# Magento Ruby library
|
2
|
+
|
3
|
+
## Install
|
4
|
+
|
5
|
+
Add in your Gemfile
|
6
|
+
|
7
|
+
```rb
|
8
|
+
gem 'magento', '~> 0.4.4'
|
9
|
+
```
|
10
|
+
|
11
|
+
or run
|
12
|
+
|
13
|
+
```sh
|
14
|
+
gem install magento
|
15
|
+
```
|
16
|
+
|
17
|
+
### Setup
|
18
|
+
|
19
|
+
```rb
|
20
|
+
Magento.url = 'https://yourstore.com'
|
21
|
+
Magento.token = 'MAGENTO_API_KEY'
|
22
|
+
Magento.store = :default # optional, Default is :all
|
23
|
+
```
|
24
|
+
|
25
|
+
## Models
|
26
|
+
```rb
|
27
|
+
Magento::Product
|
28
|
+
Magento::Order
|
29
|
+
Magento::Country
|
30
|
+
Magento::Category
|
31
|
+
```
|
32
|
+
|
33
|
+
## Get details
|
34
|
+
|
35
|
+
```rb
|
36
|
+
Magento::Product.find('sku-test')
|
37
|
+
Magento::Order.find(25)
|
38
|
+
Magento::Country.find('BR')
|
39
|
+
```
|
40
|
+
\* _same pattern to all models_
|
41
|
+
|
42
|
+
**Outside pattern**
|
43
|
+
|
44
|
+
Get customer by token
|
45
|
+
|
46
|
+
```rb
|
47
|
+
Magento::Customer.find_by_token('user_token')
|
48
|
+
```
|
49
|
+
|
50
|
+
## Get List
|
51
|
+
|
52
|
+
```rb
|
53
|
+
Magento::Product.all
|
54
|
+
```
|
55
|
+
|
56
|
+
#### Select fields:
|
57
|
+
```rb
|
58
|
+
Magento::Product.select(:id, :sku, :name).all
|
59
|
+
Magento::Product.select(:id, :sku, :name, extension_attributes: :category_links).all
|
60
|
+
Magento::Product.select(:id, :sku, :name, extension_attributes: [:category_links, :website_ids]).all
|
61
|
+
Magento::Product.select(:id, :sku, :name, extension_attributes: [:website_ids, { category_links: :category_id }]).all
|
62
|
+
```
|
63
|
+
|
64
|
+
#### Filters:
|
65
|
+
|
66
|
+
```rb
|
67
|
+
Magento::Product.where(name_like: 'IPhone%').all
|
68
|
+
Magento::Product.where(price_gt: 100).all
|
69
|
+
|
70
|
+
# price > 10 AND price < 20
|
71
|
+
Magento::Product.where(price_gt: 10)
|
72
|
+
.where(price_lt: 20).all
|
73
|
+
|
74
|
+
# price < 1 OR price > 100
|
75
|
+
Magento::Product.where(price_lt: 1, price_gt: 100).all
|
76
|
+
|
77
|
+
Magento::Order.where(status_in: [:canceled, :complete]).all
|
78
|
+
```
|
79
|
+
|
80
|
+
| Condition | Notes |
|
81
|
+
| --------- | ----- |
|
82
|
+
|eq | Equals. |
|
83
|
+
|finset | A value within a set of values |
|
84
|
+
|from | The beginning of a range. Must be used with to |
|
85
|
+
|gt | Greater than |
|
86
|
+
|gteq | Greater than or equal |
|
87
|
+
|in | In. The value is an array |
|
88
|
+
|like | Like. The value can contain the SQL wildcard characters when like is specified. |
|
89
|
+
|lt | Less than |
|
90
|
+
|lteq | Less than or equal |
|
91
|
+
|moreq | More or equal |
|
92
|
+
|neq | Not equal |
|
93
|
+
|nfinset | A value that is not within a set of values |
|
94
|
+
|nin | Not in. The value is an array |
|
95
|
+
|notnull | Not null |
|
96
|
+
|null | Null |
|
97
|
+
|to | The end of a range. Must be used with from |
|
98
|
+
|
99
|
+
|
100
|
+
#### SortOrder:
|
101
|
+
|
102
|
+
```rb
|
103
|
+
Magento::Product.order(:sku).all
|
104
|
+
Magento::Product.order(sku: :desc).all
|
105
|
+
Magento::Product.order(status: :desc, name: :asc).all
|
106
|
+
```
|
107
|
+
|
108
|
+
#### Pagination:
|
109
|
+
|
110
|
+
```rb
|
111
|
+
# Set page and quantity per page
|
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
|
118
|
+
```
|
119
|
+
|
120
|
+
#### Example of several options together:
|
121
|
+
```rb
|
122
|
+
products = Magento::Product.select(:sku, :name)
|
123
|
+
.where(name_like: 'biscoito%')
|
124
|
+
.page(1)
|
125
|
+
.page_size(5)
|
126
|
+
.all
|
127
|
+
```
|
128
|
+
|
129
|
+
\* _same pattern to all models_
|
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
|
+
products.filter_groups
|
161
|
+
>> [<Magento::FilterGroup @filters=[<Magento::Filter @field="name", @value="biscoito%", @condition_type="like">]>]
|
162
|
+
```
|
163
|
+
|
164
|
+
All Methods:
|
165
|
+
|
166
|
+
```rb
|
167
|
+
# Information about search criteria
|
168
|
+
:current_page
|
169
|
+
:page_size
|
170
|
+
:total_count
|
171
|
+
:filter_groups
|
172
|
+
|
173
|
+
# Iterating with the list of items
|
174
|
+
:count
|
175
|
+
:length
|
176
|
+
:size
|
177
|
+
|
178
|
+
:first
|
179
|
+
:last
|
180
|
+
:[]
|
181
|
+
:find
|
182
|
+
|
183
|
+
:each
|
184
|
+
:each_with_index
|
185
|
+
:sample
|
186
|
+
|
187
|
+
:map
|
188
|
+
:select
|
189
|
+
:filter
|
190
|
+
:reject
|
191
|
+
:collect
|
192
|
+
:take
|
193
|
+
:take_while
|
194
|
+
|
195
|
+
:sort
|
196
|
+
:sort_by
|
197
|
+
:reverse_each
|
198
|
+
:reverse
|
199
|
+
|
200
|
+
:all?
|
201
|
+
:any?
|
202
|
+
:none?
|
203
|
+
:one?
|
204
|
+
:empty?
|
205
|
+
```
|
206
|
+
|
207
|
+
## Create
|
208
|
+
|
209
|
+
```rb
|
210
|
+
Magento::Order.create(
|
211
|
+
customer_firstname: '',
|
212
|
+
customer_lastname: '',
|
213
|
+
customer_email: '',
|
214
|
+
# others attrbutes ...,
|
215
|
+
items: [
|
216
|
+
{
|
217
|
+
sku: '',
|
218
|
+
price: '',
|
219
|
+
qty_ordered: 1,
|
220
|
+
# others attrbutes ...,
|
221
|
+
}
|
222
|
+
],
|
223
|
+
billing_address: {
|
224
|
+
# attrbutes...
|
225
|
+
},
|
226
|
+
payment: {
|
227
|
+
# attrbutes...
|
228
|
+
},
|
229
|
+
extension_attributes: {
|
230
|
+
# attrbutes...
|
231
|
+
}
|
232
|
+
)
|
233
|
+
```
|
234
|
+
|
235
|
+
### Update
|
236
|
+
|
237
|
+
```rb
|
238
|
+
product = Magento::Product.find('sku-teste')
|
239
|
+
|
240
|
+
product.name = 'Updated name'
|
241
|
+
product.save
|
242
|
+
|
243
|
+
# or
|
244
|
+
|
245
|
+
product.update(name: 'Updated name')
|
246
|
+
```
|
247
|
+
|
248
|
+
### Delete
|
249
|
+
|
250
|
+
```rb
|
251
|
+
product = Magento::Product.find('sku-teste')
|
252
|
+
|
253
|
+
product.delete
|
254
|
+
|
255
|
+
# or
|
256
|
+
|
257
|
+
Magento::Product.delete('sku-teste')
|
258
|
+
```
|
259
|
+
|
260
|
+
___
|
261
|
+
|
262
|
+
##TODO:
|
263
|
+
|
264
|
+
### Search products
|
265
|
+
```rb
|
266
|
+
Magento::Product.search('tshort')
|
267
|
+
```
|
data/lib/magento.rb
CHANGED
@@ -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']
|
data/lib/magento/model.rb
CHANGED
@@ -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
|
data/lib/magento/model_mapper.rb
CHANGED
@@ -4,11 +4,11 @@ class ModelMapper
|
|
4
4
|
end
|
5
5
|
|
6
6
|
def to_model(model)
|
7
|
-
map_hash(model, @from)
|
7
|
+
map_hash(model, @from) if @from
|
8
8
|
end
|
9
9
|
|
10
10
|
def to_hash
|
11
|
-
self.class.to_hash(@from)
|
11
|
+
self.class.to_hash(@from) if @from
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.from_object(object)
|
@@ -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
|
data/lib/magento/query.rb
CHANGED
@@ -50,14 +50,22 @@ module Magento
|
|
50
50
|
self
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
54
|
-
|
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
|
-
|
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,14 @@ module Magento
|
|
76
84
|
end
|
77
85
|
|
78
86
|
def all
|
79
|
-
|
80
|
-
|
87
|
+
result = request.get("#{endpoint}?#{query_params}").parse
|
88
|
+
field = model == Magento::Category ? 'children_data' : 'items'
|
89
|
+
RecordCollection.from_magento_response(result, model: model, iterable_field: field)
|
81
90
|
end
|
82
91
|
|
83
92
|
private
|
84
93
|
|
85
|
-
attr_accessor :current_page, :filter_groups, :
|
94
|
+
attr_accessor :current_page, :filter_groups, :request, :sort_orders, :model, :fields
|
86
95
|
|
87
96
|
def endpoint
|
88
97
|
model.api_resource
|
@@ -100,7 +109,7 @@ module Magento
|
|
100
109
|
filterGroups: filter_groups,
|
101
110
|
currentPage: current_page,
|
102
111
|
sortOrders: sort_orders,
|
103
|
-
pageSize: page_size
|
112
|
+
pageSize: @page_size
|
104
113
|
}.compact,
|
105
114
|
fields: fields
|
106
115
|
}.compact
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
module Magento
|
6
|
+
class RecordCollection
|
7
|
+
attr_reader :items, :search_criteria, :total_count
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def initialize(items:, total_count: nil, search_criteria: nil)
|
11
|
+
@items = items || []
|
12
|
+
@total_count = total_count || @items.size
|
13
|
+
@search_criteria = search_criteria || Magento::SearchCriterium.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def_delegators :@search_criteria, :current_page, :filter_groups, :page_size
|
17
|
+
|
18
|
+
def_delegators :@items, :count, :length, :size, :first, :last, :[],
|
19
|
+
:find, :each, :each_with_index, :sample, :map, :select,
|
20
|
+
:filter, :reject, :collect, :take, :take_while, :sort,
|
21
|
+
:sort_by, :reverse_each, :reverse, :all?, :any?, :none?,
|
22
|
+
:one?, :empty?
|
23
|
+
|
24
|
+
alias per page_size
|
25
|
+
|
26
|
+
class << self
|
27
|
+
def from_magento_response(response, model:, iterable_field: 'items')
|
28
|
+
items = response[iterable_field]&.map do |item|
|
29
|
+
ModelMapper.from_hash(item).to_model(model)
|
30
|
+
end
|
31
|
+
|
32
|
+
search_criteria = ModelMapper
|
33
|
+
.from_hash(response['search_criteria'])
|
34
|
+
.to_model(Magento::SearchCriterium)
|
35
|
+
|
36
|
+
Magento::RecordCollection.new(
|
37
|
+
items: items,
|
38
|
+
total_count: response['total_count'],
|
39
|
+
search_criteria: search_criteria
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/magento/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Magento
|
2
|
-
VERSION = '0.
|
1
|
+
module Magento
|
2
|
+
VERSION = '0.4.4'
|
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.
|
4
|
+
version: 0.4.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wallas Faria
|
@@ -58,12 +58,18 @@ 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/billing_address.rb
|
66
|
+
- lib/magento/shared/bundle_product_option.rb
|
64
67
|
- lib/magento/shared/category_link.rb
|
68
|
+
- lib/magento/shared/configurable_product_option.rb
|
65
69
|
- lib/magento/shared/custom_attribute.rb
|
66
70
|
- lib/magento/shared/extension_attribute.rb
|
71
|
+
- lib/magento/shared/filter.rb
|
72
|
+
- lib/magento/shared/filter_group.rb
|
67
73
|
- lib/magento/shared/item.rb
|
68
74
|
- lib/magento/shared/media_gallery_entry.rb
|
69
75
|
- lib/magento/shared/option.rb
|
@@ -71,8 +77,11 @@ files:
|
|
71
77
|
- lib/magento/shared/payment_additional_info.rb
|
72
78
|
- lib/magento/shared/product_link.rb
|
73
79
|
- lib/magento/shared/region.rb
|
80
|
+
- lib/magento/shared/search_criterium.rb
|
74
81
|
- lib/magento/shared/shipping.rb
|
75
82
|
- lib/magento/shared/shipping_assignment.rb
|
83
|
+
- lib/magento/shared/sort_order.rb
|
84
|
+
- lib/magento/shared/status_history.rb
|
76
85
|
- lib/magento/shared/stock_item.rb
|
77
86
|
- lib/magento/shared/tier_price.rb
|
78
87
|
- lib/magento/shared/total.rb
|