discourse_subscription_client 0.1.10 → 0.1.11

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: cfa8eae85ceafc3eed01ca40cddaa4569bd0c93bc2293823351c42fa66df5bad
4
- data.tar.gz: 66dde7bd6855900991d24869c2a3c75147f3995621cbfb82b49f532ec2c0b9fc
3
+ metadata.gz: 462389d7e4335458e1f6606ce6877abccc6df5999e71a947bcd926c176df0e6e
4
+ data.tar.gz: 8df679514b5492438b34c22ec8c0346d34ebbd18c05722f2c8669c1decf807c2
5
5
  SHA512:
6
- metadata.gz: 37fdeaff464660bc50bc01488817df7ebc26152ec03af6439c3b71eb37c7c2506d8aa7c0ba5ce0fa2f8e913b2f2ed05b6466c590a5d36d0e3afb9877e4bbb87b
7
- data.tar.gz: 65da8d91da25b4f43f3233d6740fb2bf1c284741db0c3dfc2b3810622b8de68b5f70b4fc578d27b2aa890ec152fd5d8d24ccba9f1ae1ba320b6531c7027f7ead
6
+ metadata.gz: f29355c411d5a6c5d5f8ce55c12edf8573402dc8d9a04e061e331223fd87b9071ae26f8267613771c5b4ef782db4d980c9a229d3c410bb24d0e7a652d33457c3
7
+ data.tar.gz: eb485af0a0d55f09b2afd1aa101246b25deccb52b56453a37ff72ca9affcf0e770c09b840cfe1715adf9122cef5b8d2181d5acb57c5e1741fed46cebe55a7195
@@ -1,33 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "aws-sdk-s3"
4
-
5
3
  class SubscriptionClientResource < ActiveRecord::Base
6
4
  belongs_to :supplier, class_name: "SubscriptionClientSupplier"
7
5
  has_many :notices, class_name: "SubscriptionClientNotice", as: :notice_subject, dependent: :destroy
8
6
  has_many :subscriptions, foreign_key: "resource_id", class_name: "SubscriptionClientSubscription", dependent: :destroy
9
7
 
10
- def can_access_bucket?(bucket)
11
- s3_client.head_bucket(bucket: bucket)
12
- true
13
- rescue Aws::S3::Errors::BadRequest,
14
- Aws::S3::Errors::Forbidden,
15
- Aws::S3::Errors::NotFound
16
- false
17
- end
18
-
19
- def s3_client
20
- @s3_client ||= begin
21
- return nil unless access_key_id && secret_access_key
22
-
23
- Aws::S3::Client.new(
24
- region: region,
25
- access_key_id: access_key_id,
26
- secret_access_key: secret_access_key
27
- )
28
- end
29
- end
30
-
31
8
  def region
32
9
  "us-east-1"
33
10
  end
@@ -30,6 +30,7 @@ module DiscourseSubscriptionClient
30
30
  ./request
31
31
  ./authorization
32
32
  ./resources
33
+ ./s3_gem
33
34
  ./notices
34
35
  ./subscriptions
35
36
  ./subscriptions/result
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "aws-sdk-s3"
4
+
5
+ module DiscourseSubscriptionClient
6
+ class S3Gem
7
+ attr_reader :plugin_name, :access_key_id, :secret_access_key, :region, :bucket
8
+
9
+ def initialize(opts = {})
10
+ @plugin_name = opts[:plugin_name]
11
+ @access_key_id = opts[:access_key_id]
12
+ @secret_access_key = opts[:secret_access_key]
13
+ @region = opts[:region]
14
+ @bucket = opts[:bucket]
15
+ end
16
+
17
+ def ready?
18
+ plugin_name && access_key_id && secret_access_key && region && bucket && can_access_bucket?
19
+ end
20
+
21
+ def install(gems)
22
+ return unless ready?
23
+
24
+ write_gemrc
25
+
26
+ gems.each do |gem_slug, version|
27
+ klass = gem_slug.to_s.underscore.classify
28
+ gem_name = gem_slug.to_s.dasherize
29
+ opts = { require_name: gem_slug.to_s.gsub(/_/, "/"), config: gemrc_path }
30
+ load(plugin_path, gem_name, version, opts)
31
+ end
32
+ ensure
33
+ remove_gemrc
34
+ end
35
+
36
+ protected
37
+
38
+ # Compare PluginGem.load
39
+ def load(path, name, version, opts = nil)
40
+ opts ||= {}
41
+
42
+ gems_path = File.dirname(path) + "/gems/#{RUBY_VERSION}"
43
+ spec_path = "#{gems_path}/specifications"
44
+ spec_file = spec_path + "/#{name}-#{version}"
45
+
46
+ if PluginGem.platform_variants(spec_file).find(&File.method(:exist?)).blank?
47
+ command =
48
+ "gem install #{name} -v #{version} -i #{gems_path} --no-document --ignore-dependencies --no-user-install --config-file #{opts[:config]}"
49
+ puts command
50
+ Bundler.with_unbundled_env { puts `#{command}` }
51
+ end
52
+
53
+ spec_file_variant = PluginGem.platform_variants(spec_file).find(&File.method(:exist?))
54
+ if spec_file_variant.present?
55
+ Gem.path << gems_path
56
+ Gem::Specification.load(spec_file_variant).activate
57
+ require opts[:require_name]
58
+ else
59
+ puts "You are specifying the gem #{name} in #{path}, however it does not exist!"
60
+ puts "Looked for: \n- #{PluginGem.platform_variants(spec_file).join("\n- ")}"
61
+ exit(-1)
62
+ end
63
+ end
64
+
65
+ def can_access_bucket?
66
+ client.head_bucket(bucket: bucket)
67
+ true
68
+ rescue Aws::S3::Errors::BadRequest, Aws::S3::Errors::Forbidden, Aws::S3::Errors::NotFound => e
69
+ false
70
+ end
71
+
72
+ def client
73
+ @client ||=
74
+ begin
75
+ return nil unless region && access_key_id && secret_access_key
76
+
77
+ Aws::S3::Client.new(
78
+ region: region,
79
+ access_key_id: access_key_id,
80
+ secret_access_key: secret_access_key
81
+ )
82
+ end
83
+ end
84
+
85
+ def gemrc
86
+ {
87
+ sources: ["s3://#{bucket}/", "https://rubygems.org/"],
88
+ s3_source: {
89
+ "#{bucket}": {
90
+ id: access_key_id,
91
+ secret: secret_access_key,
92
+ region: region
93
+ }
94
+ }
95
+ }
96
+ end
97
+
98
+ def write_gemrc
99
+ File.write(gemrc_path, gemrc.to_yaml)
100
+ end
101
+
102
+ def remove_gemrc
103
+ File.delete(gemrc_path) if File.exist?(gemrc_path)
104
+ end
105
+
106
+ def gemrc_path
107
+ File.join(Rails.root, "tmp", ".gemrc")
108
+ end
109
+
110
+ def plugin_path
111
+ @plugin_path ||= Discourse.plugins_by_name[plugin_name].path
112
+ end
113
+ end
114
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DiscourseSubscriptionClient
4
- VERSION = "0.1.10"
4
+ VERSION = "0.1.11"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discourse_subscription_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Angus McLeod
@@ -282,6 +282,7 @@ files:
282
282
  - lib/discourse_subscription_client/notices.rb
283
283
  - lib/discourse_subscription_client/request.rb
284
284
  - lib/discourse_subscription_client/resources.rb
285
+ - lib/discourse_subscription_client/s3_gem.rb
285
286
  - lib/discourse_subscription_client/subscriptions.rb
286
287
  - lib/discourse_subscription_client/subscriptions/result.rb
287
288
  - lib/discourse_subscription_client/subscriptions/update_result.rb