plenty_client 0.0.2 → 0.0.3

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.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/README.md +9 -36
  4. data/Rakefile +3 -3
  5. data/lib/plenty_client.rb +8 -5
  6. data/lib/plenty_client/accounting.rb +2 -2
  7. data/lib/plenty_client/authentication.rb +2 -2
  8. data/lib/plenty_client/authorization.rb +2 -2
  9. data/lib/plenty_client/basket.rb +2 -2
  10. data/lib/plenty_client/basket/item.rb +2 -2
  11. data/lib/plenty_client/category.rb +6 -7
  12. data/lib/plenty_client/category/branch.rb +2 -2
  13. data/lib/plenty_client/category/template.rb +3 -3
  14. data/lib/plenty_client/comment.rb +3 -3
  15. data/lib/plenty_client/config.rb +3 -1
  16. data/lib/plenty_client/endpoint.rb +74 -68
  17. data/lib/plenty_client/item.rb +7 -2
  18. data/lib/plenty_client/item/attribute.rb +3 -3
  19. data/lib/plenty_client/item/attribute/name.rb +12 -20
  20. data/lib/plenty_client/item/attribute/value.rb +13 -18
  21. data/lib/plenty_client/item/attribute/value_name.rb +12 -12
  22. data/lib/plenty_client/item/barcode.rb +3 -3
  23. data/lib/plenty_client/item/cross_selling.rb +3 -3
  24. data/lib/plenty_client/item/image.rb +6 -6
  25. data/lib/plenty_client/item/image/availability.rb +10 -10
  26. data/lib/plenty_client/item/image/name.rb +14 -24
  27. data/lib/plenty_client/item/manufacturer.rb +4 -4
  28. data/lib/plenty_client/item/manufacturer/commission.rb +3 -3
  29. data/lib/plenty_client/item/property.rb +3 -3
  30. data/lib/plenty_client/item/property/market_reference.rb +11 -11
  31. data/lib/plenty_client/item/property/name.rb +12 -17
  32. data/lib/plenty_client/item/property_group.rb +3 -3
  33. data/lib/plenty_client/item/property_group_name.rb +8 -10
  34. data/lib/plenty_client/item/sales_price.rb +3 -4
  35. data/lib/plenty_client/item/shipping_profile.rb +6 -6
  36. data/lib/plenty_client/item/unit.rb +3 -4
  37. data/lib/plenty_client/item/unit_name.rb +12 -11
  38. data/lib/plenty_client/item/variation.rb +10 -4
  39. data/lib/plenty_client/item/variation/barcode.rb +3 -3
  40. data/lib/plenty_client/item/variation/bundle.rb +14 -16
  41. data/lib/plenty_client/item/variation/category.rb +8 -10
  42. data/lib/plenty_client/item/variation/client.rb +3 -3
  43. data/lib/plenty_client/item/variation/default_category.rb +3 -3
  44. data/lib/plenty_client/item/variation/description.rb +3 -3
  45. data/lib/plenty_client/item/variation/image.rb +5 -4
  46. data/lib/plenty_client/item/variation/market.rb +3 -3
  47. data/lib/plenty_client/item/variation/market_ident_number.rb +3 -3
  48. data/lib/plenty_client/item/variation/sales_price.rb +13 -17
  49. data/lib/plenty_client/item/variation/sku.rb +12 -15
  50. data/lib/plenty_client/item/variation/stock.rb +3 -3
  51. data/lib/plenty_client/item/variation/supplier.rb +12 -15
  52. data/lib/plenty_client/item/variation/warehouse.rb +6 -5
  53. data/lib/plenty_client/item/variation_property.rb +3 -3
  54. data/lib/plenty_client/item/variation_property_value.rb +10 -14
  55. data/lib/plenty_client/item_set.rb +4 -4
  56. data/lib/plenty_client/item_set/component.rb +15 -22
  57. data/lib/plenty_client/item_set/config.rb +3 -2
  58. data/lib/plenty_client/order.rb +5 -4
  59. data/lib/plenty_client/order/contact_wish.rb +2 -3
  60. data/lib/plenty_client/order/coupon_code.rb +5 -7
  61. data/lib/plenty_client/order/coupon_code/contact.rb +4 -6
  62. data/lib/plenty_client/order/coupon_code/validation.rb +4 -6
  63. data/lib/plenty_client/order/currency.rb +2 -3
  64. data/lib/plenty_client/order/date.rb +3 -4
  65. data/lib/plenty_client/order/item.rb +3 -3
  66. data/lib/plenty_client/order/item/date.rb +2 -2
  67. data/lib/plenty_client/order/item/property.rb +2 -2
  68. data/lib/plenty_client/order/item/serial_number.rb +3 -3
  69. data/lib/plenty_client/order/property.rb +2 -3
  70. data/lib/plenty_client/order/referrer.rb +2 -3
  71. data/lib/plenty_client/order/shipping.rb +2 -3
  72. data/lib/plenty_client/order/shipping/country.rb +2 -2
  73. data/lib/plenty_client/order/shipping/information.rb +3 -3
  74. data/lib/plenty_client/order/shipping/package.rb +4 -4
  75. data/lib/plenty_client/order/shipping/profile.rb +2 -2
  76. data/lib/plenty_client/order/shipping/service_provider.rb +2 -2
  77. data/lib/plenty_client/request.rb +121 -98
  78. data/lib/plenty_client/sales_price/account.rb +2 -3
  79. data/lib/plenty_client/sales_price/country.rb +2 -2
  80. data/lib/plenty_client/sales_price/currency.rb +2 -2
  81. data/lib/plenty_client/sales_price/customer_class.rb +2 -2
  82. data/lib/plenty_client/sales_price/name.rb +4 -4
  83. data/lib/plenty_client/sales_price/online_store.rb +2 -3
  84. data/lib/plenty_client/sales_price/referrer.rb +3 -2
  85. data/lib/plenty_client/version.rb +1 -1
  86. metadata +2 -2
@@ -1,8 +1,9 @@
1
1
  module PlentyClient
2
2
  module ItemSet
3
3
  class Config
4
- extend PlentyClient::Endpoint
5
- extend PlentyClient::Request
4
+ include PlentyClient::Endpoint
5
+ include PlentyClient::Request
6
+
6
7
  FIND_ITEM_SET_CONFIGS = '/item_sets/{itemSetId}/config'.freeze
7
8
  UPDATE_ITEM_SET_CONFIGS = '/item_sets/{itemSetId}/config'.freeze
8
9
 
@@ -1,7 +1,7 @@
1
1
  module PlentyClient
2
2
  module Order
3
- extend PlentyClient::Endpoint
4
- extend PlentyClient::Request
3
+ include PlentyClient::Endpoint
4
+ include PlentyClient::Request
5
5
 
6
6
  CREATE_ORDER = '/orders'.freeze
7
7
  LIST_ORDERS = '/orders'.freeze
@@ -23,7 +23,8 @@ module PlentyClient
23
23
  def list_contacts_orders(contact_id, headers = {}, &block)
24
24
  get(build_endpoint(LIST_CONTACTS_ORDERS, contact: contact_id), headers, &block)
25
25
  end
26
- def list_contacts_orders(order_id, headers = {}, &block)
26
+
27
+ def list_package_numbers(order_id, headers = {}, &block)
27
28
  get(build_endpoint(LIST_ORDERS_PACKAGE_NUMBERS, order: order_id), headers, &block)
28
29
  end
29
30
 
@@ -35,7 +36,7 @@ module PlentyClient
35
36
  put(build_endpoint(UPDATE_ORDER, order: order_id), body)
36
37
  end
37
38
 
38
- def delete(order_id, body = {})
39
+ def destroy(order_id, body = {})
39
40
  delete(build_endpoint(DELETE_ORDER, order: order_id), body)
40
41
  end
41
42
  end
@@ -1,8 +1,8 @@
1
1
  module PlentyClient
2
2
  module Order
3
3
  module ContactWish
4
- extend PlentyClient::Endpoint
5
- extend PlentyClient::Request
4
+ include PlentyClient::Endpoint
5
+ include PlentyClient::Request
6
6
 
7
7
  FIND_ORDER_COTACT_WISH = '/orders/{orderId}/contactWish'.freeze
8
8
 
@@ -14,4 +14,3 @@ module PlentyClient
14
14
  end
15
15
  end
16
16
  end
17
-
@@ -1,19 +1,17 @@
1
1
  module PlentyClient
2
2
  module Order
3
3
  module CouponCode
4
- extend PlentyClient::Endpoint
5
- extend PlentyClient::Request
4
+ include PlentyClient::Endpoint
5
+ include PlentyClient::Request
6
6
 
7
- UPDATE_COUPON_CODE = '/orders/{orderId}/coupons/{coupon}'.freeze
7
+ UPDATE_COUPON_CODE = '/orders/{orderId}/coupons/{coupon}'.freeze
8
8
 
9
9
  class << self
10
10
  def update(order_id, coupon_string, headers = {}, &block)
11
- post(build_endpoint(UPDATE_COUPON_CODE,
12
- order: order_id,
13
- coupon_string: coupon_string), headers, &block)
11
+ post(build_endpoint(UPDATE_COUPON_CODE, order: order_id, coupon_string: coupon_string),
12
+ headers, &block)
14
13
  end
15
14
  end
16
15
  end
17
16
  end
18
17
  end
19
-
@@ -2,19 +2,17 @@ module PlentyClient
2
2
  module Order
3
3
  module CouponCode
4
4
  class Contact
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
- LIST_COUPON_CONTACTS = '/orders/coupons/codes/contacts/{contactId}'.freeze
8
+ LIST_COUPON_CONTACTS = '/orders/coupons/codes/contacts/{contactId}'.freeze
9
9
 
10
10
  class << self
11
11
  def update(contact_id, headers = {}, &block)
12
- post(build_endpoint(LIST_COUPON_CONTACTS,
13
- contact: contact_id), headers, &block)
12
+ post(build_endpoint(LIST_COUPON_CONTACTS, contact: contact_id), headers, &block)
14
13
  end
15
14
  end
16
15
  end
17
16
  end
18
17
  end
19
18
  end
20
-
@@ -2,19 +2,17 @@ module PlentyClient
2
2
  module Order
3
3
  module CouponCode
4
4
  class Validation
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
- VALIDATE_COUPON = '/orders/coupons/codes/{coupon}'.freeze
8
+ VALIDATE_COUPON = '/orders/coupons/codes/{coupon}'.freeze
9
9
 
10
10
  class << self
11
11
  def update(coupon_string, headers = {}, &block)
12
- post(build_endpoint(VALIDATE_COUPON,
13
- coupon_string: coupon_string), headers, &block)
12
+ post(build_endpoint(VALIDATE_COUPON, coupon_string: coupon_string), headers, &block)
14
13
  end
15
14
  end
16
15
  end
17
16
  end
18
17
  end
19
18
  end
20
-
@@ -1,8 +1,8 @@
1
1
  module PlentyClient
2
2
  module Order
3
3
  class Currency
4
- extend PlentyClient::Endpoint
5
- extend PlentyClient::Request
4
+ include PlentyClient::Endpoint
5
+ include PlentyClient::Request
6
6
 
7
7
  LIST_CURRENCIES = '/orders/currencies'.freeze
8
8
  FIND_CURRENCY_ISO = '/orders/currencies/{currencyIso}'.freeze
@@ -29,4 +29,3 @@ module PlentyClient
29
29
  end
30
30
  end
31
31
  end
32
-
@@ -1,8 +1,8 @@
1
1
  module PlentyClient
2
2
  module Order
3
3
  class Date
4
- extend PlentyClient::Endpoint
5
- extend PlentyClient::Request
4
+ include PlentyClient::Endpoint
5
+ include PlentyClient::Request
6
6
 
7
7
  LIST_ORDER_DATE_TYPE_NAMES = '/orders/dates/types/{typeId}/names'.freeze
8
8
  FIND_ORDER_DATE_TYPE_NAME = '/orders/dates/types/{typeId}/names/{lang}'.freeze
@@ -18,7 +18,7 @@ module PlentyClient
18
18
  get(build_endpoint(FIND_ORDER_DATE_TYPE_NAME, type: type_id, lang: lang), headers, &block)
19
19
  end
20
20
 
21
- def list(currency_iso, headers = {}, &block)
21
+ def list(order_id, headers = {}, &block)
22
22
  get(build_endpoint(LIST_ORDER_DATES, order: order_id), headers, &block)
23
23
  end
24
24
 
@@ -29,4 +29,3 @@ module PlentyClient
29
29
  end
30
30
  end
31
31
  end
32
-
@@ -1,13 +1,13 @@
1
1
  module PlentyClient
2
2
  module Order
3
3
  module Item
4
- extend PlentyClient::Endpoint
5
- extend PlentyClient::Request
4
+ include PlentyClient::Endpoint
5
+ include PlentyClient::Request
6
6
 
7
7
  DELETE_ORDERS_ORDER_ITEM = '/orders/{orderId}/items/{orderItemId}'.freeze
8
8
 
9
9
  class << self
10
- def delete(order_id, order_item_id)
10
+ def destroy(order_id, order_item_id)
11
11
  delete(build_endpoint(DELETE_ORDERS_ORDER_ITEM,
12
12
  order: order_id,
13
13
  order_item: order_item_id))
@@ -2,8 +2,8 @@ module PlentyClient
2
2
  module Order
3
3
  module Item
4
4
  class Date
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
8
  # get /orders/items/{orderItemId}/dates
9
9
  # get /orders/items/{orderItemId}/dates/{typeId}
@@ -2,8 +2,8 @@ module PlentyClient
2
2
  module Order
3
3
  module Item
4
4
  class Property
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
8
  # get /orders/items/{orderItemId}/properties
9
9
  # get /orders/items/{orderItemId}/properties/{typeId}
@@ -2,12 +2,12 @@ module PlentyClient
2
2
  module Order
3
3
  module Item
4
4
  class SerialNumber
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
8
  LIST_SERIAL_NUMBERS = '/orders/{orderId}/items/serialNumbers'.freeze
9
9
  LIST_ORDER_ITEM_SERIAL_NUMBERS = '/orders/{orderId}/items/{orderItemId}/serialNumbers'.freeze
10
-
10
+
11
11
  class << self
12
12
  def list(order_id, headers = {}, &block)
13
13
  get(build_endpoint(LIST_SERIAL_NUMBERS, order: order_id), headers, &block)
@@ -1,8 +1,8 @@
1
1
  module PlentyClient
2
2
  module Order
3
3
  class Property
4
- extend PlentyClient::Endpoint
5
- extend PlentyClient::Request
4
+ include PlentyClient::Endpoint
5
+ include PlentyClient::Request
6
6
 
7
7
  # get /rest/orders/{orderId}/properties/{typeId?}
8
8
  # post /rest/orders/{orderId}/properties
@@ -18,4 +18,3 @@ module PlentyClient
18
18
  end
19
19
  end
20
20
  end
21
-
@@ -1,8 +1,8 @@
1
1
  module PlentyClient
2
2
  module Order
3
3
  class Referrer
4
- extend PlentyClient::Endpoint
5
- extend PlentyClient::Request
4
+ include PlentyClient::Endpoint
5
+ include PlentyClient::Request
6
6
 
7
7
  LIST_REFERRERS = '/orders/referrers'.freeze
8
8
  CREATE_ORDER_REFERRER = '/orders/referrers/{parentReferrerId}'.freeze
@@ -19,4 +19,3 @@ module PlentyClient
19
19
  end
20
20
  end
21
21
  end
22
-
@@ -1,12 +1,11 @@
1
1
  module PlentyClient
2
2
  module Order
3
3
  module Shipping
4
- extend PlentyClient::Endpoint
5
- extend PlentyClient::Request
4
+ include PlentyClient::Endpoint
5
+ include PlentyClient::Request
6
6
 
7
7
  class << self
8
8
  end
9
9
  end
10
10
  end
11
11
  end
12
-
@@ -2,8 +2,8 @@ module PlentyClient
2
2
  module Order
3
3
  module Shipping
4
4
  class Country
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
8
  LIST_SHIPPING_COUNTRIES = '/orders/shipping/countries'.freeze
9
9
 
@@ -2,8 +2,8 @@ module PlentyClient
2
2
  module Order
3
3
  module Shipping
4
4
  class Information
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
8
  BASE_ORDERS_SHIPPING_INFORMATION_PATH = '/orders/{orderId}/shipping'.freeze
9
9
 
@@ -34,7 +34,7 @@ module PlentyClient
34
34
  order: order_id), body)
35
35
  end
36
36
 
37
- def delete(order_id, body = {})
37
+ def destroy(order_id, body = {})
38
38
  delete(build_endpoint("#{BASE_ORDERS_SHIPPING_INFORMATION_PATH}#{DELETE_ORDER_SHIPPING_INFORMATION}",
39
39
  order: order_id), body)
40
40
  end
@@ -2,8 +2,8 @@ module PlentyClient
2
2
  module Order
3
3
  module Shipping
4
4
  class Package
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
8
  BASE_ORDERS_SHIPPING_PATH = '/orders/{orderId}/shipping'.freeze
9
9
 
@@ -37,13 +37,13 @@ module PlentyClient
37
37
  shipping_package: package_id), body)
38
38
  end
39
39
 
40
- def delete(order_id, package_id, body = {})
40
+ def destroy(order_id, package_id, body = {})
41
41
  delete(build_endpoint("#{BASE_ORDERS_SHIPPING_PATH}#{DELETE_ORDER_SHIPPING_PACKAGE}",
42
42
  order: order_id,
43
43
  shipping_package: package_id), body)
44
44
  end
45
45
 
46
- def delete_all(order_id, body = {})
46
+ def destroy_all(order_id, body = {})
47
47
  delete(build_endpoint("#{BASE_ORDERS_SHIPPING_PATH}#{DELETE_ORDER_SHIPPING_PACKAGES}",
48
48
  order: order_id), body)
49
49
  end
@@ -2,8 +2,8 @@ module PlentyClient
2
2
  module Order
3
3
  module Shipping
4
4
  class Profile
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
8
  LIST_SHIPPING_PRESET = '/orders/shipping/presets'.freeze
9
9
  FIND_SHIPPING_PRESETS = '/orders/shipping/presets/{presetId}'.freeze
@@ -2,8 +2,8 @@ module PlentyClient
2
2
  module Order
3
3
  module Shipping
4
4
  class ServiceProvider
5
- extend PlentyClient::Endpoint
6
- extend PlentyClient::Request
5
+ include PlentyClient::Endpoint
6
+ include PlentyClient::Request
7
7
 
8
8
  BASE_ORDERS_SHIPPING_SERVICE_PROVIDER_PATH = '/orders/shipping'.freeze
9
9
 
@@ -1,125 +1,148 @@
1
1
  require 'curl'
2
2
  module PlentyClient
3
3
  module Request
4
- def request(http_method, path, params = {})
5
- return false if http_method.nil? || path.nil?
6
- return false unless %w(post put patch delete get).include?(http_method.to_s)
7
-
8
- login if PlentyClient::Config.access_token.nil?
9
- start_time = Time.now
10
- response = perform(http_method, path, params)
11
- body = parse_body(response, http_method, path, params)
12
- log_output(http_method, base_url(path), params, time_diff_ms(start_time, Time.now)) if PlentyClient::Config.log
13
- body
14
- end
4
+ module ClassMethods
5
+ def request(http_method, path, params = {})
6
+ return false if http_method.nil? || path.nil?
7
+ return false unless %w[post put patch delete get].include?(http_method.to_s)
15
8
 
16
- def post(path, body = {})
17
- request(:post, path, body)
18
- end
9
+ if PlentyClient::Config.access_token.nil? || time_diff_ms(Time.now, PlentyClient::Config.expiry_date).negative?
10
+ login_check
11
+ end
12
+ start_time = Time.now
13
+ response = perform(http_method, path, params)
14
+ body = parse_body(response, http_method, path, params)
15
+ log_output(http_method, base_url(path), params, time_diff_ms(start_time, Time.now)) if PlentyClient::Config.log
16
+ body
17
+ end
19
18
 
20
- def put(path, body = {})
21
- request(:put, path, body)
22
- end
19
+ def post(path, body = {})
20
+ request(:post, path, body)
21
+ end
23
22
 
24
- def patch(path, body = {})
25
- request(:patch, path, body)
26
- end
23
+ def put(path, body = {})
24
+ request(:put, path, body)
25
+ end
27
26
 
28
- def delete(path, body = {})
29
- request(:delete, path, body)
30
- end
27
+ def patch(path, body = {})
28
+ request(:patch, path, body)
29
+ end
31
30
 
32
- def get(path, params = {}, &block)
33
- page = 1
34
- rval_array = []
35
- if block_given?
36
- loop do
37
- response = request(:get, path, params.merge(page: page))
38
- yield response['entries'] if block_given?
39
- page += 1
40
- break if response['isLastPage'] == true
31
+ def delete(path, body = {})
32
+ request(:delete, path, body)
33
+ end
34
+
35
+ def get(path, params = {}, &block)
36
+ page = 1
37
+ rval_array = []
38
+ if block_given?
39
+ loop do
40
+ response = request(:get, path, params.merge(page: page))
41
+ yield response['entries'] if block_given?
42
+ page += 1
43
+ break if response['isLastPage'] == true
44
+ end
45
+ else
46
+ rval_array = request(:get, path, params.merge(page: page))
41
47
  end
42
- else
43
- rval_array << request(:get, path, params.merge(page: page))
48
+ return rval_array.flatten if rval_array.is_a?(Array)
49
+ rval_array
44
50
  end
45
- rval_array.flatten
46
- end
47
51
 
48
- private
52
+ private
49
53
 
50
- def login
51
- response = perform(:post, "/login?username=#{PlentyClient::Config.api_user}" \
52
- "&password=#{PlentyClient::Config.api_password}")
53
- result = parse_body(response, :post, 'login')
54
- PlentyClient::Config.access_token = result['accessToken']
55
- PlentyClient::Config.refresh_token = result['refreshToken']
56
- end
54
+ def login_check
55
+ url = "/login?username=#{PlentyClient::Config.api_user}&password=#{PlentyClient::Config.api_password}"
56
+ response = perform(:post, url)
57
+ result = parse_body(response, :post, 'login')
58
+ PlentyClient::Config.access_token = result['accessToken']
59
+ PlentyClient::Config.refresh_token = result['refreshToken']
60
+ PlentyClient::Config.expiry_date = Time.now + result['expiresIn']
61
+ end
57
62
 
58
- def perform(http_method, path, params = {})
59
- case http_method.to_s.downcase
60
- when 'get'
61
- Curl.get(base_url(path), params) do |curl|
62
- curl_options(curl)
63
+ def perform(http_method, path, params = {})
64
+ case http_method.to_s.downcase
65
+ when 'get'
66
+ Curl.get(base_url(path), params) do |curl|
67
+ curl_options(curl)
68
+ end
69
+ when 'post'
70
+ Curl.post(base_url(path), params.to_json) do |curl|
71
+ curl_options(curl)
72
+ end
73
+ when 'put'
74
+ Curl.put(base_url(path), params.to_json) do |curl|
75
+ curl_options(curl)
76
+ end
77
+ when 'delete'
78
+ Curl.delete(base_url(path), params) do |curl|
79
+ curl_options(curl)
80
+ end
81
+ when 'patch'
82
+ Curl.patch(base_url(path), params.to_json) do |curl|
83
+ curl_options(curl)
84
+ end
63
85
  end
64
- when 'post'
65
- Curl.post(base_url(path), params) do |curl|
66
- curl_options(curl)
67
- end
68
- when 'put'
69
- Curl.put(base_url(path), params) do |curl|
70
- curl_options(curl)
71
- end
72
- when 'delete'
73
- Curl.delete(base_url(path), params) do |curl|
74
- curl_options(curl)
75
- end
76
- when 'patch'
77
- Curl.patch(base_url(path), params) do |curl|
78
- curl_options(curl)
86
+ end
87
+
88
+ def curl_options(curl)
89
+ curl.headers['Content-type'] = 'application/json'
90
+ unless PlentyClient::Config.access_token.nil?
91
+ curl.headers['Authorization'] = "Bearer #{PlentyClient::Config.access_token}"
79
92
  end
93
+ curl.headers['Accept'] = 'application/x.plentymarkets.v1+json'
94
+ curl
80
95
  end
81
- end
82
96
 
83
- def curl_options(curl)
84
- curl.headers['Content-type'] = 'application/json'
85
- unless PlentyClient::Config.access_token.nil?
86
- curl.headers['Authorization'] = "Bearer #{PlentyClient::Config.access_token}"
97
+ def base_url(path)
98
+ uri = URI(PlentyClient::Config.site_url)
99
+ url = "#{uri.scheme}://#{uri.host}/rest"
100
+ url += path.start_with?('/') ? path : "/#{path}"
101
+ url
87
102
  end
88
- curl.headers['Accept'] = 'application/x.plentymarkets.v1+json'
89
- curl
90
- end
91
103
 
92
- def base_url(path)
93
- uri = URI(PlentyClient::Config.site_url)
94
- url = "#{uri.scheme}://#{uri.host}/rest"
95
- url += path.start_with?('/') ? path : "/#{path}"
96
- url
97
- end
104
+ def parse_body(response, http_method, rest_path, params = {})
105
+ result = JSON.parse(response.body)
106
+ errors = error_check(result)
107
+ unless errors.blank?
108
+ raise PlentyClient::ResponseError, [*errors].join(', ')
109
+ log_output(http_method, path, params, '')
110
+ end
111
+ result
112
+ end
98
113
 
99
- def login_required?(path)
100
- return false if defined?(Rails) && Rails.env == 'testing'
101
- return true unless path.include?('login')
102
- false
103
- end
114
+ def log_output(http_method, path, params, duration)
115
+ puts [Time.now, "#{duration} ms", http_method.upcase, path, params].join(' # ')
116
+ end
104
117
 
105
- def parse_body(response, http_method, rest_path, params = {})
106
- result = JSON.parse(response.body)
107
- raise InvalidResponseException.new(http_method, rest_path, params) if result.nil?
108
- result
109
- end
118
+ def time_diff_ms(start, finish)
119
+ ((finish - start) * 1000.0).round(2)
120
+ end
110
121
 
111
- def log_output(http_method, path, params, duration)
112
- puts [Time.now, "#{duration} ms", http_method.upcase, path, params].join(' # ')
113
- end
122
+ def error_check(response)
123
+ rval = []
124
+ return rval if response.blank?
125
+ if response.is_a?(Array) && response.first.key?('error')
126
+ response.each do |res|
127
+ rval << extract_message(res)
128
+ end
129
+ elsif response.is_a?(Hash) && response.key?('error')
130
+ rval << extract_message(response)
131
+ end
132
+ rval
133
+ end
114
134
 
115
- def time_diff_ms(start, finish)
116
- ((finish - start) * 1000.0).round(2)
135
+ def extract_message(response)
136
+ if response.key?('validation_errors') && !response['validation_errors'].blank?
137
+ response['validation_errors'].values.flatten.join(', ')
138
+ else
139
+ response['error']['message']
140
+ end
141
+ end
117
142
  end
118
- end
119
- end
120
143
 
121
- class InvalidResponseException < StandardError
122
- def initialize(http_method, rest_path, params)
123
- super("The response was null. http_Method: #{http_method}, Path: #{rest_path}, options: #{params}")
144
+ def self.included(base)
145
+ base.extend(ClassMethods)
146
+ end
124
147
  end
125
148
  end