magentwo 0.1.9 → 0.1.81

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