magentwo 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +55 -3
- data/lib/adapter.rb +6 -3
- data/lib/connection.rb +9 -3
- data/lib/dataset.rb +51 -4
- data/lib/filter.rb +24 -0
- data/lib/magentwo.rb +16 -1
- data/lib/model/base.rb +9 -5
- data/lib/model/category.rb +1 -0
- data/lib/model/coupon.rb +4 -0
- data/lib/model/order.rb +5 -0
- data/lib/model/product.rb +4 -0
- data/lib/model/sales_rule.rb +4 -0
- data/magentwo.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76391fb49be2eacf141856ba17a36dda1edd5b08e0e7069be28ae7bedef35736
|
4
|
+
data.tar.gz: f6faa7a572b2a0c71007852272ad227032224786c8a42691cde861213a2591c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ca9a91c6c9e7cbd6df412e71fb418bf79adc68f40c10bb165ba8af3953f1717859f6562c01f5758c4669884cfd66fb14e657a6b00a6df43599624ee092e99f8
|
7
|
+
data.tar.gz: 985a28699825066decefabdbe438afcad4d4db0105ac12df34971c68c17a70c7cf7a427352a2101daa57d8d9f517f67ac5c133b06a0cfd693cff504d5dfc20ca
|
data/README.md
CHANGED
@@ -23,9 +23,21 @@ bundle
|
|
23
23
|
|
24
24
|
|
25
25
|
# How to connect to your magento 2 shop
|
26
|
-
|
26
|
+
When only using one connection simply type
|
27
27
|
```
|
28
|
-
Magentwo.connect http://example.com,
|
28
|
+
Magentwo.connect "http://example.com", "user_name", "password"
|
29
|
+
```
|
30
|
+
When using multiple connections at once you can save the result of `Magentwo.connect` and use the `Magentwo.with` method
|
31
|
+
```
|
32
|
+
connection1 = Magentwo.connect "http://example1.com", "user_name", "password"
|
33
|
+
connection2 = Magentwo.connect "http://example2.com", "user_name", "password"
|
34
|
+
|
35
|
+
Magentwo.with (connection1) do
|
36
|
+
#do things in the context of connection1
|
37
|
+
end
|
38
|
+
Magentwo.with (connection2) do
|
39
|
+
#do things in the context of connection2
|
40
|
+
end
|
29
41
|
```
|
30
42
|
|
31
43
|
# How to use
|
@@ -67,6 +79,26 @@ Look for all Products whose name includes the word "Computer"
|
|
67
79
|
Magentwo::Product.like(:name => "%Computer%").all
|
68
80
|
```
|
69
81
|
|
82
|
+
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.
|
83
|
+
```
|
84
|
+
Magentwo::Product.lt(:price => 42).all
|
85
|
+
Magentwo::Product.gt(:id => 1337).first
|
86
|
+
```
|
87
|
+
|
88
|
+
Compare using `from` and `to`, you may also use both to specify a range.
|
89
|
+
```
|
90
|
+
Magentwo::Product.from(:updated_at => Time.new(2019, 1, 1).all
|
91
|
+
Magentwo::Product.to(:created_at => Time.new(2019, 2, 1).all
|
92
|
+
```
|
93
|
+
|
94
|
+
All of these filter-functions can be chained as needed
|
95
|
+
|
96
|
+
# Select
|
97
|
+
If you know which fields you are interested in you can speed up the fetching process by only requesting these fields
|
98
|
+
```
|
99
|
+
Magentwo::Product.filter(...).select(:id, :sku).all
|
100
|
+
```
|
101
|
+
|
70
102
|
# Pagination
|
71
103
|
On default the pagesize is set to 20, you can change this with
|
72
104
|
```
|
@@ -85,6 +117,19 @@ Magentwo::Product.each_page(512) do |page|
|
|
85
117
|
p page
|
86
118
|
end
|
87
119
|
```
|
120
|
+
You may also want to fetch all pages of products that match a certain criteria
|
121
|
+
```
|
122
|
+
Magentwo::Product.from(:updated_at => my_last_sync_value).each_page(512) do |page|
|
123
|
+
p page
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
127
|
+
# Order
|
128
|
+
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.
|
129
|
+
```
|
130
|
+
Magentwo::Product.order_by(:id, "ASC").all
|
131
|
+
Magentwo::Product.order_by(:id, "DESC").all
|
132
|
+
```
|
88
133
|
|
89
134
|
# Updates
|
90
135
|
To update Models back to Magento 2 use the `save` method
|
@@ -96,4 +141,11 @@ customer.lastname = "Foo"
|
|
96
141
|
customer.save
|
97
142
|
```
|
98
143
|
|
99
|
-
|
144
|
+
# Delete
|
145
|
+
To delete a Model use the `delete` method
|
146
|
+
```
|
147
|
+
product = Magentwo::Product.first
|
148
|
+
product.delete
|
149
|
+
```
|
150
|
+
|
151
|
+
to be continued ...
|
data/lib/adapter.rb
CHANGED
@@ -10,9 +10,10 @@ module Magentwo
|
|
10
10
|
end
|
11
11
|
|
12
12
|
response = self.send(http_method, path, params)
|
13
|
-
Magentwo.logger.debug "Response body: #{response.body}"
|
13
|
+
Magentwo.logger.debug "Response body: #{response.body}" unless response.is_a? TrueClass
|
14
|
+
|
14
15
|
parsed_response = case method
|
15
|
-
when :get_with_meta_data, :put, :post
|
16
|
+
when :get_with_meta_data, :put, :post then transform( parse( response))
|
16
17
|
when :get
|
17
18
|
parsed = parse(response)
|
18
19
|
if parsed.is_a?(Hash) && (parsed.has_key? :items)
|
@@ -22,6 +23,8 @@ module Magentwo
|
|
22
23
|
else
|
23
24
|
transform parsed
|
24
25
|
end
|
26
|
+
when :delete
|
27
|
+
response
|
25
28
|
else
|
26
29
|
raise ArgumentError, "unknown method type. Expected :get, :get_with_meta_data, :post, :put or :delete. #{method} #{path}"
|
27
30
|
end
|
@@ -47,7 +50,7 @@ module Magentwo
|
|
47
50
|
|
48
51
|
def date_transform item
|
49
52
|
DateFields.each do |date_field|
|
50
|
-
item[date_field] = Time.
|
53
|
+
item[date_field] = Time.parse item[date_field] if item[date_field]
|
51
54
|
end
|
52
55
|
item
|
53
56
|
end
|
data/lib/connection.rb
CHANGED
@@ -26,11 +26,17 @@ module Magentwo
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def delete path, data
|
29
|
-
Magentwo.logger.info "DELETE #{path}"
|
29
|
+
Magentwo.logger.info "DELETE #{host}/#{base_path}/#{path}"
|
30
30
|
Magentwo.logger.debug "DATA #{data}"
|
31
31
|
|
32
|
-
|
33
|
-
|
32
|
+
url = "#{base_path}/#{path}"
|
33
|
+
Net::HTTP.start(self.host,self.port) do |http|
|
34
|
+
req = Net::HTTP::Delete.new(url)
|
35
|
+
req["Authorization"] = "Bearer #{self.token}"
|
36
|
+
req['Content-Type'] = "application/json"
|
37
|
+
req.body = data
|
38
|
+
http.request(req)
|
39
|
+
end
|
34
40
|
end
|
35
41
|
|
36
42
|
def put path, data
|
data/lib/dataset.rb
CHANGED
@@ -64,6 +64,14 @@ module Magentwo
|
|
64
64
|
Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::Like.new(args.keys.first, args.values.first)])
|
65
65
|
end
|
66
66
|
|
67
|
+
def from args
|
68
|
+
Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::From.new(args.keys.first, args.values.first)])
|
69
|
+
end
|
70
|
+
|
71
|
+
def to args
|
72
|
+
Dataset.new self.model, self.opts.merge(:filters => self.opts[:filters] + [Filter::To.new(args.keys.first, args.values.first)])
|
73
|
+
end
|
74
|
+
|
67
75
|
#################
|
68
76
|
# Pagination
|
69
77
|
################
|
@@ -108,23 +116,55 @@ module Magentwo
|
|
108
116
|
#################
|
109
117
|
# Transformation
|
110
118
|
################
|
119
|
+
def print_readable
|
120
|
+
ds = self
|
121
|
+
|
122
|
+
puts "*** Pagination ***"
|
123
|
+
puts ds.opts[:pagination][:current_page].to_s
|
124
|
+
puts ds.opts[:pagination][:page_size].to_s
|
125
|
+
|
126
|
+
puts "*** Filters ***"
|
127
|
+
ds.opts[:filters].each do |filter|
|
128
|
+
puts filter.to_s
|
129
|
+
end
|
130
|
+
|
131
|
+
puts "*** Ordering ***"
|
132
|
+
order_filters = ds.opts[:ordering]
|
133
|
+
if order_filters.size > 0
|
134
|
+
order_filters.each do |filter|
|
135
|
+
puts filter.to_s
|
136
|
+
end
|
137
|
+
else
|
138
|
+
puts "non specified"
|
139
|
+
end
|
140
|
+
|
141
|
+
puts "*** Fields ***"
|
142
|
+
if fields = ds.opts[:fields]&.fields
|
143
|
+
puts "Fetch only: #{fields}"
|
144
|
+
else
|
145
|
+
puts "Fetch everything"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
111
149
|
def to_query
|
150
|
+
self.validate
|
151
|
+
ds = self
|
112
152
|
[
|
113
|
-
|
153
|
+
ds.opts[:filters]
|
114
154
|
.each_with_index
|
115
155
|
.map { |opt, idx| opt.to_query(idx) }
|
116
156
|
.join("&"),
|
117
157
|
|
118
|
-
|
158
|
+
ds.opts[:pagination]
|
119
159
|
.map { |k, v| v.to_query}
|
120
160
|
.join("&"),
|
121
161
|
|
122
162
|
|
123
|
-
|
163
|
+
ds.opts[:ordering]
|
124
164
|
.map { |opt, idx| opt.to_query(idx) }
|
125
165
|
.join("&"),
|
126
166
|
|
127
|
-
|
167
|
+
ds.opts[:fields]? ds.opts[:fields].to_query() : ""
|
128
168
|
].reject(&:empty?)
|
129
169
|
.join("&")
|
130
170
|
end
|
@@ -156,5 +196,12 @@ module Magentwo
|
|
156
196
|
current_page += 1
|
157
197
|
end
|
158
198
|
end
|
199
|
+
|
200
|
+
#################
|
201
|
+
# Validation
|
202
|
+
################
|
203
|
+
def validate
|
204
|
+
true
|
205
|
+
end
|
159
206
|
end
|
160
207
|
end
|
data/lib/filter.rb
CHANGED
@@ -14,6 +14,10 @@ module Magentwo
|
|
14
14
|
"searchCriteria[filter_groups][#{idx}][filters][0][condition_type]=#{self.class.name.split("::").last.downcase}"]
|
15
15
|
.join("&")
|
16
16
|
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
"#{self.field} #{self.class.name.split("::").last.downcase} #{self.value}"
|
20
|
+
end
|
17
21
|
end
|
18
22
|
|
19
23
|
class CompareArray < Compare
|
@@ -24,6 +28,10 @@ module Magentwo
|
|
24
28
|
"searchCriteria[filter_groups][#{idx}][filters][0][condition_type]=#{self.class.name.split("::").last.downcase}"]
|
25
29
|
.join("&")
|
26
30
|
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
"#{self.field} #{self.class.name.split("::").last.downcase} #{self.value}"
|
34
|
+
end
|
27
35
|
end
|
28
36
|
|
29
37
|
class Simple
|
@@ -36,6 +44,10 @@ module Magentwo
|
|
36
44
|
def to_query idx=nil
|
37
45
|
"searchCriteria[#{key}]=#{value}"
|
38
46
|
end
|
47
|
+
|
48
|
+
def to_s
|
49
|
+
"#{self.key} == #{self.value}"
|
50
|
+
end
|
39
51
|
end
|
40
52
|
|
41
53
|
class Multi
|
@@ -49,6 +61,12 @@ module Magentwo
|
|
49
61
|
"searchCriteria[#{kvp[:key]}]=#{kvp[:value]}"
|
50
62
|
end.join("&")
|
51
63
|
end
|
64
|
+
|
65
|
+
def to_s
|
66
|
+
self.kvps.map do |kvp|
|
67
|
+
"#{kvp[:key]} = #{kvp[:value]}"
|
68
|
+
end.join("\n")
|
69
|
+
end
|
52
70
|
end
|
53
71
|
|
54
72
|
|
@@ -79,6 +97,12 @@ module Magentwo
|
|
79
97
|
class Lteq < Magentwo::Filter::Compare
|
80
98
|
end
|
81
99
|
|
100
|
+
class From < Magentwo::Filter::Compare
|
101
|
+
end
|
102
|
+
|
103
|
+
class To < Magentwo::Filter::Compare
|
104
|
+
end
|
105
|
+
|
82
106
|
class PageSize < Magentwo::Filter::Simple
|
83
107
|
def initialize value
|
84
108
|
super(:page_size, value)
|
data/lib/magentwo.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'uri'
|
2
2
|
require 'net/http'
|
3
3
|
require 'json'
|
4
|
+
require 'time'
|
4
5
|
require 'logger'
|
5
6
|
|
6
7
|
module Magentwo
|
7
8
|
Models = %w(base product customer order coupon sales_rule category cart)
|
9
|
+
@@mutex = Mutex.new
|
8
10
|
def self.connect host=nil, user_name=nil, password=nil
|
9
11
|
raise ArgumentError, "no host specified" unless host
|
10
12
|
raise ArgumentError, "no user_name specified" unless user_name
|
@@ -12,12 +14,25 @@ module Magentwo
|
|
12
14
|
Base.adapter = Adapter.new host, user_name, password
|
13
15
|
end
|
14
16
|
|
17
|
+
def self.with connection
|
18
|
+
raise ArgumentError, "no connection specified" unless connection
|
19
|
+
@@mutex.synchronize do
|
20
|
+
old_connection = Magentwo::Base.adapter
|
21
|
+
begin
|
22
|
+
Magentwo::Base.adapter = connection
|
23
|
+
yield
|
24
|
+
ensure
|
25
|
+
Magentwo::Base.adapter = old_connection
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
15
30
|
def self.logger= logger
|
16
31
|
@@logger = logger
|
17
32
|
end
|
18
33
|
|
19
34
|
def self.logger
|
20
|
-
@@logger ||= Logger.new STDOUT, {:level => Logger::
|
35
|
+
@@logger ||= Logger.new STDOUT, {:level => Logger::INFO}
|
21
36
|
end
|
22
37
|
|
23
38
|
def self.default_page_size
|
data/lib/model/base.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Magentwo
|
2
2
|
class Base
|
3
|
-
DatasetMethods = %i(filter exclude select fields count fields info page order_by like gt lt gteq lteq)
|
3
|
+
DatasetMethods = %i(filter exclude select fields count fields info page order_by like gt lt gteq lteq from to)
|
4
4
|
|
5
5
|
def initialize args
|
6
6
|
args.each do |key, value|
|
@@ -18,12 +18,12 @@ module Magentwo
|
|
18
18
|
|
19
19
|
def save
|
20
20
|
self.validate
|
21
|
-
response = Magentwo::Base.call :put, "#{self.class.base_path}/#{self.
|
21
|
+
response = Magentwo::Base.call :put, "#{self.class.base_path}/#{self.send(self.class.unique_identifier)}", self
|
22
22
|
self.class.new response
|
23
23
|
end
|
24
24
|
|
25
25
|
def delete
|
26
|
-
Magentwo.
|
26
|
+
Magentwo::Base.call :delete, "#{self.class.base_path}/#{self.send(self.class.unique_identifier)}", nil
|
27
27
|
end
|
28
28
|
|
29
29
|
def validate
|
@@ -62,8 +62,12 @@ module Magentwo
|
|
62
62
|
class << self
|
63
63
|
attr_accessor :adapter
|
64
64
|
|
65
|
-
def []
|
66
|
-
self.new (Magentwo::Base.get nil, path:"#{base_path}/#{
|
65
|
+
def [] unique_identifier_value
|
66
|
+
self.new (Magentwo::Base.get nil, path:"#{base_path}/#{unique_identifier_value}")
|
67
|
+
end
|
68
|
+
|
69
|
+
def unique_identifier
|
70
|
+
:id
|
67
71
|
end
|
68
72
|
|
69
73
|
def lower_case_name
|
data/lib/model/category.rb
CHANGED
data/lib/model/coupon.rb
CHANGED
data/lib/model/order.rb
CHANGED
@@ -19,6 +19,11 @@ module Magentwo
|
|
19
19
|
def [] unique_identifier
|
20
20
|
self.filter(:entity_id => unique_identifier).first
|
21
21
|
end
|
22
|
+
|
23
|
+
def unique_identifier
|
24
|
+
Magentwo::Logger.error "orders do not container id on default requests, therefore they cannot be targeted on the API"
|
25
|
+
nil
|
26
|
+
end
|
22
27
|
end
|
23
28
|
end
|
24
29
|
end
|
data/lib/model/product.rb
CHANGED
data/lib/model/sales_rule.rb
CHANGED
data/magentwo.gemspec
CHANGED