magento 0.4.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -1
- 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/record_collection.rb +36 -44
- data/lib/magento/request.rb +79 -108
- data/lib/magento/shared/currency.rb +4 -0
- data/lib/magento/shared/search_criterium.rb +6 -5
- data/lib/magento/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a31c9d80a9b75142710a4eaca0c87e47ac32c9fc0589ce967cebd450350a4e2
|
4
|
+
data.tar.gz: 16e930d0a17ccab6e520e75e4a87da495caee97977c6ed61b59b5ed046c82485
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed64d3a633a8d5e4cfd6eac464ff4d3bfc85c5105c5a2f83a4dc8d739710d584fa9ca35a17f49ea49eeadd01e3aadc6d1c6c48e3f46fbe085544b3ada8e518b4
|
7
|
+
data.tar.gz: 9ad6ce4e8e29c15eea332371f3c73b9d0c342d97b540a764ae7e8f6f03244e087e44fd72e8f1bd5e50cb5fc5b22327f9b2ee55369261ea9efa1208fc13527209
|
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.5.0'
|
9
9
|
```
|
10
10
|
|
11
11
|
or run
|
@@ -257,6 +257,25 @@ product.delete
|
|
257
257
|
Magento::Product.delete('sku-teste')
|
258
258
|
```
|
259
259
|
|
260
|
+
## GuestCart
|
261
|
+
|
262
|
+
Set payment information to finish the order
|
263
|
+
```rb
|
264
|
+
cart = Magento::GuestCart.find('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
|
265
|
+
|
266
|
+
# or use "build" to not request information from the magento API
|
267
|
+
cart = Magento::GuestCart.build(
|
268
|
+
cart_id: 'aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN'
|
269
|
+
)
|
270
|
+
|
271
|
+
cart.payment_information(
|
272
|
+
email: 'customer@gmail.com',
|
273
|
+
payment: { method: 'cashondelivery' }
|
274
|
+
)
|
275
|
+
|
276
|
+
>> "234575" # return the order id
|
277
|
+
```
|
278
|
+
|
260
279
|
___
|
261
280
|
|
262
281
|
##TODO:
|
data/lib/magento.rb
CHANGED
@@ -1,43 +1,44 @@
|
|
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/
|
9
|
-
require_relative 'magento/
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
self.
|
33
|
-
self.
|
34
|
-
self.
|
35
|
-
self.
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
ENV['
|
41
|
-
ENV['
|
42
|
-
|
43
|
-
end
|
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_mapper'
|
9
|
+
require_relative 'magento/model'
|
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
|
+
require_relative 'magento/guest_cart'
|
18
|
+
|
19
|
+
Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
|
20
|
+
|
21
|
+
module Magento
|
22
|
+
class << self
|
23
|
+
attr_accessor :url, :open_timeout, :timeout, :token, :store
|
24
|
+
|
25
|
+
def inflector
|
26
|
+
@inflector ||= Dry::Inflector.new do |inflections|
|
27
|
+
inflections.singular 'children_data', 'category'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
self.url = ENV['MAGENTO_URL']
|
33
|
+
self.open_timeout = 30
|
34
|
+
self.timeout = 90
|
35
|
+
self.token = ENV['MAGENTO_TOKEN']
|
36
|
+
self.store = ENV['MAGENTO_STORE'] || :all
|
37
|
+
|
38
|
+
def self.production?
|
39
|
+
ENV['RACK_ENV'] == 'production' ||
|
40
|
+
ENV['RAILS_ENV'] == 'production' ||
|
41
|
+
ENV['PRODUCTION'] ||
|
42
|
+
ENV['production']
|
43
|
+
end
|
44
|
+
end
|
data/lib/magento/customer.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
|
-
module Magento
|
2
|
-
class Customer < Model
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
module Magento
|
2
|
+
class Customer < Model
|
3
|
+
def fullname
|
4
|
+
"#{@firstname} #{@lastname}"
|
5
|
+
end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
alias_method :find_by_id, :find
|
9
|
+
|
10
|
+
def find_by_token(token)
|
11
|
+
user_request = Request.new(token: token)
|
12
|
+
customer_hash = user_request.get('customers/me').parse
|
13
|
+
build(customer_hash)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Magento
|
4
|
+
class GuestCart < Model
|
5
|
+
self.endpoint = 'guest-carts'
|
6
|
+
self.primary_key = :cart_id
|
7
|
+
|
8
|
+
def add_item(item_attributes)
|
9
|
+
attributes = { cartItem: item_attributes.merge(quote_id: cart_id) }
|
10
|
+
item = self.class.add_item(cart_id, attributes)
|
11
|
+
@items = @items.reject {|i| i.item_id == item.item_id} << item if @items
|
12
|
+
item
|
13
|
+
end
|
14
|
+
|
15
|
+
#
|
16
|
+
# Set payment information to finish the order
|
17
|
+
#
|
18
|
+
# Example:
|
19
|
+
# cart = Magento::GuestCart.find('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
|
20
|
+
#
|
21
|
+
# # or use "build" to not request information from the magento API
|
22
|
+
# cart = Magento::GuestCart.build({ 'cart_id' => 'aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN' })
|
23
|
+
#
|
24
|
+
# cart.payment_information(
|
25
|
+
# email: 'customer@gmail.com',
|
26
|
+
# payment: { method: 'cashondelivery' }
|
27
|
+
# )
|
28
|
+
#
|
29
|
+
# @return String: return the order id
|
30
|
+
def payment_information(email:, payment:)
|
31
|
+
attributes = { cartId: cart_id, paymentMethod: payment, email: email }
|
32
|
+
self.class.payment_information(attributes)
|
33
|
+
end
|
34
|
+
|
35
|
+
class << self
|
36
|
+
def create(load_cart_info: false)
|
37
|
+
cart = build(cart_id: request.post(api_resource).parse)
|
38
|
+
find cart.id if load_cart_info
|
39
|
+
end
|
40
|
+
|
41
|
+
def find(id)
|
42
|
+
cart = build request.get("#{api_resource}/#{id}").parse.merge(cart_id: id)
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# Set payment information to finish the order using class method
|
47
|
+
#
|
48
|
+
# Example:
|
49
|
+
# Magento::GuestCart.payment_information(
|
50
|
+
# cartId: 'aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN',
|
51
|
+
# paymentMethod: { method: 'cashondelivery' },
|
52
|
+
# email: email
|
53
|
+
# )
|
54
|
+
#
|
55
|
+
# @return String: return the order id
|
56
|
+
def payment_information(attributes)
|
57
|
+
attributes.transform_keys(&:to_sym)
|
58
|
+
url = "#{api_resource}/#{attributes[:cartId]}/payment-information"
|
59
|
+
request.post(url, attributes).parse
|
60
|
+
end
|
61
|
+
|
62
|
+
def add_item(id, attributes)
|
63
|
+
url = "#{api_resource}/#{id}/items"
|
64
|
+
hash = request.post(url, attributes).parse
|
65
|
+
Magento::ModelMapper.map_hash(Magento::Item, hash)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/magento/model.rb
CHANGED
@@ -1,79 +1,84 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'forwardable'
|
4
|
-
|
5
|
-
module Magento
|
6
|
-
class Model
|
7
|
-
|
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
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
module Magento
|
6
|
+
class Model
|
7
|
+
include Magento::ModelParser
|
8
|
+
|
9
|
+
def save
|
10
|
+
self.class.update(send(self.class.primary_key), to_h)
|
11
|
+
end
|
12
|
+
|
13
|
+
def update(attrs)
|
14
|
+
raise "#{self.class.name} not saved" if send(self.class.primary_key).nil?
|
15
|
+
|
16
|
+
attrs.each { |key, value| send("#{key}=", value) }
|
17
|
+
save
|
18
|
+
end
|
19
|
+
|
20
|
+
def delete
|
21
|
+
self.class.delete(send(self.class.primary_key))
|
22
|
+
end
|
23
|
+
|
24
|
+
def id
|
25
|
+
@id || send(self.class.primary_key)
|
26
|
+
end
|
27
|
+
|
28
|
+
class << self
|
29
|
+
extend Forwardable
|
30
|
+
|
31
|
+
def_delegators :query, :all, :page, :per, :page_size, :order, :select, :where
|
32
|
+
|
33
|
+
def find(id)
|
34
|
+
hash = request.get("#{api_resource}/#{id}").parse
|
35
|
+
build(hash)
|
36
|
+
end
|
37
|
+
|
38
|
+
def create(attributes)
|
39
|
+
body = { entity_name => attributes }
|
40
|
+
hash = request.post(api_resource, body).parse
|
41
|
+
build(hash)
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete(id)
|
45
|
+
request.delete("#{api_resource}/#{id}").status.success?
|
46
|
+
end
|
47
|
+
|
48
|
+
def update(id, attributes)
|
49
|
+
body = { entity_key => attributes }
|
50
|
+
hash = request.put("#{api_resource}/#{id}", body).parse
|
51
|
+
build(hash)
|
52
|
+
end
|
53
|
+
|
54
|
+
def api_resource
|
55
|
+
endpoint || Magento.inflector.pluralize(entity_name)
|
56
|
+
end
|
57
|
+
|
58
|
+
def entity_name
|
59
|
+
Magento.inflector.underscore(name).sub('magento/', '')
|
60
|
+
end
|
61
|
+
|
62
|
+
def primary_key
|
63
|
+
@primary_key || :id
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
attr_writer :primary_key
|
69
|
+
attr_accessor :endpoint, :entity_key
|
70
|
+
|
71
|
+
def entity_key
|
72
|
+
@entity_key || entity_name
|
73
|
+
end
|
74
|
+
|
75
|
+
def query
|
76
|
+
Query.new(self)
|
77
|
+
end
|
78
|
+
|
79
|
+
def request
|
80
|
+
@request ||= Request.new
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/magento/model_mapper.rb
CHANGED
@@ -1,67 +1,63 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
1
|
+
module Magento
|
2
|
+
module ModelMapper
|
3
|
+
def self.to_hash(object)
|
4
|
+
hash = {}
|
5
|
+
object.instance_variables.each do |attr|
|
6
|
+
key = attr.to_s.delete('@')
|
7
|
+
value = object.send(key)
|
8
|
+
value = to_hash(value) if value.class.name.include?('Magento::')
|
9
|
+
if value.is_a? Array
|
10
|
+
value = value.map do |item|
|
11
|
+
item.class.name.include?('Magento::') ? to_hash(item) : item
|
12
|
+
end
|
31
13
|
end
|
14
|
+
hash[key] = value
|
32
15
|
end
|
33
|
-
hash
|
16
|
+
hash
|
34
17
|
end
|
35
|
-
hash
|
36
|
-
end
|
37
18
|
|
38
|
-
|
19
|
+
def self.map_hash(model, values)
|
20
|
+
object = model.new
|
21
|
+
values.each do |key, value|
|
22
|
+
object.singleton_class.instance_eval { attr_accessor key }
|
23
|
+
if value.is_a?(Hash)
|
24
|
+
class_name = Magento.inflector.camelize(Magento.inflector.singularize(key))
|
25
|
+
value = map_hash(Object.const_get("Magento::#{class_name}"), value)
|
26
|
+
elsif value.is_a?(Array)
|
27
|
+
value = map_array(key, value)
|
28
|
+
end
|
29
|
+
object.send("#{key}=", value)
|
30
|
+
end
|
31
|
+
object
|
32
|
+
end
|
39
33
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
34
|
+
def self.map_array(key, values)
|
35
|
+
result = []
|
36
|
+
values.each do |value|
|
37
|
+
if value.is_a?(Hash)
|
38
|
+
class_name = Magento.inflector.camelize(Magento.inflector.singularize(key))
|
39
|
+
result << map_hash(Object.const_get("Magento::#{class_name}"), value)
|
40
|
+
else
|
41
|
+
result << value
|
42
|
+
end
|
49
43
|
end
|
50
|
-
|
44
|
+
result
|
51
45
|
end
|
52
|
-
object
|
53
46
|
end
|
54
47
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
class_name = Magento.inflector.camelize(Magento.inflector.singularize(key))
|
60
|
-
result << map_hash(Object.const_get("Magento::#{class_name}"), value)
|
61
|
-
else
|
62
|
-
result << value
|
48
|
+
module ModelParser
|
49
|
+
module ClassMethods
|
50
|
+
def build(attributes)
|
51
|
+
ModelMapper.map_hash(self, attributes)
|
63
52
|
end
|
64
53
|
end
|
65
|
-
|
54
|
+
|
55
|
+
def self.included(base)
|
56
|
+
base.extend(ClassMethods)
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_h
|
60
|
+
ModelMapper.to_hash(self)
|
61
|
+
end
|
66
62
|
end
|
67
63
|
end
|
data/lib/magento/order.rb
CHANGED
@@ -1,44 +1,36 @@
|
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
items: items,
|
38
|
-
total_count: response['total_count'],
|
39
|
-
search_criteria: search_criteria
|
40
|
-
)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
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
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
|
@@ -66,6 +67,7 @@ files:
|
|
66
67
|
- lib/magento/shared/bundle_product_option.rb
|
67
68
|
- lib/magento/shared/category_link.rb
|
68
69
|
- lib/magento/shared/configurable_product_option.rb
|
70
|
+
- lib/magento/shared/currency.rb
|
69
71
|
- lib/magento/shared/custom_attribute.rb
|
70
72
|
- lib/magento/shared/extension_attribute.rb
|
71
73
|
- lib/magento/shared/filter.rb
|