Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.

Commit 18cb8e1

Browse files
committed
Merge pull request #5 from itsMartini/imageTagging
SDK update
2 parents a06fa37 + db01f1c commit 18cb8e1

File tree

4 files changed

+180
-18
lines changed

4 files changed

+180
-18
lines changed

alchemyapi.rb

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ class AlchemyAPI
8080
@@ENDPOINTS['combined'] = {}
8181
@@ENDPOINTS['combined']['url'] = '/url/URLGetCombinedData'
8282
@@ENDPOINTS['combined']['text'] = '/text/TextGetCombinedData'
83-
@@ENDPOINTS['image'] = {}
84-
@@ENDPOINTS['image']['url'] = '/url/URLGetImage'
83+
@@ENDPOINTS['image_extract'] = {}
84+
@@ENDPOINTS['image_extract']['url'] = '/url/URLGetImage'
85+
@@ENDPOINTS['image_tag'] = {}
86+
@@ENDPOINTS['image_tag']['url'] = '/url/URLGetRankedImageKeywords'
87+
@@ENDPOINTS['image_tag']['image'] = '/image/ImageGetRankedImageKeywords'
8588

8689
@@BASE_URL = 'http://access.alchemyapi.com/calls'
8790

@@ -567,11 +570,11 @@ def combined(flavor, data, options = {})
567570
end
568571

569572

570-
# Extract image from a URL.
573+
# Extract image from a URL.
571574
#
572575
# INPUT:
573576
# flavor -> which version of the call, i.e. url.
574-
# data -> the data to analyze, either the the url, or html code.
577+
# data -> the data to analyze, i.e. the url.
575578
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
576579
#
577580
# Available Options:
@@ -580,20 +583,50 @@ def combined(flavor, data, options = {})
580583
# OUTPUT:
581584
# The response, already converted from JSON to a Ruby object.
582585
#
583-
def image(flavor, data, options = {})
584-
unless @@ENDPOINTS['image'].key?(flavor)
586+
def image_extract(flavor, data, options = {})
587+
unless @@ENDPOINTS['image_extract'].key?(flavor)
585588
return { 'status'=>'ERROR', 'statusInfo'=>'Image for ' + flavor + ' not available' }
586589
end
587590

588591
#Add the URL encoded data to the options and analyze
589592
options[flavor] = data
590-
return analyze(@@ENDPOINTS['image'][flavor], options)
593+
return analyze(@@ENDPOINTS['image_extract'][flavor], options)
591594
end
592595

593596

594-
private
597+
# Tag image from a URL or raw image data.
598+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/image-tagging/
599+
# For the docs, please refer to: http://www.alchemyapi.com/api/image-tagging/
600+
#
601+
# INPUT:
602+
# flavor -> which version of the call, i.e. url or image.
603+
# data -> the data to analyze, the url
604+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
605+
#
606+
# Available Options:
607+
# extractMode -> trust-metadata: less CPU-intensive and less accurate, always-infer: more CPU-intensive and more accurate
608+
# (image flavor only)
609+
# imagePostMode -> how you will post the image
610+
# raw: pass an unencoded image file using POST
611+
#
612+
# OUTPUT:
613+
# The response, already converted from JSON to a Ruby object.
614+
#
615+
def image_tag(flavor, data, options = {}, image = '')
616+
unless @@ENDPOINTS['image_tag'].key?(flavor)
617+
return { 'status'=>'ERROR', 'statusInfo'=>'Image tagging for ' + flavor + ' not available' }
618+
end
619+
620+
#Add the URL encoded data to the options and analyze
621+
unless data.empty?
622+
options[flavor] = data
623+
end
624+
return analyze_image(@@ENDPOINTS['image_tag'][flavor], options, image)
625+
end
595626

596627

628+
private
629+
597630
# HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface.
598631
# It makes the call, then converts the returned JSON string into a Ruby object.
599632
#
@@ -611,9 +644,62 @@ def analyze(url, options)
611644
#Add the API key and set the output mode to JSON
612645
options['apikey'] = @apiKey
613646
options['outputMode'] = 'json'
614-
647+
648+
uri = URI.parse(url)
649+
request = Net::HTTP::Post.new(uri.request_uri)
650+
request.set_form_data(options)
651+
652+
# disable gzip encoding which blows up in Zlib due to Ruby 2.0 bug
653+
# otherwise you'll get Zlib::BufError: buffer error
654+
request['Accept-Encoding'] = 'identity'
655+
656+
#Fire off the HTTP request
657+
res = Net::HTTP.start(uri.host, uri.port) do |http|
658+
http.request(request)
659+
end
660+
661+
#parse and return the response
662+
return JSON.parse(res.body)
663+
end
664+
665+
# HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface.
666+
# It makes the call, then converts the returned JSON string into a Ruby object.
667+
#
668+
# INPUT:
669+
# url -> the full URI encoded url
670+
# body -> the raw binary image data
671+
#
672+
# OUTPUT:
673+
# The response, already converted from JSON to a Ruby object.
674+
#
675+
def analyze_image(url, options, body)
676+
677+
#Insert the base URL
678+
url = @@BASE_URL + url
679+
680+
#Add the API key and set the output mode to JSON
681+
options['apikey'] = @apiKey
682+
options['outputMode'] = 'json'
683+
684+
url += '?'
685+
options.each { |h,v|
686+
url += h + '=' + v + '&'
687+
}
688+
689+
#Parse URL
690+
uri = URI.parse(url)
691+
692+
request = Net::HTTP::Post.new(uri.request_uri)
693+
request.body = body.to_s
694+
695+
# disable gzip encoding which blows up in Zlib due to Ruby 2.0 bug
696+
# otherwise you'll get Zlib::BufError: buffer error
697+
request['Accept-Encoding'] = 'identity'
698+
615699
#Fire off the HTTP request
616-
res = Net::HTTP::post_form(URI.parse(url), options)
700+
res = Net::HTTP.start(uri.host, uri.port) do |http|
701+
http.request(request)
702+
end
617703

618704
#parse and return the response
619705
return JSON.parse(res.body)

dog.jpg

81.3 KB
Loading

example.rb

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
alchemyapi = AlchemyAPI.new()
2424

25-
2625
puts ''
2726
puts ''
2827
puts '############################################'
@@ -487,7 +486,7 @@
487486
puts 'Processing url: ' + demo_url
488487
puts ''
489488

490-
response = alchemyapi.image('url', demo_url, { 'extractMode'=>'trust-metadata' })
489+
response = alchemyapi.image_extract('url', demo_url, { 'extractMode'=>'trust-metadata' })
491490

492491
if response['status'] == 'OK'
493492
puts '## Response Object ##'
@@ -568,3 +567,65 @@
568567
puts ''
569568
puts ''
570569

570+
571+
puts ''
572+
puts ''
573+
puts ''
574+
puts '############################################'
575+
puts '# Image Tagging Example #'
576+
puts '############################################'
577+
puts ''
578+
puts ''
579+
580+
puts 'Processing url: ' + demo_url
581+
puts ''
582+
583+
response = alchemyapi.image_tag('url', demo_url, { 'extractMode'=>'trust-metadata' })
584+
585+
if response['status'] == 'OK'
586+
puts '## Response Object ##'
587+
puts JSON.pretty_generate(response)
588+
589+
590+
puts ''
591+
puts '## Image Tagging ##'
592+
if response.key?('imageKeywords')
593+
puts 'Keywords:'
594+
for keyword in response['imageKeywords']
595+
puts "\ttext: " + keyword['text']
596+
puts "\tscore: " + keyword['score']
597+
end
598+
end
599+
puts ''
600+
else
601+
puts 'Error in image tag call: ' + response['statusInfo']
602+
end
603+
604+
path_to_test_image = 'dog.jpg'
605+
test_image = File.binread(path_to_test_image)
606+
puts 'Processing image: ' + path_to_test_image
607+
puts ''
608+
609+
response = alchemyapi.image_tag('image', '', { 'imagePostMode'=>'raw' }, test_image)
610+
611+
if response['status'] == 'OK'
612+
puts '## Response Object ##'
613+
puts JSON.pretty_generate(response)
614+
615+
616+
puts ''
617+
puts '## Image Tagging ##'
618+
if response.key?('imageKeywords')
619+
puts 'Keywords:'
620+
for keyword in response['imageKeywords']
621+
puts "\ttext: " + keyword['text']
622+
puts "\tscore: " + keyword['score']
623+
end
624+
end
625+
puts ''
626+
else
627+
puts 'Error in image tag call: ' + response['statusInfo']
628+
end
629+
630+
puts ''
631+
puts ''

tests.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Tests < Test::Unit::TestCase
2323
@@test_text = 'Bob broke my heart, and then made up this silly sentence to test the Ruby SDK'
2424
@@test_html = '<html><head><title>The best SDK Test | AlchemyAPI</title></head><body><h1>Hello World!</h1><p>My favorite language is Ruby</p></body></html>'
2525
@@test_url = 'http://www.nytimes.com/2013/07/13/us/politics/a-day-of-friction-notable-even-for-a-fractious-congress.html?_r=0'
26+
@@test_image = File.binread('dog.jpg')
2627

2728
def test_entities
2829
puts 'Checking entities . . . '
@@ -222,18 +223,32 @@ def test_combined
222223
puts ''
223224
end
224225

225-
def test_image
226-
puts 'Checking image . . . '
227-
response = @@alchemyapi.image('text', @@test_text)
226+
def test_image_extract
227+
puts 'Checking image extract . . . '
228+
response = @@alchemyapi.image_extract('text', @@test_text)
228229
assert_equal response['status'], 'ERROR' #only valid for URL content
229-
response = @@alchemyapi.image('html', @@test_html, { 'url'=>'test' })
230+
response = @@alchemyapi.image_extract('html', @@test_html, { 'url'=>'test' })
230231
assert_equal response['status'], 'ERROR' #only valid for URL content
231-
response = @@alchemyapi.image('url', @@test_url)
232+
response = @@alchemyapi.image_extract('url', @@test_url)
232233
assert_equal response['status'], 'OK'
233-
puts 'Image tests complete'
234+
puts 'Image extract tests complete'
234235
puts ''
235236
end
236237

238+
def test_image_tag
239+
puts 'Checking image tag . . . '
240+
response = @@alchemyapi.image_tag('text', @@test_text)
241+
assert_equal response['status'], 'ERROR' #only valid for URL or image content
242+
response = @@alchemyapi.image_tag('html', @@test_html, { 'url'=>'test' })
243+
assert_equal response['status'], 'ERROR' #only valid for URL or image content
244+
response = @@alchemyapi.image_tag('url', @@test_url)
245+
assert_equal response['status'], 'OK'
246+
response = @@alchemyapi.image_tag('image', '', { 'imagePostMode'=>'raw' }, @@test_image)
247+
assert_equal response['status'], 'OK'
248+
puts 'Image tag tests complete'
249+
puts ''
250+
end
251+
237252
end
238253

239254

0 commit comments

Comments
 (0)