magentwo 0.1.83 → 0.1.91

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8971d5fe11cfc1ec134a18f9af85f9dcca858ca2068ed85a7cc7af82b520105b
4
- data.tar.gz: 70c047455ec6ca0773f4274c21b5bd1c7af151972c6adbab99e1aced62b1fd04
3
+ metadata.gz: 15cb810f21e6410e34b764567e40378ca481d8b2ba9133a6981a2b6738818c8a
4
+ data.tar.gz: c5f1fe3851888baf2d6371e849fa44a87087c54ea91adcf574ed2ff79f384d3b
5
5
  SHA512:
6
- metadata.gz: b564e84256ee706f9f0ac8fa5437bc3cdd8ece89bae0467181eddce99fd9747f12ee1c6d758e7dcc6ee8dfe24bd5ef20981a89c453f38efdfb64845dd8465954
7
- data.tar.gz: 05f575ebbb8541056bddc170f441f0ae99e4c89b54a3bdef0b9e390c44ce89e6dd53208dc008894843b38413fef6c883ebae2f4cbae413ad614b1b1a4850164d
6
+ metadata.gz: a18862fc3e2792a23539d840ffea2591d90c232ce17d2fa0d114707d759554bbd114bcd726c7d1a5bc13ce67bb4c9c7d482ff038d0e2e98ca4d55eaec4e6535c
7
+ data.tar.gz: 49fca1ac96a80a6a3373f65340fd1e5bc8861fba62c3957b0bf6e2d4154c94ee90ecb5dd98a4e89265dd3e16acbca2104e8c2f76bbbd7813a17dc00cd6345863
data/.gitignore CHANGED
@@ -52,3 +52,4 @@ build-iPhoneSimulator/
52
52
  .rvmrc
53
53
 
54
54
  /cplus
55
+ run.rb
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.connect "http://example2.com", "user_name", "password"
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 "request failed #{response.code} #{response.body}"
40
+ puts "error #{response.code}: #{JSON.parse(response.body)}"
41
+ return nil
41
42
  end
42
43
  end
43
44
 
data/lib/connection.rb CHANGED
@@ -2,15 +2,23 @@ module Magentwo
2
2
  class Connection
3
3
  attr_accessor :host, :port, :user, :password, :token, :base_path, :scheme
4
4
 
5
- def initialize uri, user, password, base_path:nil
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
- @user = user
10
9
  @scheme = uri.scheme
11
- @password = password
12
10
  @base_path = base_path || "/rest/V1"
13
- request_token
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
+
14
22
  end
15
23
 
16
24
  def request_token
@@ -26,13 +34,13 @@ module Magentwo
26
34
  end
27
35
  end
28
36
 
29
- def delete path, data
30
- Magentwo.logger.info "DELETE #{host}/#{base_path}/#{path}"
37
+ def request verb, path:, data:nil
38
+ Magentwo.logger.info "#{verb.to_s} #{host}/#{base_path}/#{path}"
31
39
  Magentwo.logger.debug "DATA #{data}"
32
40
 
33
41
  url = "#{base_path}/#{path}"
34
42
  Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
35
- req = Net::HTTP::Delete.new(url)
43
+ req = verb.new(url)
36
44
  req["Authorization"] = "Bearer #{self.token}"
37
45
  req['Content-Type'] = "application/json"
38
46
  req.body = data
@@ -40,42 +48,20 @@ module Magentwo
40
48
  end
41
49
  end
42
50
 
51
+ def delete path, data
52
+ request Net::HTTP::Delete, path:path, data:data
53
+ end
54
+
43
55
  def put path, data
44
- Magentwo.logger.info "PUT #{host}/#{base_path}/#{path}"
45
- Magentwo.logger.debug "DATA #{data}"
46
- url = "#{base_path}/#{path}"
47
- Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
48
- req = Net::HTTP::Put.new(url)
49
- req["Authorization"] = "Bearer #{self.token}"
50
- req['Content-Type'] = "application/json"
51
- req.body = data
52
- http.request(req)
53
- end
56
+ request Net::HTTP::Put, path:path, data:data
54
57
  end
55
58
 
56
59
  def post path, data
57
- Magentwo.logger.info "POST #{host}/#{path}"
58
- Magentwo.logger.debug "DATA #{data}"
59
- url = "#{base_path}/#{path}"
60
- Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
61
- req = Net::HTTP::Post.new(url)
62
- req["Authorization"] = "Bearer #{self.token}"
63
- req['Content-Type'] = "application/json"
64
- req.body = data
65
- http.request(req)
66
- end
60
+ request Net::HTTP::Put, path:path, data:data
67
61
  end
68
62
 
69
-
70
63
  def get path, query
71
- Magentwo.logger.info "GET #{host}#{base_path}/#{path}?#{query}"
72
- url = "#{base_path}/#{path}?#{query}"
73
- Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
74
- req = Net::HTTP::Get.new(url)
75
- req["Authorization"] = "Bearer #{self.token}"
76
- req['Content-Type'] = "application/json"
77
- http.request(req)
78
- end
64
+ request Net::HTTP::Get, path:"#{path}?#{query}"
79
65
  end
80
66
  end
81
- end
67
+ end
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
- self.new (Magentwo::Base.get nil, path:"#{base_path}/#{unique_identifier_value}")
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
- self.new self.get(ds.page(1, 1).to_query).first
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'magentwo'
3
- s.version = '0.1.83'
3
+ s.version = '0.1.91'
4
4
  s.date = '2019-01-17'
5
5
  s.summary = "Magento 2 API Wrapper"
6
6
  s.description = "Provides a simple Ruby Interface to interact with the Magento 2 API"
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.83
4
+ version: 0.1.91
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.0.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: []