magento 0.4.0 → 0.5.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 +4 -4
- data/.github/workflows/gem-push.yml +2 -2
- data/README.md +286 -221
- data/lib/magento.rb +44 -43
- data/lib/magento/customer.rb +17 -13
- data/lib/magento/guest_cart.rb +69 -0
- data/lib/magento/model.rb +84 -79
- data/lib/magento/model_mapper.rb +50 -54
- data/lib/magento/order.rb +1 -0
- data/lib/magento/query.rb +2 -1
- data/lib/magento/record_collection.rb +36 -66
- data/lib/magento/request.rb +79 -108
- data/lib/magento/shared/billing_address.rb +4 -0
- data/lib/magento/shared/currency.rb +4 -0
- data/lib/magento/shared/search_criterium.rb +6 -5
- 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 +6 -1
data/lib/magento/order.rb
CHANGED
data/lib/magento/query.rb
CHANGED
@@ -85,7 +85,8 @@ module Magento
|
|
85
85
|
|
86
86
|
def all
|
87
87
|
result = request.get("#{endpoint}?#{query_params}").parse
|
88
|
-
|
88
|
+
field = model == Magento::Category ? 'children_data' : 'items'
|
89
|
+
RecordCollection.from_magento_response(result, model: model, iterable_field: field)
|
89
90
|
end
|
90
91
|
|
91
92
|
private
|
@@ -1,66 +1,36 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'forwardable'
|
4
|
-
|
5
|
-
module Magento
|
6
|
-
class RecordCollection
|
7
|
-
|
8
|
-
extend Forwardable
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
@items
|
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
|
-
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
|
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
|
+
Magento::RecordCollection.new(
|
29
|
+
items: response[iterable_field]&.map { |item| model.build(item) },
|
30
|
+
total_count: response['total_count'],
|
31
|
+
search_criteria: Magento::SearchCriterium.build(response['search_criteria'])
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/magento/request.rb
CHANGED
@@ -1,108 +1,79 @@
|
|
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
|
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
|
-
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
|
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 handle_error(resp)
|
52
|
+
return resp if resp.status.success?
|
53
|
+
|
54
|
+
begin
|
55
|
+
msg = resp.parse['message']
|
56
|
+
errors = resp.parse['errors']
|
57
|
+
if resp.parse['parameters'].is_a? Hash
|
58
|
+
resp.parse['parameters'].each { |k, v| msg.sub! "%#{k}", v }
|
59
|
+
end
|
60
|
+
rescue StandardError
|
61
|
+
msg = 'Failed access to the magento server'
|
62
|
+
errors = []
|
63
|
+
end
|
64
|
+
|
65
|
+
raise Magento::NotFound.new(msg, resp.status.code, errors, @request) if resp.status.not_found?
|
66
|
+
|
67
|
+
raise Magento::MagentoError.new(msg, resp.status.code, errors, @request)
|
68
|
+
end
|
69
|
+
|
70
|
+
def save_request(method, url, body = nil)
|
71
|
+
begin
|
72
|
+
body = body[:product].reject { |e| e == :media_gallery_entries }
|
73
|
+
rescue StandardError
|
74
|
+
end
|
75
|
+
|
76
|
+
@request = { method: method, url: url, body: body }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
module Magento
|
2
|
-
class SearchCriterium
|
3
|
-
|
4
|
-
|
5
|
-
end
|
1
|
+
module Magento
|
2
|
+
class SearchCriterium
|
3
|
+
include Magento::ModelParser
|
4
|
+
attr_accessor :current_page, :filter_groups, :page_size
|
5
|
+
end
|
6
|
+
end
|
data/lib/magento/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Magento
|
2
|
-
VERSION = '0.
|
1
|
+
module Magento
|
2
|
+
VERSION = '0.5.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.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wallas Faria
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- lib/magento/country.rb
|
54
54
|
- lib/magento/customer.rb
|
55
55
|
- lib/magento/errors.rb
|
56
|
+
- lib/magento/guest_cart.rb
|
56
57
|
- lib/magento/model.rb
|
57
58
|
- lib/magento/model_mapper.rb
|
58
59
|
- lib/magento/order.rb
|
@@ -62,9 +63,11 @@ files:
|
|
62
63
|
- lib/magento/request.rb
|
63
64
|
- lib/magento/shared/address.rb
|
64
65
|
- lib/magento/shared/available_regions.rb
|
66
|
+
- lib/magento/shared/billing_address.rb
|
65
67
|
- lib/magento/shared/bundle_product_option.rb
|
66
68
|
- lib/magento/shared/category_link.rb
|
67
69
|
- lib/magento/shared/configurable_product_option.rb
|
70
|
+
- lib/magento/shared/currency.rb
|
68
71
|
- lib/magento/shared/custom_attribute.rb
|
69
72
|
- lib/magento/shared/extension_attribute.rb
|
70
73
|
- lib/magento/shared/filter.rb
|
@@ -79,6 +82,8 @@ files:
|
|
79
82
|
- lib/magento/shared/search_criterium.rb
|
80
83
|
- lib/magento/shared/shipping.rb
|
81
84
|
- lib/magento/shared/shipping_assignment.rb
|
85
|
+
- lib/magento/shared/sort_order.rb
|
86
|
+
- lib/magento/shared/status_history.rb
|
82
87
|
- lib/magento/shared/stock_item.rb
|
83
88
|
- lib/magento/shared/tier_price.rb
|
84
89
|
- lib/magento/shared/total.rb
|