lib/jss/api_object/extension_attribute.rb in ruby-jss-1.0.4 vs lib/jss/api_object/extension_attribute.rb in ruby-jss-1.1.0b1

- old
+ new

@@ -64,17 +64,36 @@ # Class Constants ################################### # What kinds of data can be created by EAs? # Note, Dates must be in the format "YYYY-MM-DD hh:mm:ss" - DATA_TYPES = %w[String Date Integer].freeze - DEFAULT_DATA_TYPE = 'String'.freeze + DATA_TYPE_STRING = 'String'.freeze + DATA_TYPE_NUMBER = 'Number'.freeze + DATA_TYPE_INTEGER = 'Integer'.freeze + DATA_TYPE_DATE = 'Date'.freeze + + DATA_TYPES = [DATA_TYPE_STRING, DATA_TYPE_DATE, DATA_TYPE_INTEGER].freeze + + DEFAULT_DATA_TYPE = DATA_TYPE_STRING + + # ExtensionAttributes refer to the numeric data type as "Integer" + # but the ext. attr values that come with extendable objects refer to + # that data type as "Number". Here's an array with both, so we can + # work with ether more easily. + NUMERIC_TYPES = [DATA_TYPE_NUMBER, DATA_TYPE_INTEGER].freeze + # Where does the data come from? - INPUT_TYPES = ['Text Field', 'Pop-up Menu', 'script', 'LDAP Attribute Mapping'].freeze - DEFAULT_INPUT_TYPE = 'Text Field'.freeze + INPUT_TYPE_FIELD = 'Text Field'.freeze + INPUT_TYPE_POPUP = 'Pop-up Menu'.freeze + INPUT_TYPE_SCRIPT = 'script'.freeze + INPUT_TYPE_LDAP = 'LDAP Attribute Mapping'.freeze + + INPUT_TYPES = [INPUT_TYPE_FIELD, INPUT_TYPE_POPUP, INPUT_TYPE_SCRIPT, INPUT_TYPE_LDAP].freeze + DEFAULT_INPUT_TYPE = INPUT_TYPE_FIELD + # Where can it be displayed in the WebApp? # subclasses can add to this list WEB_DISPLAY_CHOICES = [ 'General', 'Operating System', @@ -140,28 +159,26 @@ # the name of the EA might have spaces and caps, which the will come to us as symbols with the spaces # as underscores, like this. @symbolized_name = @name.gsub(/-| /, '_').to_sym end # init - # Public Instance Methods ################################### - # @see JSS::Creatable#create # def create - if @input_type == 'Pop-up Menu' + if @input_type == INPUT_TYPE_POPUP raise MissingDataError, 'No popup_choices set for Pop-up Menu input_type.' unless @popup_choices.is_a?(Array) && !@popup_choices.empty? end super end # @see JSS::Updatable#update # def update - if @input_type == 'Pop-up Menu' + if @input_type == INPUT_TYPE_POPUP raise MissingDataError, 'No popup_choices set for Pop-up Menu input_type.' unless @popup_choices.is_a?(Array) && !@popup_choices.empty? end super end @@ -178,10 +195,26 @@ @api.timeout = orig_timeout @api.open_timeout = orig_open_timeout end end + def from_text_field? + @input_type == INPUT_TYPE_FIELD + end + + def from_popup_menu? + @input_type == INPUT_TYPE_POPUP + end + + def from_ldap? + @input_type == INPUT_TYPE_LDAP + end + + def from_script? + @input_type == INPUT_TYPE_SCRIPT + end + # Change the description of this EA # # @param new_val[String] the new value # # @return [void] @@ -226,11 +259,11 @@ # def input_type=(new_val) return nil if @input_type == new_val raise JSS::InvalidDataError, "input_type must be a string, one of: #{INPUT_TYPES.join(', ')}" unless INPUT_TYPES.include? new_val @input_type = new_val - @popup_choices = nil if @input_type == 'Text Field' + @popup_choices = nil if @input_type == INPUT_TYPE_FIELD @need_to_update = true end # # Change the Popup Choices of this EA # New value must be an Array, the items will be converted to Strings. @@ -251,18 +284,18 @@ # convert each one to a String, # and check that it matches the @data_type new_val.map! do |v| v = v.to_s.strip case @data_type - when 'Date' + when DATA_TYPE_DATE raise JSS::InvalidDataError, "data_type is Date, but '#{v}' is not formatted 'YYYY-MM-DD hh:mm:ss'" unless v =~ /^\d{4}(-\d\d){2} (\d\d:){2}\d\d$/ when 'Integer' raise JSS::InvalidDataError, "data_type is Integer, but '#{v}' is not one" unless v =~ /^\d+$/ end v end - self.input_type = 'Pop-up Menu' + self.input_type = INPUT_TYPE_POPUP @popup_choices = new_val @need_to_update = true end # # Get an Array of Hashes for all inventory objects @@ -317,11 +350,11 @@ # # Each Hash is one inventory object (computer, mobile device, user), with these keys: # :id - the jss id # :name - the object (computer, user, mobiledevice) name # :value - the most recent ext attr value for the object. - # :as_of - the timestamp of when the value was collected (nil for User EAs) + # :as_of - the timestamp of when the value was collected (nil for User EAs, or for devices that have never collected inventory) # :username - the username associated with the object # # This is done by creating a temporary {AdvancedSearch} # for all objects, with the EA as a display field. The #search_result # then contains the desired data. @@ -360,10 +393,10 @@ when 'Date' then JSS.parse_datetime i[@symbolized_name] when 'Integer' then i[@symbolized_name].to_i else i[@symbolized_name] end # case - as_of = Time.parse(i[LAST_RECON_FIELD_SYM]) if i[LAST_RECON_FIELD_SYM] + as_of = Time.parse(i[LAST_RECON_FIELD_SYM]) if i[LAST_RECON_FIELD_SYM] != '' results << { id: i[:id], name: i[:name], username: i[USERNAME_FIELD_SYM], value: value, as_of: as_of } end # acs.search_results.each ensure if defined? acs