Class: SightEngineE

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

Overview

The sightengine class.

Constant Summary collapse

BASE_URL =

The sightengine endpoint.

"https://api.sightengine.com/1.0/check.json"
VALID_CATEGORIES =

The valid categories that sightengine api takes.

[
    "nudity", 
    "wad", 
    "properties", 
    "celebrities", 
    "offensive", 
    "faces", 
    "scam", 
    "text-content", 
    "face-attributes", 
    "text"
]

Instance Method Summary collapse

Constructor Details

#initialize(api_user, api_secret, workflow = nil) ⇒ SightEngineE

Returns a new instance of SightEngineE.

Parameters:

  • api_user (String)

    the api_user credential provided by sightengine.

  • api_secret (String)

    the api_secret credential provided by sightengine.

  • worklfow (String)

    the workflow you have created in the sightengine interface, this is optional.



30
31
32
33
34
35
36
37
# File 'lib/rubysightengine/client.rb', line 30

def initialize(api_user, api_secret, workflow=nil)

    @api_user = api_user
    @api_secret = api_secret
    @workflow = nil 
    @min = 0.9     

end

Instance Method Details

#check(target_url, *type) ⇒ Object

Checks a image for any content by making an HTTP request to the sightengine servers.

Parameters:

  • target_url (String)

    the url of the target image.

  • type (String)

    the type of content to be scanned for.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rubysightengine/client.rb', line 50

def check(target_url, *type)

 
    if type.any? {|t| VALID_CATEGORIES.include?(t) === false}

        raise "while checking, one or more of your sightengine types were not found"

    end

    uri = URI("https://api.sightengine.com/1.0/check.json")

    form_h = {api_user: @api_user, api_secret: @api_secret, url: target_url} 
    form_h[:workflow] = @workflow if @workflow != nil
    form_h[:models] = type.join(",")
    uri.query = URI.encode_www_form(form_h)

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (uri.scheme == 'https')

    request = Net::HTTP::Get.new(uri)
    request.body = ""
    request.add_field 'Accept', 'application/json'
    response = http.request(request)

    if response.is_a?(Net::HTTPSuccess) === false 
        raise "sightengine server gave a #{response.code} error: #{response.msg}"
    end 

    # @return [Checked] if request is 200 
    return Checked.new(response.body)

end

#set_workflow(workflow) ⇒ Object

Sets the workflow for the class.

Parameters:

  • workflow (String)

    the target workflow to set as the class workflow.



41
42
43
44
45
# File 'lib/rubysightengine/client.rb', line 41

def set_workflow(workflow)

    @workflow = workflow

end