magento 0.3.1 → 0.4.1

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,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/query'
11
- require_relative 'magento/product'
12
- require_relative 'magento/country'
13
- require_relative 'magento/customer'
14
- require_relative 'magento/order'
15
-
16
- Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
17
-
18
- module Magento
19
- class << self
20
- attr_accessor :url, :open_timeout, :timeout, :token, :store
21
- end
22
-
23
- self.url = ENV['MAGENTO_URL']
24
- self.open_timeout = 30
25
- self.timeout = 90
26
- self.token = ENV['MAGENTO_TOKEN']
27
- self.store = ENV['MAGENTO_STORE'] || :all
28
-
29
- def self.production?
30
- ENV['RACK_ENV'] == 'production' ||
31
- ENV['RAILS_ENV'] == 'production' ||
32
- ENV['PRODUCTION'] ||
33
- ENV['production']
34
- end
35
- 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'
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,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
@@ -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
@@ -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
- class << self
24
- extend Forwardable
25
-
26
- def_delegators :query, :all, :page, :per, :order, :select, :where
27
-
28
- def find(id)
29
- hash = request.get("#{api_resource}/#{id}").parse
30
- ModelMapper.from_hash(hash).to_model(self)
31
- end
32
-
33
- def create(attributes)
34
- body = { entity_name => attributes }
35
- hash = request.post(api_resource, body).parse
36
- ModelMapper.from_hash(hash).to_model(self)
37
- end
38
-
39
- def delete(id)
40
- request.delete("#{api_resource}/#{id}")
41
- end
42
-
43
- def update(id, attributes)
44
- body = { entity_name => attributes }
45
- hash = request.put("#{api_resource}/#{id}", body).parse
46
- ModelMapper.from_hash(hash).to_model(self)
47
- end
48
-
49
- def api_resource
50
- endpoint || inflector.pluralize(entity_name)
51
- end
52
-
53
- def entity_name
54
- inflector.underscore(name).sub('magento/', '')
55
- end
56
-
57
- def primary_key
58
- @primary_key || :id
59
- end
60
-
61
- protected
62
-
63
- attr_writer :primary_key
64
- attr_accessor :endpoint
65
-
66
- def query
67
- Query.new(self)
68
- end
69
-
70
- def request
71
- @request ||= Request.new
72
- end
73
-
74
- def inflector
75
- @inflector ||= Dry::Inflector.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
+ 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
@@ -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
@@ -1,7 +1,9 @@
1
- module Magento
2
- class Product < Model
3
- class << self
4
- alias_method :find_by_sku, :find
5
- end
6
- end
7
- end
1
+ module Magento
2
+ class Product < Model
3
+ self.primary_key = :sku
4
+
5
+ class << self
6
+ alias_method :find_by_sku, :find
7
+ end
8
+ end
9
+ end
@@ -38,6 +38,7 @@ module Magento
38
38
  filters = []
39
39
  attributes.each do |key, value|
40
40
  field, condition = parse_filter(key)
41
+ value = parse_value_filter(condition, value)
41
42
  filters << { field: field, conditionType: condition, value: value }
42
43
  end
43
44
  filter_groups << { filters: filters }
@@ -49,14 +50,22 @@ module Magento
49
50
  self
50
51
  end
51
52
 
52
- def per(page_size)
53
- self.page_size = page_size
53
+ def page_size(page_size)
54
+ @page_size = page_size
54
55
  self
55
56
  end
56
57
 
58
+ alias_method :per, :page_size
59
+
57
60
  def select(*fields)
58
61
  fields = fields.map { |field| parse_field(field) }
59
- self.fields = "items[#{fields.join(',')}]"
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
+
60
69
  self
61
70
  end
62
71
 
@@ -75,13 +84,14 @@ module Magento
75
84
  end
76
85
 
77
86
  def all
78
- items = request.get("#{endpoint}?#{query_params}").parse['items']
79
- items ? items.map { |i| ModelMapper.from_hash(i).to_model(model) } : []
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)
80
90
  end
81
91
 
82
92
  private
83
93
 
84
- attr_accessor :current_page, :filter_groups, :page_size, :request, :sort_orders, :model, :fields
94
+ attr_accessor :current_page, :filter_groups, :request, :sort_orders, :model, :fields
85
95
 
86
96
  def endpoint
87
97
  model.api_resource
@@ -99,7 +109,7 @@ module Magento
99
109
  filterGroups: filter_groups,
100
110
  currentPage: current_page,
101
111
  sortOrders: sort_orders,
102
- pageSize: page_size
112
+ pageSize: @page_size
103
113
  }.compact,
104
114
  fields: fields
105
115
  }.compact
@@ -115,6 +125,14 @@ module Magento
115
125
  key.match(patter).to_a[1..2]
116
126
  end
117
127
 
128
+ def parse_value_filter(condition, value)
129
+ if ['in', 'nin'].include?(condition) && value.is_a?(Array)
130
+ value = value.join(',')
131
+ end
132
+
133
+ value
134
+ end
135
+
118
136
  def parse_field(value)
119
137
  return verify_id(value) unless value.is_a? Hash
120
138
 
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Magento
6
+ class RecordCollection
7
+ attr_accessor :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, :last, :each_index, :sample, :sort, :count, :[],
19
+ :find_index, :select, :filter, :reject, :collect, :map,
20
+ :first, :all?, :any?, :none?, :one?, :reverse_each, :take,
21
+ :empty?, :reverse, :length, :size, :each, :find,
22
+ :take_while, :index, :sort_by
23
+
24
+ alias_method :per, :page_size
25
+
26
+ class << self
27
+ def from_magento_response(response, model:, iterable_field: 'items')
28
+ items = response[iterable_field]&.map do
29
+ |item| 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