Sha256: 6864ad3d6e8bd7dd8496ff1b9b8617338f644957b0bf858ba14a031231637e7e

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module VirusTotal
  module Client
    class Object < Base
      CONVERT_TABLE = {
        ipaddress: "ip_addresses",
        domain: "domains",
        url: "urls",
        file: "file"
      }.freeze

      def get(id)
        id = to_id(id)
        _get("/#{name}/#{id}") { |json| json }
      end

      def comments(id)
        id = to_id(id)
        _get("/#{name}/#{id}/comments") { |json| json }
      end

      def add_comment(id, text)
        id = to_id(id)
        params = {
          data: {
            type: "comment",
            attributes: {
              text: text
            }
          }
        }
        _post("/#{name}/#{id}/comments", params) { |json| json }
      end

      def relationships
        []
      end

      def method_missing(method, *args)
        if relationships.include?(method)
          id = to_id(args.first)
          params = args.length == 2 ? args[1] : {}

          _get("/#{name}/#{id}/#{method}", params) { |json| json }
        else
          super
        end
      end

      def respond_to?(sym, *)
        return true if relationships.include? sym

        super
      end

      private

      def to_id(id)
        id
      end

      def klass
        self.class.to_s.split("::").last.to_s.downcase.to_sym
      end

      def name
        CONVERT_TABLE.fetch klass
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
virustotalx-1.0.0 lib/virustotal/clients/object.rb