Class: Clarification::Objectifier

Inherits:
Object
  • Object
show all
Defined in:
lib/clarification/objectifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Objectifier

Returns a new instance of Objectifier.



6
7
8
9
10
11
12
13
14
# File 'lib/clarification/objectifier.rb', line 6

def initialize(response)
  @response_json = JSON.parse(response)
  @status = OpenStruct.new(@response_json["status"])
  @concepts = []
  @error = nil
  
  build_concept_objects
  
end

Instance Attribute Details

#conceptsObject (readonly)

Returns the value of attribute concepts.



4
5
6
# File 'lib/clarification/objectifier.rb', line 4

def concepts
  @concepts
end

#errorObject (readonly)

Returns the value of attribute error.



4
5
6
# File 'lib/clarification/objectifier.rb', line 4

def error
  @error
end

#response_jsonObject (readonly)

Returns the value of attribute response_json.



4
5
6
# File 'lib/clarification/objectifier.rb', line 4

def response_json
  @response_json
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/clarification/objectifier.rb', line 4

def status
  @status
end

Instance Method Details

#build_concept_objectsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/clarification/objectifier.rb', line 16

def build_concept_objects
  
  begin
    concepts = @response_json.fetch("outputs").fetch(0).fetch("data", nil).fetch("concepts", nil)
  rescue StandardError => e
    @error = e
    return
  end

  if concepts  
    concepts.each do |concept_hash|
  
      name = concept_hash["name"]
      value = concept_hash["value"]

      concept = Concept.new(name, value)

      @concepts << concept
    end
  else
    data = @response_json.fetch("outputs").fetch(0).fetch("data", nil)
    unless data.empty?
      case @response_json['outputs'][0]['model']['name']
      when "focus"
        # concept.class = concept::focus
        focus = OpenStruct.new(data["focus"])
        regions = []
        data["regions"].each do |region|
          regions << OpenStruct.new(region)
        end
        focus_hash = {focus: focus, regions: regions}
        @concepts << OpenStruct.new(focus_hash)
        @concepts.first.name = "focus"
      when "color"
        # concept.class = concept::color
        data["colors"].each do |color|
          @concepts << OpenStruct.new(color)
        end
      when "general_embedding"
        # concept.class = concept::embedding
        @concepts << OpenStruct.new(data["embeddings"][0])
        @concepts.first.name = "embedding"
      end
    end
  end

end