mirador 0.1.0 → 0.1.1
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.
- checksums.yaml +8 -8
- data/README.md +1 -1
- data/bin/mirador-client +3 -3
- data/examples/sinatra-example.rb +67 -0
- data/lib/mirador/version.rb +1 -1
- data/lib/mirador.rb +57 -8
- data/test/test_mirador.rb +64 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZWEzZDk2NDYzYTdmMWVlZDVkMjQwNDliMDhkNDVjMjk1NzQ5ODAwZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjJhZjAzNWVjYWM5ODE0NWZiNDk3OTc4ZmZjMjA2MzFlNzdhMWIxNw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OTk3ZmI2YjliNDU0NzJjNGU5M2JjMjQ3NTRhMzdlMTY3MDg4ODQ5NWQxMmJl
|
10
|
+
MzI0NGQ3NGNjNzgzZGI3NmJiNjY4MzUyYTM2NTVjYWIyZWRjZDJhMTg2YmVh
|
11
|
+
MTViNzhmNWViNzg1NWQ0NTkwMzJkNWUzYTAwMDZmYzMxMTBlOTE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YTQ5OGViY2VjMTk4NWM5MDEwMjVkZGMzN2Y5MDBhNzkwNmYzNWEwZmY4YTVh
|
14
|
+
MjdlYTQxZmEzMzVjZTU3OWNjMDdlOTMyYzQ0ZDc4N2YxODhiMmQ5MDJlMTFk
|
15
|
+
OWEyNzZhZjljZTJjMWQ5NDE3MTNmOTUzMDQ0ZGE2ODAzYmJhNjI=
|
data/README.md
CHANGED
data/bin/mirador-client
CHANGED
@@ -33,14 +33,14 @@ end
|
|
33
33
|
|
34
34
|
# mirador client
|
35
35
|
client = Mirador::Client.new(api_key)
|
36
|
+
out = Mirador::ResultList.new
|
36
37
|
|
37
|
-
out = []
|
38
38
|
if urls and urls.length > 0
|
39
|
-
out
|
39
|
+
out.update(client.classify_urls(urls).to_h)
|
40
40
|
end
|
41
41
|
|
42
42
|
if files and files.length > 0
|
43
|
-
out
|
43
|
+
out.update(client.classify_files(files).to_h)
|
44
44
|
end
|
45
45
|
|
46
46
|
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
require 'mirador'
|
3
|
+
|
4
|
+
mirador_client = Mirador::Client.new ENV['MIRADOR_API_KEY']
|
5
|
+
|
6
|
+
post '/proxy/mirador/url' do
|
7
|
+
content_type :json
|
8
|
+
|
9
|
+
mirador_client.classify_url(request['url']).to_json
|
10
|
+
end
|
11
|
+
|
12
|
+
post '/proxy/mirador/datauri' do
|
13
|
+
content_type :json
|
14
|
+
|
15
|
+
mirador_client.classify_data_uri(request['id'] => request['data']).to_json
|
16
|
+
end
|
17
|
+
|
18
|
+
get '/' do
|
19
|
+
|
20
|
+
<<-eot
|
21
|
+
<!doctype html>
|
22
|
+
<style> #display.safe { border: 5px solid #0e0; } #display.unsafe { border: 5px solid #e00; }</style>
|
23
|
+
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
|
24
|
+
|
25
|
+
<input type='url' id='url'/>
|
26
|
+
<input type='file' id='file'/>
|
27
|
+
<img src='' width=400 id='display'/>
|
28
|
+
<span id='res-safe'></span>
|
29
|
+
|
30
|
+
<script>
|
31
|
+
var $doc = $(document),
|
32
|
+
$display = $('#display'),
|
33
|
+
$safe = $('#res-safe');
|
34
|
+
|
35
|
+
function onresult(res) {
|
36
|
+
var safetxt = res.safe ? 'safe' : 'unsafe';
|
37
|
+
$safe.text(safetxt + ': ' + res.value.toString().substr(0, 4));
|
38
|
+
$display.attr('class', safetxt);
|
39
|
+
}
|
40
|
+
|
41
|
+
$doc.on('change', '#url', function (e) {
|
42
|
+
$.post('/proxy/mirador/url', { url: this.value }).done(onresult);
|
43
|
+
$display.attr('src', this.value);
|
44
|
+
});
|
45
|
+
|
46
|
+
$doc.on('change', '#file', function (e) {
|
47
|
+
var file = this.files[0];
|
48
|
+
if (!file) return;
|
49
|
+
|
50
|
+
var reader = new FileReader();
|
51
|
+
reader.onload = function (e) {
|
52
|
+
$display.attr('src', e.target.result);
|
53
|
+
|
54
|
+
$.post(
|
55
|
+
'/proxy/mirador/datauri',
|
56
|
+
{ id: file.name, data: e.target.result }
|
57
|
+
).done(onresult);
|
58
|
+
|
59
|
+
};
|
60
|
+
|
61
|
+
|
62
|
+
reader.readAsDataURL(file);
|
63
|
+
});
|
64
|
+
</script>
|
65
|
+
eot
|
66
|
+
|
67
|
+
end
|
data/lib/mirador/version.rb
CHANGED
data/lib/mirador.rb
CHANGED
@@ -126,28 +126,66 @@ module Mirador
|
|
126
126
|
include HTTParty
|
127
127
|
base_uri 'api.mirador.im'
|
128
128
|
|
129
|
-
default_timeout
|
129
|
+
default_timeout 20
|
130
130
|
|
131
|
-
MAX_LEN =
|
131
|
+
MAX_LEN = 4
|
132
132
|
MAX_ID_LEN = 256
|
133
133
|
DATA_URI_PRE = ';base64,'
|
134
134
|
DATA_URI_PRELEN = 8
|
135
135
|
|
136
136
|
def initialize(api_key)
|
137
137
|
@options = { api_key: api_key }
|
138
|
+
|
139
|
+
if block_given?
|
140
|
+
@parser = Proc.new
|
141
|
+
else
|
142
|
+
@parser = nil
|
143
|
+
end
|
144
|
+
|
138
145
|
end
|
139
146
|
|
140
147
|
# metaprogramming extreme
|
141
148
|
[:url, :file, :buffer, :encoded_string, :data_uri].each do |datatype|
|
142
|
-
define_method("classify_#{datatype.to_s}s") do |args, params={}|
|
143
|
-
|
149
|
+
define_method("classify_#{datatype.to_s}s") do |args, params={}, &block|
|
150
|
+
|
151
|
+
if block != nil
|
152
|
+
old_parser = @parser
|
153
|
+
@parser = block
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
res = flexible_request args, params do |item|
|
144
158
|
fmt_items(datatype, item)
|
145
159
|
end
|
160
|
+
|
161
|
+
if block != nil
|
162
|
+
@parser = old_parser
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
res
|
146
167
|
end
|
147
168
|
|
148
|
-
define_method("classify_#{datatype.to_s}") do |args, params={}|
|
169
|
+
define_method("classify_#{datatype.to_s}") do |args, params={}, &block|
|
170
|
+
|
171
|
+
if block != nil
|
172
|
+
old_parser = @parser
|
173
|
+
@parser = block
|
174
|
+
end
|
175
|
+
|
149
176
|
res = self.send("classify_#{datatype.to_s}s", args, params)
|
150
|
-
|
177
|
+
|
178
|
+
if @parser
|
179
|
+
out = if res != nil then res.values()[0] else nil end
|
180
|
+
else
|
181
|
+
out = if res != nil then res[0] else nil end
|
182
|
+
end
|
183
|
+
|
184
|
+
if block != nil
|
185
|
+
@parser = old_parser
|
186
|
+
end
|
187
|
+
|
188
|
+
out
|
151
189
|
end
|
152
190
|
|
153
191
|
end
|
@@ -238,7 +276,13 @@ module Mirador
|
|
238
276
|
# call the block X number of times
|
239
277
|
# where X is request.length / MAX_LEN
|
240
278
|
def chunked_request req, &mthd
|
241
|
-
|
279
|
+
|
280
|
+
if @parser != nil
|
281
|
+
output = {}
|
282
|
+
else
|
283
|
+
output = ResultList.new
|
284
|
+
end
|
285
|
+
|
242
286
|
req.each_slice(MAX_LEN).each do |slice|
|
243
287
|
output.update(mthd.call(slice))
|
244
288
|
end
|
@@ -339,7 +383,12 @@ module Mirador
|
|
339
383
|
raise ApiError, "no response: #{ res.code }"
|
340
384
|
end
|
341
385
|
|
342
|
-
|
386
|
+
if @parser != nil
|
387
|
+
return @parser.call(res[k])
|
388
|
+
else
|
389
|
+
|
390
|
+
return ResultList.parse_results res[k]
|
391
|
+
end
|
343
392
|
end
|
344
393
|
|
345
394
|
end
|
data/test/test_mirador.rb
CHANGED
@@ -159,6 +159,41 @@ class MiradorTest < Test::Unit::TestCase
|
|
159
159
|
|
160
160
|
end
|
161
161
|
|
162
|
+
def test_switch_parser
|
163
|
+
|
164
|
+
res = MM.classify_url 'http://static.mirador.im/test/nsfw.jpg' do |results|
|
165
|
+
|
166
|
+
r = Hash[results.map do |x|
|
167
|
+
[x['id'], x['result']]
|
168
|
+
end]
|
169
|
+
|
170
|
+
r
|
171
|
+
end
|
172
|
+
|
173
|
+
assert res.is_a?(Hash)
|
174
|
+
|
175
|
+
res_norm = MM.classify_url 'http://static.mirador.im/test/sfw.jpg'
|
176
|
+
|
177
|
+
assert (not res_norm.is_a?(Hash))
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_multiple_custom_parse
|
182
|
+
|
183
|
+
res = MM.classify_urls('http://static.mirador.im/test/nsfw.jpg', 'http://static.mirador.im/test/sfw.jpg') do |results|
|
184
|
+
|
185
|
+
r = Hash[results.map do |x|
|
186
|
+
[x['id'], x['result']]
|
187
|
+
end]
|
188
|
+
|
189
|
+
r
|
190
|
+
end
|
191
|
+
|
192
|
+
assert res.is_a?(Hash)
|
193
|
+
assert res.has_key?('http://static.mirador.im/test/nsfw.jpg')
|
194
|
+
|
195
|
+
end
|
196
|
+
|
162
197
|
def test_item_error
|
163
198
|
|
164
199
|
res = MM.classify_urls([{ id: :nsfw, data: 'invalid-url'}, { id: :sfw, data: SFW_URL }])
|
@@ -170,4 +205,33 @@ class MiradorTest < Test::Unit::TestCase
|
|
170
205
|
|
171
206
|
end
|
172
207
|
|
208
|
+
def test_custom_parser
|
209
|
+
|
210
|
+
mc = Mirador::Client.new ENV['MIRADOR_API_KEY'] do |results|
|
211
|
+
|
212
|
+
res = Hash[results.map do |x|
|
213
|
+
r = x['result']
|
214
|
+
|
215
|
+
[x['id'], {
|
216
|
+
id: x['id'],
|
217
|
+
breast: r['breast4'],
|
218
|
+
penis: r['penis4'],
|
219
|
+
vagina: r['vagina4'],
|
220
|
+
butt: r['butt4'],
|
221
|
+
}]
|
222
|
+
|
223
|
+
end]
|
224
|
+
|
225
|
+
res
|
226
|
+
end
|
227
|
+
|
228
|
+
res = mc.classify_url 'http://static.mirador.im/test/nsfw.jpg'
|
229
|
+
|
230
|
+
assert res.is_a?(Hash)
|
231
|
+
|
232
|
+
assert res[:breast]
|
233
|
+
assert res[:penis]
|
234
|
+
|
235
|
+
end
|
236
|
+
|
173
237
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mirador
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Jacob
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- README.md
|
45
45
|
- Rakefile
|
46
46
|
- bin/mirador-client
|
47
|
+
- examples/sinatra-example.rb
|
47
48
|
- lib/mirador.rb
|
48
49
|
- lib/mirador/version.rb
|
49
50
|
- mirador.gemspec
|