Class: DiscourseSubscriptionClient::Resources
- Inherits:
-
Object
- Object
- DiscourseSubscriptionClient::Resources
- Defined in:
- lib/discourse_subscription_client/resources.rb
Instance Attribute Summary collapse
-
#resources ⇒ Object
Returns the value of attribute resources.
-
#suppliers ⇒ Object
Returns the value of attribute suppliers.
Class Method Summary collapse
Instance Method Summary collapse
- #find_all ⇒ Object
- #find_plugins ⇒ Object
- #find_resources ⇒ Object
- #find_suppliers ⇒ Object
-
#initialize ⇒ Resources
constructor
A new instance of Resources.
- #save_resources ⇒ Object
- #setup_plugins ⇒ Object
- #setup_resources ⇒ Object
- #valid_supplier_data?(data) ⇒ Boolean
Constructor Details
#initialize ⇒ Resources
Returns a new instance of Resources.
8 9 10 11 |
# File 'lib/discourse_subscription_client/resources.rb', line 8 def initialize @suppliers = [] @resources = [] end |
Instance Attribute Details
#resources ⇒ Object
Returns the value of attribute resources.
5 6 7 |
# File 'lib/discourse_subscription_client/resources.rb', line 5 def resources @resources end |
#suppliers ⇒ Object
Returns the value of attribute suppliers.
5 6 7 |
# File 'lib/discourse_subscription_client/resources.rb', line 5 def suppliers @suppliers end |
Class Method Details
.find_all ⇒ Object
13 14 15 16 17 |
# File 'lib/discourse_subscription_client/resources.rb', line 13 def self.find_all RailsMultisite::ConnectionManagement.each_connection do new.find_all end end |
Instance Method Details
#find_all ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/discourse_subscription_client/resources.rb', line 19 def find_all return unless DiscourseSubscriptionClient.database_exists? setup_resources find_resources return unless @resources.any? ActiveRecord::Base.transaction do find_suppliers save_resources end end |
#find_plugins ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/discourse_subscription_client/resources.rb', line 82 def find_plugins plugins = [] Dir["#{DiscourseSubscriptionClient.root}/plugins/*/plugin.rb"].sort.each do |path| source = File.read(path) = Plugin::Metadata.parse(source) next unless .subscription_url.present? plugins << { name: .name, supplier_url: ENV["TEST_SUBSCRIPTION_URL"] || .subscription_url } end plugins end |
#find_resources ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/discourse_subscription_client/resources.rb', line 37 def find_resources resources = find_plugins resources.each do |r| @resources << { name: r[:name], supplier_url: r[:supplier_url] } end end |
#find_suppliers ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/discourse_subscription_client/resources.rb', line 48 def find_suppliers supplier_urls = @resources.map { |resource| resource[:supplier_url] }.uniq.compact supplier_urls.each do |url| supplier = SubscriptionClientSupplier.find_or_create_by(url: url) request = DiscourseSubscriptionClient::Request.new(:supplier, supplier.id) data = request.perform("#{url}/subscription-server") if valid_supplier_data?(data) supplier.update(name: data[:supplier], products: data[:products]) @suppliers << supplier end end end |
#save_resources ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/discourse_subscription_client/resources.rb', line 63 def save_resources @resources.each do |resource| supplier = @suppliers.select { |s| s.url === resource[:supplier_url] }.first next unless supplier.present? attrs = { supplier_id: supplier.id, name: resource[:name] } SubscriptionClientResource.create!(attrs) unless SubscriptionClientResource.exists?(attrs) end end |
#setup_plugins ⇒ Object
77 78 79 80 |
# File 'lib/discourse_subscription_client/resources.rb', line 77 def setup_plugins Plugin::Metadata::FIELDS << :subscription_url unless Plugin::Metadata::FIELDS.include?(:subscription_url) Plugin::Metadata.attr_accessor(:subscription_url) end |
#setup_resources ⇒ Object
33 34 35 |
# File 'lib/discourse_subscription_client/resources.rb', line 33 def setup_resources setup_plugins end |
#valid_supplier_data?(data) ⇒ Boolean
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/discourse_subscription_client/resources.rb', line 98 def valid_supplier_data?(data) return false unless data.present? && data.is_a?(Hash) return false unless %i[supplier products].all? { |key| data.key?(key) } return false unless data[:supplier].is_a?(String) return false unless data[:products].is_a?(Hash) data[:products].all? do |_resource, products| products.is_a?(Array) && products.all? do |product| %i[product_id product_slug].all? do |key| product.key?(key) && product[key].is_a?(String) end end end end |