implement iqdb wrapper
This commit is contained in:
6
app/logical/iqdb/responses/base.rb
Normal file
6
app/logical/iqdb/responses/base.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Base
|
||||
end
|
||||
end
|
||||
end
|
||||
27
app/logical/iqdb/responses/collection.rb
Normal file
27
app/logical/iqdb/responses/collection.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Collection
|
||||
def initialize(response_string)
|
||||
@responses = response_string.split(/\n/).map do |string|
|
||||
::Iqdb::Responses.const_get("Response_#{string[0..2]}").new(string[4..-1])
|
||||
end
|
||||
end
|
||||
|
||||
def matches
|
||||
@matches ||= responses.select {|x| x.is_a?(Iqdb::Responses::Response_200) && x.score >= 0.9}
|
||||
end
|
||||
|
||||
def empty?
|
||||
matches.empty?
|
||||
end
|
||||
|
||||
def errored?
|
||||
errors.any?
|
||||
end
|
||||
|
||||
def errors
|
||||
@errors ||= responses.select {|x| x.is_a?(Iqdb::Responses::Error)}.map {|x| x.to_s}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
6
app/logical/iqdb/responses/error.rb
Normal file
6
app/logical/iqdb/responses/error.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Error < Base
|
||||
end
|
||||
end
|
||||
end
|
||||
8
app/logical/iqdb/responses/response_000.rb
Normal file
8
app/logical/iqdb/responses/response_000.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_000 < Base
|
||||
def initialize(response_string)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
app/logical/iqdb/responses/response_100.rb
Normal file
11
app/logical/iqdb/responses/response_100.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_100 < Base
|
||||
attr_reader :message
|
||||
|
||||
def initialize(response_string)
|
||||
@message = response_string
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
app/logical/iqdb/responses/response_101.rb
Normal file
11
app/logical/iqdb/responses/response_101.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_101 < Base
|
||||
attr_reader :key, :value
|
||||
|
||||
def initialize(response_string)
|
||||
@key, @value = response_string.split(/\=/)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
app/logical/iqdb/responses/response_102.rb
Normal file
11
app/logical/iqdb/responses/response_102.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_102 < Base
|
||||
attr_reader :dbid, :filename
|
||||
|
||||
def initialize(response_string)
|
||||
@dbid, @filename = response_string.split(/ /)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
18
app/logical/iqdb/responses/response_200.rb
Normal file
18
app/logical/iqdb/responses/response_200.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_200 < Base
|
||||
attr_reader :imgid, :score, :width, :height
|
||||
|
||||
def initialize(response_string)
|
||||
@imgid, @score, @width, @height = response_string.split(/ /)
|
||||
@score = score.to_f
|
||||
@width = width.to_i
|
||||
@height = height.to_i
|
||||
end
|
||||
|
||||
def post_id
|
||||
imgid.to_i(16)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
19
app/logical/iqdb/responses/response_201.rb
Normal file
19
app/logical/iqdb/responses/response_201.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_201 < Base
|
||||
attr_reader :dbid, :imgid, :score, :width, :height
|
||||
|
||||
def initialize(response_string)
|
||||
@dbid, @imgid, @score, @width, @height = response_string.split(/ /)
|
||||
@dbid = dbid.to_i
|
||||
@score = score.to_f
|
||||
@width = width.to_i
|
||||
@height = height.to_i
|
||||
end
|
||||
|
||||
def post_id
|
||||
imgid.to_i(16)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
19
app/logical/iqdb/responses/response_202.rb
Normal file
19
app/logical/iqdb/responses/response_202.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_202 < Base
|
||||
attr_reader :original_id, :stddev, :dupes
|
||||
|
||||
def initialize(response_string)
|
||||
response_string =~ /^(\d+)=([0-9.]+)/
|
||||
@original_id = $1
|
||||
@stddev = $2
|
||||
|
||||
@dupes = response_string.scan(/(\d+):([0-9.]+)/).map {|x| [x[0].to_i(16), x[1].to_f]}
|
||||
end
|
||||
|
||||
def original_post_id
|
||||
original_id.to_i(16)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
15
app/logical/iqdb/responses/response_300.rb
Normal file
15
app/logical/iqdb/responses/response_300.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_300 < Error
|
||||
attr_reader :message
|
||||
|
||||
def initialize(response_string)
|
||||
@message = response_string
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Error: #{message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
17
app/logical/iqdb/responses/response_301.rb
Normal file
17
app/logical/iqdb/responses/response_301.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_301 < Error
|
||||
attr_reader :exception, :description
|
||||
|
||||
def initialize(response_string)
|
||||
response_string =~ /^(\S+) (.+)/
|
||||
@exception = $1
|
||||
@description = $2
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Exception: #{exception}: #{description}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
17
app/logical/iqdb/responses/response_302.rb
Normal file
17
app/logical/iqdb/responses/response_302.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module Iqdb
|
||||
module Responses
|
||||
class Response_302 < Error
|
||||
attr_reader :exception, :description
|
||||
|
||||
def initialize(response_string)
|
||||
response_string =~ /^(\S+) (.+)/
|
||||
@exception = $1
|
||||
@description = $2
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Fatal Exception: #{exception}: #{description}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user