posmoni 0.0.5

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.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/.ruby-version +1 -0
  4. data/Gemfile +3 -0
  5. data/Gemfile.lock +42 -0
  6. data/INSTALL.md +7 -0
  7. data/README.md +184 -0
  8. data/Rakefile +12 -0
  9. data/lib/generators/posmoni/install_generator.rb +18 -0
  10. data/lib/generators/templates/posmoni_api.rb +7 -0
  11. data/lib/posmoni.rb +26 -0
  12. data/lib/posmoni/connector.rb +107 -0
  13. data/lib/posmoni/error.rb +15 -0
  14. data/lib/posmoni/interface.rb +22 -0
  15. data/lib/posmoni/moderation.rb +13 -0
  16. data/lib/posmoni/response.rb +40 -0
  17. data/lib/posmoni/version.rb +3 -0
  18. data/posmoni.gemspec +25 -0
  19. data/test/fixtures/image_choice/all.json +27 -0
  20. data/test/fixtures/image_choice/create.json +25 -0
  21. data/test/fixtures/image_closed_question/all.json +21 -0
  22. data/test/fixtures/image_closed_question/create.json +19 -0
  23. data/test/fixtures/image_document_verification/all.json +143 -0
  24. data/test/fixtures/image_document_verification/create.json +27 -0
  25. data/test/fixtures/image_message/all.json +22 -0
  26. data/test/fixtures/image_message/create.json +20 -0
  27. data/test/fixtures/image_photo_tag/all.json +22 -0
  28. data/test/fixtures/image_photo_tag/create.json +20 -0
  29. data/test/fixtures/moderation/all.json +68 -0
  30. data/test/fixtures/moderation/create.json +26 -0
  31. data/test/fixtures/moderation/find_by.json +26 -0
  32. data/test/fixtures/nanameue_human/all.json +25 -0
  33. data/test/fixtures/nanameue_human/create.json +21 -0
  34. data/test/fixtures/prediction/all.json +21 -0
  35. data/test/fixtures/prediction/create.json +19 -0
  36. data/test/fixtures/text_category/all.json +34 -0
  37. data/test/fixtures/text_category/create.json +27 -0
  38. data/test/fixtures/text_closed_question/all.json +27 -0
  39. data/test/fixtures/text_closed_question/create.json +20 -0
  40. data/test/fixtures/text_conversation/all.json +33 -0
  41. data/test/fixtures/text_conversation/create.json +26 -0
  42. data/test/fixtures/text_ja/all.json +35 -0
  43. data/test/fixtures/text_ja/create.json +28 -0
  44. data/test/fixtures/video_classification/all.json +21 -0
  45. data/test/fixtures/video_classification/create.json +19 -0
  46. data/test/helper/file_reader.rb +11 -0
  47. data/test/posmoni/moderation_test.rb +46 -0
  48. data/test/posmoni_test.rb +14 -0
  49. data/test/test_helper.rb +25 -0
  50. metadata +174 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3aee8e47665f588407207860ceeffcc18ebd020ed7c553c40b037d4fb51a7428
4
+ data.tar.gz: c9f85312236758626d9637d2fad95e783fde80d74eef8265ba09908391be229a
5
+ SHA512:
6
+ metadata.gz: f27a45ffecd3364b7ea645e866145619b13af7f70d660f12a44f5f4f05e20c9b1d1a21c131c610528bbeb4179ff29452ca60198e34f137cf791481b23023fc68
7
+ data.tar.gz: ec64c2d8da687b6ede05b35dbf2c32f44226e9706b75349d490ad5f286ff89568ede37ac22f0208f140d6a1a4bcd949a440a1ae155ec6b5cff3ab4cb7ed2c4e0
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ coverage
2
+ datawow-*
3
+ tags
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ posmoni (0.0.5)
5
+ json (~> 2.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
14
+ docile (1.1.5)
15
+ hashdiff (1.0.0)
16
+ json (2.2.0)
17
+ minitest (5.13.0)
18
+ public_suffix (4.0.1)
19
+ rake (12.3.3)
20
+ safe_yaml (1.0.5)
21
+ simplecov (0.15.1)
22
+ docile (~> 1.1.0)
23
+ json (>= 1.8, < 3)
24
+ simplecov-html (~> 0.10.0)
25
+ simplecov-html (0.10.2)
26
+ webmock (3.7.6)
27
+ addressable (>= 2.3.6)
28
+ crack (>= 0.3.2)
29
+ hashdiff (>= 0.4.0, < 2.0.0)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ minitest (~> 5.11, >= 5.11.3)
36
+ posmoni!
37
+ rake (~> 12.3)
38
+ simplecov (~> 0.15.1)
39
+ webmock (~> 3.3)
40
+
41
+ BUNDLED WITH
42
+ 1.17.1
data/INSTALL.md ADDED
@@ -0,0 +1,7 @@
1
+ After install CM
2
+
3
+ Run the generator
4
+
5
+ rails generate cm:install
6
+
7
+ This will install an initializer which describes ALL of CM's configuration options.
data/README.md ADDED
@@ -0,0 +1,184 @@
1
+ # Content Moderation Ruby Client
2
+
3
+ # Installation
4
+
5
+ Installing via rubygems:
6
+
7
+ ```ruby
8
+ gem 'posmoni', '~> 0.0.5'
9
+ ```
10
+
11
+ ### Rails
12
+
13
+ ```bash
14
+ $ rails generate posmoni:install
15
+ ```
16
+
17
+ # Configuration
18
+
19
+ First configure your project key:
20
+
21
+ ```ruby
22
+ require 'posmoni'
23
+
24
+ Posmoni.project_key = YOUR_PROJECT_KEY
25
+ ```
26
+ # Usage
27
+
28
+ There are 3 methods available in moderation class (`create`, `all`, `find_by`). Each
29
+ method returns a response of type `Posmoni::Response`, which has two attributes,
30
+ `body` and `code`. You can simply access response's body by calling `.body`
31
+
32
+ ### Create
33
+ ```ruby
34
+ require 'posmoni'
35
+
36
+ Posmoni.project_key = YOUR_PROJECT_KEY
37
+ params = {
38
+ data: data,
39
+ postback_url: postback_url,
40
+ postback_method: postback_method,
41
+ custom_id: custom_id
42
+ }
43
+ Posmoni.moderation.create(params)
44
+ ```
45
+
46
+ `data` could be text or image's url depends on your project's template.
47
+
48
+ #### params
49
+
50
+ | Field | Type | Required | Description |
51
+ | ------------- |:-------------:| :-----:| :-----|
52
+ | data | string | **Yes** | URL of image |
53
+ | postback_url| string| No | URL for answer callback once image has been checked |
54
+ | postback_method| string | No | Configuration HTTP method GET POST PUT PATCH |
55
+ | custom_id | string | No | Custom ID that used for search |
56
+
57
+ #### result
58
+ ```
59
+ response = Posmoni.moderation.create({
60
+ data: YOUR_DATA,
61
+ custom_id: 'data-1'
62
+ })
63
+
64
+ print(response.body)
65
+ ```
66
+
67
+ You will receive response like below, once you created moderation successfully.
68
+
69
+ ```ruby
70
+ {
71
+ "data" => {
72
+ "id" => "5dbab19ebbadfc32kefb56bf",
73
+ "type" => "moderation",
74
+ "attributes" => {
75
+ "custom_id" => "data-1",
76
+ "data" => YOUR_DATA,
77
+ "postback" => false,
78
+ "postback_url" => YOUR_POSTBACK_URL,
79
+ "postback_method" => YOUR_POSTBACK_METHOD,
80
+ "answer" => nil,
81
+ "created_at" => "2019-10-31T17:04:14.540+07:00",
82
+ "processed_at" => nil,
83
+ "status" => "unprocessed",
84
+ "id" => "5dbab19ebbadfc32kefb56bf",
85
+ "project_template" => YOUR_PROJECT_TEMPLATE,
86
+ "project_id" => YOUR_PROJECT_ID
87
+ }
88
+ },
89
+ "meta" => {
90
+ "code" => 201,
91
+ "message" => "Created"
92
+ }
93
+ }
94
+ ```
95
+
96
+ ### All
97
+ Method `all` is used to retrieve all of your moderations
98
+
99
+ ```ruby
100
+ require 'posmoni'
101
+
102
+ Posmoni.project_key = YOUR_PROJECT_KEY
103
+ Posmoni.moderation.all
104
+ ```
105
+
106
+ #### result
107
+ ```ruby
108
+ {
109
+ "data" => [
110
+ {
111
+ "id" => "5dbab19ebbadfc32kefb56bf",
112
+ "type" => "moderation",
113
+ "attributes" => {
114
+ "custom_id" => "data-1",
115
+ "data" => YOUR_DATA,
116
+ "postback" => false,
117
+ "postback_url" => YOUR_POSTBACK_URL,
118
+ "postback_method" => YOUR_POSTBACK_METHOD,
119
+ "answer" => "approved",
120
+ "project_template" => YOUR_PROJECT_TEMPLATE,
121
+ "created_at" => "2019-10-31T17:15:15.302+07:00",
122
+ "processed_at" => "2019-10-31T17:16:15.814+07:00",
123
+ "status" => "processed",
124
+ "id" => "5dbab19ebbadfc32kefb56bf",
125
+ "project_template" => YOUR_PROJECT_TEMPLATE,
126
+ "project_id" => YOUR_PROJECT_ID,
127
+ }
128
+ }
129
+ ],
130
+ "meta" => {
131
+ "code" => 200,
132
+ "message" => "OK"
133
+ }
134
+ }
135
+ ```
136
+
137
+ Fields `result` and `processed_at` will be present if your data was successfully processed.
138
+
139
+ ### Find by
140
+ Method `find_by` is used to find a particular moderation. You can use either its
141
+ ID or Custom ID. This method will return only moderation with fully matched ID.
142
+
143
+ ```ruby
144
+ require 'posmoni'
145
+
146
+ Posmoni.project_key = YOUR_PROJECT_KEY
147
+ id = 'data-1' # or use an ID from creation response (example: '5dbab19ebbadfc32kefb56bf')
148
+ Posmoni.moderation.find_by(id: id)
149
+ ```
150
+
151
+ #### params
152
+
153
+ | Field | Type | Required | Description |
154
+ | ------------- |:-------------:| :-----:| :-----|
155
+ | id | string | **Yes** | ID or Custom ID of a moderation |
156
+
157
+ #### result
158
+ ```ruby
159
+ {
160
+ "data" => {
161
+ "id" => "5dbab19ebbadfc32kefb56bf",
162
+ "type" => "moderation",
163
+ "attributes" => {
164
+ "custom_id" => "tp-1",
165
+ "data" => YOUR_DATA,
166
+ "postback" => false,
167
+ "postback_url" => YOUR_POSTBACK_URL,
168
+ "postback_method" => YOUR_POSTBACK_METHOD,
169
+ "answer" => "approved",
170
+ "project_template" => YOUR_PROJECT_TEMPLATE,
171
+ "created_at" => "2019-10-31T17:04:14.540+07:00",
172
+ "processed_at" => "2019-10-31T17:05:16.243+07:00",
173
+ "status" => "processed",
174
+ "id" => "5dbab19ebbadfc32kefb56bf",
175
+ "project_template" => YOUR_PROJECT_TEMPLATE,
176
+ "project_id" => YOUR_PROJECT_ID,
177
+ }
178
+ },
179
+ "meta" => {
180
+ "code" => 200,
181
+ "message" => "OK"
182
+ }
183
+ }
184
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,18 @@
1
+ require 'rails/generators/base'
2
+ require 'securerandom'
3
+
4
+ module Posmoni
5
+ module Generators
6
+ # :nodoc:
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('../../templates', __FILE__)
9
+
10
+ desc 'Creates a Posmoni initializer
11
+ and copy locale files to your application.'
12
+
13
+ def copy_initializer
14
+ template 'posmoni_api.rb', 'config/initializers/posmoni_api.rb'
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'posmoni'
2
+
3
+ Posmoni.setup do |config|
4
+ # ==> Secret key Configuration
5
+ # You can change it below and use your own secret key.
6
+ config.project_key = 'Project token key'
7
+ end
data/lib/posmoni.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'posmoni/response'
4
+ require_relative 'posmoni/connector'
5
+ require_relative 'posmoni/error'
6
+ require_relative 'posmoni/version'
7
+ require_relative 'posmoni/interface'
8
+
9
+ require_relative 'posmoni/moderation'
10
+
11
+ # :nodoc:
12
+ module Posmoni
13
+ extend self
14
+
15
+ attr_accessor :project_key
16
+
17
+ class << self
18
+ def setup
19
+ yield self
20
+ end
21
+
22
+ def moderation
23
+ Moderation.new
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'net/https'
5
+ require 'uri'
6
+ require 'json'
7
+
8
+ require File.expand_path('response.rb', __dir__)
9
+
10
+ module Posmoni
11
+ # :nodoc:
12
+ class Connector
13
+ def initialize(path, type, token: '', version_api: 'v1')
14
+ @version_api = version_api
15
+ @type = type
16
+ @path = path
17
+ @token = token
18
+ @method = :GET
19
+ end
20
+
21
+ def post(data = {})
22
+ @method = :POST
23
+ response = send_request(data)
24
+ response_body = JSON.parse response.body
25
+ Response.new(response_body, response.code)
26
+ end
27
+
28
+ def get(data = {}, query_str = true)
29
+ @method = :GET
30
+ response = send_request(data, query_str)
31
+ response_body = JSON.parse response.body
32
+ Response.new(response_body, response.code)
33
+ end
34
+
35
+ def put(data = {})
36
+ @method = :PUT
37
+ response = send_request(data)
38
+ response_body = JSON.parse response.body
39
+ Response.new(response_body, response.code)
40
+ end
41
+
42
+ def delete(data = {}, query_str = true)
43
+ @method = :DELETE
44
+ response = send_request(data, query_str)
45
+ response_body = JSON.parse response.body
46
+ Response.new(response_body, response.code)
47
+ end
48
+
49
+ private
50
+
51
+ def send_request(data = {}, query_str = true)
52
+ base_uri = base_point(@type)
53
+ url_base = "#{base_uri}/#{@version_api}/#{@path}"
54
+ uri = URI.parse(url_base)
55
+
56
+ if @method == :GET
57
+ if query_str
58
+ uri.query = URI.encode_www_form(data)
59
+ else
60
+ url_base = "#{url_base}/#{data[:id]}"
61
+ uri = URI.parse(url_base)
62
+ end
63
+ end
64
+
65
+ https = Net::HTTP.new(uri.host, uri.port)
66
+ https.use_ssl = true
67
+ request = build_request(uri)
68
+ request.body = data.to_json
69
+ https.request(request)
70
+ end
71
+
72
+ def build_request(uri)
73
+ request = {}
74
+ token = @token || Posmoni.project_key
75
+
76
+ if (token || '').empty?
77
+ raise ArgumentError, 'project\'s token has missed. To config about token check our document'
78
+ end
79
+
80
+ case @method
81
+ when :POST
82
+ request = Net::HTTP::Post.new(uri.request_uri)
83
+ when :GET
84
+ request = Net::HTTP::Get.new(uri.request_uri)
85
+ when :PUT
86
+ request = Net::HTTP::Put.new(uri.request_uri)
87
+ when :DELETE
88
+ request = Net::HTTP::Delete.new(uri.request_uri)
89
+ else
90
+ raise ArgumentError, 'HTTP method is not exist, We allowed GET, POST, PUT, DELET only'
91
+ end
92
+
93
+ request['User-Agent'] = 'Posmoni Ruby gem client'
94
+ request['Accept'] = 'application/json'
95
+ request['Content-Type'] = 'application/json'
96
+ request['Authorization'] = token
97
+
98
+ request
99
+ end
100
+
101
+ def base_point(type)
102
+ {
103
+ moderation: 'https://api.posmoni.com/api'
104
+ }[type]
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,15 @@
1
+ module Posmoni
2
+
3
+ # Custom error class for rescuing from all API errors
4
+ class Error < StandardError; end
5
+
6
+ # Raised when API returns the HTTP status code 400
7
+ class BadRequest < Error; end
8
+
9
+ # Raised when API returns the HTTP status code 403
10
+ class NotPermitted < Error; end
11
+
12
+ # Raised when API returns the HTTP status code 404
13
+ class NotFound < Error; end
14
+
15
+ end