magentwo 0.1.8 → 0.1.9
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 +4 -4
- data/.gitignore +2 -0
- data/README.md +43 -6
- data/lib/adapter.rb +3 -2
- data/lib/connection.rb +19 -10
- data/lib/dataset.rb +1 -1
- data/lib/filter.rb +8 -1
- data/lib/magentwo.rb +8 -2
- data/lib/model/base.rb +5 -2
- data/lib/model/product.rb +4 -0
- data/lib/model/stock_item.rb +12 -0
- data/magentwo.gemspec +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20edcef61f1dd6dcb2d941e61c8c2ec307b3b34f5a8cc20a6bd4b7e5ac13fb78
|
4
|
+
data.tar.gz: 69d77fdbd17a9379a6f17427a3e2095b30aa8f4d36e8f4dc0cba6400767439e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0e08d0b6d433b7e3c8a67a0c1e31a1e15784facf810c3f959cf4c078feb38f3dfde82bc84d55cb3f8ee1fb815b2c87fcfb8273b2ec6bad2598a8f8683c6c2c3
|
7
|
+
data.tar.gz: 6068da51a75fa3cf357354341d9c8a8b27976321454ae44dc0a6e7294db97b57e737cc5f4dedb05f81691f587f33630cabe71265ae3c794e98687147a12839e0
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,37 +1,50 @@
|
|
1
|
-
|
2
1
|
This gem is under developement and nowhere near finished but feel free to play around with it.
|
3
2
|
I am grateful for any ideas and suggestions
|
4
3
|
|
5
4
|
# Magentwo
|
6
|
-
Ruby-Wrapper for the Magento 2 REST API
|
7
5
|
|
6
|
+
Ruby-Wrapper for the Magento 2 REST API
|
8
7
|
|
9
8
|
# How to install
|
9
|
+
|
10
10
|
To install the Gem directly use
|
11
|
+
|
11
12
|
```
|
12
13
|
gem install magentwo
|
13
14
|
```
|
14
15
|
|
15
16
|
or add the following line to your Gemfile
|
17
|
+
|
16
18
|
```
|
17
19
|
gem 'magentwo'
|
18
20
|
```
|
21
|
+
|
19
22
|
and call bundler
|
23
|
+
|
20
24
|
```
|
21
25
|
bundle
|
22
26
|
```
|
23
27
|
|
24
|
-
|
25
28
|
# How to connect to your magento 2 shop
|
29
|
+
|
26
30
|
When only using one connection simply type
|
31
|
+
|
27
32
|
```
|
28
|
-
Magentwo.connect "http://example.com", "user_name", "password"
|
33
|
+
Magentwo.connect "http://example.com", "user_name", "password"
|
29
34
|
```
|
35
|
+
|
36
|
+
or
|
37
|
+
|
38
|
+
```
|
39
|
+
Magentwo.connect_with_token "http://example.com", "my_secret_token"
|
40
|
+
```
|
41
|
+
|
30
42
|
When using multiple connections at once you can save the result of `Magentwo.connect` and use the `Magentwo.with` method
|
43
|
+
|
31
44
|
```
|
32
45
|
connection1 = Magentwo.connect "http://example1.com", "user_name", "password"
|
33
|
-
connection2 = Magentwo.
|
34
|
-
|
46
|
+
connection2 = Magentwo.connect_with_token "http://example2.com", "my_secret_token"
|
47
|
+
|
35
48
|
Magentwo.with (connection1) do
|
36
49
|
#do things in the context of connection1
|
37
50
|
end
|
@@ -41,6 +54,7 @@ When using multiple connections at once you can save the result of `Magentwo.con
|
|
41
54
|
```
|
42
55
|
|
43
56
|
# How to use
|
57
|
+
|
44
58
|
In Magentwo you interact with the API using Models. These are named according the the REST-API specifications of Magento 2
|
45
59
|
The basic functionality is the same for all Models. For products some simple requests would look like this
|
46
60
|
|
@@ -52,40 +66,49 @@ Magentwo::Product.fields #returns an array of productfields
|
|
52
66
|
```
|
53
67
|
|
54
68
|
# Filtering
|
69
|
+
|
55
70
|
You can filter requests to search for specific elements
|
56
71
|
Here are some examples
|
57
72
|
|
58
73
|
Look for all customers whose firstname is Foobar
|
74
|
+
|
59
75
|
```
|
60
76
|
Magentwo::Customer.filter(:firstname => "Foobar").all
|
61
77
|
```
|
62
78
|
|
63
79
|
Look for all customers whose id is not 42
|
80
|
+
|
64
81
|
```
|
65
82
|
Magentwo::Customer.exclude(:id => 42).all
|
66
83
|
```
|
67
84
|
|
68
85
|
You can also combine these
|
86
|
+
|
69
87
|
```
|
70
88
|
Magentwo::Customer.filter(:firstname => "Foobar").exclude(:id => 42).all
|
71
89
|
```
|
90
|
+
|
72
91
|
The `filter` and `exclude` methods can also be used to filter for a set. To Request all Customers whose firstname is either Foo or Bar you could write
|
92
|
+
|
73
93
|
```
|
74
94
|
Magentwo::Customer.filter(:firstname => ["Foo", "bar"]).all
|
75
95
|
```
|
76
96
|
|
77
97
|
Look for all Products whose name includes the word "Computer"
|
98
|
+
|
78
99
|
```
|
79
100
|
Magentwo::Product.like(:name => "%Computer%").all
|
80
101
|
```
|
81
102
|
|
82
103
|
Compare using `gt`, `gteq`, `lt` or `lteq`. These methods do not seem to work with dates, please use `from` and `to` when e.g. trying to fetch all Products that changed within a certain period.
|
104
|
+
|
83
105
|
```
|
84
106
|
Magentwo::Product.lt(:price => 42).all
|
85
107
|
Magentwo::Product.gt(:id => 1337).first
|
86
108
|
```
|
87
109
|
|
88
110
|
Compare using `from` and `to`, you may also use both to specify a range.
|
111
|
+
|
89
112
|
```
|
90
113
|
Magentwo::Product.from(:updated_at => Time.new(2019, 1, 1).all
|
91
114
|
Magentwo::Product.to(:created_at => Time.new(2019, 2, 1).all
|
@@ -94,30 +117,38 @@ Magentwo::Product.to(:created_at => Time.new(2019, 2, 1).all
|
|
94
117
|
All of these filter-functions can be chained as needed
|
95
118
|
|
96
119
|
# Select
|
120
|
+
|
97
121
|
If you know which fields you are interested in you can speed up the fetching process by only requesting these fields
|
122
|
+
|
98
123
|
```
|
99
124
|
Magentwo::Product.filter(...).select(:id, :sku).all
|
100
125
|
```
|
101
126
|
|
102
127
|
# Pagination
|
128
|
+
|
103
129
|
On default the pagesize is set to 20, you can change this with
|
130
|
+
|
104
131
|
```
|
105
132
|
Magentwo.default_page_size=42
|
106
133
|
```
|
107
134
|
|
108
135
|
The pagesize can also be set on the fly
|
109
136
|
To request page 2 with a pagesize of 100 simply write the following. The second paramter is optional
|
137
|
+
|
110
138
|
```
|
111
139
|
Magentwo::Product.exclude(:name => "foobar").page(2, 100).all
|
112
140
|
```
|
113
141
|
|
114
142
|
To iterate threw all the pages use `each_page`. Again the pagesize parameter is optional
|
143
|
+
|
115
144
|
```
|
116
145
|
Magentwo::Product.each_page(512) do |page|
|
117
146
|
p page
|
118
147
|
end
|
119
148
|
```
|
149
|
+
|
120
150
|
You may also want to fetch all pages of products that match a certain criteria
|
151
|
+
|
121
152
|
```
|
122
153
|
Magentwo::Product.from(:updated_at => my_last_sync_value).each_page(512) do |page|
|
123
154
|
p page
|
@@ -125,15 +156,19 @@ end
|
|
125
156
|
```
|
126
157
|
|
127
158
|
# Order
|
159
|
+
|
128
160
|
By default the results are ordered as Magento2 "thinks" its best. At any place you may add the `order_by` to sepcify this to your liking. If you skip the `ASC/DESC` argument, `ASC` will be set.
|
161
|
+
|
129
162
|
```
|
130
163
|
Magentwo::Product.order_by(:id, "ASC").all
|
131
164
|
Magentwo::Product.order_by(:id, "DESC").all
|
132
165
|
```
|
133
166
|
|
134
167
|
# Updates
|
168
|
+
|
135
169
|
To update Models back to Magento 2 use the `save` method
|
136
170
|
This switches the first and last name of the Customer Foo Bar
|
171
|
+
|
137
172
|
```
|
138
173
|
customer = Magentwo::Customer.filter(:first_name => "Foo", :last_name => "Bar").first
|
139
174
|
customer.firstname = "Bar"
|
@@ -142,7 +177,9 @@ customer.save
|
|
142
177
|
```
|
143
178
|
|
144
179
|
# Delete
|
180
|
+
|
145
181
|
To delete a Model use the `delete` method
|
182
|
+
|
146
183
|
```
|
147
184
|
product = Magentwo::Product.first
|
148
185
|
product.delete
|
data/lib/adapter.rb
CHANGED
@@ -37,7 +37,8 @@ module Magentwo
|
|
37
37
|
when "200"
|
38
38
|
JSON.parse response.body, :symbolize_names => true
|
39
39
|
else
|
40
|
-
puts "
|
40
|
+
puts "error #{response.code}: #{JSON.parse(response.body)}"
|
41
|
+
return nil
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
@@ -54,7 +55,7 @@ module Magentwo
|
|
54
55
|
item[date_field] = Date.parse item[date_field] if item[date_field]
|
55
56
|
end
|
56
57
|
DateTimeFields.each do |datetime_field|
|
57
|
-
item[datetime_field] = Time.parse item[datetime_field] if item[datetime_field]
|
58
|
+
item[datetime_field] = (Time.parse "#{item[datetime_field]} UTC").getlocal if item[datetime_field]
|
58
59
|
end
|
59
60
|
item
|
60
61
|
end
|
data/lib/connection.rb
CHANGED
@@ -1,19 +1,28 @@
|
|
1
1
|
module Magentwo
|
2
2
|
class Connection
|
3
|
-
attr_accessor :host, :port, :user, :password, :token, :base_path
|
3
|
+
attr_accessor :host, :port, :user, :password, :token, :base_path, :scheme
|
4
4
|
|
5
|
-
def initialize uri
|
5
|
+
def initialize uri:, user:nil, password:nil, base_path:nil, token:nil
|
6
6
|
uri = URI(uri)
|
7
7
|
@host = uri.host
|
8
8
|
@port = uri.port
|
9
|
-
@
|
10
|
-
@password = password
|
9
|
+
@scheme = uri.scheme
|
11
10
|
@base_path = base_path || "/rest/V1"
|
12
|
-
|
11
|
+
|
12
|
+
if (user && password)
|
13
|
+
@user = user
|
14
|
+
@password = password
|
15
|
+
request_token
|
16
|
+
elsif (token)
|
17
|
+
@token = token
|
18
|
+
else
|
19
|
+
raise ArgumentError, "expected user/password or token"
|
20
|
+
end
|
21
|
+
|
13
22
|
end
|
14
23
|
|
15
24
|
def request_token
|
16
|
-
Net::HTTP.start(self.host,self.port) do |http|
|
25
|
+
Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
|
17
26
|
url = "#{base_path}/integration/admin/token"
|
18
27
|
Magentwo.logger.info "POST #{url}"
|
19
28
|
req = Net::HTTP::Post.new(url)
|
@@ -30,7 +39,7 @@ module Magentwo
|
|
30
39
|
Magentwo.logger.debug "DATA #{data}"
|
31
40
|
|
32
41
|
url = "#{base_path}/#{path}"
|
33
|
-
Net::HTTP.start(self.host,self.port) do |http|
|
42
|
+
Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
|
34
43
|
req = Net::HTTP::Delete.new(url)
|
35
44
|
req["Authorization"] = "Bearer #{self.token}"
|
36
45
|
req['Content-Type'] = "application/json"
|
@@ -43,7 +52,7 @@ module Magentwo
|
|
43
52
|
Magentwo.logger.info "PUT #{host}/#{base_path}/#{path}"
|
44
53
|
Magentwo.logger.debug "DATA #{data}"
|
45
54
|
url = "#{base_path}/#{path}"
|
46
|
-
Net::HTTP.start(self.host,self.port) do |http|
|
55
|
+
Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
|
47
56
|
req = Net::HTTP::Put.new(url)
|
48
57
|
req["Authorization"] = "Bearer #{self.token}"
|
49
58
|
req['Content-Type'] = "application/json"
|
@@ -56,7 +65,7 @@ module Magentwo
|
|
56
65
|
Magentwo.logger.info "POST #{host}/#{path}"
|
57
66
|
Magentwo.logger.debug "DATA #{data}"
|
58
67
|
url = "#{base_path}/#{path}"
|
59
|
-
Net::HTTP.start(self.host,self.port) do |http|
|
68
|
+
Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
|
60
69
|
req = Net::HTTP::Post.new(url)
|
61
70
|
req["Authorization"] = "Bearer #{self.token}"
|
62
71
|
req['Content-Type'] = "application/json"
|
@@ -69,7 +78,7 @@ module Magentwo
|
|
69
78
|
def get path, query
|
70
79
|
Magentwo.logger.info "GET #{host}#{base_path}/#{path}?#{query}"
|
71
80
|
url = "#{base_path}/#{path}?#{query}"
|
72
|
-
Net::HTTP.start(self.host,self.port) do |http|
|
81
|
+
Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
|
73
82
|
req = Net::HTTP::Get.new(url)
|
74
83
|
req["Authorization"] = "Bearer #{self.token}"
|
75
84
|
req['Content-Type'] = "application/json"
|
data/lib/dataset.rb
CHANGED
@@ -188,7 +188,7 @@ module Magentwo
|
|
188
188
|
received_element_count = page_size
|
189
189
|
current_page = 1
|
190
190
|
total_count = nil
|
191
|
-
until(total_count && current_page*page_size > total_count) do
|
191
|
+
until(total_count && current_page*page_size > (total_count + page_size)) do
|
192
192
|
page = self.page(current_page, page_size).all meta_data:true
|
193
193
|
total_count = page[:total_count] unless total_count
|
194
194
|
block.call(page[:items])
|
data/lib/filter.rb
CHANGED
@@ -7,7 +7,7 @@ module Magentwo
|
|
7
7
|
@value = value
|
8
8
|
end
|
9
9
|
|
10
|
-
def to_query idx
|
10
|
+
def to_query idx, field:self.field, value:self.value
|
11
11
|
[
|
12
12
|
"searchCriteria[filter_groups][#{idx}][filters][0][field]=#{self.field}",
|
13
13
|
"searchCriteria[filter_groups][#{idx}][filters][0][value]=#{URI::encode(self.value.to_s)}",
|
@@ -98,6 +98,13 @@ module Magentwo
|
|
98
98
|
end
|
99
99
|
|
100
100
|
class From < Magentwo::Filter::Compare
|
101
|
+
def to_query idx
|
102
|
+
value = case self.value
|
103
|
+
when Time then self.value.utc
|
104
|
+
else self.value
|
105
|
+
end
|
106
|
+
super idx, value:value
|
107
|
+
end
|
101
108
|
end
|
102
109
|
|
103
110
|
class To < Magentwo::Filter::Compare
|
data/lib/magentwo.rb
CHANGED
@@ -5,13 +5,19 @@ require 'time'
|
|
5
5
|
require 'logger'
|
6
6
|
|
7
7
|
module Magentwo
|
8
|
-
Models = %w(base product customer order coupon sales_rule category cart)
|
8
|
+
Models = %w(base product customer order coupon sales_rule category cart stock_item)
|
9
9
|
@@mutex = Mutex.new
|
10
10
|
def self.connect host=nil, user_name=nil, password=nil
|
11
11
|
raise ArgumentError, "no host specified" unless host
|
12
12
|
raise ArgumentError, "no user_name specified" unless user_name
|
13
13
|
raise ArgumentError, "no password specified" unless password
|
14
|
-
Base.adapter = Adapter.new host, user_name, password
|
14
|
+
Base.adapter = Adapter.new ({uri: host, user: user_name, password: password})
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.connect_with_token host=nil, token=nil
|
18
|
+
raise ArgumentError, "no host specified" unless host
|
19
|
+
raise ArgumentError, "no token specified" unless token
|
20
|
+
Base.adapter = Adapter.new ({token: token, uri: host})
|
15
21
|
end
|
16
22
|
|
17
23
|
def self.with connection
|
data/lib/model/base.rb
CHANGED
@@ -65,7 +65,8 @@ module Magentwo
|
|
65
65
|
attr_accessor :adapter
|
66
66
|
|
67
67
|
def [] unique_identifier_value
|
68
|
-
|
68
|
+
result = Magentwo::Base.get nil, path:"#{base_path}/#{unique_identifier_value}"
|
69
|
+
self.new result if result
|
69
70
|
end
|
70
71
|
|
71
72
|
def unique_identifier
|
@@ -87,6 +88,7 @@ module Magentwo
|
|
87
88
|
|
88
89
|
def all ds=self.dataset, meta_data:false
|
89
90
|
response = self.get(ds.to_query, :meta_data => meta_data)
|
91
|
+
return [] if response.nil?
|
90
92
|
items = (meta_data ? response[:items] : response)
|
91
93
|
.map do |item|
|
92
94
|
self.new item
|
@@ -100,7 +102,8 @@ module Magentwo
|
|
100
102
|
end
|
101
103
|
|
102
104
|
def first ds=self.dataset
|
103
|
-
|
105
|
+
response = self.get(ds.page(1, 1).to_query).first
|
106
|
+
self.new response if response
|
104
107
|
end
|
105
108
|
|
106
109
|
def each_page page_size=Magentwo.default_page_size, &block
|
data/lib/model/product.rb
CHANGED
@@ -3,6 +3,10 @@ module Magentwo
|
|
3
3
|
Attributes = %i(id sku name attribute_set_id price status visibility type_id created_at updated_at extension_attributes product_links options media_gallery_entries tier_prices custom_attributes)
|
4
4
|
Attributes.each do |attr| attr_accessor attr end
|
5
5
|
|
6
|
+
def stocks
|
7
|
+
Magentwo::StockItem[self.sku]
|
8
|
+
end
|
9
|
+
|
6
10
|
class << self
|
7
11
|
def types
|
8
12
|
Magentwo::Base.get nil, path:"#{base_path}/types"
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# Usage:
|
2
|
+
# Magentwo::StockItems['sku']
|
3
|
+
|
4
|
+
module Magentwo
|
5
|
+
class StockItem < Base
|
6
|
+
Attributes = %i(item_id product_id stock_id qty is_in_stock is_qty_decimal show_default_notification_message use_config_min_qty min_qty use_config_min_sale_qty min_sale_qty use_config_max_sale_qty max_sale_qty use_config_backorders backorders use_config_notify_stock_qty notify_stock_qty use_config_qty_increments qty_increments use_config_enable_qty_inc enable_qty_increments use_config_manage_stock manage_stock low_stock_date is_decimal_divided stock_status_changed_auto)
|
7
|
+
Attributes.each do |attr| attr_accessor attr end
|
8
|
+
|
9
|
+
class << self
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/magentwo.gemspec
CHANGED
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magentwo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- André Mueß
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2019-01-17 00:00:00.000000000 Z
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- lib/model/order.rb
|
35
35
|
- lib/model/product.rb
|
36
36
|
- lib/model/sales_rule.rb
|
37
|
+
- lib/model/stock_item.rb
|
37
38
|
- lib/util/validator.rb
|
38
39
|
- magentwo.gemspec
|
39
40
|
- spec/base_model_spec.rb
|
@@ -45,7 +46,7 @@ licenses:
|
|
45
46
|
- MIT
|
46
47
|
metadata:
|
47
48
|
source_code_uri: https://github.com/Arkad82x/magentwo
|
48
|
-
post_install_message:
|
49
|
+
post_install_message:
|
49
50
|
rdoc_options: []
|
50
51
|
require_paths:
|
51
52
|
- lib
|
@@ -60,8 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
61
|
- !ruby/object:Gem::Version
|
61
62
|
version: '0'
|
62
63
|
requirements: []
|
63
|
-
rubygems_version: 3.
|
64
|
-
signing_key:
|
64
|
+
rubygems_version: 3.2.15
|
65
|
+
signing_key:
|
65
66
|
specification_version: 4
|
66
67
|
summary: Magento 2 API Wrapper
|
67
68
|
test_files: []
|