From e326c7d2bd79f086ecd0cb8a19805f3548c3d5ca Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sat, 25 Aug 2018 12:05:09 -0400 Subject: [PATCH] Refactor Clarifai spec --- spec/lib/clarifai_spec.rb | 27 +++++++-------------------- spec/support/clarifai_helpers.rb | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 spec/support/clarifai_helpers.rb diff --git a/spec/lib/clarifai_spec.rb b/spec/lib/clarifai_spec.rb index 7d3e46a..ff1c056 100644 --- a/spec/lib/clarifai_spec.rb +++ b/spec/lib/clarifai_spec.rb @@ -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 diff --git a/spec/support/clarifai_helpers.rb b/spec/support/clarifai_helpers.rb new file mode 100644 index 0000000..1d864be --- /dev/null +++ b/spec/support/clarifai_helpers.rb @@ -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