rubysightengine 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 708021926ab3de966aafefff79704209a9f849fd57253c9e88e14487b59b4cc3
4
+ data.tar.gz: bb11400f404695329622743e7d08d3fd5739bc3f57485af572ea36c0aef79291
5
+ SHA512:
6
+ metadata.gz: fcd20c98b8b32be1bfad99258be71799ce9241dc045626968d19e9aa32a5084d31a2bd715536c41ac8b2f74aa8dd705a05aa25e763bf32c9d3252c01659d90d2
7
+ data.tar.gz: 8129869074aecd4bb8cd2370cfb40f51e08863d82b5c9d534ddff7e9aa93992f1d5338f3d0053d8886a86c56c1773e8a5e55ff64215c29f65164906f660179fd
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rubysightengine.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubysightengine (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (10.5.0)
11
+ rspec (3.10.0)
12
+ rspec-core (~> 3.10.0)
13
+ rspec-expectations (~> 3.10.0)
14
+ rspec-mocks (~> 3.10.0)
15
+ rspec-core (3.10.1)
16
+ rspec-support (~> 3.10.0)
17
+ rspec-expectations (3.10.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.10.0)
20
+ rspec-mocks (3.10.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-support (3.10.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 2.0)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ rubysightengine!
33
+
34
+ BUNDLED WITH
35
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 k1tsu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # Rubysightengine
2
+
3
+ An easy to use sightengine wrapper for ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rubysightengine'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rubysightengine
20
+
21
+ ## Usage
22
+
23
+ ### General
24
+
25
+ A SightEngine class is provided which will give access to the method `check`. Once the data has been receieved from SightEngine, a new `Checked` object will be created.
26
+ There are five different `Checked` methods used to get the data from the request, all are below, discluding the method `.raw` which will give the request's body. The minimum testing value is defaulted at `0.9`.
27
+
28
+ ### Workflows
29
+
30
+ `.set_workflow(...)` can be used to set the current workflow, or it can be set as an argument when creating a new SightEngine object. These are completely optional and are manipulated in the SightEngine interface.
31
+
32
+ ```ruby
33
+ s = SightEngine.new(api_user, api_secret, optional_workflow)
34
+
35
+ s.set_workflow(new_workflow) # Optional
36
+
37
+ b = s.check (
38
+ "https://images.unsplash.com/photo-1533227268428-f9ed0900fb3b?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aGFwcHklMjBtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80",
39
+ 'nudity',
40
+ 'offensive'
41
+ )
42
+
43
+ b.set_min(0.5) # Optional
44
+
45
+ b.safe? # => {"nudity"=>true}
46
+
47
+ b.has? 'nudity', 'offensive' # => {"nudity"=>false, "offensive"=>false}
48
+
49
+ b.stat_hash 'offensive', 'nudity' # => [{"prob"=>0.117, "boxes"=>[{"x1"=>0.29997, "y1"=>0.49264, "x2"=>0.39483, "y2"=>0.6242, "label"=>"middlefinger", "prob"=>0.117}, {"x1"=>0.61118, "y1"=>0.43277, "x2"=>0.70978, "y2"=>0.56579, "label"=>"middlefinger", "prob"=>0.052}]}, {"raw"=>0.01, "partial"=>0.01}]
50
+
51
+ b.boxes # => [{"x1"=>0.29997, "y1"=>0.49264, "x2"=>0.39483, "y2"=>0.6242, "label"=>"middlefinger", "prob"=>0.117}]
52
+ ```
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubysightengine.
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,24 @@
1
+ ## rubysightengine
2
+ #### An easy to use sightengine SDK for ruby.
3
+
4
+ ## Installation
5
+
6
+ ##
7
+ ```ruby
8
+ s = SightEngine.new(api_user, api_secret, optional_workflow)
9
+ s.set_workflow(new_workflow)
10
+
11
+ b = s.check (
12
+ "https://images.unsplash.com/photo-1533227268428-f9ed0900fb3b?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aGFwcHklMjBtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80",
13
+ 'nudity',
14
+ 'offensive'
15
+ )
16
+
17
+ b.safe? # => {"nudity"=>true}
18
+
19
+ b.has? 'nudity', 'offensive' # => {"nudity"=>false, "offensive"=>false}
20
+
21
+ b.stat_hash 'offensive', 'nudity' # => [{"prob"=>0.117, "boxes"=>[{"x1"=>0.29997, "y1"=>0.49264, "x2"=>0.39483, "y2"=>0.6242, "label"=>"middlefinger", "prob"=>0.117}, {"x1"=>0.61118, "y1"=>0.43277, "x2"=>0.70978, "y2"=>0.56579, "label"=>"middlefinger", "prob"=>0.052}]}, {"raw"=>0.01, "partial"=>0.01}]
22
+
23
+ b.boxes # => [{"x1"=>0.29997, "y1"=>0.49264, "x2"=>0.39483, "y2"=>0.6242, "label"=>"middlefinger", "prob"=>0.117}]
24
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubysightengine"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ require "rubysightengine/version"
2
+ require_relative "rubysightengine/client"
3
+
4
+ module SightEngine
5
+ class Error < StandardError; end
6
+ class << self
7
+
8
+ def new(a, b, c=nil)
9
+
10
+ return SightEngineE.new(a, b, c)
11
+
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,127 @@
1
+ require "json"
2
+
3
+ # The Checked class, used for handling responses.
4
+ class Checked
5
+
6
+ # Creates a new Checked object.
7
+ # @param data [String] the data from the request.
8
+ # @param min [Float] the minimum score from the content filter that will pass.
9
+ def initialize(data, min=0.9)
10
+
11
+ @raw = JSON.parse(data)
12
+ @min = min
13
+ @type = @raw.keys.delete_if {|h| ["status", "media", "request"].include? h}
14
+
15
+ end
16
+
17
+ # Changes the testing value minimum content is compared against.
18
+ # @param min [Float] the minimum score.
19
+ def set_min(min)
20
+
21
+ @min = min
22
+
23
+ end
24
+
25
+ # Returns the raw JSON parsed data
26
+ # @return [Hash]
27
+ def raw
28
+
29
+ @raw
30
+
31
+ end
32
+
33
+ # Returns the statistic hash of each given type.
34
+ # @param type [string] the types of content filters.
35
+ # @return [Array]
36
+ def stat_hash(*type)
37
+
38
+ if type === []
39
+ type = @type
40
+ end
41
+
42
+ return type.map {|t| @raw[t]}
43
+
44
+ end
45
+
46
+ # Returns all box coordinates from location based filters.
47
+ # @return [Array]
48
+ def boxes
49
+
50
+ all_boxes = []
51
+ @raw.each do |h|
52
+
53
+ if ["status", "media", "request"].include?(h[0]) == false && h[1]['boxes'] != nil
54
+ all_boxes << h[1]['boxes'][0]
55
+ end
56
+ end
57
+ return all_boxes
58
+ end
59
+
60
+ # Checks if response has filter type. You can include multiple types.
61
+ # @param type [string] the types of content filters.
62
+ def has?(*type)
63
+
64
+ if type === []
65
+ type = @type
66
+ end
67
+
68
+ checker = {}
69
+
70
+ type.each do |t|
71
+
72
+ if @raw.keys.include? t
73
+
74
+ avg = 0
75
+ current = @raw[t]
76
+
77
+ current.keys.each do |stat|
78
+
79
+ misc = nil
80
+
81
+ if stat != "safe" and stat != "boxes"
82
+
83
+ avg += current[stat]
84
+
85
+ end
86
+
87
+ end
88
+
89
+ if (avg/current.delete_if {|x| x === "safe"}.count) <= @min
90
+ checker[t] = false
91
+ else
92
+ checker[t] = true
93
+ end
94
+
95
+ else
96
+
97
+ raise "sightengine category '#{t}' not found, check spelling?"
98
+
99
+ end
100
+
101
+ end
102
+
103
+ checker
104
+ # @return [Array]
105
+
106
+ end
107
+
108
+ #Checks if image has 'safe' key and compares it to minimum score.
109
+ # @param type [string] the types of content filters.
110
+ # @return [Array]  
111
+ def safe?(*type)
112
+
113
+ if type === []
114
+ type = @type
115
+ end
116
+
117
+ type.delete_if {|t| @raw[t]['safe'] === nil}
118
+ type.map {|t| [t, (@raw[t]['safe'] >= @min)]}.to_h
119
+
120
+ end
121
+
122
+ end
123
+
124
+
125
+
126
+
127
+
@@ -0,0 +1,83 @@
1
+ require 'net/http'
2
+ require_relative "checked.rb"
3
+
4
+
5
+ # The sightengine class.
6
+ class SightEngineE
7
+
8
+ # The sightengine endpoint.
9
+ BASE_URL = "https://api.sightengine.com/1.0/check.json"
10
+
11
+ # The valid categories that sightengine api takes.
12
+ VALID_CATEGORIES = [
13
+ "nudity",
14
+ "wad",
15
+ "properties",
16
+ "celebrities",
17
+ "offensive",
18
+ "faces",
19
+ "scam",
20
+ "text-content",
21
+ "face-attributes",
22
+ "text"
23
+ ]
24
+
25
+ # Creates a new sightengine object with credentials and an optional workflow.
26
+
27
+ # @param [String] api_user the api_user credential provided by sightengine.
28
+ # @param api_secret [String] the api_secret credential provided by sightengine.
29
+ # @param worklfow [String] the workflow you have created in the sightengine interface, this is optional.
30
+ def initialize(api_user, api_secret, workflow=nil)
31
+
32
+ @api_user = api_user
33
+ @api_secret = api_secret
34
+ @workflow = nil
35
+ @min = 0.9
36
+
37
+ end
38
+
39
+ # Sets the workflow for the class.
40
+ # @param workflow [String] the target workflow to set as the class workflow.
41
+ def set_workflow(workflow)
42
+
43
+ @workflow = workflow
44
+
45
+ end
46
+
47
+ # Checks a image for any content by making an HTTP request to the sightengine servers.
48
+ # @param target_url [String] the url of the target image.
49
+ # @param type [String] the type of content to be scanned for.
50
+ def check(target_url, *type)
51
+
52
+
53
+ if type.any? {|t| VALID_CATEGORIES.include?(t) === false}
54
+
55
+ raise "while checking, one or more of your sightengine types were not found"
56
+
57
+ end
58
+
59
+ uri = URI("https://api.sightengine.com/1.0/check.json")
60
+
61
+ form_h = {api_user: @api_user, api_secret: @api_secret, url: target_url}
62
+ form_h[:workflow] = @workflow if @workflow != nil
63
+ form_h[:models] = type.join(",")
64
+ uri.query = URI.encode_www_form(form_h)
65
+
66
+ http = Net::HTTP.new(uri.host, uri.port)
67
+ http.use_ssl = (uri.scheme == 'https')
68
+
69
+ request = Net::HTTP::Get.new(uri)
70
+ request.body = ""
71
+ request.add_field 'Accept', 'application/json'
72
+ response = http.request(request)
73
+
74
+ if response.is_a?(Net::HTTPSuccess) === false
75
+ raise "sightengine server gave a #{response.code} error: #{response.msg}"
76
+ end
77
+
78
+ # @return [Checked] if request is 200
79
+ return Checked.new(response.body)
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,3 @@
1
+ module Rubysightengine
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rubysightengine/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubysightengine"
8
+ spec.version = Rubysightengine::VERSION
9
+ spec.authors = ["k1tsu"]
10
+ spec.email = ["k1tsu@github.com"]
11
+
12
+ spec.summary = "An easy to use sightengine wrapper for ruby."
13
+ spec.homepage = "https://github.com/k1tsu/rubysightengine"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 2.0"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysightengine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - k1tsu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - k1tsu@github.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - README.rdoc
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/rubysightengine.rb
74
+ - lib/rubysightengine/checked.rb
75
+ - lib/rubysightengine/client.rb
76
+ - lib/rubysightengine/version.rb
77
+ - rubysightengine.gemspec
78
+ homepage: https://github.com/k1tsu/rubysightengine
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: An easy to use sightengine wrapper for ruby.
101
+ test_files: []