From f3dbe1adc504f4aecd0b6380b044f5c4b05e423f Mon Sep 17 00:00:00 2001 From: fiona Date: Thu, 2 Jun 2016 15:09:24 -0400 Subject: [PATCH 01/21] Unified entities and enitites/linked --- examples/entities.rb | 2 ++ examples/entities_linked.rb | 14 ----------- examples/relationships.rb | 2 +- lib/document_parameters.rb | 2 ++ lib/name_similarity_parameters.rb | 3 +++ lib/name_translation_parameters.rb | 8 +++++++ lib/rosette_api.rb | 23 ++----------------- ...ties_linked.json => entities_no_qids.json} | 2 +- tests/tests_spec.rb | 18 ++++++++------- 9 files changed, 29 insertions(+), 45 deletions(-) delete mode 100644 examples/entities_linked.rb rename mock-data/request/{entities_linked.json => entities_no_qids.json} (62%) diff --git a/examples/entities.rb b/examples/entities.rb index ed7625b..0bade64 100644 --- a/examples/entities.rb +++ b/examples/entities.rb @@ -10,5 +10,7 @@ entities_text_data = 'Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a cameo in Boston this… http://dlvr.it/BnsFfS' params = DocumentParameters.new(content: entities_text_data) +# to improve performance, and if you don't need the QID, set this option +# params.rosette_options = { linkEntities: false } response = rosette_api.get_entities(params) puts JSON.pretty_generate(response) diff --git a/examples/entities_linked.rb b/examples/entities_linked.rb deleted file mode 100644 index cb96868..0000000 --- a/examples/entities_linked.rb +++ /dev/null @@ -1,14 +0,0 @@ -require 'rosette_api' - -api_key, url = ARGV - -if !url - rosette_api = RosetteAPI.new(api_key) -else - rosette_api = RosetteAPI.new(api_key, url) -end - -entities_linked_text_data = 'Last month director Paul Feig announced the movie will have an all-star female cast including Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon.' -params = DocumentParameters.new(content: entities_linked_text_data, genre: 'social-media') -response = rosette_api.get_entities(params, true) -puts JSON.pretty_generate(response) \ No newline at end of file diff --git a/examples/relationships.rb b/examples/relationships.rb index 7111182..ab1387f 100644 --- a/examples/relationships.rb +++ b/examples/relationships.rb @@ -10,6 +10,6 @@ relationships_text_data = 'The Ghostbusters movie was filmed in Boston.' params = DocumentParameters.new(content: relationships_text_data) -params.rosette_options = { "accuracyMode" => "PRECISION" } +params.rosette_options = { accuracyMode: 'PRECISION' } response = rosette_api.get_relationships(params) puts JSON.pretty_generate(response) diff --git a/lib/document_parameters.rb b/lib/document_parameters.rb index 08f5f8e..3befdae 100644 --- a/lib/document_parameters.rb +++ b/lib/document_parameters.rb @@ -43,6 +43,8 @@ def validate_params elsif [@content, @content_uri, @file_path].all?(&:nil?) raise BadRequestFormatError.new 'The format of the request is invalid: no content provided; must' \ ' be one of an attachment, an inline "content" field, or an external "contentUri"' + elsif !@rosette_options.nil? + raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash end end diff --git a/lib/name_similarity_parameters.rb b/lib/name_similarity_parameters.rb index ffbd556..d6c4607 100644 --- a/lib/name_similarity_parameters.rb +++ b/lib/name_similarity_parameters.rb @@ -21,6 +21,7 @@ def initialize(name1, name2, options = {}) #:notnew: @genre = options[:genre] @name1 = name1 @name2 = name2 + @rosette_options = options[:rosette_options] end # Validates the parameters by checking if name1 and name2 are instances of @@ -30,6 +31,8 @@ def validate_params raise BadRequestError.new('name1 option can only be an instance of a String or NameParameter') elsif [String, NameParameter].none? { |clazz| @name2.is_a? clazz } raise BadRequestError.new('name2 option can only be an instance of a String or NameParameter') + elsif !@rosette_options.nil? + raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash end end diff --git a/lib/name_translation_parameters.rb b/lib/name_translation_parameters.rb index 5b13aa1..f8135b0 100644 --- a/lib/name_translation_parameters.rb +++ b/lib/name_translation_parameters.rb @@ -47,10 +47,18 @@ def initialize(name, target_language, options = {}) #:notnew: @target_script = options[:target_script] end + # Validates the parameters by checking if rosette_options is an instance of a Hash. + def validate_params + if !@rosette_options.nil? + raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash + end + end + # Converts this class to Hash with its keys in lower CamelCase. # # Returns the new Hash. def load_params + self.validate_params self.to_hash.select { |_key, value| !value.nil? } .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] } .to_h diff --git a/lib/rosette_api.rb b/lib/rosette_api.rb index fa45459..354de20 100644 --- a/lib/rosette_api.rb +++ b/lib/rosette_api.rb @@ -155,36 +155,17 @@ def get_parts_of_speech(params) # ==== Attributes # # * +params+ - DocumentParameters helps to build the request body in RequestBuilder. - # * +resolve_entities+ - Enables entities to be linked in application endpoints. # # Returns each entity extracted from the input. - def get_entities(params, resolve_entities = false) + def get_entities(params) check_params params - raise BadRequestError.new('Expects boolean for resolve_entities') unless !!resolve_entities == resolve_entities - params = params.load_params - endpoint = resolve_entities ? (ENTITIES_ENDPOINT + '/linked') : ENTITIES_ENDPOINT - - RequestBuilder.new(@user_key, @alternate_url + endpoint, params, BINDING_VERSION) + RequestBuilder.new(@user_key, @alternate_url + ENTITIES_ENDPOINT, params, BINDING_VERSION) .send_post_request end - # Extracts entities from the input. - # - # ==== Attributes - # - # * +params+ - DocumentParameters helps to build the request body in RequestBuilder. - # - # Returns list of entities that have been linked to entities in the knowledge - # base. - def get_entities_linked(params) - warn '[DEPRECATION] `get_entities_linked` is deprecated. Please use ' \ - '`get_entities` instead with `resolve_entities` param set to true.' - get_entities(params, true) - end - # Extracts Tier 1 contextual categories from the input. # # ==== Attributes diff --git a/mock-data/request/entities_linked.json b/mock-data/request/entities_no_qids.json similarity index 62% rename from mock-data/request/entities_linked.json rename to mock-data/request/entities_no_qids.json index 8027d1c..10ff3c7 100644 --- a/mock-data/request/entities_linked.json +++ b/mock-data/request/entities_no_qids.json @@ -1 +1 @@ -{"content":"Last month director Paul Feig announced the movie will have an all-star female cast including Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon."} \ No newline at end of file +{"content":"Last month director Paul Feig announced the movie will have an all-star female cast including Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon.","options":{"linkEntities":false}} \ No newline at end of file diff --git a/tests/tests_spec.rb b/tests/tests_spec.rb index 4ca672a..06cdc6a 100644 --- a/tests/tests_spec.rb +++ b/tests/tests_spec.rb @@ -177,10 +177,10 @@ end end - describe '.get_entities_linked' do - request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/entities_linked.json')) + describe '.get_entities_no_qids' do + request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/entities_no_qids.json')) before do - stub_request(:post, 'https://api.rosette.com/rest/v1/entities/linked'). + stub_request(:post, 'https://api.rosette.com/rest/v1/entities'). with(body: request_file, headers: {'Accept' => 'application/json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', @@ -189,21 +189,23 @@ 'X-Rosetteapi-Key' => '0123456789', 'X-Rosetteapi-Binding' => 'ruby', 'X-Rosetteapi-Binding-Version' => '1.1.1'}). - to_return(status: 200, body: {'test': 'entities/linked'}.to_json, headers: {}) + to_return(status: 200, body: {'test': 'entities'}.to_json, headers: {}) end - it 'test entities linked' do + it 'test entities without qids' do params = DocumentParameters.new params.content = 'Last month director Paul Feig announced the movie will have an all-star female cast including' \ ' Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon.' - response = RosetteAPI.new('0123456789').get_entities(params, true) + params.rosette_options = { linkEntities: false} + response = RosetteAPI.new('0123456789').get_entities(params) expect(response).instance_of? Hash end - it 'test entities linked for resolve_entities is not a boolean' do + it 'test rosette_options is not a Hash' do params = DocumentParameters.new params.content = 'Last month director Paul Feig announced the movie will have an all-star female cast including' \ ' Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon.' - expect { RosetteAPI.new('0123456789').get_entities(params, 'smth') }.to raise_error(BadRequestError) + params.rosette_options = 1 + expect { RosetteAPI.new('0123456789').get_entities(params) }.to raise_error(BadRequestError) end end From 65f3d7e203edcb01755f327c3baf9642ec40def9 Mon Sep 17 00:00:00 2001 From: fiona Date: Thu, 2 Jun 2016 16:30:06 -0400 Subject: [PATCH 02/21] Added genre "social-media" in entities example --- examples/entities.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/entities.rb b/examples/entities.rb index 0bade64..f51b39a 100644 --- a/examples/entities.rb +++ b/examples/entities.rb @@ -9,7 +9,7 @@ end entities_text_data = 'Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a cameo in Boston this… http://dlvr.it/BnsFfS' -params = DocumentParameters.new(content: entities_text_data) +params = DocumentParameters.new(content: entities_text_data, genre: 'social-media') # to improve performance, and if you don't need the QID, set this option # params.rosette_options = { linkEntities: false } response = rosette_api.get_entities(params) From 613afb31e8d3c05667916bf46c0cbceb2f0811e2 Mon Sep 17 00:00:00 2001 From: fiona Date: Mon, 6 Jun 2016 13:51:14 -0400 Subject: [PATCH 03/21] Updated docker that runs against development source to not generate gh-pages. --- docker/README.md | 2 +- docker/run_ruby.sh | 24 ++---------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/docker/README.md b/docker/README.md index b1e8aa7..ed7ca41 100644 --- a/docker/README.md +++ b/docker/README.md @@ -9,4 +9,4 @@ Build the docker image, e.g. `docker build -t basistech/ruby:1.1 .` Run an example as `docker run -e API_KEY=api-key -v "path-to-local-ruby-dir:/source" basistech/ruby:1.1` -To test against a specific source file, add `-e FILENAME=filename` before the `-v`, to test against an alternate url, add `-e ALT_URL=alternate_url`, and optionally if you would like to regenerate gh-pages from the changes made to the development source you can add `-e GIT_USERNAME=git-username -e VERSION=version` before the `-v`. In order to push the gh-pages to git remember to mount .ssh and .gitconfig to the root dir `-v path-to-.ssh-dir:/root/.ssh -v path-to-.gitconfig:/root/.gitconfig`. \ No newline at end of file +To test against a specific source file, add `-e FILENAME=filename` before the `-v`, to test against an alternate url, add `-e ALT_URL=alternate_url`. \ No newline at end of file diff --git a/docker/run_ruby.sh b/docker/run_ruby.sh index 5c8e1e9..7aa997a 100644 --- a/docker/run_ruby.sh +++ b/docker/run_ruby.sh @@ -12,8 +12,6 @@ function HELP { echo " API_KEY - Rosette API key (required)" echo " FILENAME - Ruby source file (optional)" echo " ALT_URL - Alternate service URL (optional)" - echo " GIT_USERNAME - Git username where you would like to push regenerated gh-pages (optional)" - echo " VERSION - Build version (optional)" exit 1 } @@ -55,7 +53,7 @@ function runExample() { #------------ End Functions ---------------------------- #Gets API_KEY, FILENAME and ALT_URL if present -while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do +while getopts ":API_KEY:FILENAME:ALT_URL" arg; do case "${arg}" in API_KEY) API_KEY=${OPTARG} @@ -74,7 +72,7 @@ while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do usage ;; VERSION) - VERSION={OPTARG} + VERSION=${OPTARG} usage ;; esac @@ -106,22 +104,4 @@ else HELP fi -#Generate gh-pages and push them to git account (if git username is provided) -if [ ! -z ${GIT_USERNAME} ] && [ ! -z ${VERSION} ]; then - #clone ruby git repo - cd / - git clone git@github.com:${GIT_USERNAME}/ruby.git - cd ruby - git checkout origin/gh-pages -b gh-pages - git branch -d develop - #generate gh-pages and set ouput dir to git repo (gh-pages branch) - cd /ruby-dev/lib - rdoc -o /doc - cp -r /doc/. /ruby - cd /ruby - git add . - git commit -a -m "publish ruby apidocs ${VERSION}" - git push -fi - exit ${retcode} From 877cd9704ff1074e53aaaf793f74ae1e38268fb5 Mon Sep 17 00:00:00 2001 From: fiona Date: Mon, 6 Jun 2016 14:12:08 -0400 Subject: [PATCH 04/21] Cleaned up docker images --- docker/run_ruby.sh | 5 ----- examples/docker/run_ruby.sh | 3 --- 2 files changed, 8 deletions(-) diff --git a/docker/run_ruby.sh b/docker/run_ruby.sh index 7aa997a..990622f 100644 --- a/docker/run_ruby.sh +++ b/docker/run_ruby.sh @@ -57,23 +57,18 @@ while getopts ":API_KEY:FILENAME:ALT_URL" arg; do case "${arg}" in API_KEY) API_KEY=${OPTARG} - usage ;; ALT_URL) ALT_URL=${OPTARG} - usage ;; FILENAME) FILENAME=${OPTARG} - usage ;; GIT_USERNAME) GIT_USERNAME=${OPTARG} - usage ;; VERSION) VERSION=${OPTARG} - usage ;; esac done diff --git a/examples/docker/run_ruby.sh b/examples/docker/run_ruby.sh index 30f6c1f..48f7ab1 100644 --- a/examples/docker/run_ruby.sh +++ b/examples/docker/run_ruby.sh @@ -57,15 +57,12 @@ while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do case "${arg}" in API_KEY) API_KEY=${OPTARG} - usage ;; ALT_URL) ALT_URL=${OPTARG} - usage ;; FILENAME) FILENAME=${OPTARG} - usage ;; esac done From 28f94395b02f3d6185e4df863e25391b51f5e38d Mon Sep 17 00:00:00 2001 From: fiona Date: Tue, 7 Jun 2016 13:39:49 -0400 Subject: [PATCH 05/21] Cleaned up docker image --- docker/Dockerfile | 2 +- docker/run_ruby.sh | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index a3f2e0d..c51e9b3 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -17,6 +17,6 @@ COPY run_ruby.sh run_ruby.sh RUN chmod 0755 run_ruby.sh # allow interactive bash inside docker container -CMD ./run_ruby.sh $API_KEY $FILENAME $ALT_URL +CMD ./run_ruby.sh VOLUME ["/source"] diff --git a/docker/run_ruby.sh b/docker/run_ruby.sh index 990622f..446719f 100644 --- a/docker/run_ruby.sh +++ b/docker/run_ruby.sh @@ -64,12 +64,6 @@ while getopts ":API_KEY:FILENAME:ALT_URL" arg; do FILENAME) FILENAME=${OPTARG} ;; - GIT_USERNAME) - GIT_USERNAME=${OPTARG} - ;; - VERSION) - VERSION=${OPTARG} - ;; esac done From 45def6e6e8c7297a11369bc1dee9a6386ac22739 Mon Sep 17 00:00:00 2001 From: fiona Date: Thu, 9 Jun 2016 11:08:39 -0400 Subject: [PATCH 06/21] Improved exception checks for docker --- docker/run_ruby.sh | 8 +++++++- examples/docker/Dockerfile | 2 +- examples/docker/run_ruby.sh | 10 ++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/docker/run_ruby.sh b/docker/run_ruby.sh index 446719f..9e283c2 100644 --- a/docker/run_ruby.sh +++ b/docker/run_ruby.sh @@ -2,7 +2,7 @@ ping_url="https://api.rosette.com/rest/v1" retcode=0 -errors=( "Exception" "processingFailure" ) +errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError") #------------ Start Functions -------------------------- @@ -15,6 +15,10 @@ function HELP { exit 1 } +if [ ! -z ${ALT_URL} ]; then + ping_url=${ALT_URL} +fi + #Checks if Rosette API key is valid function checkAPI { match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden") @@ -83,8 +87,10 @@ if [ ! -z ${API_KEY} ]; then rspec tests_spec.rb cd ../examples if [ ! -z ${FILENAME} ]; then + echo -e "\nRunning example against: ${ping_url}\n" runExample ${FILENAME} else + echo -e "\nRunning examples against: ${ping_url}\n" for file in *.rb; do runExample ${file} done diff --git a/examples/docker/Dockerfile b/examples/docker/Dockerfile index 192f63a..46edb1f 100644 --- a/examples/docker/Dockerfile +++ b/examples/docker/Dockerfile @@ -12,6 +12,6 @@ COPY run_ruby.sh run_ruby.sh RUN chmod 0755 run_ruby.sh # allow interactive bash inside docker container -CMD ./run_ruby.sh $API_KEY $FILENAME $ALT_URL +CMD ./run_ruby.sh VOLUME ["/source"] diff --git a/examples/docker/run_ruby.sh b/examples/docker/run_ruby.sh index 48f7ab1..693c065 100644 --- a/examples/docker/run_ruby.sh +++ b/examples/docker/run_ruby.sh @@ -2,7 +2,7 @@ ping_url="https://api.rosette.com/rest/v1" retcode=0 -errors=( "Exception" "processingFailure" ) +errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError") #------------ Start Functions -------------------------- @@ -15,6 +15,10 @@ function HELP { exit 1 } +if [ ! -z ${ALT_URL} ]; then + ping_url=${ALT_URL} +fi + #Checks if Rosette API key is valid function checkAPI { match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden") @@ -53,7 +57,7 @@ function runExample() { #------------ End Functions ---------------------------- #Gets API_KEY, FILENAME and ALT_URL if present -while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do +while getopts ":API_KEY:FILENAME:ALT_URL" arg; do case "${arg}" in API_KEY) API_KEY=${OPTARG} @@ -79,8 +83,10 @@ cp /source/examples/*.* . if [ ! -z ${API_KEY} ]; then checkAPI if [ ! -z ${FILENAME} ]; then + echo -e "\nRunning example against: ${ping_url}\n" runExample ${FILENAME} else + echo -e "\nRunning examples against: ${ping_url}\n" for file in *.rb; do runExample ${file} done From 52d7cc7bb920f25db9a15c674f3f4346089957a3 Mon Sep 17 00:00:00 2001 From: fiona Date: Thu, 9 Jun 2016 11:26:58 -0400 Subject: [PATCH 07/21] Added more exception in docker image --- docker/run_ruby.sh | 2 +- examples/docker/run_ruby.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/run_ruby.sh b/docker/run_ruby.sh index 9e283c2..5338649 100644 --- a/docker/run_ruby.sh +++ b/docker/run_ruby.sh @@ -2,7 +2,7 @@ ping_url="https://api.rosette.com/rest/v1" retcode=0 -errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError") +errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError" "AttributeError" "ImportError" ) #------------ Start Functions -------------------------- diff --git a/examples/docker/run_ruby.sh b/examples/docker/run_ruby.sh index 693c065..66636fd 100644 --- a/examples/docker/run_ruby.sh +++ b/examples/docker/run_ruby.sh @@ -2,7 +2,7 @@ ping_url="https://api.rosette.com/rest/v1" retcode=0 -errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError") +errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError" "AttributeError" "ImportError" ) #------------ Start Functions -------------------------- From 055177d5e5519a9f58464f6a92943eeb83781237 Mon Sep 17 00:00:00 2001 From: SamHausmann Date: Mon, 13 Jun 2016 14:46:32 -0400 Subject: [PATCH 08/21] custom header capability added --- examples/language.rb | 1 + lib/document_parameters.rb | 9 +++++++-- lib/request_builder.rb | 21 +++++++++++++++++++++ lib/rosette_api.rb | 2 ++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/examples/language.rb b/examples/language.rb index 8175b2a..465cff1 100644 --- a/examples/language.rb +++ b/examples/language.rb @@ -10,5 +10,6 @@ language_data = 'Por favor Señorita, says the man.?' params = DocumentParameters.new(content: language_data) +params.custom_headers = { "X-RosetteAPI-App": "ruby-app"} response = rosette_api.get_language(params) puts JSON.pretty_generate(response) \ No newline at end of file diff --git a/lib/document_parameters.rb b/lib/document_parameters.rb index 3befdae..43cc36e 100644 --- a/lib/document_parameters.rb +++ b/lib/document_parameters.rb @@ -15,6 +15,8 @@ class DocumentParameters attr_accessor :language # Rosette API options (optional, should be a hash) attr_accessor :rosette_options + # custom Rosette API headers + attr_accessor :custom_headers def initialize(options = {}) #:notnew: options = { @@ -23,7 +25,8 @@ def initialize(options = {}) #:notnew: file_path: nil, genre: nil, language: nil, - rosette_options: nil + rosette_options: nil, + custom_headers: nil }.update options @content = options[:content] @content_uri = options[:content_uri] @@ -31,6 +34,7 @@ def initialize(options = {}) #:notnew: @genre = options[:genre] @language = options[:language] @rosette_options = options[:rosette_options] + @custom_headers = options[:custom_headers] end # Validates the parameters by checking if there are multiple content sources @@ -68,7 +72,8 @@ def to_hash file_path: @file_path, genre: @genre, language: @language, - options: @rosette_options + options: @rosette_options, + custom_headers: @custom_headers } end end diff --git a/lib/request_builder.rb b/lib/request_builder.rb index 60967ff..60c1604 100644 --- a/lib/request_builder.rb +++ b/lib/request_builder.rb @@ -41,6 +41,16 @@ def prepare_plain_request(params) raise RosetteAPIError.new 'connectionError', 'Failed to establish connection with Rosette API server.' end + if params["customHeaders"] != nil + keys_array = params["customHeaders"].keys + for k in keys_array + if k.to_s =~ /^X-RosetteAPI-/ + request[k] = params['customHeaders'][k] + end + end + params.delete "customHeaders" + end + request['X-RosetteAPI-Key'] = @user_key request['Content-Type'] = 'application/json' request['Accept'] = 'application/json' @@ -92,6 +102,17 @@ def prepare_multipart_request(params) rescue raise RosetteAPIError.new 'connectionError', 'Failed to establish connection with Rosette API server.' end + + # add any custom headers from the user + if params["customHeaders"] != nil + keys_array = params["customHeaders"].keys + for k in keys_array + if k.to_s =~ /^X-RosetteAPI-/ + request.add_field k, params['customHeaders'][k] + end + end + params.delete "customHeaders" + end request.add_field 'Content-Type', "multipart/form-data; boundary=#{boundary}" request.add_field 'X-RosetteAPI-Key', @user_key diff --git a/lib/rosette_api.rb b/lib/rosette_api.rb index 354de20..b2896a6 100644 --- a/lib/rosette_api.rb +++ b/lib/rosette_api.rb @@ -39,6 +39,8 @@ class RosetteAPI attr_accessor :user_key # Alternate Rosette API URL attr_accessor :alternate_url + # custom Rosette API headers + attr_accessor :custom_headers def initialize(user_key, alternate_url = 'https://api.rosette.com/rest/v1') #:notnew: @user_key = user_key From 3a33f0d93b75d24aae1eacecbff20e11544abfcc Mon Sep 17 00:00:00 2001 From: SamHausmann Date: Mon, 13 Jun 2016 15:18:16 -0400 Subject: [PATCH 09/21] exception handling and invalid header test --- lib/request_builder.rb | 4 ++++ tests/tests_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/request_builder.rb b/lib/request_builder.rb index 60c1604..507abf4 100644 --- a/lib/request_builder.rb +++ b/lib/request_builder.rb @@ -46,6 +46,8 @@ def prepare_plain_request(params) for k in keys_array if k.to_s =~ /^X-RosetteAPI-/ request[k] = params['customHeaders'][k] + else + raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"' end end params.delete "customHeaders" @@ -109,6 +111,8 @@ def prepare_multipart_request(params) for k in keys_array if k.to_s =~ /^X-RosetteAPI-/ request.add_field k, params['customHeaders'][k] + else + raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"' end end params.delete "customHeaders" diff --git a/tests/tests_spec.rb b/tests/tests_spec.rb index 06cdc6a..8b1510a 100644 --- a/tests/tests_spec.rb +++ b/tests/tests_spec.rb @@ -393,5 +393,28 @@ end end + describe '.get_language_custom_header' do + request_file = File.read File.expand_path(File.join(File.dirname(__FILE__), '../mock-data/request/language.json')) + before do + stub_request(:post, 'https://api.rosette.com/rest/v1/language'). + with(body: request_file, + headers: {'Accept' => 'application/json', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789', + 'X-Rosetteapi-Binding' => 'ruby', + 'X-Rosetteapi-Binding-Version' => '1.1.1', + 'X-Rosetteapi-App' => 'ruby-app'}). + to_return(status: 200, body: {'test': 'language'}.to_json, headers: {}) + end + + it 'test custom_headers is invalid' do + params = DocumentParameters.new + params.content = 'Por favor Senorita, says the man.?' + params.custom_headers = { 'test': 'ruby-app'} + expect { RosetteAPI.new('0123456789').get_entities(params) }.to raise_error(BadRequestError) + end + end end From 4686b4b2bc9c12cfa1c197d5d2ce2908c59dc569 Mon Sep 17 00:00:00 2001 From: SamHausmann Date: Mon, 13 Jun 2016 15:21:01 -0400 Subject: [PATCH 10/21] cleanup --- tests/tests_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests_spec.rb b/tests/tests_spec.rb index 8b1510a..32bb9b7 100644 --- a/tests/tests_spec.rb +++ b/tests/tests_spec.rb @@ -413,7 +413,7 @@ params = DocumentParameters.new params.content = 'Por favor Senorita, says the man.?' params.custom_headers = { 'test': 'ruby-app'} - expect { RosetteAPI.new('0123456789').get_entities(params) }.to raise_error(BadRequestError) + expect { RosetteAPI.new('0123456789').get_language(params) }.to raise_error(invalidHeader) end end From 8e23c05db84c73ab018a7e6490492515adcfec8c Mon Sep 17 00:00:00 2001 From: SamHausmann Date: Mon, 13 Jun 2016 15:34:32 -0400 Subject: [PATCH 11/21] cleanup --- tests/tests_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests_spec.rb b/tests/tests_spec.rb index 32bb9b7..0f6769a 100644 --- a/tests/tests_spec.rb +++ b/tests/tests_spec.rb @@ -413,7 +413,7 @@ params = DocumentParameters.new params.content = 'Por favor Senorita, says the man.?' params.custom_headers = { 'test': 'ruby-app'} - expect { RosetteAPI.new('0123456789').get_language(params) }.to raise_error(invalidHeader) + expect { RosetteAPI.new('0123456789').get_language(params) }.to raise_error("invalidHeader") end end From 16c16ebb3c65185a0179688d898893879b242481 Mon Sep 17 00:00:00 2001 From: SamHausmann Date: Mon, 13 Jun 2016 15:41:53 -0400 Subject: [PATCH 12/21] fixed invalid header test --- tests/tests_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests_spec.rb b/tests/tests_spec.rb index 0f6769a..d10ff9b 100644 --- a/tests/tests_spec.rb +++ b/tests/tests_spec.rb @@ -405,7 +405,7 @@ 'X-Rosetteapi-Key' => '0123456789', 'X-Rosetteapi-Binding' => 'ruby', 'X-Rosetteapi-Binding-Version' => '1.1.1', - 'X-Rosetteapi-App' => 'ruby-app'}). + 'X-RosetteApi-App' => 'ruby-app'}). to_return(status: 200, body: {'test': 'language'}.to_json, headers: {}) end @@ -413,7 +413,7 @@ params = DocumentParameters.new params.content = 'Por favor Senorita, says the man.?' params.custom_headers = { 'test': 'ruby-app'} - expect { RosetteAPI.new('0123456789').get_language(params) }.to raise_error("invalidHeader") + expect { RosetteAPI.new('0123456789').get_language(params) }.to raise_error(RosetteAPIError) end end From ebcb4363411a1847d3eec9324e415445a5fb8c04 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Mon, 20 Jun 2016 09:20:29 -0400 Subject: [PATCH 13/21] Added test for 409, set rspce to be more verbose --- docker/run_ruby.sh | 2 +- tests/tests_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/docker/run_ruby.sh b/docker/run_ruby.sh index 5338649..1a3e86d 100644 --- a/docker/run_ruby.sh +++ b/docker/run_ruby.sh @@ -84,7 +84,7 @@ gem install ./rosette_api-*.gem if [ ! -z ${API_KEY} ]; then checkAPI cd tests - rspec tests_spec.rb + rspec tests_spec.rb --format documentation cd ../examples if [ ! -z ${FILENAME} ]; then echo -e "\nRunning example against: ${ping_url}\n" diff --git a/tests/tests_spec.rb b/tests/tests_spec.rb index d10ff9b..e49bbbc 100644 --- a/tests/tests_spec.rb +++ b/tests/tests_spec.rb @@ -417,4 +417,19 @@ end end + describe '.error_409_incompatible_client_version' do + before do + stub_request(:get, 'https://api.rosette.com/rest/v1/info'). + with(headers: {'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'User-Agent' => 'Ruby', + 'X-Rosetteapi-Key' => '0123456789'}). + to_return(status: 409, body: {'code': 'incompatibleClientVersion'}.to_json, headers: {}) + end + it 'test error 409 properly handled' do + expect { RosetteAPI.new('0123456789').info }.to raise_error(RosetteAPIError) + end + end + + end From 3c736f9eb75099e61bc518a5226385fba7e53f3f Mon Sep 17 00:00:00 2001 From: Chris Park Date: Tue, 21 Jun 2016 11:42:27 -0400 Subject: [PATCH 14/21] Removed filePath before processing request to prevent it being included in parameters --- lib/request_builder.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/request_builder.rb b/lib/request_builder.rb index 507abf4..ad47e64 100644 --- a/lib/request_builder.rb +++ b/lib/request_builder.rb @@ -80,6 +80,7 @@ def prepare_multipart_request(params) boundary = SecureRandom.hex post_body = [] + params.delete 'filePath' request_file = params.to_json # Add the content data From adfcd5bd02b144c53d28df64b5f8db5bf26ee84c Mon Sep 17 00:00:00 2001 From: Chris Park Date: Tue, 21 Jun 2016 11:48:08 -0400 Subject: [PATCH 15/21] Style fix "customHeaders" to 'customHeaders' --- lib/request_builder.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/request_builder.rb b/lib/request_builder.rb index ad47e64..0dd5558 100644 --- a/lib/request_builder.rb +++ b/lib/request_builder.rb @@ -41,8 +41,8 @@ def prepare_plain_request(params) raise RosetteAPIError.new 'connectionError', 'Failed to establish connection with Rosette API server.' end - if params["customHeaders"] != nil - keys_array = params["customHeaders"].keys + if params['customHeaders'] != nil + keys_array = params['customHeaders'].keys for k in keys_array if k.to_s =~ /^X-RosetteAPI-/ request[k] = params['customHeaders'][k] @@ -50,7 +50,7 @@ def prepare_plain_request(params) raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"' end end - params.delete "customHeaders" + params.delete 'customHeaders' end request['X-RosetteAPI-Key'] = @user_key @@ -107,8 +107,8 @@ def prepare_multipart_request(params) end # add any custom headers from the user - if params["customHeaders"] != nil - keys_array = params["customHeaders"].keys + if params['customHeaders'] != nil + keys_array = params['customHeaders'].keys for k in keys_array if k.to_s =~ /^X-RosetteAPI-/ request.add_field k, params['customHeaders'][k] @@ -116,7 +116,7 @@ def prepare_multipart_request(params) raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"' end end - params.delete "customHeaders" + params.delete 'customHeaders' end request.add_field 'Content-Type', "multipart/form-data; boundary=#{boundary}" From ea8a8cd8901b92748615379d1fce4fff5104741b Mon Sep 17 00:00:00 2001 From: fiona Date: Tue, 28 Jun 2016 15:04:20 -0400 Subject: [PATCH 16/21] Removed "linkEntities" from entities example --- examples/entities.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/entities.rb b/examples/entities.rb index f51b39a..0ec64d0 100644 --- a/examples/entities.rb +++ b/examples/entities.rb @@ -10,7 +10,5 @@ entities_text_data = 'Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a cameo in Boston this… http://dlvr.it/BnsFfS' params = DocumentParameters.new(content: entities_text_data, genre: 'social-media') -# to improve performance, and if you don't need the QID, set this option -# params.rosette_options = { linkEntities: false } response = rosette_api.get_entities(params) puts JSON.pretty_generate(response) From 6013b957b5d30ff09f7326865189bd830be4f9fb Mon Sep 17 00:00:00 2001 From: Hannah Date: Wed, 29 Jun 2016 15:38:14 -0400 Subject: [PATCH 17/21] removing double quotes --- examples/language.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/language.rb b/examples/language.rb index 465cff1..42124a3 100644 --- a/examples/language.rb +++ b/examples/language.rb @@ -10,6 +10,6 @@ language_data = 'Por favor Señorita, says the man.?' params = DocumentParameters.new(content: language_data) -params.custom_headers = { "X-RosetteAPI-App": "ruby-app"} +params.custom_headers = { 'X-RosetteAPI-App': 'ruby-app'} response = rosette_api.get_language(params) puts JSON.pretty_generate(response) \ No newline at end of file From da042b3a863defb3795a548a7fe6181df8a1c16d Mon Sep 17 00:00:00 2001 From: Hannah Date: Wed, 29 Jun 2016 15:46:26 -0400 Subject: [PATCH 18/21] changing custom header --- examples/language.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/language.rb b/examples/language.rb index 42124a3..c0f7ef9 100644 --- a/examples/language.rb +++ b/examples/language.rb @@ -10,6 +10,6 @@ language_data = 'Por favor Señorita, says the man.?' params = DocumentParameters.new(content: language_data) -params.custom_headers = { 'X-RosetteAPI-App': 'ruby-app'} +params.custom_headers = { 'X-RosetteAPI-App'=> 'ruby-app'} response = rosette_api.get_language(params) puts JSON.pretty_generate(response) \ No newline at end of file From 22a3c56fb21bc6c8daf6258d941d5cef9b289e90 Mon Sep 17 00:00:00 2001 From: Hannah Date: Tue, 12 Jul 2016 08:17:17 -0400 Subject: [PATCH 19/21] restoring entities/linked --- examples/entities_linked.rb | 14 ++++++++++++++ lib/rosette_api.rb | 24 ++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 examples/entities_linked.rb diff --git a/examples/entities_linked.rb b/examples/entities_linked.rb new file mode 100644 index 0000000..82a3b71 --- /dev/null +++ b/examples/entities_linked.rb @@ -0,0 +1,14 @@ +require 'rosette_api' + +api_key, url = ARGV + +if !url + rosette_api = RosetteAPI.new(api_key) +else + rosette_api = RosetteAPI.new(api_key, url) +end + +entities_linked_text_data = 'Last month director Paul Feig announced the movie will have an all-star female cast including Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon.' +params = DocumentParameters.new(content: entities_linked_text_data, genre: 'social-media') +response = rosette_api.get_entities(params, true) +puts JSON.pretty_generate(response) diff --git a/lib/rosette_api.rb b/lib/rosette_api.rb index b2896a6..2e30368 100644 --- a/lib/rosette_api.rb +++ b/lib/rosette_api.rb @@ -157,17 +157,37 @@ def get_parts_of_speech(params) # ==== Attributes # # * +params+ - DocumentParameters helps to build the request body in RequestBuilder. + # * +resolve_entities+ - Enables entities to be linked in application endpoints. # # Returns each entity extracted from the input. - def get_entities(params) + def get_entities(params, resolve_entities = false) check_params params + raise BadRequestError.new('Expects boolean for resolve_entities') unless !!resolve_entities == resolve_entities + params = params.load_params - RequestBuilder.new(@user_key, @alternate_url + ENTITIES_ENDPOINT, params, BINDING_VERSION) + endpoint = resolve_entities ? (ENTITIES_ENDPOINT + '/linked') : ENTITIES_ENDPOINT + RequestBuilder.new(@user_key, @alternate_url + endpoint, params, BINDING_VERSION) .send_post_request end + # Extracts entities from the input. + # + # ==== Attributes + # + # * +params+ - DocumentParameters helps to build the request body in RequestBuilder. + # + # Returns list of entities that have been linked to entities in the knowledge + # base. + def get_entities_linked(params) + warn '[DEPRECATION] `get_entities_linked` is deprecated. Please use ' \ + '`get_entities` instead.' + get_entities(params, true) + end + + + # Extracts Tier 1 contextual categories from the input. # # ==== Attributes From e34caaa027941d5117c912cfdd16c619768a92a9 Mon Sep 17 00:00:00 2001 From: SamHausmann Date: Tue, 12 Jul 2016 21:16:56 -0400 Subject: [PATCH 20/21] update travis rvm from 2.3.0 to 2.3.1 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 574671c..7a324f9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: ruby rvm: - - 2.3.0 + - 2.3.1 script: rspec tests notifications: From 6a57e836def6272673c16f2795ba3d21644dd228 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Fri, 22 Jul 2016 14:25:29 +0000 Subject: [PATCH 21/21] Version 1.2.0 --- lib/rosette_api.rb | 2 +- rosette_api.gemspec | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/rosette_api.rb b/lib/rosette_api.rb index 2e30368..0ad688a 100644 --- a/lib/rosette_api.rb +++ b/lib/rosette_api.rb @@ -9,7 +9,7 @@ # This class allows you to access all Rosette API endpoints. class RosetteAPI # Version of Ruby binding - BINDING_VERSION = '1.1.1' + BINDING_VERSION = '1.2.0' # Rosette API language endpoint LANGUAGE_ENDPOINT = '/language' # Rosette API morphology endpoint diff --git a/rosette_api.gemspec b/rosette_api.gemspec index a8b42f7..11c0861 100644 --- a/rosette_api.gemspec +++ b/rosette_api.gemspec @@ -8,7 +8,7 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 2.0.0' s.name = 'rosette_api' - s.version = '1.1.1' + s.version = '1.2.0' s.license = 'MIT' s.summary = 'Rosette API gem that supports multilingual text-analytics.' @@ -19,7 +19,7 @@ Gem::Specification.new do |s| s.authors = ['Basis Technology Corp'] s.email = %q{support@rosette.com} s.homepage = %q{https://developer.rosette.com/} - s.date = %q{2016-06-08} + s.date = %q{2016-07-22} all_files = `git ls-files -z`.split("\x0") s.files = all_files.grep(%r{^(bin|lib)/|^.rubocop.yml$})