magento 0.1.0 → 0.3.3
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 +4 -4
- data/.gitignore +3 -1
- data/README.md +112 -30
- data/lib/magento.rb +8 -3
- data/lib/magento/category.rb +4 -0
- data/lib/magento/country.rb +1 -6
- data/lib/magento/customer.rb +13 -0
- data/lib/magento/model.rb +63 -27
- data/lib/magento/model_mapper.rb +71 -0
- data/lib/magento/order.rb +5 -0
- data/lib/magento/product.rb +4 -5
- data/lib/magento/query.rb +150 -0
- data/lib/magento/request.rb +108 -98
- data/lib/magento/shared/address.rb +4 -0
- data/lib/magento/{country → shared}/available_regions.rb +0 -0
- data/lib/magento/{product → shared}/category_link.rb +6 -6
- data/lib/magento/{product → shared}/custom_attribute.rb +4 -4
- data/lib/magento/{product → shared}/extension_attribute.rb +0 -0
- data/lib/magento/shared/item.rb +3 -0
- data/lib/magento/{product → shared}/media_gallery_entry.rb +4 -4
- data/lib/magento/{product → shared}/option.rb +4 -4
- data/lib/magento/shared/payment.rb +3 -0
- data/lib/magento/shared/payment_additional_info.rb +3 -0
- data/lib/magento/{product → shared}/product_link.rb +4 -4
- data/lib/magento/shared/region.rb +4 -0
- data/lib/magento/shared/shipping.rb +3 -0
- data/lib/magento/shared/shipping_assignment.rb +3 -0
- data/lib/magento/{product → shared}/stock_item.rb +3 -3
- data/lib/magento/{product → shared}/tier_price.rb +4 -4
- data/lib/magento/shared/total.rb +3 -0
- data/lib/magento/shared/value.rb +3 -0
- data/magento.gemspec +1 -1
- metadata +24 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2ef9e4abebd84117c6020d833b54c06358ad77a09c879152094d46ee1dd8167
|
4
|
+
data.tar.gz: 905d0add93e45c83bf986bd3d041139ebaf7fa6242f8ee190dbc253d8bd65a31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b9c0be888fa8efe844d2a3ce4dece5d66e86ffaa2260b02814fd18daf81f9fa32e09c12cca553048519f37b2bc56cba62c2e3806aa5441f8e96686227c8a73e
|
7
|
+
data.tar.gz: 6888f1ad953b3184f24b3f8c68f7cd40441169fcfc1eed94e31c8889fa386e4101844d7f1a69da0bd5ff0f5878df5b6c0b5eac0903237bfc0d1d723e20a96a84
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
Add in your Gemfile
|
6
6
|
|
7
7
|
```rb
|
8
|
-
gem 'magento', '~> 0.
|
8
|
+
gem 'magento', '~> 0.3.3'
|
9
9
|
```
|
10
10
|
|
11
11
|
or run
|
@@ -17,65 +17,113 @@ gem install magento
|
|
17
17
|
### Setup
|
18
18
|
|
19
19
|
```rb
|
20
|
-
Magento.url
|
21
|
-
Magento.token = '
|
20
|
+
Magento.url = 'https://yourstore.com'
|
21
|
+
Magento.token = 'MAGENTO_API_KEY'
|
22
|
+
Magento.store = :default # optional, Default is :all
|
22
23
|
```
|
23
24
|
|
24
|
-
##
|
25
|
-
|
26
|
-
### Get product details by sku
|
27
|
-
|
25
|
+
## Models
|
28
26
|
```rb
|
29
|
-
Magento::Product
|
27
|
+
Magento::Product
|
28
|
+
Magento::Order
|
29
|
+
Magento::Country
|
30
|
+
Magento::Category
|
30
31
|
```
|
31
32
|
|
32
|
-
|
33
|
-
### Get product list
|
33
|
+
## Get details
|
34
34
|
|
35
|
-
Get all
|
36
35
|
```rb
|
37
|
-
Magento::Product.
|
36
|
+
Magento::Product.find('sku-test')
|
37
|
+
Magento::Order.find(25)
|
38
|
+
Magento::Country.find('BR')
|
38
39
|
```
|
40
|
+
\* _same pattern to all models_
|
41
|
+
|
42
|
+
**Outside pattern**
|
43
|
+
|
44
|
+
Get customer by token
|
39
45
|
|
40
|
-
Set page and quantity per page
|
41
46
|
```rb
|
42
|
-
Magento::
|
47
|
+
Magento::Customer.find_by_token('user_token')
|
43
48
|
```
|
44
49
|
|
45
|
-
|
46
|
-
```rb
|
47
|
-
Magento::Product.all(name_like: 'IPhone%')
|
50
|
+
## Get List
|
48
51
|
|
49
|
-
|
52
|
+
```rb
|
53
|
+
Magento::Product.all
|
50
54
|
```
|
51
55
|
|
52
|
-
|
56
|
+
#### Select fields:
|
53
57
|
```rb
|
54
|
-
Magento::Product.
|
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
|
55
62
|
```
|
56
63
|
|
57
|
-
|
64
|
+
#### Filters:
|
58
65
|
|
59
|
-
### Get current customer
|
60
66
|
```rb
|
61
|
-
Magento.
|
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
|
62
73
|
|
63
|
-
|
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
|
64
78
|
```
|
65
79
|
|
66
|
-
|
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:
|
67
101
|
|
68
102
|
```rb
|
69
|
-
|
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:
|
70
109
|
|
71
|
-
|
110
|
+
```rb
|
111
|
+
# Set page and quantity per page
|
112
|
+
Magento::Product.page(1).per(25) # Default per is 50
|
72
113
|
```
|
73
114
|
|
74
|
-
|
115
|
+
#### Example of several options together:
|
116
|
+
```rb
|
117
|
+
Magento::Product.select(:sku, :name, :price)
|
118
|
+
.where(name_like: 'Tshort%')
|
119
|
+
.order(price: :desc)
|
120
|
+
.per(10)
|
121
|
+
.all
|
122
|
+
```
|
75
123
|
|
76
|
-
|
124
|
+
\* _same pattern to all models_
|
77
125
|
|
78
|
-
|
126
|
+
## Create
|
79
127
|
|
80
128
|
```rb
|
81
129
|
Magento::Order.create(
|
@@ -102,3 +150,37 @@ Magento::Order.create(
|
|
102
150
|
}
|
103
151
|
)
|
104
152
|
```
|
153
|
+
|
154
|
+
### Update
|
155
|
+
|
156
|
+
```rb
|
157
|
+
product = Magento::Product.find('sku-teste')
|
158
|
+
|
159
|
+
product.name = 'Updated name'
|
160
|
+
product.save
|
161
|
+
|
162
|
+
# or
|
163
|
+
|
164
|
+
product.update(name: 'Updated name')
|
165
|
+
```
|
166
|
+
|
167
|
+
### Delete
|
168
|
+
|
169
|
+
```rb
|
170
|
+
product = Magento::Product.find('sku-teste')
|
171
|
+
|
172
|
+
product.delete
|
173
|
+
|
174
|
+
# or
|
175
|
+
|
176
|
+
Magento::Product.delete('sku-teste')
|
177
|
+
```
|
178
|
+
|
179
|
+
___
|
180
|
+
|
181
|
+
##TODO:
|
182
|
+
|
183
|
+
### Search products
|
184
|
+
```rb
|
185
|
+
Magento::Product.search('tshort')
|
186
|
+
```
|
data/lib/magento.rb
CHANGED
@@ -1,25 +1,30 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'time'
|
4
|
+
require 'dry/inflector'
|
4
5
|
|
5
6
|
require_relative 'magento/errors'
|
6
7
|
require_relative 'magento/request'
|
7
8
|
require_relative 'magento/model'
|
9
|
+
require_relative 'magento/model_mapper'
|
10
|
+
require_relative 'magento/query'
|
8
11
|
require_relative 'magento/product'
|
9
12
|
require_relative 'magento/country'
|
13
|
+
require_relative 'magento/customer'
|
14
|
+
require_relative 'magento/order'
|
10
15
|
|
11
|
-
Dir[File.expand_path('magento/
|
12
|
-
Dir[File.expand_path('magento/country/*.rb', __dir__)].map { |path| require path }
|
16
|
+
Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
|
13
17
|
|
14
18
|
module Magento
|
15
19
|
class << self
|
16
|
-
attr_accessor :url, :open_timeout, :timeout, :token
|
20
|
+
attr_accessor :url, :open_timeout, :timeout, :token, :store
|
17
21
|
end
|
18
22
|
|
19
23
|
self.url = ENV['MAGENTO_URL']
|
20
24
|
self.open_timeout = 30
|
21
25
|
self.timeout = 90
|
22
26
|
self.token = ENV['MAGENTO_TOKEN']
|
27
|
+
self.store = ENV['MAGENTO_STORE'] || :all
|
23
28
|
|
24
29
|
def self.production?
|
25
30
|
ENV['RACK_ENV'] == 'production' ||
|
data/lib/magento/country.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Magento
|
2
|
+
class Customer < Model
|
3
|
+
class << self
|
4
|
+
alias_method :find_by_id, :find
|
5
|
+
|
6
|
+
def find_by_token(token)
|
7
|
+
user_request = Request.new(token: token)
|
8
|
+
customer_hash = user_request.get('customers/me').parse
|
9
|
+
ModelMapper.from_hash(customer_hash).to_model(Customer)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/magento/model.rb
CHANGED
@@ -1,38 +1,74 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'forwardable'
|
4
4
|
|
5
5
|
module Magento
|
6
6
|
class Model
|
7
|
+
def save
|
8
|
+
body = ModelMapper.from_object(self).to_hash
|
9
|
+
self.class.update(send(self.class.primary_key), body)
|
10
|
+
end
|
11
|
+
|
12
|
+
def update(attrs)
|
13
|
+
raise "#{self.class.name} not saved" if send(self.class.primary_key).nil?
|
14
|
+
|
15
|
+
attrs.each { |key, value| send("#{key}=", value) }
|
16
|
+
save
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete
|
20
|
+
self.class.delete(send(self.class.primary_key))
|
21
|
+
end
|
22
|
+
|
7
23
|
class << self
|
24
|
+
extend Forwardable
|
25
|
+
|
26
|
+
def_delegators :query, :all, :page, :per, :order, :select, :where
|
27
|
+
|
28
|
+
def find(id)
|
29
|
+
hash = request.get("#{api_resource}/#{id}").parse
|
30
|
+
ModelMapper.from_hash(hash).to_model(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def create(attributes)
|
34
|
+
body = { entity_name => attributes }
|
35
|
+
hash = request.post(api_resource, body).parse
|
36
|
+
ModelMapper.from_hash(hash).to_model(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete(id)
|
40
|
+
request.delete("#{api_resource}/#{id}").status.success?
|
41
|
+
end
|
42
|
+
|
43
|
+
def update(id, attributes)
|
44
|
+
body = { entity_name => attributes }
|
45
|
+
hash = request.put("#{api_resource}/#{id}", body).parse
|
46
|
+
ModelMapper.from_hash(hash).to_model(self)
|
47
|
+
end
|
48
|
+
|
49
|
+
def api_resource
|
50
|
+
endpoint || inflector.pluralize(entity_name)
|
51
|
+
end
|
52
|
+
|
53
|
+
def entity_name
|
54
|
+
inflector.underscore(name).sub('magento/', '')
|
55
|
+
end
|
56
|
+
|
57
|
+
def primary_key
|
58
|
+
@primary_key || :id
|
59
|
+
end
|
60
|
+
|
8
61
|
protected
|
9
62
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
object.send("#{key}=", value)
|
21
|
-
end
|
22
|
-
object
|
23
|
-
end
|
24
|
-
|
25
|
-
def mapArray(key, values)
|
26
|
-
result = []
|
27
|
-
values.each do |value|
|
28
|
-
if value.is_a?(Hash)
|
29
|
-
class_name = inflector.camelize(inflector.singularize(key))
|
30
|
-
result << mapHash(Object.const_get("Magento::#{class_name}"), value)
|
31
|
-
else
|
32
|
-
result << value
|
33
|
-
end
|
34
|
-
end
|
35
|
-
result
|
63
|
+
attr_writer :primary_key
|
64
|
+
attr_accessor :endpoint
|
65
|
+
|
66
|
+
def query
|
67
|
+
Query.new(self)
|
68
|
+
end
|
69
|
+
|
70
|
+
def request
|
71
|
+
@request ||= Request.new
|
36
72
|
end
|
37
73
|
|
38
74
|
def inflector
|
@@ -0,0 +1,71 @@
|
|
1
|
+
class ModelMapper
|
2
|
+
def initialize(from)
|
3
|
+
@from = from
|
4
|
+
end
|
5
|
+
|
6
|
+
def to_model(model)
|
7
|
+
map_hash(model, @from)
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_hash
|
11
|
+
self.class.to_hash(@from)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.from_object(object)
|
15
|
+
new(object)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.from_hash(values)
|
19
|
+
new(values)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.to_hash(object)
|
23
|
+
hash = {}
|
24
|
+
object.instance_variables.each do |attr|
|
25
|
+
key = attr.to_s.delete('@')
|
26
|
+
value = object.send(key)
|
27
|
+
value = to_hash(value) if value.class.name.include?('Magento::')
|
28
|
+
if value.is_a? Array
|
29
|
+
value = value.map do |item|
|
30
|
+
item.class.name.include?('Magento::') ? to_hash(item) : item
|
31
|
+
end
|
32
|
+
end
|
33
|
+
hash[key] = value
|
34
|
+
end
|
35
|
+
hash
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def map_hash(model, values)
|
41
|
+
object = model.new
|
42
|
+
values.each do |key, value|
|
43
|
+
object.singleton_class.instance_eval { attr_accessor key }
|
44
|
+
if value.is_a?(Hash)
|
45
|
+
class_name = inflector.camelize(inflector.singularize(key))
|
46
|
+
value = map_hash(Object.const_get("Magento::#{class_name}"), value)
|
47
|
+
elsif value.is_a?(Array)
|
48
|
+
value = map_array(key, value)
|
49
|
+
end
|
50
|
+
object.send("#{key}=", value)
|
51
|
+
end
|
52
|
+
object
|
53
|
+
end
|
54
|
+
|
55
|
+
def map_array(key, values)
|
56
|
+
result = []
|
57
|
+
values.each do |value|
|
58
|
+
if value.is_a?(Hash)
|
59
|
+
class_name = inflector.camelize(inflector.singularize(key))
|
60
|
+
result << map_hash(Object.const_get("Magento::#{class_name}"), value)
|
61
|
+
else
|
62
|
+
result << value
|
63
|
+
end
|
64
|
+
end
|
65
|
+
result
|
66
|
+
end
|
67
|
+
|
68
|
+
def inflector
|
69
|
+
@inflector ||= Dry::Inflector.new
|
70
|
+
end
|
71
|
+
end
|
data/lib/magento/product.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
module Magento
|
2
2
|
class Product < Model
|
3
|
+
self.primary_key = :sku
|
4
|
+
|
3
5
|
class << self
|
4
|
-
|
5
|
-
product_hash = Request.get("products/#{sku}").parse
|
6
|
-
mapHash Product, product_hash
|
7
|
-
end
|
6
|
+
alias_method :find_by_sku, :find
|
8
7
|
end
|
9
8
|
end
|
10
|
-
end
|
9
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
module Magento
|
6
|
+
class Query
|
7
|
+
ACCEPTED_CONDITIONS = [
|
8
|
+
'eq', # Equals.
|
9
|
+
'finset', # A value within a set of values
|
10
|
+
'from', # The beginning of a range. Must be used with to
|
11
|
+
'gt', # Greater than
|
12
|
+
'gteq', # Greater than or equal
|
13
|
+
'in', # In. The value can contain a comma-separated list of values.
|
14
|
+
'like', # Like. The value can contain the SQL wildcard characters when like is specified.
|
15
|
+
'lt', # Less than
|
16
|
+
'lteq', # Less than or equal
|
17
|
+
'moreq', # More or equal
|
18
|
+
'neq', # Not equal
|
19
|
+
'nfinset', # A value that is not within a set of values
|
20
|
+
'nin', # Not in. The value can contain a comma-separated list of values.
|
21
|
+
'notnull', # Not null
|
22
|
+
'null', # Null
|
23
|
+
'to' # The end of a range. Must be used with from
|
24
|
+
].freeze
|
25
|
+
|
26
|
+
def initialize(model, request: Request.new)
|
27
|
+
@model = model
|
28
|
+
@request = request
|
29
|
+
@filter_groups = nil
|
30
|
+
@current_page = 1
|
31
|
+
@page_size = 50
|
32
|
+
@sort_orders = nil
|
33
|
+
@fields = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def where(attributes)
|
37
|
+
self.filter_groups = [] unless filter_groups
|
38
|
+
filters = []
|
39
|
+
attributes.each do |key, value|
|
40
|
+
field, condition = parse_filter(key)
|
41
|
+
value = parse_value_filter(condition, value)
|
42
|
+
filters << { field: field, conditionType: condition, value: value }
|
43
|
+
end
|
44
|
+
filter_groups << { filters: filters }
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def page(current_page)
|
49
|
+
self.current_page = current_page
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def per(page_size)
|
54
|
+
self.page_size = page_size
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
def select(*fields)
|
59
|
+
fields = fields.map { |field| parse_field(field) }
|
60
|
+
self.fields = "items[#{fields.join(',')}]"
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
def order(attributes)
|
65
|
+
if attributes.is_a?(String)
|
66
|
+
self.sort_orders = [{ field: verify_id(attributes), direction: :asc }]
|
67
|
+
elsif attributes.is_a?(Hash)
|
68
|
+
self.sort_orders = []
|
69
|
+
attributes.each do |field, direction|
|
70
|
+
raise "Invalid sort order direction '#{direction}'" unless %w[asc desc].include?(direction.to_s)
|
71
|
+
|
72
|
+
sort_orders << { field: verify_id(field), direction: direction }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
self
|
76
|
+
end
|
77
|
+
|
78
|
+
def all
|
79
|
+
items = request.get("#{endpoint}?#{query_params}").parse['items']
|
80
|
+
items ? items.map { |i| ModelMapper.from_hash(i).to_model(model) } : []
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
attr_accessor :current_page, :filter_groups, :page_size, :request, :sort_orders, :model, :fields
|
86
|
+
|
87
|
+
def endpoint
|
88
|
+
model.api_resource
|
89
|
+
end
|
90
|
+
|
91
|
+
def verify_id(field)
|
92
|
+
return model.primary_key if (field.to_s == 'id') && (field.to_s != model.primary_key.to_s)
|
93
|
+
|
94
|
+
field
|
95
|
+
end
|
96
|
+
|
97
|
+
def query_params
|
98
|
+
query = {
|
99
|
+
searchCriteria: {
|
100
|
+
filterGroups: filter_groups,
|
101
|
+
currentPage: current_page,
|
102
|
+
sortOrders: sort_orders,
|
103
|
+
pageSize: page_size
|
104
|
+
}.compact,
|
105
|
+
fields: fields
|
106
|
+
}.compact
|
107
|
+
|
108
|
+
encode query
|
109
|
+
end
|
110
|
+
|
111
|
+
def parse_filter(key)
|
112
|
+
patter = /(.*)_([a-z]+)$/
|
113
|
+
raise 'Invalid format' unless key.match(patter)
|
114
|
+
raise 'Condition not accepted' unless ACCEPTED_CONDITIONS.include?(key.match(patter)[2])
|
115
|
+
|
116
|
+
key.match(patter).to_a[1..2]
|
117
|
+
end
|
118
|
+
|
119
|
+
def parse_value_filter(condition, value)
|
120
|
+
if ['in', 'nin'].include?(condition) && value.is_a?(Array)
|
121
|
+
value = value.join(',')
|
122
|
+
end
|
123
|
+
|
124
|
+
value
|
125
|
+
end
|
126
|
+
|
127
|
+
def parse_field(value)
|
128
|
+
return verify_id(value) unless value.is_a? Hash
|
129
|
+
|
130
|
+
value.map do |k, v|
|
131
|
+
fields = v.is_a?(Array) ? v.map { |field| parse_field(field) } : [parse_field(v)]
|
132
|
+
"#{k}[#{fields.join(',')}]"
|
133
|
+
end.join(',')
|
134
|
+
end
|
135
|
+
|
136
|
+
def encode(value, key = nil)
|
137
|
+
case value
|
138
|
+
when Hash then value.map { |k, v| encode(v, append_key(key, k)) }.join('&')
|
139
|
+
when Array then value.each_with_index.map { |v, i| encode(v, "#{key}[#{i}]") }.join('&')
|
140
|
+
when nil then ''
|
141
|
+
else
|
142
|
+
"#{key}=#{CGI.escape(value.to_s)}"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def append_key(root_key, key)
|
147
|
+
root_key.nil? ? key : "#{root_key}[#{key}]"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
data/lib/magento/request.rb
CHANGED
@@ -1,98 +1,108 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'uri'
|
4
|
-
require 'http'
|
5
|
-
|
6
|
-
module Magento
|
7
|
-
class Request
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require 'http'
|
5
|
+
|
6
|
+
module Magento
|
7
|
+
class Request
|
8
|
+
attr_reader :token, :store
|
9
|
+
|
10
|
+
def initialize(token: Magento.token, store: Magento.store)
|
11
|
+
@token = token
|
12
|
+
@store = store
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(resource)
|
16
|
+
save_request(:get, url(resource))
|
17
|
+
handle_error http_auth.get(url(resource))
|
18
|
+
end
|
19
|
+
|
20
|
+
def put(resource, body)
|
21
|
+
save_request(:put, url(resource), body)
|
22
|
+
handle_error http_auth.put(url(resource), json: body)
|
23
|
+
end
|
24
|
+
|
25
|
+
def post(resource, body = nil, url_completa = false)
|
26
|
+
url = url_completa ? resource : url(resource)
|
27
|
+
save_request(:post, url, body)
|
28
|
+
handle_error http_auth.post(url, json: body)
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete(resource)
|
32
|
+
save_request(:delete, url(resource))
|
33
|
+
handle_error http_auth.delete(url(resource))
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def http_auth
|
39
|
+
HTTP.auth("Bearer #{token}")
|
40
|
+
end
|
41
|
+
|
42
|
+
def base_url
|
43
|
+
url = Magento.url.to_s.sub(%r{/$}, '')
|
44
|
+
"#{url}/rest/#{store}/V1"
|
45
|
+
end
|
46
|
+
|
47
|
+
def url(resource)
|
48
|
+
"#{base_url}/#{resource}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def search_params(field:, value:, conditionType: :eq)
|
52
|
+
create_params(
|
53
|
+
filter_groups: {
|
54
|
+
'0': {
|
55
|
+
filters: {
|
56
|
+
'0': {
|
57
|
+
field: field,
|
58
|
+
conditionType: conditionType,
|
59
|
+
value: value
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
def field_params(fields:)
|
68
|
+
create_params(fields: fields)
|
69
|
+
end
|
70
|
+
|
71
|
+
def create_params(filter_groups: nil, fields: nil, current_page: 1)
|
72
|
+
CGI.unescape(
|
73
|
+
{
|
74
|
+
searchCriteria: {
|
75
|
+
currentPage: current_page,
|
76
|
+
filterGroups: filter_groups
|
77
|
+
}.compact,
|
78
|
+
fields: fields
|
79
|
+
}.compact.to_query
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
def handle_error(resp)
|
84
|
+
return resp if resp.status.success?
|
85
|
+
|
86
|
+
begin
|
87
|
+
msg = resp.parse['message']
|
88
|
+
errors = resp.parse['errors']
|
89
|
+
rescue StandardError
|
90
|
+
msg = 'Failed access to the magento server'
|
91
|
+
errors = []
|
92
|
+
end
|
93
|
+
|
94
|
+
raise Magento::NotFound.new(msg, resp.status.code, errors, @request) if resp.status.not_found?
|
95
|
+
|
96
|
+
raise Magento::MagentoError.new(msg, resp.status.code, errors, @request)
|
97
|
+
end
|
98
|
+
|
99
|
+
def save_request(method, url, body = nil)
|
100
|
+
begin
|
101
|
+
body = body[:product].reject { |e| e == :media_gallery_entries }
|
102
|
+
rescue StandardError
|
103
|
+
end
|
104
|
+
|
105
|
+
@request = { method: method, url: url, body: body }
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
File without changes
|
@@ -1,6 +1,6 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Magento
|
4
|
-
class CategoryLink
|
5
|
-
end
|
6
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Magento
|
4
|
+
class CategoryLink
|
5
|
+
end
|
6
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class CustomAttribute
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module Magento
|
2
|
+
class CustomAttribute
|
3
|
+
end
|
4
|
+
end
|
File without changes
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class MediaGalleryEntry
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module Magento
|
2
|
+
class MediaGalleryEntry
|
3
|
+
end
|
4
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class Option
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module Magento
|
2
|
+
class Option
|
3
|
+
end
|
4
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class ProductLink
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module Magento
|
2
|
+
class ProductLink
|
3
|
+
end
|
4
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class StockItem
|
3
|
-
end
|
1
|
+
module Magento
|
2
|
+
class StockItem
|
3
|
+
end
|
4
4
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class TierPrice
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module Magento
|
2
|
+
class TierPrice
|
3
|
+
end
|
4
|
+
end
|
data/magento.gemspec
CHANGED
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.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wallas Faria
|
@@ -48,20 +48,34 @@ files:
|
|
48
48
|
- Gemfile
|
49
49
|
- README.md
|
50
50
|
- lib/magento.rb
|
51
|
+
- lib/magento/category.rb
|
51
52
|
- lib/magento/country.rb
|
52
|
-
- lib/magento/
|
53
|
+
- lib/magento/customer.rb
|
53
54
|
- lib/magento/errors.rb
|
54
55
|
- lib/magento/model.rb
|
56
|
+
- lib/magento/model_mapper.rb
|
57
|
+
- lib/magento/order.rb
|
55
58
|
- lib/magento/product.rb
|
56
|
-
- lib/magento/
|
57
|
-
- lib/magento/product/custom_attribute.rb
|
58
|
-
- lib/magento/product/extension_attribute.rb
|
59
|
-
- lib/magento/product/media_gallery_entry.rb
|
60
|
-
- lib/magento/product/option.rb
|
61
|
-
- lib/magento/product/product_link.rb
|
62
|
-
- lib/magento/product/stock_item.rb
|
63
|
-
- lib/magento/product/tier_price.rb
|
59
|
+
- lib/magento/query.rb
|
64
60
|
- lib/magento/request.rb
|
61
|
+
- lib/magento/shared/address.rb
|
62
|
+
- lib/magento/shared/available_regions.rb
|
63
|
+
- lib/magento/shared/category_link.rb
|
64
|
+
- lib/magento/shared/custom_attribute.rb
|
65
|
+
- lib/magento/shared/extension_attribute.rb
|
66
|
+
- lib/magento/shared/item.rb
|
67
|
+
- lib/magento/shared/media_gallery_entry.rb
|
68
|
+
- lib/magento/shared/option.rb
|
69
|
+
- lib/magento/shared/payment.rb
|
70
|
+
- lib/magento/shared/payment_additional_info.rb
|
71
|
+
- lib/magento/shared/product_link.rb
|
72
|
+
- lib/magento/shared/region.rb
|
73
|
+
- lib/magento/shared/shipping.rb
|
74
|
+
- lib/magento/shared/shipping_assignment.rb
|
75
|
+
- lib/magento/shared/stock_item.rb
|
76
|
+
- lib/magento/shared/tier_price.rb
|
77
|
+
- lib/magento/shared/total.rb
|
78
|
+
- lib/magento/shared/value.rb
|
65
79
|
- magento.gemspec
|
66
80
|
homepage: https://github.com/WallasFaria/magento-ruby
|
67
81
|
licenses: []
|