magento 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 727df9b49e09adb1dcf49a88cb1a6725ac499a2ff87d36934874f5c91a401fcb
4
- data.tar.gz: a59a7aceeb508d199bd36b9876a339e8a22ccdc1ea26d9bf42338aa1d9fb9a82
3
+ metadata.gz: d8bc5f8a39bdb839264e76b211f08b5ce3621340c64e866295456ec136180155
4
+ data.tar.gz: 4711ecfd3c80b9f52dce500b8d832ebe9dd2f3ef981e9245eb4730bcfc1bc529
5
5
  SHA512:
6
- metadata.gz: ed4f5da0dc2ce6370803458b809759ca103593a8125d5c87f20258710df176ca75b573147972e8efe390585adb3fd9848be498bef835c82043e001def7cf3168
7
- data.tar.gz: 69b1f401af9ff65d0228a8cf4cdf8fa2805637ffe5d71044c03cdf21a36c86e6f20b5a1f7dd86bbe376ebd7cb0aae942a59048e822d11b9e92e7c2b098d2540e
6
+ metadata.gz: c4eeca2012769013e626ffa61689d487ebcf92430cd9a0740254f16bffb2ec11f62b0b8c519446fe314a571a8def6c20e46ce80def22f1ea599ff4350d81c9c3
7
+ data.tar.gz: df313171a3767e8bf326fbf6bb45aa53010f1a8b3f7469b4cb27cb44b82ea2e30fa19eeb7165fe3380e8c50c89760c38721cdeeb5d24b3236325da94411a1fb9
data/.gitignore CHANGED
@@ -23,4 +23,6 @@ _yardoc
23
23
  doc/
24
24
  test/vcr_cassettes
25
25
 
26
- .vscode
26
+ .vscode
27
+ .byebug_history
28
+ test.rb
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Add in your Gemfile
6
6
 
7
7
  ```rb
8
- gem 'magento', '~> 0.1.0'
8
+ gem 'magento', '~> 0.2.0'
9
9
  ```
10
10
 
11
11
  or run
@@ -17,8 +17,9 @@ gem install magento
17
17
  ### Setup
18
18
 
19
19
  ```rb
20
- Magento.url = 'https://dev.superbomemcasa.com.br'
21
- Magento.token = 'pyppkwezopag85x4vd8y7eoiaixzo1z7'
20
+ Magento.url = 'https://yourstore.com'
21
+ Magento.token = 'MAGENTO_API_KEY'
22
+ Magento.store = :default # optional, Default is :all
22
23
  ```
23
24
 
24
25
  ## Product
@@ -29,6 +30,29 @@ Magento.token = 'pyppkwezopag85x4vd8y7eoiaixzo1z7'
29
30
  Magento::Product.find_by_sku('sku-test')
30
31
  ```
31
32
 
33
+ ## Customer
34
+
35
+ ### Get customer by id
36
+ ```rb
37
+ Magento::Customer.find(id) # or
38
+ Magento::Customer.find_by_id(id)
39
+ ```
40
+
41
+ ### Get customer by token
42
+ ```rb
43
+ Magento::Customer.find_by_token('user_token')
44
+ ```
45
+
46
+ ## Countries
47
+
48
+ ### Get available regions for a country
49
+
50
+ ```rb
51
+ country = Magento::Country.find('BR')
52
+
53
+ country.available_regions
54
+ ```
55
+
32
56
  # TODO
33
57
  ### Get product list
34
58
 
@@ -54,23 +78,6 @@ Magento::Product.all(price_gt: 100, page: 2)
54
78
  Magento::Product.search('tshort')
55
79
  ```
56
80
 
57
- ## Customer
58
-
59
- ### Get current customer
60
- ```rb
61
- Magento.token = 'CUSTOMER_TOKEN'
62
-
63
- Magento::Customer.me
64
- ```
65
-
66
- ### Get available regions for a country
67
-
68
- ```rb
69
- country = Magento::Country.find('BR')
70
-
71
- country.available_regions
72
- ```
73
-
74
81
  ## Order
75
82
 
76
83
  ### Create Order as admin user
@@ -7,19 +7,20 @@ require_relative 'magento/request'
7
7
  require_relative 'magento/model'
8
8
  require_relative 'magento/product'
9
9
  require_relative 'magento/country'
10
+ require_relative 'magento/customer'
10
11
 
11
- Dir[File.expand_path('magento/product/*.rb', __dir__)].map { |path| require path }
12
- Dir[File.expand_path('magento/country/*.rb', __dir__)].map { |path| require path }
12
+ Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
13
13
 
14
14
  module Magento
15
15
  class << self
16
- attr_accessor :url, :open_timeout, :timeout, :token
16
+ attr_accessor :url, :open_timeout, :timeout, :token, :store
17
17
  end
18
18
 
19
19
  self.url = ENV['MAGENTO_URL']
20
20
  self.open_timeout = 30
21
21
  self.timeout = 90
22
22
  self.token = ENV['MAGENTO_TOKEN']
23
+ self.store = ENV['MAGENTO_STORE'] || :all
23
24
 
24
25
  def self.production?
25
26
  ENV['RACK_ENV'] == 'production' ||
@@ -2,8 +2,8 @@ module Magento
2
2
  class Country < Model
3
3
  class << self
4
4
  def find(code)
5
- country_hash = Request.get("directory/countries/#{code}").parse
6
- mapHash Country, country_hash
5
+ country_hash = request.get("directory/countries/#{code}").parse
6
+ map_hash Country, country_hash
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,18 @@
1
+ module Magento
2
+ class Customer < Model
3
+ class << self
4
+ def find_by_id(id)
5
+ customer_hash = request.get("customers/#{id}").parse
6
+ map_hash Customer, customer_hash
7
+ end
8
+
9
+ alias_method :find, :find_by_id
10
+
11
+ def find_by_token(token)
12
+ user_request = Request.new(token: token)
13
+ customer_hash = user_request.get('customers/me').parse
14
+ map_hash Customer, customer_hash
15
+ end
16
+ end
17
+ end
18
+ end
@@ -7,27 +7,27 @@ module Magento
7
7
  class << self
8
8
  protected
9
9
 
10
- def mapHash(klass, values)
10
+ def map_hash(klass, values)
11
11
  object = klass.new
12
12
  values.each do |key, value|
13
13
  object.singleton_class.instance_eval { attr_accessor key }
14
14
  if value.is_a?(Hash)
15
15
  class_name = inflector.camelize(inflector.singularize(key))
16
- value = mapHash(Object.const_get("Magento::#{class_name}"), value)
16
+ value = map_hash(Object.const_get("Magento::#{class_name}"), value)
17
17
  elsif value.is_a?(Array)
18
- value = mapArray(key, value)
18
+ value = map_array(key, value)
19
19
  end
20
20
  object.send("#{key}=", value)
21
21
  end
22
22
  object
23
23
  end
24
24
 
25
- def mapArray(key, values)
25
+ def map_array(key, values)
26
26
  result = []
27
27
  values.each do |value|
28
28
  if value.is_a?(Hash)
29
29
  class_name = inflector.camelize(inflector.singularize(key))
30
- result << mapHash(Object.const_get("Magento::#{class_name}"), value)
30
+ result << map_hash(Object.const_get("Magento::#{class_name}"), value)
31
31
  else
32
32
  result << value
33
33
  end
@@ -38,6 +38,10 @@ module Magento
38
38
  def inflector
39
39
  @inflector ||= Dry::Inflector.new
40
40
  end
41
+
42
+ def request
43
+ @request ||= Request.new
44
+ end
41
45
  end
42
46
  end
43
47
  end
@@ -2,9 +2,9 @@ module Magento
2
2
  class Product < Model
3
3
  class << self
4
4
  def find_by_sku(sku)
5
- product_hash = Request.get("products/#{sku}").parse
6
- mapHash Product, product_hash
5
+ product_hash = request.get("products/#{sku}").parse
6
+ map_hash Product, product_hash
7
7
  end
8
8
  end
9
9
  end
10
- end
10
+ end
@@ -1,98 +1,103 @@
1
- # frozen_string_literal: true
2
-
3
- require 'uri'
4
- require 'http'
5
-
6
- module Magento
7
- class Request
8
- class << self
9
- def get(resource)
10
- save_request(:get, url(resource))
11
- handle_error http_auth.get(url(resource))
12
- end
13
-
14
- def put(resource, body)
15
- save_request(:put, url(resource), body)
16
- handle_error http_auth.put(url(resource), json: body)
17
- end
18
-
19
- def post(resource, body = nil, url_completa = false)
20
- url = url_completa ? resource : url(resource)
21
- save_request(:post, url, body)
22
- handle_error http_auth.post(url, json: body)
23
- end
24
-
25
- private
26
-
27
- def http_auth
28
- HTTP.auth("Bearer #{Magento.token}")
29
- end
30
-
31
- def base_url
32
- url = Magento.url.to_s.sub(%r{/$}, '')
33
- "#{url}/rest/all/V1"
34
- end
35
-
36
- def url(resource)
37
- "#{base_url}/#{resource}"
38
- end
39
-
40
- def parametros_de_busca(field:, value:, conditionType: :eq)
41
- criar_parametros(
42
- filter_groups: {
43
- '0': {
44
- filters: {
45
- '0': {
46
- field: field,
47
- conditionType: conditionType,
48
- value: value
49
- }
50
- }
51
- }
52
- }
53
- )
54
- end
55
-
56
- def parametros_de_campos(campos:)
57
- criar_parametros(fields: campos)
58
- end
59
-
60
- def criar_parametros(filter_groups: nil, fields: nil, current_page: 1)
61
- CGI.unescape(
62
- {
63
- searchCriteria: {
64
- currentPage: current_page,
65
- filterGroups: filter_groups
66
- }.compact,
67
- fields: fields
68
- }.compact.to_query
69
- )
70
- end
71
-
72
- def handle_error(resposta)
73
- unless resposta.status.success?
74
- errors = []
75
- begin
76
- msg = resposta.parse['message']
77
- errors = resposta.parse['errors']
78
- rescue StandardError
79
- msg = resposta.to_s
80
- end
81
- raise Magento::NotFound.new(msg, resposta.status.code, errors, @request) if resposta.status.not_found?
82
-
83
- raise Magento::MagentoError.new(msg, resposta.status.code, errors, @request)
84
- end
85
- resposta
86
- end
87
-
88
- def save_request(method, url, body = nil)
89
- begin
90
- body = body[:product].reject { |e| e == :media_gallery_entries }
91
- rescue StandardError
92
- end
93
-
94
- @request = { method: method, url: url, body: body }
95
- end
96
- end
97
- end
98
- 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, token: nil)
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
+ private
32
+
33
+ def http_auth
34
+ HTTP.auth("Bearer #{token}")
35
+ end
36
+
37
+ def base_url
38
+ url = Magento.url.to_s.sub(%r{/$}, '')
39
+ "#{url}/rest/#{store}/V1"
40
+ end
41
+
42
+ def url(resource)
43
+ "#{base_url}/#{resource}"
44
+ end
45
+
46
+ def search_params(field:, value:, conditionType: :eq)
47
+ create_params(
48
+ filter_groups: {
49
+ '0': {
50
+ filters: {
51
+ '0': {
52
+ field: field,
53
+ conditionType: conditionType,
54
+ value: value
55
+ }
56
+ }
57
+ }
58
+ }
59
+ )
60
+ end
61
+
62
+ def field_params(fields:)
63
+ create_params(fields: fields)
64
+ end
65
+
66
+ def create_params(filter_groups: nil, fields: nil, current_page: 1)
67
+ CGI.unescape(
68
+ {
69
+ searchCriteria: {
70
+ currentPage: current_page,
71
+ filterGroups: filter_groups
72
+ }.compact,
73
+ fields: fields
74
+ }.compact.to_query
75
+ )
76
+ end
77
+
78
+ def handle_error(resp)
79
+ return resp if resp.status.success?
80
+
81
+ begin
82
+ msg = resp.parse['message']
83
+ errors = resp.parse['errors']
84
+ rescue StandardError
85
+ msg = 'Failed access to the magento server'
86
+ errors = []
87
+ end
88
+
89
+ raise Magento::NotFound.new(msg, resp.status.code, errors, @request) if resp.status.not_found?
90
+
91
+ raise Magento::MagentoError.new(msg, resp.status.code, errors, @request)
92
+ end
93
+
94
+ def save_request(method, url, body = nil)
95
+ begin
96
+ body = body[:product].reject { |e| e == :media_gallery_entries }
97
+ rescue StandardError
98
+ end
99
+
100
+ @request = { method: method, url: url, body: body }
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class Address
3
+ end
4
+ end
@@ -1,6 +1,6 @@
1
- # frozen_string_literal: true
2
-
3
- module Magento
4
- class CategoryLink
5
- end
6
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Magento
4
+ class CategoryLink
5
+ end
6
+ end
@@ -1,4 +1,4 @@
1
- module Magento
2
- class CustomAttribute
3
- end
4
- end
1
+ module Magento
2
+ class CustomAttribute
3
+ end
4
+ end
@@ -1,4 +1,4 @@
1
- module Magento
2
- class MediaGalleryEntry
3
- end
4
- end
1
+ module Magento
2
+ class MediaGalleryEntry
3
+ end
4
+ end
@@ -1,4 +1,4 @@
1
- module Magento
2
- class Option
3
- end
4
- end
1
+ module Magento
2
+ class Option
3
+ end
4
+ end
@@ -1,4 +1,4 @@
1
- module Magento
2
- class ProductLink
3
- end
4
- end
1
+ module Magento
2
+ class ProductLink
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Magento
2
+ class Region
3
+ end
4
+ end
@@ -1,4 +1,4 @@
1
- module Magento
2
- class StockItem
3
- end
1
+ module Magento
2
+ class StockItem
3
+ end
4
4
  end
@@ -1,4 +1,4 @@
1
- module Magento
2
- class TierPrice
3
- end
4
- end
1
+ module Magento
2
+ class TierPrice
3
+ end
4
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'magento'
5
- s.version = '0.1.0'
5
+ s.version = '0.2.0'
6
6
  s.date = '2020-07-31'
7
7
  s.summary = 'Magento Ruby library'
8
8
  s.description = 'Magento Ruby library'
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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wallas Faria
@@ -49,19 +49,22 @@ files:
49
49
  - README.md
50
50
  - lib/magento.rb
51
51
  - lib/magento/country.rb
52
- - lib/magento/country/available_regions.rb
52
+ - lib/magento/customer.rb
53
53
  - lib/magento/errors.rb
54
54
  - lib/magento/model.rb
55
55
  - lib/magento/product.rb
56
- - lib/magento/product/category_link.rb
57
- - lib/magento/product/custom_attribute.rb
58
- - lib/magento/product/extension_attribute.rb
59
- - lib/magento/product/media_gallery_entry.rb
60
- - lib/magento/product/option.rb
61
- - lib/magento/product/product_link.rb
62
- - lib/magento/product/stock_item.rb
63
- - lib/magento/product/tier_price.rb
64
56
  - lib/magento/request.rb
57
+ - lib/magento/shared/address.rb
58
+ - lib/magento/shared/available_regions.rb
59
+ - lib/magento/shared/category_link.rb
60
+ - lib/magento/shared/custom_attribute.rb
61
+ - lib/magento/shared/extension_attribute.rb
62
+ - lib/magento/shared/media_gallery_entry.rb
63
+ - lib/magento/shared/option.rb
64
+ - lib/magento/shared/product_link.rb
65
+ - lib/magento/shared/region.rb
66
+ - lib/magento/shared/stock_item.rb
67
+ - lib/magento/shared/tier_price.rb
65
68
  - magento.gemspec
66
69
  homepage: https://github.com/WallasFaria/magento-ruby
67
70
  licenses: []