Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Park committed Jul 22, 2016
2 parents f23ba07 + 6a57e83 commit 48c1243
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: ruby
rvm:
- 2.3.0
- 2.3.1
script: rspec tests

notifications:
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
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`.
43 changes: 9 additions & 34 deletions docker/run_ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ping_url="https://api.rosette.com/rest/v1"
retcode=0
errors=( "Exception" "processingFailure" )
errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError" "AttributeError" "ImportError" )

#------------ Start Functions --------------------------

Expand All @@ -12,11 +12,13 @@ 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
}

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")
Expand Down Expand Up @@ -55,27 +57,16 @@ 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}
usage
;;
ALT_URL)
ALT_URL=${OPTARG}
usage
;;
FILENAME)
FILENAME=${OPTARG}
usage
;;
GIT_USERNAME)
GIT_USERNAME=${OPTARG}
usage
;;
VERSION)
VERSION={OPTARG}
usage
;;
esac
done
Expand All @@ -93,11 +84,13 @@ 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"
runExample ${FILENAME}
else
echo -e "\nRunning examples against: ${ping_url}\n"
for file in *.rb; do
runExample ${file}
done
Expand All @@ -106,22 +99,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 [email protected]:${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}
2 changes: 1 addition & 1 deletion examples/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
13 changes: 8 additions & 5 deletions examples/docker/run_ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ping_url="https://api.rosette.com/rest/v1"
retcode=0
errors=( "Exception" "processingFailure" )
errors=( "Exception" "processingFailure" "badRequest" "ParseError" "ValueError" "SyntaxError" "AttributeError" "ImportError" )

#------------ Start Functions --------------------------

Expand All @@ -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")
Expand Down Expand Up @@ -53,19 +57,16 @@ 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}
usage
;;
ALT_URL)
ALT_URL=${OPTARG}
usage
;;
FILENAME)
FILENAME=${OPTARG}
usage
;;
esac
done
Expand All @@ -82,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
Expand Down
2 changes: 1 addition & 1 deletion examples/entities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
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')
response = rosette_api.get_entities(params)
puts JSON.pretty_generate(response)
2 changes: 1 addition & 1 deletion examples/entities_linked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
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)
puts JSON.pretty_generate(response)
1 change: 1 addition & 0 deletions examples/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion examples/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 9 additions & 2 deletions lib/document_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -23,14 +25,16 @@ 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]
@file_path = options[:file_path]
@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
Expand All @@ -43,6 +47,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

Expand All @@ -66,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
3 changes: 3 additions & 0 deletions lib/name_similarity_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
8 changes: 8 additions & 0 deletions lib/name_translation_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions lib/request_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ 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]
else
raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"'
end
end
params.delete 'customHeaders'
end

request['X-RosetteAPI-Key'] = @user_key
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
Expand Down Expand Up @@ -68,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
Expand All @@ -92,6 +105,19 @@ 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]
else
raise RosetteAPIError.new 'invalidHeader', 'Custom header must begin with "X-RosetteAPI-"'
end
end
params.delete 'customHeaders'
end

request.add_field 'Content-Type', "multipart/form-data; boundary=#{boundary}"
request.add_field 'X-RosetteAPI-Key', @user_key
Expand Down
9 changes: 6 additions & 3 deletions lib/rosette_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -166,7 +168,6 @@ def get_entities(params, resolve_entities = false)
params = params.load_params

endpoint = resolve_entities ? (ENTITIES_ENDPOINT + '/linked') : ENTITIES_ENDPOINT

RequestBuilder.new(@user_key, @alternate_url + endpoint, params, BINDING_VERSION)
.send_post_request
end
Expand All @@ -181,10 +182,12 @@ def get_entities(params, resolve_entities = false)
# 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` instead.'
get_entities(params, true)
end



# Extracts Tier 1 contextual categories from the input.
#
# ==== Attributes
Expand Down
Original file line number Diff line number Diff line change
@@ -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."}
{"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}}
4 changes: 2 additions & 2 deletions rosette_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand All @@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.authors = ['Basis Technology Corp']
s.email = %q{[email protected]}
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$})
Expand Down
Loading

0 comments on commit 48c1243

Please sign in to comment.