From f27285b419ec120cedb353a8fbd6d3c878e3f9b7 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 24 Aug 2018 22:56:52 -0400 Subject: [PATCH] Refactor Clarifai class --- lib/clarifai.rb | 57 +++++++++++++++++++++------------------ spec/lib/clarifai_spec.rb | 37 +++++++++++-------------- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/lib/clarifai.rb b/lib/clarifai.rb index d9c49c8..a12efb2 100644 --- a/lib/clarifai.rb +++ b/lib/clarifai.rb @@ -2,43 +2,48 @@ require "base64" require "http" class Clarifai - KEY = ENV.fetch("CLARIFAI_API_KEY").freeze + API_KEY = ENV.fetch("CLARIFAI_API_KEY").freeze API_URL = "https://api.clarifai.com/v2/models/" \ "aaa03c23b3724a16a56b629203edc62c/outputs".freeze - attr_reader :tags - def initialize(image_path) @image_path = image_path end - def predict! - headers = { - "Authorization": "Key #{KEY}", - "Content-Type": "application/json", - } - params = { - "inputs": [ - { - "data": { - "image": { - "base64": Base64.encode64(File.read(@image_path)), - }, - }, - }, - ], - } - - resp = HTTP. - headers(headers). - post(API_URL, json: params) - - extract_tags(JSON.parse(resp.body)) + def tags + @tags ||= extract_tags(JSON.parse(post_image.body)) end + private + def extract_tags(response_hash) - @tags = response_hash["outputs"][0]["data"]["concepts"].map do |concept| + response_hash["outputs"][0]["data"]["concepts"].map do |concept| concept["name"] end end + + def post_image + HTTP. + headers(headers). + post(API_URL, json: payload) + end + + def headers + { + "Authorization": "Key #{API_KEY}", + "Content-Type": "application/json", + } + end + + def payload + { "inputs": [{ "data": { "image": { "base64": image_base64 } } }] } + end + + def image_base64 + Base64.encode64(image_file) + end + + def image_file + File.read(@image_path) + end end diff --git a/spec/lib/clarifai_spec.rb b/spec/lib/clarifai_spec.rb index 8fe658b..7d3e46a 100644 --- a/spec/lib/clarifai_spec.rb +++ b/spec/lib/clarifai_spec.rb @@ -3,37 +3,30 @@ ENV["CLARIFAI_API_KEY"] = "1234" require "clarifai" describe Clarifai do - describe ".predict" do + describe ".tags" do it "predicts tags for our image" do - stub_body = { + WebMock. + stub_request(:post, Clarifai::API_URL). + to_return(status: 200, body: stub_body.to_json) + + clarifai_image = Clarifai.new("spec/fixtures/spectrum.jpg") + + expect(clarifai_image.tags).to eq(["computer", "technology"]) + end + + def stub_body + { "outputs": [ { "data": { "concepts": [ - { - "id": "ai_PpTcwbdQ", - "name": "computer", - "value": 0.96887743, - }, - { - "id": "ai_62K34TR4", - "name": "technology", - "value": 0.96544206, - }, + { "name": "computer", "value": 0.96887743 }, + { "name": "technology", "value": 0.96544206 }, ], }, }, ], - }.to_json - - WebMock. - stub_request(:post, Clarifai::API_URL). - to_return(status: 200, body: stub_body) - - clarifai_image = Clarifai.new("spec/fixtures/spectrum.jpg") - clarifai_image.predict! - - expect(clarifai_image.tags).to eq(["computer", "technology"]) + } end end end