magento 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/README.md +27 -20
- data/lib/magento.rb +4 -3
- data/lib/magento/country.rb +2 -2
- data/lib/magento/customer.rb +18 -0
- data/lib/magento/model.rb +9 -5
- data/lib/magento/product.rb +3 -3
- data/lib/magento/request.rb +103 -98
- data/lib/magento/shared/address.rb +4 -0
- data/lib/magento/{country → shared}/available_regions.rb +0 -0
- data/lib/magento/{product → shared}/category_link.rb +6 -6
- data/lib/magento/{product → shared}/custom_attribute.rb +4 -4
- data/lib/magento/{product → shared}/extension_attribute.rb +0 -0
- data/lib/magento/{product → shared}/media_gallery_entry.rb +4 -4
- data/lib/magento/{product → shared}/option.rb +4 -4
- data/lib/magento/{product → shared}/product_link.rb +4 -4
- data/lib/magento/shared/region.rb +4 -0
- data/lib/magento/{product → shared}/stock_item.rb +3 -3
- data/lib/magento/{product → shared}/tier_price.rb +4 -4
- data/magento.gemspec +1 -1
- metadata +13 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8bc5f8a39bdb839264e76b211f08b5ce3621340c64e866295456ec136180155
|
4
|
+
data.tar.gz: 4711ecfd3c80b9f52dce500b8d832ebe9dd2f3ef981e9245eb4730bcfc1bc529
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4eeca2012769013e626ffa61689d487ebcf92430cd9a0740254f16bffb2ec11f62b0b8c519446fe314a571a8def6c20e46ce80def22f1ea599ff4350d81c9c3
|
7
|
+
data.tar.gz: df313171a3767e8bf326fbf6bb45aa53010f1a8b3f7469b4cb27cb44b82ea2e30fa19eeb7165fe3380e8c50c89760c38721cdeeb5d24b3236325da94411a1fb9
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
Add in your Gemfile
|
6
6
|
|
7
7
|
```rb
|
8
|
-
gem 'magento', '~> 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
|
21
|
-
Magento.token = '
|
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
|
data/lib/magento.rb
CHANGED
@@ -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/
|
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' ||
|
data/lib/magento/country.rb
CHANGED
@@ -2,8 +2,8 @@ module Magento
|
|
2
2
|
class Country < Model
|
3
3
|
class << self
|
4
4
|
def find(code)
|
5
|
-
country_hash =
|
6
|
-
|
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
|
data/lib/magento/model.rb
CHANGED
@@ -7,27 +7,27 @@ module Magento
|
|
7
7
|
class << self
|
8
8
|
protected
|
9
9
|
|
10
|
-
def
|
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 =
|
16
|
+
value = map_hash(Object.const_get("Magento::#{class_name}"), value)
|
17
17
|
elsif value.is_a?(Array)
|
18
|
-
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
|
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 <<
|
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
|
data/lib/magento/product.rb
CHANGED
@@ -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 =
|
6
|
-
|
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
|
data/lib/magento/request.rb
CHANGED
@@ -1,98 +1,103 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'uri'
|
4
|
-
require 'http'
|
5
|
-
|
6
|
-
module Magento
|
7
|
-
class Request
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
File without changes
|
@@ -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
|
File without changes
|
@@ -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
|
@@ -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
|
data/magento.gemspec
CHANGED
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
|
+
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/
|
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: []
|