lib/misp/attribute.rb in misp-0.1.0 vs lib/misp/attribute.rb in misp-0.1.1

- old
+ new

@@ -1,26 +1,43 @@ # frozen_string_literal: true module MISP class Attribute < Base + # @return [String] attr_reader :id + # @return [String] attr_accessor :type + # @return [String] attr_accessor :category + # @return [Boolean] attr_accessor :to_ids + # @return [String] attr_reader :uuid + # @return [String] attr_reader :event_id + # @return [String] attr_accessor :distribution + # @return [String] attr_accessor :timestamp + # @return [String] attr_accessor :comment + # @return [String] attr_accessor :sharing_group_id + # @return [Boolean] attr_accessor :deleted + # @return [Boolean] attr_accessor :disable_correlation + # @return [String] attr_accessor :value + # @return [String] attr_accessor :data + # @return [Array<MISP::SharingGroup>] attr_accessor :sharing_groups + # @return [Array<MISP::Attribute>] attr_accessor :shadow_attributes + # @return [Array<MISP::Tag>] attr_accessor :tags def initialize(**attributes) attributes = normalize_attributes(attributes) @@ -42,10 +59,15 @@ @sharing_groups = build_plural_attribute(items: attributes.dig(:SharingGroup), klass: SharingGroup) @shadow_attributes = build_plural_attribute(items: attributes.dig(:ShadowAttribute), klass: Attribute ) @tags = build_plural_attribute(items: attributes.dig(:Tag), klass: Tag) end + # + # Returns a hash representation of the attribute data. + # + # @return [Hash] + # def to_h { id: id, type: type, category: category, @@ -64,65 +86,110 @@ ShadowAttribute: shadow_attributes.map(&:to_h), Tag: tags.map(&:to_h) }.compact end + # + # Get an attribute + # + # @return [MISP::Attribute] + # def get - _get("/attributes/#{id}") { |attribute| Attribute.new symbolize_keys(attribute) } + _get("/attributes/#{id}") { |attribute| Attribute.new attribute } end - def self.get(id) - new(id: id).get - end - + # + # Delete an attribute + # + # @return [Hash] + # def delete _post("/attributes/delete/#{id}") { |json| json } end - def self.delete(id) - new(id: id).delete - end - + # + # Create an attribute + # + # @return [MISP::Attribute] + # def create(event_id) - _post("/attributes/add/#{event_id}", wrap(to_h)) { |attribute| Attribute.new symbolize_keys(attribute) } + _post("/attributes/add/#{event_id}", wrap(to_h)) { |attribute| Attribute.new attribute } end - def self.create(event_id, **attributes) - new(attributes).create(event_id) - end - + # + # Update an attribute + # + # @param [Hash] **attrs attributes + # + # @return [MISP::Attribute] + # def update(**attrs) payload = to_h.merge(attrs) payload[:timestamp] = nil - _post("/attributes/edit/#{id}", wrap(payload)) { |json| Attribute.new symbolize_keys(json.dig("response", "Attribute")) } + _post("/attributes/edit/#{id}", wrap(payload)) { |json| Attribute.new json.dig(:response, :Attribute) } end + # + # Search for attributes + # + # @param [Hash] **params parameters + # + # @return [Array<MISP::Attributes>] + # def search(**params) base = { returnFormat: "json", limit: "100", page: "0" } _post("/attributes/restSearch", base.merge(params)) do |json| - attributes = json.dig("response", "Attribute") || [] - attributes.map { |attribute| Attribute.new symbolize_keys(attribute) } + attributes = json.dig(:response, :Attribute) || [] + attributes.map { |attribute| Attribute.new attribute } end end - def self.search(**params) - new.search params - end - + # + # Add a tag to an attribute + # + # @param [MISP::Tag, Hash] tag + # + # @return [MISP::Tag] + # def add_tag(tag) - tag = Tag.new(symbolize_keys(tag)) unless tag.is_a?(MISP::Tag) + tag = Tag.new(tag) unless tag.is_a?(MISP::Tag) payload = { uuid: uuid, tag: tag.name } - _post("/tags/attachTagToObject", payload) { |json| Tag.new symbolize_keys(json) } + _post("/tags/attachTagToObject", payload) { |json| Tag.new json } end + # + # Remove a tag from an attribute + # + # @param [MISP::Tag, Hash] tag + # + # @return [Hash] + # def remove_tag(tag) - tag = Tag.new(symbolize_keys(tag)) unless tag.is_a?(MISP::Tag) + tag = Tag.new(tag) unless tag.is_a?(MISP::Tag) payload = { uuid: uuid, tag: tag.name } _post("/tags/removeTagFromObject", payload) { |json| json } + end + + class << self + def get(id) + new(id: id).get + end + + def delete(id) + new(id: id).delete + end + + def create(event_id, **attributes) + new(attributes).create(event_id) + end + + def search(**params) + new.search params + end end end end