Class: Checked

Inherits:
Object
  • Object
show all
Defined in:
lib/rubysightengine/checked.rb

Overview

The Checked class, used for handling responses.

Instance Method Summary collapse

Constructor Details

#initialize(data, min = 0.9) ⇒ Checked

Creates a new Checked object.

Parameters:

  • data (String)

    the data from the request.

  • min (Float) (defaults to: 0.9)

    the minimum score from the content filter that will pass.



9
10
11
12
13
14
15
# 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

Instance Method Details

#boxesArray

Returns all box coordinates from location based filters.

Returns:

  • (Array)


48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Boolean

Checks if response has filter type. You can include multiple types.

Parameters:

  • type (string)

    the types of content filters.

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

#rawHash

Returns the raw JSON parsed data

Returns:

  • (Hash)


27
28
29
30
31
# File 'lib/rubysightengine/checked.rb', line 27

def raw

    @raw

end

#safe?(*type) ⇒ Array

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

Parameters:

  • type (string)

    the types of content filters.

Returns:

  • (Array)

     



111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object

Changes the testing value minimum content is compared against.

Parameters:

  • min (Float)

    the minimum score.



19
20
21
22
23
# File 'lib/rubysightengine/checked.rb', line 19

def set_min(min)

    @min = min 

end

#stat_hash(*type) ⇒ Array

Returns the statistic hash of each given type.

Parameters:

  • type (string)

    the types of content filters.

Returns:

  • (Array)


36
37
38
39
40
41
42
43
44
# File 'lib/rubysightengine/checked.rb', line 36

def stat_hash(*type)

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

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

end