1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00

Refactor Clarifai spec

This commit is contained in:
Daniel Barber 2018-08-25 12:05:09 -04:00
parent d933f8aa3f
commit e326c7d2bd
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 24 additions and 20 deletions

View File

@ -1,32 +1,19 @@
require "spec_helper"
ENV["CLARIFAI_API_KEY"] = "1234"
require "spec_helper"
require "clarifai"
require_relative "../support/clarifai_helpers"
describe Clarifai do
include ClarifaiHelpers
describe ".tags" do
it "predicts tags for our image" do
WebMock.
stub_request(:post, Clarifai::API_URL).
to_return(status: 200, body: stub_body.to_json)
stub_clarifai(%w[computer technology])
clarifai_image = Clarifai.new("spec/fixtures/spectrum.jpg")
expect(clarifai_image.tags).to eq(["computer", "technology"])
end
def stub_body
{
"outputs": [
{
"data": {
"concepts": [
{ "name": "computer", "value": 0.96887743 },
{ "name": "technology", "value": 0.96544206 },
],
},
},
],
}
expect(clarifai_image.tags).to eq(%w[computer technology])
end
end
end

View File

@ -0,0 +1,17 @@
module ClarifaiHelpers
def stub_clarifai(tags)
WebMock.
stub_request(:post, Clarifai::API_URL).
to_return(status: 200, body: stub_body(tags).to_json)
end
def stub_body(tags)
{ "outputs": [{ "data": { "concepts": tag_structure(tags) } }] }
end
def tag_structure(tags)
tags.map do |tag|
{ "name": tag, "value": 0.95 }
end
end
end