magento 0.3.2 → 0.4.2
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 +42 -0
- data/README.md +267 -184
- data/lib/magento.rb +43 -35
- data/lib/magento/country.rb +5 -5
- data/lib/magento/customer.rb +13 -13
- data/lib/magento/model.rb +79 -79
- data/lib/magento/model_mapper.rb +4 -8
- data/lib/magento/product.rb +9 -7
- data/lib/magento/query.rb +16 -7
- data/lib/magento/record_collection.rb +44 -0
- data/lib/magento/request.rb +108 -103
- data/lib/magento/shared/address.rb +4 -4
- data/lib/magento/shared/available_regions.rb +4 -4
- data/lib/magento/shared/bundle_product_option.rb +4 -0
- data/lib/magento/shared/category_link.rb +6 -6
- data/lib/magento/shared/configurable_product_option.rb +4 -0
- data/lib/magento/shared/custom_attribute.rb +4 -4
- data/lib/magento/shared/extension_attribute.rb +3 -3
- data/lib/magento/shared/filter.rb +4 -0
- data/lib/magento/shared/filter_group.rb +4 -0
- data/lib/magento/shared/media_gallery_entry.rb +4 -4
- data/lib/magento/shared/option.rb +4 -4
- data/lib/magento/shared/product_link.rb +4 -4
- data/lib/magento/shared/region.rb +4 -4
- data/lib/magento/shared/search_criterium.rb +5 -0
- data/lib/magento/shared/stock_item.rb +3 -3
- data/lib/magento/shared/tier_price.rb +4 -4
- data/lib/magento/version.rb +3 -0
- data/magento-ruby.gemspec +22 -0
- data/magento.gemspec +6 -1
- metadata +14 -5
data/lib/magento.rb
CHANGED
@@ -1,35 +1,43 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'time'
|
4
|
-
require 'dry/inflector'
|
5
|
-
|
6
|
-
require_relative 'magento/errors'
|
7
|
-
require_relative 'magento/request'
|
8
|
-
require_relative 'magento/model'
|
9
|
-
require_relative 'magento/model_mapper'
|
10
|
-
require_relative 'magento/
|
11
|
-
require_relative 'magento/
|
12
|
-
require_relative 'magento/
|
13
|
-
require_relative 'magento/
|
14
|
-
require_relative 'magento/
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
require 'dry/inflector'
|
5
|
+
|
6
|
+
require_relative 'magento/errors'
|
7
|
+
require_relative 'magento/request'
|
8
|
+
require_relative 'magento/model'
|
9
|
+
require_relative 'magento/model_mapper'
|
10
|
+
require_relative 'magento/record_collection'
|
11
|
+
require_relative 'magento/query'
|
12
|
+
require_relative 'magento/category'
|
13
|
+
require_relative 'magento/product'
|
14
|
+
require_relative 'magento/country'
|
15
|
+
require_relative 'magento/customer'
|
16
|
+
require_relative 'magento/order'
|
17
|
+
|
18
|
+
Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
|
19
|
+
|
20
|
+
module Magento
|
21
|
+
class << self
|
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
|
29
|
+
end
|
30
|
+
|
31
|
+
self.url = ENV['MAGENTO_URL']
|
32
|
+
self.open_timeout = 30
|
33
|
+
self.timeout = 90
|
34
|
+
self.token = ENV['MAGENTO_TOKEN']
|
35
|
+
self.store = ENV['MAGENTO_STORE'] || :all
|
36
|
+
|
37
|
+
def self.production?
|
38
|
+
ENV['RACK_ENV'] == 'production' ||
|
39
|
+
ENV['RAILS_ENV'] == 'production' ||
|
40
|
+
ENV['PRODUCTION'] ||
|
41
|
+
ENV['production']
|
42
|
+
end
|
43
|
+
end
|
data/lib/magento/country.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
module Magento
|
2
|
-
class Country < Model
|
3
|
-
self.endpoint = 'directory/countries'
|
4
|
-
end
|
5
|
-
end
|
1
|
+
module Magento
|
2
|
+
class Country < Model
|
3
|
+
self.endpoint = 'directory/countries'
|
4
|
+
end
|
5
|
+
end
|
data/lib/magento/customer.rb
CHANGED
@@ -1,13 +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
|
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,79 +1,79 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'forwardable'
|
4
|
-
|
5
|
-
module Magento
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
def
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
def
|
54
|
-
inflector.
|
55
|
-
end
|
56
|
-
|
57
|
-
def
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
def
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
def
|
75
|
-
@
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
module Magento
|
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
|
+
|
23
|
+
def id
|
24
|
+
@id || send(self.class.primary_key)
|
25
|
+
end
|
26
|
+
|
27
|
+
class << self
|
28
|
+
extend Forwardable
|
29
|
+
|
30
|
+
def_delegators :query, :all, :page, :per, :page_size, :order, :select, :where
|
31
|
+
|
32
|
+
def find(id)
|
33
|
+
hash = request.get("#{api_resource}/#{id}").parse
|
34
|
+
ModelMapper.from_hash(hash).to_model(self)
|
35
|
+
end
|
36
|
+
|
37
|
+
def create(attributes)
|
38
|
+
body = { entity_name => attributes }
|
39
|
+
hash = request.post(api_resource, body).parse
|
40
|
+
ModelMapper.from_hash(hash).to_model(self)
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete(id)
|
44
|
+
request.delete("#{api_resource}/#{id}").status.success?
|
45
|
+
end
|
46
|
+
|
47
|
+
def update(id, attributes)
|
48
|
+
body = { entity_name => attributes }
|
49
|
+
hash = request.put("#{api_resource}/#{id}", body).parse
|
50
|
+
ModelMapper.from_hash(hash).to_model(self)
|
51
|
+
end
|
52
|
+
|
53
|
+
def api_resource
|
54
|
+
endpoint || Magento.inflector.pluralize(entity_name)
|
55
|
+
end
|
56
|
+
|
57
|
+
def entity_name
|
58
|
+
Magento.inflector.underscore(name).sub('magento/', '')
|
59
|
+
end
|
60
|
+
|
61
|
+
def primary_key
|
62
|
+
@primary_key || :id
|
63
|
+
end
|
64
|
+
|
65
|
+
protected
|
66
|
+
|
67
|
+
attr_writer :primary_key
|
68
|
+
attr_accessor :endpoint
|
69
|
+
|
70
|
+
def query
|
71
|
+
Query.new(self)
|
72
|
+
end
|
73
|
+
|
74
|
+
def request
|
75
|
+
@request ||= Request.new
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
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/product.rb
CHANGED
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/request.rb
CHANGED
@@ -1,103 +1,108 @@
|
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
"#{
|
40
|
-
end
|
41
|
-
|
42
|
-
def
|
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
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
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 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
|