module Metamind

Constants

CRLF
METAMIND_VISION_API
VERSION

Public Instance Methods

access_token() click to toggle source
# File lib/metamind.rb, line 28
def access_token
  get_access_token if @access_token.nil?
  @access_token
end
create_dataset(name, labels) click to toggle source
# File lib/metamind.rb, line 45
def create_dataset name, labels
  post "#{METAMIND_VISION_API}/datasets", {name: name, labels: labels}
end
create_example(dataset_id, params) click to toggle source
# File lib/metamind.rb, line 69
def create_example dataset_id, params
  post "#{METAMIND_VISION_API}/datasets/#{dataset_id}/examples", params
end
create_label(dataset_id, name) click to toggle source
# File lib/metamind.rb, line 61
def create_label dataset_id, name
  post "#{METAMIND_VISION_API}/datasets/#{dataset_id}/labels", name: name
end
delete_dataset(dataset_id) click to toggle source
# File lib/metamind.rb, line 57
def delete_dataset dataset_id
  delete "#{METAMIND_VISION_API}/datasets/#{dataset_id}"
end
delete_example(dataset_id, example_id) click to toggle source
# File lib/metamind.rb, line 81
def delete_example dataset_id, example_id
  delete "#{METAMIND_VISION_API}/datasets/#{dataset_id}/examples/#{example_id}"
end
get_all_datasets() click to toggle source
# File lib/metamind.rb, line 49
def get_all_datasets
  get "#{METAMIND_VISION_API}/datasets"
end
get_all_example(dataset_id) click to toggle source
# File lib/metamind.rb, line 77
def get_all_example dataset_id
  get "#{METAMIND_VISION_API}/datasets/#{dataset_id}/examples"
end
get_all_models(dataset_id) click to toggle source
# File lib/metamind.rb, line 97
def get_all_models dataset_id
  get "#{METAMIND_VISION_API}/datasets/#{dataset_id}/models"
end
get_dataset(dataset_id) click to toggle source
# File lib/metamind.rb, line 53
def get_dataset dataset_id
  get "#{METAMIND_VISION_API}/datasets/#{dataset_id}"
end
get_example(dataset_id, example_id) click to toggle source
# File lib/metamind.rb, line 73
def get_example dataset_id, example_id
  get "#{METAMIND_VISION_API}/datasets/#{dataset_id}/examples/#{example_id}"
end
get_label(dataset_id, label_id) click to toggle source
# File lib/metamind.rb, line 65
def get_label dataset_id, label_id
  get "#{METAMIND_VISION_API}/datasets/#{dataset_id}/labels/#{label_id}"
end
get_model_metrics(model_id) click to toggle source
# File lib/metamind.rb, line 93
def get_model_metrics model_id
  get "#{METAMIND_VISION_API}/models/#{model_id}"
end
get_training_status(model_id) click to toggle source
# File lib/metamind.rb, line 89
def get_training_status model_id
  get "#{METAMIND_VISION_API}/train/#{model_id}"
end
predict_with_base64(base64_string, modelId = 'GeneralImageClassifier') click to toggle source

def predict_with_file path, modelId = 'GeneralImageClassifier'

post "#{METAMIND_VISION_API}/predict", {sampleContent: path, modelId: modelId}

end

# File lib/metamind.rb, line 41
def predict_with_base64 base64_string, modelId = 'GeneralImageClassifier'
  post "#{METAMIND_VISION_API}/predict", {sampleBase64Content: base64_string, modelId: modelId}
end
predict_with_url(url, modelId = 'GeneralImageClassifier') click to toggle source
# File lib/metamind.rb, line 33
def predict_with_url url, modelId = 'GeneralImageClassifier'
  post "#{METAMIND_VISION_API}/predict", {sampleLocation: url, modelId: modelId}
end
train_dataset(params) click to toggle source
# File lib/metamind.rb, line 85
def train_dataset params
  post "#{METAMIND_VISION_API}/train", params
end

Private Instance Methods

build_multipart_query(params) click to toggle source
# File lib/metamind.rb, line 148
def build_multipart_query params
  parts = []
  params.each do |k, v|
    lines = []
    if v.is_a?(File)
      lines << "--#{@boundary}"
      lines << %Q{Content-Disposition: attachment; name="#{k}"}
      lines << "Content-type: image/#{File.extname(v)[1..-1]}"
      lines << "Content-Transfer-Encoding: binary"
      lines << ""
      lines << v.read
    else
      lines << "--#{@boundary}"
      lines << %Q{Content-Disposition: form-data; name="#{k}"}
      lines << ""
      lines << v
    end
    parts << lines.join(CRLF)
  end
  parts.join(CRLF) + "#{CRLF}--#{@boundary}--"
end
delete(url) click to toggle source
# File lib/metamind.rb, line 134
def delete url
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  req = Net::HTTP::Delete.new(uri.path)
  req['Authorization'] = "Bearer #{access_token}"

  res = http.request(req)
  JSON.parse(res.body)
end
get(url) click to toggle source
# File lib/metamind.rb, line 103
def get url
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  req = Net::HTTP::Get.new(uri.path)
  req['Accept-Encoding'] = 'identity'
  req['Authorization'] = "Bearer #{access_token}"

  res = http.request(req)
  JSON.parse(res.body)
end
get_access_token() click to toggle source
# File lib/metamind.rb, line 170
def get_access_token
  jwt = JWT.encode({
                       iss: 'developer.force.com',
                       sub: @email,
                       aud: 'https://api.metamind.io/v1/oauth2/token',
                       iat: Time.now.to_i,
                       exp: Time.now.to_i + 3600
                   }, @private_key, 'RS256')

  uri = URI.parse('https://api.metamind.io/v1/oauth2/token')
  http = Net::HTTP.new(uri.host, uri.port)

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  req = Net::HTTP::Post.new(uri.path)
  req.set_form_data({grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', assertion: jwt})

  res = http.request(req)
  @access_token = JSON.parse(res.body)['access_token']
end
post(url, params) click to toggle source
# File lib/metamind.rb, line 118
def post url, params
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  req = Net::HTTP::Post.new(uri.path)
  req['Content-Type'] = "multipart/form-data; boundary=#{@boundary}"
  req['Authorization'] = "Bearer #{access_token}"
  req.body = build_multipart_query(params)

  res = http.request(req)
  JSON.parse(res.body)
end