magento 0.4.2 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +286 -267
- 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 +22 -1
- data/lib/magento/record_collection.rb +36 -44
- 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.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_key => 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
|