krb 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecf21c9b4c6334eb37db6b5d866740a2aedf8c8fd8b1298cb0583e25cb43f82e
4
- data.tar.gz: 40449dbee0aa51d2bc2a7a1c952929302ffec9b0ba5ff9d568f7b910fa2dc309
3
+ metadata.gz: 16db5a121841d07289f437da043c705b62c22f19db2a7503afae4c766725a1c0
4
+ data.tar.gz: 9beacfeba66d2accc9815101adaa319b89cea0c98607994b8d5ea63de586e0f0
5
5
  SHA512:
6
- metadata.gz: c587a0ce2e7eda95fb7bff6abbd7f829a1a2a687ce3b372a3a32092521f6ab17a61b75e1ba8603c6f47900a4b793402359150ea62799c6463875e8574cd2a3ef
7
- data.tar.gz: '06078ad2b15baa1d89869e6ecf0156e50cf0a3c9235c9a31a0227a3d58bf07efd341c27ed474f8a332d7b64f83b03fb832ad630430b981faf7cd24933a5859ba'
6
+ metadata.gz: 724899b75a0c9907673b7abbbc0b08a3cdc9ed6e5da1a4ec759299a75f2f057a337cf6631d2c5e635fb2ed011ce3d15a28cf3245e18577ce59708c78e9721ba5
7
+ data.tar.gz: 89a6a067fedc3400f786310f4f2e4d5db2ae58d2c163da3db3ce8410d14a3f6d5546c8ff443714fb8a7677fba924d3e5dac5d71ed904ffbcf3304351a95cc008
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  /.storefront_setup
13
+ /content
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- krb (0.1.1)
4
+ krb (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/exe/krb CHANGED
@@ -10,6 +10,8 @@ elsif entrypoint == 'import'
10
10
  Krb::Import::Base.process
11
11
  elsif entrypoint == 'download'
12
12
  Krb::Download::Base.process
13
+ elsif entrypoint == 'webhooks'
14
+ Krb::Webhooks::Base.process
13
15
  else
14
16
  puts 'Please specify a command.'
15
17
  end
@@ -21,13 +21,21 @@ module Krb
21
21
  end
22
22
 
23
23
  def verify_setup
24
- return if key?
25
- msg = <<-END.gsub(/^\s+\|/, '')
26
- |-----
27
- |ok
28
- |-----
29
- END
30
- print msg
24
+ return if base_setup?
25
+ ::Krb::Setup::Base.process
26
+ remove_instance_variable(:@setup)
27
+ end
28
+
29
+ def verify_production
30
+ return if production_setup?
31
+ ::Krb::Setup::ApplyProductionNamespace.process
32
+ remove_instance_variable(:@setup)
33
+ end
34
+
35
+ def verify_admin
36
+ return if admin_setup?
37
+ ::Krb::Setup::ApplyAdminCredentials.process
38
+ remove_instance_variable(:@setup)
31
39
  end
32
40
 
33
41
  protected
@@ -35,7 +43,7 @@ module Krb
35
43
  def setup
36
44
  @setup ||= begin
37
45
  if File.exist?(setup_file_path)
38
- YAML.load_file(setup_file_path)
46
+ JSON.parse(File.open(setup_file_path).read)
39
47
  else
40
48
  {}
41
49
  end
@@ -44,21 +52,18 @@ module Krb
44
52
 
45
53
  private
46
54
 
47
- def key?
48
- config ? config.storefront_token.present? : false
49
- end
50
-
51
- def config_file_path
52
- [Dir.pwd, '.krb'].join('/')
55
+ def base_setup?
56
+ setup && !setup.dig('access_key').nil?
53
57
  end
54
58
 
55
- def config_file?
56
- File.exist?(config_file_path)
59
+ def production_setup?
60
+ setup && !setup.dig('production_namespace').nil?
57
61
  end
58
62
 
59
- def config
60
- return unless config_file?
61
- YAML.load_file(config_file_path)
63
+ def admin_setup?
64
+ setup &&
65
+ !setup.dig('admin_api_key').nil? &&
66
+ !setup.dig('admin_password').nil?
62
67
  end
63
68
  end
64
69
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'tty/command'
4
+
3
5
  module Krb
4
6
  module Download
5
7
  # Download methods
@@ -7,10 +9,15 @@ module Krb
7
9
  def initialize
8
10
  super
9
11
  verify_setup
12
+ verify_production
13
+ @cmd = ::TTY::Command.new
10
14
  end
11
15
 
12
16
  def process
13
- 'ok download'
17
+ @cmd.run('rm -rf ./content')
18
+ @cmd.run("wget -q https://#{setup.dig('production_namespace')}.ngx.host/download -O ./content.zip")
19
+ @cmd.run("unzip -q ./content.zip -d ./content")
20
+ @cmd.run("rm -rf ./content/lost+found content.zip")
14
21
  end
15
22
  end
16
23
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'tty/command'
4
+
3
5
  module Krb
4
6
  module Import
5
7
  # Import methods
@@ -7,10 +9,15 @@ module Krb
7
9
  def initialize
8
10
  super
9
11
  verify_setup
12
+ verify_production
13
+ @cmd = ::TTY::Command.new
10
14
  end
11
15
 
12
16
  def process
13
- 'ok import'
17
+ @cmd.run('rm -rf ./content')
18
+ @cmd.run("wget https://#{setup.dig('production_namespace')}.ngx.host/download -O ./content.zip")
19
+ @cmd.run("unzip ./content.zip -d ./content")
20
+ @cmd.run("rm -rf ./content/lost+found content.zip")
14
21
  end
15
22
  end
16
23
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+ require 'tty/prompt'
5
+
6
+ module Krb
7
+ module Setup
8
+ # Sets up configuration
9
+ class ApplyAdminCredentials < ApplicationService
10
+ def process
11
+ ask_api_key
12
+ ask_password
13
+ end
14
+
15
+ private
16
+
17
+ def ask_password
18
+ input = prompt.mask(ask_password_phrase) { |q| q.required true }
19
+ ::Krb::Setup::Update.process('admin_password' => input)
20
+ end
21
+
22
+ def ask_password_phrase
23
+ 'Please enter your shopify admin password key : '
24
+ end
25
+
26
+ def ask_api_key
27
+ input = prompt.mask(ask_api_key_phrase) { |q| q.required true }
28
+ ::Krb::Setup::Update.process('admin_api_key' => input)
29
+ end
30
+
31
+ def ask_api_key_phrase
32
+ 'Please enter your shopify admin api key : '
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+ require 'tty/prompt'
5
+
6
+ module Krb
7
+ module Setup
8
+ # Sets up configuration
9
+ class ApplyProductionNamespace < ApplicationService
10
+ def process
11
+ input = prompt.ask(ask_phrase) { |q| q.required true }
12
+ ::Krb::Setup::Update.process('production_namespace' => input)
13
+ end
14
+
15
+ def ask_phrase
16
+ 'Please enter your production namespace (*namespace*.ngx.host) : '
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tty/prompt'
4
+ require 'yaml'
5
+
6
+ module Krb
7
+ module Setup
8
+ # Sets up configuration
9
+ class Update < ApplicationService
10
+ def initialize(payload)
11
+ @payload = payload
12
+ end
13
+
14
+ def process
15
+ updated_setup = setup.merge(@payload)
16
+ File.open(setup_file_path, 'w+') { |f| f.write(updated_setup.to_json) }
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/krb/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Krb
4
- VERSION = '0.1.1'.freeze
4
+ VERSION = '0.1.2'.freeze
5
5
  end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+
5
+ module Krb
6
+ module Webhooks
7
+ # Webhooks methods
8
+ class Base < ApplicationService
9
+ def initialize
10
+ super
11
+ verify_setup
12
+ verify_production
13
+ verify_admin
14
+ end
15
+
16
+ def process
17
+ ::Krb::Webhooks::Reset.process
18
+ %w[create update delete].each do |meth|
19
+ HTTP.headers(headers).post(shopify_webhooks_url, body: body(meth).to_json)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def body(meth)
26
+ {
27
+ webhook: {
28
+ topic: "products/#{meth}",
29
+ address: "https://#{setup.dig('production_namespace')}.ngx.host/webhook",
30
+ format: :json,
31
+ fields: %w[handle id]
32
+ }
33
+ }
34
+ end
35
+
36
+ def shopify_webhooks_url
37
+ "https://#{setup.dig('shop_name')}.myshopify.com/admin/webhooks.json"
38
+ end
39
+
40
+ def shopify_webhooks_delete_url(id)
41
+ "https://#{setup.dig('shop_name')}.myshopify.com/admin/webhooks/#{id}.json"
42
+ end
43
+
44
+ def headers
45
+ {
46
+ 'Content-Type' => 'application/json',
47
+ 'Authorization' => "Basic #{basic_auth_token}"
48
+ }
49
+ end
50
+
51
+ def basic_auth_token
52
+ Base64.strict_encode64(password)
53
+ end
54
+
55
+ def password
56
+ [setup.dig('admin_api_key'), setup.dig('admin_password')].join(':')
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+
5
+ module Krb
6
+ module Webhooks
7
+ # Webhooks methods
8
+ class Reset < Base
9
+ def process
10
+ webhooks_list.each do |webhook|
11
+ delete(webhook.dig('id'))
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def delete(id)
18
+ HTTP.headers(headers).delete(shopify_webhooks_delete_url(id))
19
+ end
20
+
21
+ def webhooks_list
22
+ resp = HTTP.headers(headers).get(shopify_webhooks_url)
23
+ JSON
24
+ .parse(resp.to_s)
25
+ .dig('webhooks')
26
+ .select { |node| node.dig('topic').match(/products\//) }
27
+ end
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: krb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - bbnnt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-27 00:00:00.000000000 Z
11
+ date: 2018-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -78,11 +78,16 @@ files:
78
78
  - lib/krb/application_service.rb
79
79
  - lib/krb/download/base.rb
80
80
  - lib/krb/import/base.rb
81
+ - lib/krb/setup/apply_admin_credentials.rb
82
+ - lib/krb/setup/apply_production_namespace.rb
81
83
  - lib/krb/setup/ask_shopify_access_key.rb
82
84
  - lib/krb/setup/ask_shopify_shop_name.rb
83
85
  - lib/krb/setup/base.rb
84
86
  - lib/krb/setup/git_ignore.rb
87
+ - lib/krb/setup/update.rb
85
88
  - lib/krb/version.rb
89
+ - lib/krb/webhooks/base.rb
90
+ - lib/krb/webhooks/reset.rb
86
91
  homepage: https://ngx.host
87
92
  licenses:
88
93
  - MIT