class Clarifai::Rails::Detector

Attributes

model_code[R]
urls[R]

Public Class Methods

new(urls, opts={}) click to toggle source
# File lib/clarifai/rails/detector.rb, line 8
def initialize(urls, opts={})
  raise 'Input data not supported! (String Array or String only)' unless valid_params?(urls)
  @urls = urls.is_a?(Array) ? urls : [urls]
  @model_code = opts[:model] || Clarifai::Rails.model_code
  raise 'Model need to set' if @model_code.blank?
end

Public Instance Methods

error() click to toggle source

Status method

# File lib/clarifai/rails/detector.rb, line 36
def error
  Clarifai::Rails::Error.detector(to_hash[:status])
end
error?() click to toggle source
# File lib/clarifai/rails/detector.rb, line 40
def error?
  error.present?
end
image() click to toggle source
# File lib/clarifai/rails/detector.rb, line 30
def image
  Clarifai::Rails::Image.new(results.first)
end
images() click to toggle source
# File lib/clarifai/rails/detector.rb, line 24
def images
  results.map do |image|
    Clarifai::Rails::Image.new(image)
  end
end
results() click to toggle source
# File lib/clarifai/rails/detector.rb, line 20
def results
  to_hash[:outputs]
end
success?() click to toggle source
# File lib/clarifai/rails/detector.rb, line 44
def success?
  error.blank?
end
to_hash() click to toggle source
# File lib/clarifai/rails/detector.rb, line 15
def to_hash
  @data_urls ||= fetch
  @data_urls.is_a?(Hash) ? @data_urls : JSON.parse(@data_urls).symbolize_keys
end

Private Instance Methods

endpoint() click to toggle source
# File lib/clarifai/rails/detector.rb, line 51
def endpoint
  "#{Clarifai::Rails.tag_url}/#{model_code}/outputs"
end
fetch() click to toggle source
# File lib/clarifai/rails/detector.rb, line 59
def fetch
  params = { inputs: urls.map{ |url| url_data(url) } }
  result = RestClient.post(endpoint, params.to_json, Authorization: "Key #{Clarifai::Rails.api_key}")
  JSON.parse(result).symbolize_keys
end
url_data(url) click to toggle source
# File lib/clarifai/rails/detector.rb, line 65
def url_data(url)
  { data: { image: { url: url } } }
end
valid_params?(urls) click to toggle source
# File lib/clarifai/rails/detector.rb, line 55
def valid_params?(urls)
  urls.is_a?(Array) || urls.is_a?(String)
end