class Checked

The Checked class, used for handling responses.

Public Class Methods

new(data, min=0.9) click to toggle source

Creates a new Checked object. @param data [String] the data from the request. @param min [Float] the minimum score from the content filter that will pass.

# File lib/rubysightengine/checked.rb, line 9
def initialize(data, min=0.9)

    @raw = JSON.parse(data)
    @min = min 
    @type = @raw.keys.delete_if {|h| ["status", "media", "request"].include? h}
    
end

Public Instance Methods

boxes() click to toggle source

Returns all box coordinates from location based filters. @return [Array]

# File lib/rubysightengine/checked.rb, line 48
def boxes 

    all_boxes = []
    @raw.each do |h|

        if ["status", "media", "request"].include?(h[0]) == false && h[1]['boxes'] != nil 
            all_boxes << h[1]['boxes'][0]   
        end  
    end 
    return all_boxes
end
has?(*type) click to toggle source

Checks if response has filter type. You can include multiple types. @param type [string] the types of content filters.

# File lib/rubysightengine/checked.rb, line 62
  def has?(*type)

    if type === []
        type = @type
    end

    checker = {}

    type.each do |t|

        if @raw.keys.include? t 

            avg = 0 
            current = @raw[t]

            current.keys.each do |stat|
        
                misc = nil 

                if stat != "safe" and stat != "boxes"

                    avg += current[stat] 
                    
                end 

            end 
          
            if (avg/current.delete_if {|x| x === "safe"}.count) <= @min
                checker[t] = false 
            else
                checker[t] = true
            end 

        else 

            raise "sightengine category '#{t}' not found, check spelling?"

        end

    end 

    checker
    # @return [Array]

end
raw() click to toggle source

Returns the raw JSON parsed data @return [Hash]

# File lib/rubysightengine/checked.rb, line 27
def raw

    @raw

end
safe?(*type) click to toggle source

Checks if image has 'safe' key and compares it to minimum score.

@param type [string] the types of content filters.
@return [Array]  
# File lib/rubysightengine/checked.rb, line 111
def safe?(*type)

    if type === []
        type = @type 
    end 

    type.delete_if {|t| @raw[t]['safe'] === nil}
    type.map {|t| [t, (@raw[t]['safe'] >= @min)]}.to_h

end
set_min(min) click to toggle source

Changes the testing value minimum content is compared against. @param min [Float] the minimum score.

# File lib/rubysightengine/checked.rb, line 19
def set_min(min)

    @min = min 

end
stat_hash(*type) click to toggle source

Returns the statistic hash of each given type. @param type [string] the types of content filters. @return [Array]

# File lib/rubysightengine/checked.rb, line 36
def stat_hash(*type)

    if type === []
        type = @type 
    end 

    return type.map {|t| @raw[t]}

end