webpurify_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a4ea6940d9d2711e3bfdee6291b989844830c37
4
+ data.tar.gz: 50ace9352abfe313b9ea5bb584666f724db45f20
5
+ SHA512:
6
+ metadata.gz: b4d89746dc1a46f4815b0e52fa870b2b974ee3ecb82fb7eeb78e19fef08a1bd71f964be93cdef4d53e01e65ac68cf4393b44c24eaf7e8b1455bd467c490b853d
7
+ data.tar.gz: aa034514dfa5adee93d8280f6a7b65513c98c77e0e42af6e513c9d46acf75c46a34f07cfcd6f1dcd8b44bee7bfa7353b811eeb21e15ffc26516bcb41da35ef60
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in webpurify_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Olivier
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # WebpurifyApi
2
+
3
+ API Wrapper for http://webpurify.com image moderation api
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'webpurify_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install webpurify_api
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ image = WebpurifyApi::Image.new(api_key: 'XXX', live: false)
23
+ res = image.check(image_url)
24
+ puts image.status(res['imgid']).inspect
25
+ ```
26
+
27
+ See API docs for more informations: http://www.webpurify.com/image-moderation/documentation/
28
+
29
+
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/veilleperso/webpurify_api_/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,8 @@
1
+ require 'rest-client'
2
+ require 'active_support/all'
3
+ require "webpurify_api/version"
4
+ require "webpurify_api/base"
5
+ require "webpurify_api/image"
6
+
7
+ module WebpurifyApi
8
+ end
@@ -0,0 +1,78 @@
1
+ class WebpurifyApi::Base
2
+ attr_accessor :api_key, :endpoint
3
+
4
+ def initialize(api_key: nil, live: nil, endpoint: nil)
5
+ self.api_key = api_key || ENV['WEBPURIFY_APIKEY']
6
+ self.live = live.nil? ? ENV['WEBPURIFY_LIVE'].to_s == 'true' : live
7
+ self.endpoint = endpoint if endpoint
8
+ end
9
+
10
+ def live?
11
+ !!self.live
12
+ end
13
+
14
+ def logger
15
+ return @logger if defined?(@logger)
16
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
17
+ end
18
+
19
+ def logger=(val)
20
+ @logger = val
21
+ end
22
+
23
+ def error_message_for(code)
24
+ case code.to_i
25
+ when 100 then "Invalid API Key"
26
+ when 101 then "API Key is inactive"
27
+ when 102 then "API Key is missing in request"
28
+ when 103 then "Not a valid URL"
29
+ when 105 then "Unable to locate image"
30
+ when 106 then "Out of Requests"
31
+ else "Unknown error #{code}"
32
+ end
33
+ end
34
+
35
+ protected
36
+
37
+ attr_accessor :live
38
+
39
+ def do_request(method, options = {})
40
+ return false if api_key.blank?
41
+ return false if endpoint.blank?
42
+ return false unless valid_method?(method)
43
+
44
+ url = build_url_for(method, options.delete(:params))
45
+ res = RestClient.get(url, content_type: :json, accept: :json)
46
+ if res.body.to_s.match('<?xml')
47
+ # errors seems to be always returned in xml, whatever the format we ask
48
+ (Hash.from_xml(res.body) || {})['rsp']
49
+ else
50
+ (JSON.parse(res.body) || {})['rsp']
51
+ end
52
+ rescue RestClient::BadGateway, RestClient::ServiceUnavailable => err
53
+ logger.error("#{err.inspect}")
54
+ {}
55
+ end
56
+
57
+ private
58
+
59
+ def valid_method?(method)
60
+ %w(imgcheck imgstatus imgaccount).include?(method.to_s)
61
+ end
62
+
63
+ def request_method(method)
64
+ live_request?(method) ? "webpurify.live.#{method}" : "webpurify.sandbox.#{method}"
65
+ end
66
+
67
+ def live_request?(method)
68
+ method.to_s == 'imgaccount' ? true : live?
69
+ end
70
+
71
+ def build_url_for(method, params = nil)
72
+ params ||= {}
73
+ params.reverse_merge!(format: 'json', api_key: self.api_key)
74
+ params[:method] = request_method(method)
75
+ query = params.select { |k, v| !v.blank? }.collect { |k, v| "#{k}=#{CGI.escape(v.to_s)}"}.join("&")
76
+ "#{endpoint}?#{query}"
77
+ end
78
+ end
@@ -0,0 +1,20 @@
1
+ class WebpurifyApi::Image < WebpurifyApi::Base
2
+ def initialize(api_key: nil, live: nil)
3
+ super(api_key: api_key, live: live, endpoint: "https://im-api1.webpurify.com/services/rest/")
4
+ end
5
+
6
+ # callback url receive a get request with +imgid+ and +status+ parameters
7
+ # status: 1 = approved , status: 2 = declined
8
+ def check(url, custom_image_id: nil, callback: nil)
9
+ res = do_request(:imgcheck, params: { imgurl: url, customimgid: custom_image_id, callback: callback })
10
+ res
11
+ end
12
+
13
+ def status(image_id)
14
+ do_request(:imgstatus, params: { imgid: image_id })
15
+ end
16
+
17
+ def account
18
+ do_request(:imgaccount)
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module WebpurifyApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'webpurify_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "webpurify_api"
8
+ spec.version = WebpurifyApi::VERSION
9
+ spec.authors = ["Olivier"]
10
+ spec.email = ["olivier@veilleperso.com"]
11
+ spec.summary = %q{Small wrapper aroud webpurify.com image moderation api.}
12
+ spec.description = %q{Wrapper for webpurify.com image moderation api, built for my own needs.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_runtime_dependency "rest-client", "~> 1.7.2"
24
+ spec.add_runtime_dependency "activesupport"
25
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webpurify_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Olivier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-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: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.7.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.7.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Wrapper for webpurify.com image moderation api, built for my own needs.
70
+ email:
71
+ - olivier@veilleperso.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/webpurify_api.rb
82
+ - lib/webpurify_api/base.rb
83
+ - lib/webpurify_api/image.rb
84
+ - lib/webpurify_api/version.rb
85
+ - webpurify_api.gemspec
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Small wrapper aroud webpurify.com image moderation api.
110
+ test_files: []