Class: Multitagger::Provider::Imagga

Inherits:
Object
  • Object
show all
Defined in:
lib/multitagger/provider/imagga.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Imagga

Returns a new instance of Imagga.



6
7
8
9
# File 'lib/multitagger/provider/imagga.rb', line 6

def initialize(config)
  @key = config["key"]
  @secret = config["secret"]
end

Instance Method Details

#tag(image) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/multitagger/provider/imagga.rb', line 11

def tag(image)
  auth = 'Basic ' + Base64.strict_encode64("#{@key}:#{@secret}").chomp
  image_response_raw = RestClient.post "https://api.imagga.com/v1/content",
                                       { image: File.new(image, 'rb') },
                                       { Authorization: auth }
  image_response = JSON.parse image_response_raw
  image_id = image_response["uploaded"].first["id"]
  tag_response_raw = RestClient.get "https://api.imagga.com/v1/tagging?content=#{image_id}",
                                    { Authorization: auth }
  tag_response = JSON.parse tag_response_raw
  tags = tag_response["results"].first["tags"].map { |x| [ x["tag"], x["confidence"] ] }
  tags.map { |x| Tag.new(x.first, x.last / tags.first.last) }
end