module MxHero::API module MetadataLoader def initialize(data = {}) super(data) load_metadata end private def load_metadata is_empty = metadata.nil? || metadata == '' || metadata == 'null' #@metadata = JSON.parse(metadata) if metadata @metadata = is_empty ? nil : JSON.parse(metadata, symbolize_names: true) end end class Domain < Resource attributes :domain, :server, :aliases, :features, :cos attribute :creation_date, map: 'creationDate', date: true attribute :update_date, map: 'updateDate', date: true attribute :cos_last_change, map: 'cosLastChange', date: true attribute :source attribute :metadata include MetadataLoader def initialize(data = {}) super(data) load_features(features) load_cos(cos) # load_metadata end def change_to_trial if cos.type == 'free' cos.type = 'trial' end end private def load_cos(cos) @cos = Cos.new cos end def load_features(features) @features = features.map { |feature| Feature.new feature } end end class Feature < Resource attribute :component, map: 'feature' attributes :created, :updated, :metadata attribute :max_rules_amount, map: 'maxRulesAmount' include MetadataLoader def initialize(data = {}) super(data) if max_rules_amount == 0 || max_rules_amount.nil? @max_rules_amount = Float::INFINITY end end def unlimited_use? max_rules_amount == Float::INFINITY end end class Cos < Resource attribute :type, map: 'cos' attribute :edition end end