arctic-vendor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +52 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/arctic/vendor/api.rb +82 -0
- data/lib/arctic/vendor/uri.rb +14 -0
- data/lib/arctic/vendor/vendor.rb +73 -0
- data/lib/arctic/vendor/version.rb +5 -0
- data/lib/arctic/vendor.rb +22 -0
- data/vendor.gemspec +29 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 239c7af4a6b145200d169f22d350aa7aebe01496d8b1e2be98eb479a158cfbec
|
4
|
+
data.tar.gz: fada75547532d60ac435d80179b2fd48b522660b9f3aabea3a4912c92c51b196
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 592b3d9e056971697dad561ded2209100fddbe5f41a41e1a704a0751bc82fc9c6d95c59dfea53bbcdf59c81525033e9ce061f5cce4ae76b3df8c7b7a9a88931c
|
7
|
+
data.tar.gz: 99c78d3e6c95c7a1af693d2dc18dc6d527e02075818ed3817cfc58c0dd7c872295b3c35287027518d3242c9ca9baa5053205eff9c087a641162a06e4353701f8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
arctic-vendor (0.1.0)
|
5
|
+
activesupport (~> 5.2)
|
6
|
+
faraday (~> 0.14)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (5.2.0)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
concurrent-ruby (1.0.5)
|
17
|
+
diff-lcs (1.3)
|
18
|
+
faraday (0.15.1)
|
19
|
+
multipart-post (>= 1.2, < 3)
|
20
|
+
i18n (1.0.1)
|
21
|
+
concurrent-ruby (~> 1.0)
|
22
|
+
minitest (5.11.3)
|
23
|
+
multipart-post (2.0.0)
|
24
|
+
rake (10.5.0)
|
25
|
+
rspec (3.7.0)
|
26
|
+
rspec-core (~> 3.7.0)
|
27
|
+
rspec-expectations (~> 3.7.0)
|
28
|
+
rspec-mocks (~> 3.7.0)
|
29
|
+
rspec-core (3.7.1)
|
30
|
+
rspec-support (~> 3.7.0)
|
31
|
+
rspec-expectations (3.7.0)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.7.0)
|
34
|
+
rspec-mocks (3.7.0)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.7.0)
|
37
|
+
rspec-support (3.7.1)
|
38
|
+
thread_safe (0.3.6)
|
39
|
+
tzinfo (1.2.5)
|
40
|
+
thread_safe (~> 0.1)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
arctic-vendor!
|
47
|
+
bundler (~> 1.16)
|
48
|
+
rake (~> 10.0)
|
49
|
+
rspec (~> 3.0)
|
50
|
+
|
51
|
+
BUNDLED WITH
|
52
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Emil Kampp
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Arctic vendor
|
2
|
+
|
3
|
+
This gem is the backbone used for communication from Vendors to the Core API.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Install the gem:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'arctic-vendor'
|
11
|
+
```
|
12
|
+
|
13
|
+
Then in your code:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require 'bundler/setup'
|
17
|
+
|
18
|
+
module Arctic
|
19
|
+
module Vendor
|
20
|
+
module Dandomain # <-- Change this depending on the vendor you're using
|
21
|
+
def collect_products
|
22
|
+
Arctic::Vendor.collect_products do |shop|
|
23
|
+
# Retrieve products from the shop and return them to the block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
module_function :collect_products
|
27
|
+
|
28
|
+
def distribute_products
|
29
|
+
Arctic::Vendor.distribute_products do |shop, products|
|
30
|
+
# Send the products to the shop and return the products to the block
|
31
|
+
end
|
32
|
+
end
|
33
|
+
module_function :distribute_products
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
## Testing
|
40
|
+
|
41
|
+
Run all the tests:
|
42
|
+
|
43
|
+
```bash
|
44
|
+
rake test
|
45
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "vendor"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
require 'active_support/all'
|
4
|
+
|
5
|
+
module Arctic
|
6
|
+
module Vendor
|
7
|
+
class API
|
8
|
+
attr_reader :token, :connection
|
9
|
+
|
10
|
+
def initialize(**options)
|
11
|
+
api_token = options.fetch(:token, ENV.fetch('ARCTIC_CORE_API_TOKEN'))
|
12
|
+
api_url = options.fetch(:url,
|
13
|
+
ENV.fetch('ARCTIC_CORE_API_URL',
|
14
|
+
'http://localhost:5000/v1/vendors'))
|
15
|
+
|
16
|
+
@token = api_token
|
17
|
+
headers = {
|
18
|
+
Authorization: "Vendor #{token}",
|
19
|
+
'Content-Type': 'application/json',
|
20
|
+
Accept: 'application/json',
|
21
|
+
}
|
22
|
+
@connection = Faraday.new url: api_url.chomp('/'), headers: headers
|
23
|
+
end
|
24
|
+
|
25
|
+
# List the current accounts available to the vendor
|
26
|
+
def list_accounts
|
27
|
+
make_request :get, 'accounts'
|
28
|
+
end
|
29
|
+
|
30
|
+
# Show details about a single account
|
31
|
+
def show_account(account_id)
|
32
|
+
make_request :get, "accounts/#{account_id}"
|
33
|
+
end
|
34
|
+
|
35
|
+
# List shops for a single account
|
36
|
+
def list_shops(account_id)
|
37
|
+
make_request :get, "accounts/#{account_id}/shops"
|
38
|
+
end
|
39
|
+
|
40
|
+
# Send products to the Core API
|
41
|
+
def send_products(account_id, shop_id, products)
|
42
|
+
products.tap do |px|
|
43
|
+
make_batch_request :post, "accounts/#{account_id}/shops/#{shop_id}/products", body: px
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def make_batch_request(*args, **options)
|
50
|
+
batches = Array(options.delete(:body)).flatten.in_groups_of(1000, false)
|
51
|
+
Arctic::Vendor.threaded(batches) { |batch| make_request *args, **(options.merge(body: batch)) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def make_request(method, path, body: {}, params: {})
|
55
|
+
# Remove preceeding slash to avoid going from base url /v1 to /
|
56
|
+
path = path.reverse.chomp('/').reverse
|
57
|
+
|
58
|
+
response = connection.public_send(method, path) do |r|
|
59
|
+
if params.any?
|
60
|
+
params.each do |k, v|
|
61
|
+
r.params[k.to_s] = v
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
r.body = body.to_json if body.any?
|
66
|
+
end
|
67
|
+
|
68
|
+
json = begin
|
69
|
+
JSON.parse(response.body)
|
70
|
+
rescue JSON::ParserError
|
71
|
+
{}
|
72
|
+
end
|
73
|
+
|
74
|
+
raise json['error'] if json.is_a?(Hash) && json['error']
|
75
|
+
|
76
|
+
Arctic.logger.info "#{method.to_s.upcase} #{path}: #{response.status}"
|
77
|
+
|
78
|
+
json
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module URI
|
2
|
+
# Merges the `new_params` with the existing query parameters from the `url`
|
3
|
+
def self.replace_params(url, new_params)
|
4
|
+
uri = parse url
|
5
|
+
params = params_hash(uri).merge new_params
|
6
|
+
new_url = "#{uri.scheme}://#{uri.host}#{uri.path}?#{new_params.to_query}##{uri.fragment}"
|
7
|
+
parse new_url
|
8
|
+
end
|
9
|
+
|
10
|
+
# Extracts a ruby hash from the URI query string
|
11
|
+
def self.params_hash(uri)
|
12
|
+
Hash[CGI.parse(uri.query).collect { |k, v| [k, v.join('')] }]
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Arctic
|
2
|
+
module Vendor
|
3
|
+
MAX_THREADS = ENV.fetch('MAX_THREADS', 4).to_s.to_i.freeze
|
4
|
+
|
5
|
+
def threaded(collection, &block)
|
6
|
+
threads = Arctic::Vendor::MAX_THREADS.times.collect do
|
7
|
+
Thread.new do
|
8
|
+
while item = collection.pop do
|
9
|
+
yield item
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
threads.compact.map &:join
|
14
|
+
end
|
15
|
+
module_function :threaded
|
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
|
22
|
+
end
|
23
|
+
end
|
24
|
+
module_function :each_shop
|
25
|
+
|
26
|
+
def api
|
27
|
+
@api ||= Arctic::Vendor::API.new
|
28
|
+
end
|
29
|
+
module_function :api
|
30
|
+
|
31
|
+
def time
|
32
|
+
t1 = Time.now.to_f
|
33
|
+
yield
|
34
|
+
Time.now.to_f - t1
|
35
|
+
end
|
36
|
+
module_function :time
|
37
|
+
|
38
|
+
# Fetches all products from all shops, where this vendor is the source
|
39
|
+
# vendor and pushes them to the Core API.
|
40
|
+
def collect_products(&block)
|
41
|
+
Arctic.logger.info "Collecting products from source vendor..."
|
42
|
+
products_count = 0
|
43
|
+
|
44
|
+
seconds = time do
|
45
|
+
each_shop do |shop, account|
|
46
|
+
products = api.send_products account['id'], shop['id'], yield(shop)
|
47
|
+
products_count += products.size
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
Arctic.logger.info "Collected #{products_count} products in #{seconds} seconds"
|
52
|
+
end
|
53
|
+
module_function :collect_products
|
54
|
+
|
55
|
+
# Fetches all products from the Core API and distributes them to the
|
56
|
+
# target vendors
|
57
|
+
def distribute_products
|
58
|
+
Arctic.logger.info "Distributing products to target vendor..."
|
59
|
+
products_count = 0
|
60
|
+
|
61
|
+
seconds = time do
|
62
|
+
each_shop(type: :target) do |shop, account|
|
63
|
+
products = api.list_products account['id'], shop['id']
|
64
|
+
products_count += products.size
|
65
|
+
yield shop, products
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
Arctic.logger.info "Distributed #{products_count} products in #{seconds} seconds"
|
70
|
+
end
|
71
|
+
module_function :distribute_products
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "vendor/version"
|
2
|
+
require 'arctic/vendor/api'
|
3
|
+
require 'arctic/vendor/vendor'
|
4
|
+
require 'arctic/vendor/uri'
|
5
|
+
|
6
|
+
module Arctic
|
7
|
+
def logger
|
8
|
+
@logger ||= begin
|
9
|
+
STDOUT.sync = true
|
10
|
+
Logger.new STDOUT
|
11
|
+
end
|
12
|
+
end
|
13
|
+
module_function :logger
|
14
|
+
|
15
|
+
def logger=(new_logger)
|
16
|
+
@logger = new_logger
|
17
|
+
end
|
18
|
+
module_function :logger=
|
19
|
+
|
20
|
+
module Vendor
|
21
|
+
end
|
22
|
+
end
|
data/vendor.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "arctic/vendor/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "arctic-vendor"
|
8
|
+
spec.version = Arctic::Vendor::VERSION
|
9
|
+
spec.authors = ["Emil Kampp"]
|
10
|
+
spec.email = ["emil@kampp.me"]
|
11
|
+
|
12
|
+
spec.summary = "Core API communcation from and to vendors"
|
13
|
+
spec.description = "This exposes a series of normal usage endpoints for Vendors to communicate with the Core API"
|
14
|
+
spec.homepage = "https://github.com/YouWeApS/arctic-vendor"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = %w(lib/arctic)
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_runtime_dependency "faraday", "~> 0.14"
|
28
|
+
spec.add_runtime_dependency "activesupport", "~> 5.2"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arctic-vendor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Emil Kampp
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.14'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.14'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.2'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.2'
|
83
|
+
description: This exposes a series of normal usage endpoints for Vendors to communicate
|
84
|
+
with the Core API
|
85
|
+
email:
|
86
|
+
- emil@kampp.me
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/arctic/vendor.rb
|
102
|
+
- lib/arctic/vendor/api.rb
|
103
|
+
- lib/arctic/vendor/uri.rb
|
104
|
+
- lib/arctic/vendor/vendor.rb
|
105
|
+
- lib/arctic/vendor/version.rb
|
106
|
+
- vendor.gemspec
|
107
|
+
homepage: https://github.com/YouWeApS/arctic-vendor
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib/arctic
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.7.6
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Core API communcation from and to vendors
|
131
|
+
test_files: []
|