arctic-vendor 0.2.4 → 1.0.0

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: 3c35888a69388854e55aeb41044e86f0d253bfc641b27c0ccdc8adafdfe11175
4
- data.tar.gz: 7e5a474dd62488a6f7c164e5b14a39f2e1497d95d198fac0bd74027f284eaf68
3
+ metadata.gz: 05635e7489082f52c9eae52ca87fe80b6ab52088d9bec08b2307c97aa2f290e3
4
+ data.tar.gz: 4487c41c01720ff83bbcaa93244d926f9d88d5780f34c1c0974505e87aa6a364
5
5
  SHA512:
6
- metadata.gz: 555a66bc09875998d978d5f456ae6d1362a93bdceb7e5cb50d871b8f5e18f9cdd607f9e899523f0c3bb6cce856c88456f0685f13a33361969e5f5c91ec2c49e1
7
- data.tar.gz: 287cf6d8345aed3828442774c887e04cda17da8782694eb8f47cd998e612cb6d438e3af430d0e0a03b1d8c48f877e3614fd2869e4cd694814a8c572caa90b212
6
+ metadata.gz: 1b07dfd68ee2df32d7aab75385f69b3ec16befd8621030be5017c2844ad5646f4f11cd6c8bc0b4f87536e272afa6c64327ee601049c72cf40d5d2d3280629801
7
+ data.tar.gz: 9ab9a0a28d01b1344c16956ffe75aade1522157407da2dc07562325505276d9673f29390d2939bcc7781c448baa948376bf0af60418e4386bee3759a48853bbb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ### 1.0.0
4
+
5
+ * Remove a number of superfluous endpoints
6
+ * Remove need for account_id
7
+ * Rename some existing endpoints
8
+ * Remove Product objetc. Use POR hashes
9
+ * Add call to update when the shop was last collected after each collection cycle
10
+
11
+ ### 0.2.5
12
+
13
+ * Add latest_only key to distribute_products
14
+ * Added api method for adding orders to the Core API
15
+
3
16
  ### 0.2.1
4
17
 
5
18
  * Added Arctic::Vendor::API#list_products method to allow vendors to retrieve products from the Core API - Emil Kampp <emil@youwe.dk>
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arctic-vendor (0.2.3)
4
+ arctic-vendor (1.0.0)
5
5
  activesupport (~> 5.2)
6
6
  faraday (~> 0.14)
7
7
 
@@ -2,79 +2,87 @@ require 'faraday'
2
2
  require 'json'
3
3
  require 'active_support/all'
4
4
 
5
- require_relative 'product'
6
-
7
5
  module Arctic
8
6
  module Vendor
9
7
  class API
10
- attr_reader :token, :connection
8
+ attr_reader :vendor_id, :token, :connection
11
9
 
12
10
  def initialize(**options)
13
- api_token = options.fetch(:token, ENV.fetch('ARCTIC_CORE_API_TOKEN'))
11
+ @vendor_id = options.fetch(:vendor_id, ENV.fetch('VENDOR_ID'))
12
+
13
+ @token = options.fetch(:token, ENV.fetch('ARCTIC_CORE_API_TOKEN'))
14
+
14
15
  api_url = options.fetch(:url,
15
16
  ENV.fetch('ARCTIC_CORE_API_URL',
16
17
  'http://localhost:5000/v1/vendors'))
17
18
 
18
- @token = api_token
19
19
  headers = {
20
- Authorization: "Vendor #{token}",
21
20
  'Content-Type': 'application/json',
22
21
  Accept: 'application/json',
23
22
  }
24
- @connection = Faraday.new url: api_url.chomp('/'), headers: headers
25
- end
26
23
 
27
- # List the current accounts available to the vendor
28
- def list_accounts
29
- make_request :get, 'accounts'
30
- end
31
-
32
- # Show details about a single account
33
- def show_account(account_id)
34
- make_request :get, "accounts/#{account_id}"
24
+ @connection = Faraday.new url: api_url.chomp('/'), headers: headers do |conn|
25
+ conn.basic_auth(vendor_id, token)
26
+ conn.adapter Faraday.default_adapter
27
+ end
35
28
  end
36
29
 
37
30
  # List shops for a single account
38
- def list_shops(account_id)
39
- make_request :get, "accounts/#{account_id}/shops"
31
+ def list_shops
32
+ make_request :get, "shops"
40
33
  end
41
34
 
42
35
  # Send products to the Core API
43
- def send_products(account_id, shop_id, products)
44
- products.tap do |px|
45
- make_batch_request :post, "accounts/#{account_id}/shops/#{shop_id}/products", body: px
36
+ def send_products(shop_id, products)
37
+ Arctic::Vendor.threaded(products.dup) do |prod|
38
+ begin
39
+ make_request :post, "shops/#{shop_id}/products", body: prod
40
+ rescue => e
41
+ Arctic.logger.error "Failed to send product (#{e.class}): #{e.message} -- #{prod}"
42
+ end
46
43
  end
44
+ make_request :put, "shops/#{shop_id}", params: { collected_at: Time.now.to_s(:db) }
45
+ products
47
46
  end
48
47
 
49
- # Retrieve products from the Core API
50
- def list_products(account_id, shop_id, **params)
51
- make_paginated_request(:get, "accounts/#{account_id}/shops/#{shop_id}/products", params: params) do |products|
52
- yield products.collect { |prod| Arctic::Vendor::Product.new account_id, shop_id, prod, self }
48
+ # Send products to the Core API
49
+ def send_currencies(shop_id, currencies)
50
+ Arctic::Vendor.threaded(currencies.dup) do |curr|
51
+ begin
52
+ make_request :put, "shops/#{shop_id}/currencies", body: curr
53
+ rescue => e
54
+ Arctic.logger.error "Failed to send currency (#{e.class}): #{e.message} -- #{curr}"
55
+ end
53
56
  end
57
+ currencies
54
58
  end
55
59
 
56
- def get_product(account_id, shop_id, product_id)
57
- product_id = URI.escape product_id
58
- make_request :get, "accounts/#{account_id}/shops/#{shop_id}/products/#{product_id}"
60
+ # Retrieve products from the Core API
61
+ def list_products(shop_id, **params)
62
+ params[:per_page] = params.delete(:batch_size) || 100
63
+ make_paginated_request(:get, "shops/#{shop_id}/products", params: params) do |products|
64
+ yield products
65
+ end
59
66
  end
60
67
 
61
68
  # Marks the shop as synchronized by the vendor
62
- def synchronized(account_id, shop_id)
63
- make_request :put, "accounts/#{account_id}/shops/#{shop_id}/synchronized"
69
+ def update_product(shop_id, sku, **params)
70
+ Arctic.logger.debug "Updating Product(#{sku}).."
71
+ make_request :patch, "shops/#{shop_id}/products/#{sku}", params: params
64
72
  end
65
73
 
66
- # Marks the shop as synchronized by the vendor
67
- def update_product_state(account_id, shop_id, product_id, state)
68
- make_request :put, "accounts/#{account_id}/shops/#{shop_id}/products/#{product_id}/state/#{state}"
74
+ def update_products(shop_id, products, **params)
75
+ Arctic::Vendor.threaded(products) do |prod|
76
+ begin
77
+ update_product shop_id, prod.fetch('sku'), **params
78
+ rescue KeyError => e
79
+ Arctic.logger.error "Invalid product: #{e.message} -- #{prod}"
80
+ end
81
+ end
69
82
  end
70
83
 
71
84
  private
72
85
 
73
- def make_batch_request(*args, **options)
74
- batches = Array(options.delete(:body)).flatten.in_groups_of(1000, false)
75
- Arctic::Vendor.threaded(batches) { |batch| make_request *args, **(options.merge(body: batch)) }
76
- end
77
-
78
86
  def raw_request(method, path, body: {}, params: {})
79
87
  # Remove preceeding slash to avoid going from base url /v1 to /
80
88
  path = path.reverse.chomp('/').reverse
@@ -14,15 +14,29 @@ module Arctic
14
14
  end
15
15
  module_function :threaded
16
16
 
17
- def each_shop(type: :source)
18
- api.list_accounts.each do |account|
19
- api.list_shops(account['id']).each do |shop|
20
- yield shop, account if shop['type'] == type.to_s
21
- end
17
+ def each_shop(type = :collection)
18
+ api.list_shops.with_indifferent_access[type].each do |shop|
19
+ yield shop
22
20
  end
23
21
  end
24
22
  module_function :each_shop
25
23
 
24
+ def collect_currencies(&block)
25
+ Arctic.logger.info "Collecting currencies from collection shop"
26
+
27
+ currencies_count = 0
28
+
29
+ seconds = time do
30
+ each_shop(:collection) do |shop|
31
+ currencies = api.send_currencies shop['id'], yield(shop)
32
+ currencies_count += currencies.size
33
+ end
34
+ end
35
+
36
+ Arctic.logger.info "Collected #{currencies_count} exchange rates in #{seconds} seconds."
37
+ end
38
+ module_function :collect_currencies
39
+
26
40
  def api(*args)
27
41
  @api ||= Arctic::Vendor::API.new(*args)
28
42
  end
@@ -38,14 +52,13 @@ module Arctic
38
52
  # Fetches all products from all shops, where this vendor is the source
39
53
  # vendor and pushes them to the Core API.
40
54
  def collect_products(&block)
41
- Arctic.logger.info "Collecting products from source vendor..."
55
+ Arctic.logger.info "Collecting products from vendor..."
42
56
  products_count = 0
43
57
 
44
58
  seconds = time do
45
- each_shop do |shop, account|
46
- products = api.send_products account['id'], shop['id'], yield(shop)
59
+ each_shop(:collection) do |shop|
60
+ products = api.send_products shop['id'], yield(shop)
47
61
  products_count += products.size
48
- api.synchronized account['id'], shop['id']
49
62
  end
50
63
  end
51
64
 
@@ -55,17 +68,20 @@ module Arctic
55
68
 
56
69
  # Fetches all products from the Core API and distributes them to the
57
70
  # target vendors
58
- def distribute_products(batch_size: 100)
71
+ def distribute_products(**params)
59
72
  Arctic.logger.info "Distributing products to target vendor..."
60
73
  products_count = 0
61
74
 
75
+ params.reverse_merge! \
76
+ batch_size: 100
77
+
62
78
  seconds = time do
63
- each_shop(type: :target) do |shop, account|
64
- api.list_products(account['id'], shop['id'], per_page: batch_size) do |products|
79
+ each_shop(:dispersal) do |shop|
80
+ api.list_products(shop['id'], params) do |products|
65
81
  products_count += products.size
66
82
  yield shop, products
83
+ # api.update_products shop['id'], products, dispersed_at: Time.now.to_s(:db)
67
84
  end
68
- api.synchronized account['id'], shop['id']
69
85
  end
70
86
  end
71
87
 
@@ -1,5 +1,5 @@
1
1
  module Arctic
2
2
  module Vendor
3
- VERSION = "0.2.4"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arctic-vendor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Kampp
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-10 00:00:00.000000000 Z
11
+ date: 2018-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -141,7 +141,6 @@ files:
141
141
  - documentation/source/stylesheets/screen.css.scss
142
142
  - lib/arctic/vendor.rb
143
143
  - lib/arctic/vendor/api.rb
144
- - lib/arctic/vendor/product.rb
145
144
  - lib/arctic/vendor/uri.rb
146
145
  - lib/arctic/vendor/vendor.rb
147
146
  - lib/arctic/vendor/version.rb
@@ -1,47 +0,0 @@
1
- module Arctic
2
- module Vendor
3
- class Product
4
- class Characteristics
5
- def initialize(characteristics)
6
- @characteristics = characteristics
7
- end
8
-
9
- def method_missing(name, *args)
10
- @characteristics[name.to_s]
11
- end
12
- end
13
-
14
- attr_reader \
15
- :product_hash,
16
- :sku,
17
- :characteristics,
18
- :api,
19
- :account_id,
20
- :shop_id
21
-
22
- def initialize(account_id, shop_id, product_hash, api_instance)
23
- @product_hash = product_hash
24
-
25
- @api = api_instance
26
-
27
- @shop_id = shop_id
28
- @account_id = account_id
29
-
30
- @sku = product_hash.fetch 'sku'
31
- @characteristics = Characteristics.new product_hash.fetch 'characteristics'
32
- end
33
-
34
- def update_state(state)
35
- api.update_product_state account_id, shop_id, sku, state
36
- end
37
-
38
- def method_missing(name, *args)
39
- if product_hash.stringify_keys.keys.include? name.to_s
40
- product_hash.stringify_keys[name.to_s]
41
- else
42
- super name, *args
43
- end
44
- end
45
- end
46
- end
47
- end