@@ -80,8 +80,11 @@ class AlchemyAPI
80
80
@@ENDPOINTS [ 'combined' ] = { }
81
81
@@ENDPOINTS [ 'combined' ] [ 'url' ] = '/url/URLGetCombinedData'
82
82
@@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'
85
88
86
89
@@BASE_URL = 'http://access.alchemyapi.com/calls'
87
90
@@ -567,11 +570,11 @@ def combined(flavor, data, options = {})
567
570
end
568
571
569
572
570
- # Extract image from a URL.
573
+ # Extract image from a URL.
571
574
#
572
575
# INPUT:
573
576
# 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.
575
578
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
576
579
#
577
580
# Available Options:
@@ -580,20 +583,50 @@ def combined(flavor, data, options = {})
580
583
# OUTPUT:
581
584
# The response, already converted from JSON to a Ruby object.
582
585
#
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 )
585
588
return { 'status' => 'ERROR' , 'statusInfo' => 'Image for ' + flavor + ' not available' }
586
589
end
587
590
588
591
#Add the URL encoded data to the options and analyze
589
592
options [ flavor ] = data
590
- return analyze ( @@ENDPOINTS [ 'image ' ] [ flavor ] , options )
593
+ return analyze ( @@ENDPOINTS [ 'image_extract ' ] [ flavor ] , options )
591
594
end
592
595
593
596
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
595
626
596
627
628
+ private
629
+
597
630
# HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface.
598
631
# It makes the call, then converts the returned JSON string into a Ruby object.
599
632
#
@@ -611,9 +644,62 @@ def analyze(url, options)
611
644
#Add the API key and set the output mode to JSON
612
645
options [ 'apikey' ] = @apiKey
613
646
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
+
615
699
#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
617
703
618
704
#parse and return the response
619
705
return JSON . parse ( res . body )
0 commit comments