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,5 +1,6 @@
1
1
  module Magento
2
2
  class Order < Model
3
3
  self.primary_key = :entity_id
4
+ self.entity_key = :entity
4
5
  end
5
6
  end
@@ -85,7 +85,8 @@ module Magento
85
85
 
86
86
  def all
87
87
  result = request.get("#{endpoint}?#{query_params}").parse
88
- RecordCollection.from_magento_response(result, model: model)
88
+ field = model == Magento::Category ? 'children_data' : 'items'
89
+ RecordCollection.from_magento_response(result, model: model, iterable_field: field)
89
90
  end
90
91
 
91
92
  private
@@ -1,66 +1,36 @@
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
11
- @items = []
12
- @search_criteria = Magento::SearchCriterium
13
- end
14
-
15
- def_delegators :@search_criteria, :current_page, :filter_groups, :page_size
16
-
17
- def_delegators :@items, :last, :each_index, :sample, :sort, :count, :[],
18
- :find_index, :select, :filter, :reject, :collect, :map,
19
- :first, :all?, :any?, :none?, :one?, :reverse_each, :take,
20
- :take_while, :drop, :drop_while, :cycle, :index, :sort_by,
21
- :empty?, :reverse, :length, :size, :each, :find
22
-
23
- alias_method :per, :page_size
24
-
25
- class << self
26
- def from_magento_response(response, model:)
27
- if model == Magento::Category
28
- handle_category_response(response, model)
29
- else
30
- handle_response(response, model)
31
- end
32
- end
33
-
34
- private
35
-
36
- def handle_category_response(response, model)
37
- collection = Magento::RecordCollection.new
38
-
39
- collection.items = response['children_data']&.map do |item|
40
- ModelMapper.from_hash(item).to_model(model)
41
- end || []
42
-
43
- collection.total_count = response['children_data']&.size || 0
44
- collection
45
- end
46
-
47
- def handle_response(response, model)
48
- collection = Magento::RecordCollection.new
49
-
50
- collection.items = response['items']&.map do |item|
51
- ModelMapper.from_hash(item).to_model(model)
52
- end || []
53
-
54
- collection.total_count = response['total_count'] if response['total_count']
55
-
56
- if response['search_criteria']
57
- collection.search_criteria = ModelMapper
58
- .from_hash(response['search_criteria'])
59
- .to_model(Magento::SearchCriterium)
60
- end
61
-
62
- collection
63
- end
64
- end
65
- end
66
- 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
@@ -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 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
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
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class BillingAddress
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class Currency
3
+ end
4
+ end
@@ -1,5 +1,6 @@
1
- module Magento
2
- class SearchCriterium
3
- attr_accessor :current_page, :filter_groups, :page_size
4
- end
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
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class SortOrder
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class StatusHistory
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
- module Magento
2
- VERSION = '0.4.0'
1
+ module Magento
2
+ VERSION = '0.5.0'
3
3
  end
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.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
@@ -62,9 +63,11 @@ files:
62
63
  - lib/magento/request.rb
63
64
  - lib/magento/shared/address.rb
64
65
  - lib/magento/shared/available_regions.rb
66
+ - lib/magento/shared/billing_address.rb
65
67
  - lib/magento/shared/bundle_product_option.rb
66
68
  - lib/magento/shared/category_link.rb
67
69
  - lib/magento/shared/configurable_product_option.rb
70
+ - lib/magento/shared/currency.rb
68
71
  - lib/magento/shared/custom_attribute.rb
69
72
  - lib/magento/shared/extension_attribute.rb
70
73
  - lib/magento/shared/filter.rb
@@ -79,6 +82,8 @@ files:
79
82
  - lib/magento/shared/search_criterium.rb
80
83
  - lib/magento/shared/shipping.rb
81
84
  - lib/magento/shared/shipping_assignment.rb
85
+ - lib/magento/shared/sort_order.rb
86
+ - lib/magento/shared/status_history.rb
82
87
  - lib/magento/shared/stock_item.rb
83
88
  - lib/magento/shared/tier_price.rb
84
89
  - lib/magento/shared/total.rb