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.
@@ -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/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
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
@@ -1,13 +1,17 @@
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
+ 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
@@ -1,79 +1,84 @@
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
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
@@ -1,67 +1,63 @@
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
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[key] = value
16
+ hash
34
17
  end
35
- hash
36
- end
37
18
 
38
- private
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
- 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 = Magento.inflector.camelize(Magento.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)
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
- object.send("#{key}=", value)
44
+ result
51
45
  end
52
- object
53
46
  end
54
47
 
55
- def map_array(key, values)
56
- result = []
57
- values.each do |value|
58
- if value.is_a?(Hash)
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
- result
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