magento 0.3.3 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/gem-push.yml +42 -0
- data/README.md +186 -186
- data/lib/magento.rb +35 -35
- data/lib/magento/country.rb +5 -5
- data/lib/magento/customer.rb +13 -13
- data/lib/magento/model.rb +79 -79
- data/lib/magento/product.rb +9 -9
- data/lib/magento/request.rb +108 -108
- data/lib/magento/shared/address.rb +4 -4
- data/lib/magento/shared/available_regions.rb +4 -4
- data/lib/magento/shared/category_link.rb +6 -6
- data/lib/magento/shared/custom_attribute.rb +4 -4
- data/lib/magento/shared/extension_attribute.rb +3 -3
- data/lib/magento/shared/media_gallery_entry.rb +4 -4
- data/lib/magento/shared/option.rb +4 -4
- data/lib/magento/shared/product_link.rb +4 -4
- data/lib/magento/shared/region.rb +4 -4
- data/lib/magento/shared/stock_item.rb +3 -3
- data/lib/magento/shared/tier_price.rb +4 -4
- data/lib/magento/version.rb +3 -0
- data/magento-ruby.gemspec +22 -0
- data/magento.gemspec +6 -1
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4683ba34529189e0ae2a417065da3514b380463a51ea7b80a95d720e0db5a1e4
|
4
|
+
data.tar.gz: 5ac940a291de0ad07891cf34958d014c0b82e2a4249bf0b205d1622b066c6fff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57a3425ec1c2cc3a110b650569f8a11ff8957092a52095c47b17c3b4aa6efaea586ebf13543ed10968e7e87047058a894a6d64d136764359cd749b37c286237f
|
7
|
+
data.tar.gz: 966333d6449626d8a929ac6cb6dab27b1e14d18e9d342d599888372d3bd1a2812d2bb4e364f40f38cfdad1e579b38d8f46bfe848c53002fcdeb2c5d63594575e
|
@@ -0,0 +1,42 @@
|
|
1
|
+
name: Ruby Gem
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
build:
|
11
|
+
name: Build + Publish
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
- name: Set up Ruby 2.6
|
17
|
+
uses: actions/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: 2.6.x
|
20
|
+
|
21
|
+
- name: Publish to GPR
|
22
|
+
run: |
|
23
|
+
mkdir -p $HOME/.gem
|
24
|
+
touch $HOME/.gem/credentials
|
25
|
+
chmod 0600 $HOME/.gem/credentials
|
26
|
+
printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
27
|
+
gem build magento-ruby.gemspec
|
28
|
+
gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
|
29
|
+
env:
|
30
|
+
GEM_HOST_API_KEY: "Bearer ${{secrets.GITHUB_TOKEN}}"
|
31
|
+
OWNER: ${{ github.repository_owner }}
|
32
|
+
|
33
|
+
- name: Publish to RubyGems
|
34
|
+
run: |
|
35
|
+
mkdir -p $HOME/.gem
|
36
|
+
touch $HOME/.gem/credentials
|
37
|
+
chmod 0600 $HOME/.gem/credentials
|
38
|
+
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
39
|
+
gem build magento.gemspec
|
40
|
+
gem push magento-[0-9]*.gem
|
41
|
+
env:
|
42
|
+
GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
|
data/README.md
CHANGED
@@ -1,186 +1,186 @@
|
|
1
|
-
# Magento Ruby library
|
2
|
-
|
3
|
-
## Install
|
4
|
-
|
5
|
-
Add in your Gemfile
|
6
|
-
|
7
|
-
```rb
|
8
|
-
gem 'magento', '~> 0.3.
|
9
|
-
```
|
10
|
-
|
11
|
-
or run
|
12
|
-
|
13
|
-
```sh
|
14
|
-
gem install magento
|
15
|
-
```
|
16
|
-
|
17
|
-
### Setup
|
18
|
-
|
19
|
-
```rb
|
20
|
-
Magento.url = 'https://yourstore.com'
|
21
|
-
Magento.token = 'MAGENTO_API_KEY'
|
22
|
-
Magento.store = :default # optional, Default is :all
|
23
|
-
```
|
24
|
-
|
25
|
-
## Models
|
26
|
-
```rb
|
27
|
-
Magento::Product
|
28
|
-
Magento::Order
|
29
|
-
Magento::Country
|
30
|
-
Magento::Category
|
31
|
-
```
|
32
|
-
|
33
|
-
## Get details
|
34
|
-
|
35
|
-
```rb
|
36
|
-
Magento::Product.find('sku-test')
|
37
|
-
Magento::Order.find(25)
|
38
|
-
Magento::Country.find('BR')
|
39
|
-
```
|
40
|
-
\* _same pattern to all models_
|
41
|
-
|
42
|
-
**Outside pattern**
|
43
|
-
|
44
|
-
Get customer by token
|
45
|
-
|
46
|
-
```rb
|
47
|
-
Magento::Customer.find_by_token('user_token')
|
48
|
-
```
|
49
|
-
|
50
|
-
## Get List
|
51
|
-
|
52
|
-
```rb
|
53
|
-
Magento::Product.all
|
54
|
-
```
|
55
|
-
|
56
|
-
#### Select fields:
|
57
|
-
```rb
|
58
|
-
Magento::Product.select(:id, :sku, :name).all
|
59
|
-
Magento::Product.select(:id, :sku, :name, extension_attributes: :category_links).all
|
60
|
-
Magento::Product.select(:id, :sku, :name, extension_attributes: [:category_links, :website_ids]).all
|
61
|
-
Magento::Product.select(:id, :sku, :name, extension_attributes: [:website_ids, { category_links: :category_id }]).all
|
62
|
-
```
|
63
|
-
|
64
|
-
#### Filters:
|
65
|
-
|
66
|
-
```rb
|
67
|
-
Magento::Product.where(name_like: 'IPhone%').all
|
68
|
-
Magento::Product.where(price_gt: 100).all
|
69
|
-
|
70
|
-
# price > 10 AND price < 20
|
71
|
-
Magento::Product.where(price_gt: 10)
|
72
|
-
.where(price_lt: 20).all
|
73
|
-
|
74
|
-
# price < 1 OR price > 100
|
75
|
-
Magento::Product.where(price_lt: 1, price_gt: 100).all
|
76
|
-
|
77
|
-
Magento::Order.where(status_in: [:canceled, :complete]).all
|
78
|
-
```
|
79
|
-
|
80
|
-
| Condition | Notes |
|
81
|
-
| --------- | ----- |
|
82
|
-
|eq | Equals. |
|
83
|
-
|finset | A value within a set of values |
|
84
|
-
|from | The beginning of a range. Must be used with to |
|
85
|
-
|gt | Greater than |
|
86
|
-
|gteq | Greater than or equal |
|
87
|
-
|in | In. The value is an array |
|
88
|
-
|like | Like. The value can contain the SQL wildcard characters when like is specified. |
|
89
|
-
|lt | Less than |
|
90
|
-
|lteq | Less than or equal |
|
91
|
-
|moreq | More or equal |
|
92
|
-
|neq | Not equal |
|
93
|
-
|nfinset | A value that is not within a set of values |
|
94
|
-
|nin | Not in. The value is an array |
|
95
|
-
|notnull | Not null |
|
96
|
-
|null | Null |
|
97
|
-
|to | The end of a range. Must be used with from |
|
98
|
-
|
99
|
-
|
100
|
-
#### SortOrder:
|
101
|
-
|
102
|
-
```rb
|
103
|
-
Magento::Product.order(:sku).all
|
104
|
-
Magento::Product.order(sku: :desc).all
|
105
|
-
Magento::Product.order(status: :desc, name: :asc).all
|
106
|
-
```
|
107
|
-
|
108
|
-
#### Pagination:
|
109
|
-
|
110
|
-
```rb
|
111
|
-
# Set page and quantity per page
|
112
|
-
Magento::Product.page(1).per(25) # Default per is 50
|
113
|
-
```
|
114
|
-
|
115
|
-
#### Example of several options together:
|
116
|
-
```rb
|
117
|
-
Magento::Product.select(:sku, :name, :price)
|
118
|
-
.where(name_like: 'Tshort%')
|
119
|
-
.order(price: :desc)
|
120
|
-
.per(10)
|
121
|
-
.all
|
122
|
-
```
|
123
|
-
|
124
|
-
\* _same pattern to all models_
|
125
|
-
|
126
|
-
## Create
|
127
|
-
|
128
|
-
```rb
|
129
|
-
Magento::Order.create(
|
130
|
-
customer_firstname: '',
|
131
|
-
customer_lastname: '',
|
132
|
-
customer_email: '',
|
133
|
-
# others attrbutes ...,
|
134
|
-
items: [
|
135
|
-
{
|
136
|
-
sku: '',
|
137
|
-
price: '',
|
138
|
-
qty_ordered: 1,
|
139
|
-
# others attrbutes ...,
|
140
|
-
}
|
141
|
-
],
|
142
|
-
billing_address: {
|
143
|
-
# attrbutes...
|
144
|
-
},
|
145
|
-
payment: {
|
146
|
-
# attrbutes...
|
147
|
-
},
|
148
|
-
extension_attributes: {
|
149
|
-
# attrbutes...
|
150
|
-
}
|
151
|
-
)
|
152
|
-
```
|
153
|
-
|
154
|
-
### Update
|
155
|
-
|
156
|
-
```rb
|
157
|
-
product = Magento::Product.find('sku-teste')
|
158
|
-
|
159
|
-
product.name = 'Updated name'
|
160
|
-
product.save
|
161
|
-
|
162
|
-
# or
|
163
|
-
|
164
|
-
product.update(name: 'Updated name')
|
165
|
-
```
|
166
|
-
|
167
|
-
### Delete
|
168
|
-
|
169
|
-
```rb
|
170
|
-
product = Magento::Product.find('sku-teste')
|
171
|
-
|
172
|
-
product.delete
|
173
|
-
|
174
|
-
# or
|
175
|
-
|
176
|
-
Magento::Product.delete('sku-teste')
|
177
|
-
```
|
178
|
-
|
179
|
-
___
|
180
|
-
|
181
|
-
##TODO:
|
182
|
-
|
183
|
-
### Search products
|
184
|
-
```rb
|
185
|
-
Magento::Product.search('tshort')
|
186
|
-
```
|
1
|
+
# Magento Ruby library
|
2
|
+
|
3
|
+
## Install
|
4
|
+
|
5
|
+
Add in your Gemfile
|
6
|
+
|
7
|
+
```rb
|
8
|
+
gem 'magento', '~> 0.3.7'
|
9
|
+
```
|
10
|
+
|
11
|
+
or run
|
12
|
+
|
13
|
+
```sh
|
14
|
+
gem install magento
|
15
|
+
```
|
16
|
+
|
17
|
+
### Setup
|
18
|
+
|
19
|
+
```rb
|
20
|
+
Magento.url = 'https://yourstore.com'
|
21
|
+
Magento.token = 'MAGENTO_API_KEY'
|
22
|
+
Magento.store = :default # optional, Default is :all
|
23
|
+
```
|
24
|
+
|
25
|
+
## Models
|
26
|
+
```rb
|
27
|
+
Magento::Product
|
28
|
+
Magento::Order
|
29
|
+
Magento::Country
|
30
|
+
Magento::Category
|
31
|
+
```
|
32
|
+
|
33
|
+
## Get details
|
34
|
+
|
35
|
+
```rb
|
36
|
+
Magento::Product.find('sku-test')
|
37
|
+
Magento::Order.find(25)
|
38
|
+
Magento::Country.find('BR')
|
39
|
+
```
|
40
|
+
\* _same pattern to all models_
|
41
|
+
|
42
|
+
**Outside pattern**
|
43
|
+
|
44
|
+
Get customer by token
|
45
|
+
|
46
|
+
```rb
|
47
|
+
Magento::Customer.find_by_token('user_token')
|
48
|
+
```
|
49
|
+
|
50
|
+
## Get List
|
51
|
+
|
52
|
+
```rb
|
53
|
+
Magento::Product.all
|
54
|
+
```
|
55
|
+
|
56
|
+
#### Select fields:
|
57
|
+
```rb
|
58
|
+
Magento::Product.select(:id, :sku, :name).all
|
59
|
+
Magento::Product.select(:id, :sku, :name, extension_attributes: :category_links).all
|
60
|
+
Magento::Product.select(:id, :sku, :name, extension_attributes: [:category_links, :website_ids]).all
|
61
|
+
Magento::Product.select(:id, :sku, :name, extension_attributes: [:website_ids, { category_links: :category_id }]).all
|
62
|
+
```
|
63
|
+
|
64
|
+
#### Filters:
|
65
|
+
|
66
|
+
```rb
|
67
|
+
Magento::Product.where(name_like: 'IPhone%').all
|
68
|
+
Magento::Product.where(price_gt: 100).all
|
69
|
+
|
70
|
+
# price > 10 AND price < 20
|
71
|
+
Magento::Product.where(price_gt: 10)
|
72
|
+
.where(price_lt: 20).all
|
73
|
+
|
74
|
+
# price < 1 OR price > 100
|
75
|
+
Magento::Product.where(price_lt: 1, price_gt: 100).all
|
76
|
+
|
77
|
+
Magento::Order.where(status_in: [:canceled, :complete]).all
|
78
|
+
```
|
79
|
+
|
80
|
+
| Condition | Notes |
|
81
|
+
| --------- | ----- |
|
82
|
+
|eq | Equals. |
|
83
|
+
|finset | A value within a set of values |
|
84
|
+
|from | The beginning of a range. Must be used with to |
|
85
|
+
|gt | Greater than |
|
86
|
+
|gteq | Greater than or equal |
|
87
|
+
|in | In. The value is an array |
|
88
|
+
|like | Like. The value can contain the SQL wildcard characters when like is specified. |
|
89
|
+
|lt | Less than |
|
90
|
+
|lteq | Less than or equal |
|
91
|
+
|moreq | More or equal |
|
92
|
+
|neq | Not equal |
|
93
|
+
|nfinset | A value that is not within a set of values |
|
94
|
+
|nin | Not in. The value is an array |
|
95
|
+
|notnull | Not null |
|
96
|
+
|null | Null |
|
97
|
+
|to | The end of a range. Must be used with from |
|
98
|
+
|
99
|
+
|
100
|
+
#### SortOrder:
|
101
|
+
|
102
|
+
```rb
|
103
|
+
Magento::Product.order(:sku).all
|
104
|
+
Magento::Product.order(sku: :desc).all
|
105
|
+
Magento::Product.order(status: :desc, name: :asc).all
|
106
|
+
```
|
107
|
+
|
108
|
+
#### Pagination:
|
109
|
+
|
110
|
+
```rb
|
111
|
+
# Set page and quantity per page
|
112
|
+
Magento::Product.page(1).per(25) # Default per is 50
|
113
|
+
```
|
114
|
+
|
115
|
+
#### Example of several options together:
|
116
|
+
```rb
|
117
|
+
Magento::Product.select(:sku, :name, :price)
|
118
|
+
.where(name_like: 'Tshort%')
|
119
|
+
.order(price: :desc)
|
120
|
+
.per(10)
|
121
|
+
.all
|
122
|
+
```
|
123
|
+
|
124
|
+
\* _same pattern to all models_
|
125
|
+
|
126
|
+
## Create
|
127
|
+
|
128
|
+
```rb
|
129
|
+
Magento::Order.create(
|
130
|
+
customer_firstname: '',
|
131
|
+
customer_lastname: '',
|
132
|
+
customer_email: '',
|
133
|
+
# others attrbutes ...,
|
134
|
+
items: [
|
135
|
+
{
|
136
|
+
sku: '',
|
137
|
+
price: '',
|
138
|
+
qty_ordered: 1,
|
139
|
+
# others attrbutes ...,
|
140
|
+
}
|
141
|
+
],
|
142
|
+
billing_address: {
|
143
|
+
# attrbutes...
|
144
|
+
},
|
145
|
+
payment: {
|
146
|
+
# attrbutes...
|
147
|
+
},
|
148
|
+
extension_attributes: {
|
149
|
+
# attrbutes...
|
150
|
+
}
|
151
|
+
)
|
152
|
+
```
|
153
|
+
|
154
|
+
### Update
|
155
|
+
|
156
|
+
```rb
|
157
|
+
product = Magento::Product.find('sku-teste')
|
158
|
+
|
159
|
+
product.name = 'Updated name'
|
160
|
+
product.save
|
161
|
+
|
162
|
+
# or
|
163
|
+
|
164
|
+
product.update(name: 'Updated name')
|
165
|
+
```
|
166
|
+
|
167
|
+
### Delete
|
168
|
+
|
169
|
+
```rb
|
170
|
+
product = Magento::Product.find('sku-teste')
|
171
|
+
|
172
|
+
product.delete
|
173
|
+
|
174
|
+
# or
|
175
|
+
|
176
|
+
Magento::Product.delete('sku-teste')
|
177
|
+
```
|
178
|
+
|
179
|
+
___
|
180
|
+
|
181
|
+
##TODO:
|
182
|
+
|
183
|
+
### Search products
|
184
|
+
```rb
|
185
|
+
Magento::Product.search('tshort')
|
186
|
+
```
|
data/lib/magento.rb
CHANGED
@@ -1,35 +1,35 @@
|
|
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/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
|
data/lib/magento/country.rb
CHANGED
@@ -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
|
data/lib/magento/customer.rb
CHANGED
@@ -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
|
data/lib/magento/model.rb
CHANGED
@@ -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}").status.success?
|
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
|
+
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}").status.success?
|
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
|
data/lib/magento/product.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
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
|
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
|
data/lib/magento/request.rb
CHANGED
@@ -1,108 +1,108 @@
|
|
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 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,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class Address
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module Magento
|
2
|
+
class Address
|
3
|
+
end
|
4
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class AvailableRegion
|
3
|
-
end
|
4
|
-
end
|
1
|
+
module Magento
|
2
|
+
class AvailableRegion
|
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 ExtensionAttribute
|
3
|
-
end
|
1
|
+
module Magento
|
2
|
+
class ExtensionAttribute
|
3
|
+
end
|
4
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
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Magento
|
2
|
-
class Region
|
3
|
-
end
|
4
|
-
end
|
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
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'magento/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = 'magento-ruby'
|
10
|
+
s.version = Magento::VERSION
|
11
|
+
s.date = '2020-07-31'
|
12
|
+
s.summary = 'Magento Ruby library'
|
13
|
+
s.description = 'Magento Ruby library'
|
14
|
+
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
15
|
+
s.authors = ['Wallas Faria']
|
16
|
+
s.email = 'wallasfaria@hotmail.com'
|
17
|
+
s.homepage = 'https://github.com/WallasFaria/magento-ruby'
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.add_dependency 'dry-inflector', '~> 0.2.0'
|
21
|
+
s.add_dependency 'http', '~> 4.4'
|
22
|
+
end
|
data/magento.gemspec
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'magento/version'
|
7
|
+
|
3
8
|
Gem::Specification.new do |s|
|
4
9
|
s.name = 'magento'
|
5
|
-
s.version =
|
10
|
+
s.version = Magento::VERSION
|
6
11
|
s.date = '2020-07-31'
|
7
12
|
s.summary = 'Magento Ruby library'
|
8
13
|
s.description = 'Magento Ruby library'
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magento
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wallas Faria
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2020-07-31 00:00:00.000000000 Z
|
@@ -44,6 +44,7 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
+
- ".github/workflows/gem-push.yml"
|
47
48
|
- ".gitignore"
|
48
49
|
- Gemfile
|
49
50
|
- README.md
|
@@ -76,11 +77,13 @@ files:
|
|
76
77
|
- lib/magento/shared/tier_price.rb
|
77
78
|
- lib/magento/shared/total.rb
|
78
79
|
- lib/magento/shared/value.rb
|
80
|
+
- lib/magento/version.rb
|
81
|
+
- magento-ruby.gemspec
|
79
82
|
- magento.gemspec
|
80
83
|
homepage: https://github.com/WallasFaria/magento-ruby
|
81
84
|
licenses: []
|
82
85
|
metadata: {}
|
83
|
-
post_install_message:
|
86
|
+
post_install_message:
|
84
87
|
rdoc_options: []
|
85
88
|
require_paths:
|
86
89
|
- lib
|
@@ -95,8 +98,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
98
|
- !ruby/object:Gem::Version
|
96
99
|
version: '0'
|
97
100
|
requirements: []
|
98
|
-
rubygems_version: 3.0.
|
99
|
-
signing_key:
|
101
|
+
rubygems_version: 3.0.3
|
102
|
+
signing_key:
|
100
103
|
specification_version: 4
|
101
104
|
summary: Magento Ruby library
|
102
105
|
test_files: []
|