Skip to content

Commit

Permalink
Merge pull request #13 from cp2boston/develop
Browse files Browse the repository at this point in the history
No jira Docker and script cleanup
  • Loading branch information
cp2boston committed Mar 25, 2016
2 parents 4104859 + 067488c commit 21f4c28
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 40 deletions.
9 changes: 5 additions & 4 deletions examples/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ MAINTAINER Fiona Hasanaj
# install necessary software
RUN apt-get -y update && apt-get install -y vim && apt-get install -y git && gem install rubysl-securerandom

COPY run_ruby.sh /ruby/examples/run_ruby.sh
RUN chmod 755 /ruby/examples/run_ruby.sh
WORKDIR /ruby/examples
RUN mkdir /ruby
WORKDIR /ruby
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

VOLUME ["/source"]
VOLUME ["/source"]
102 changes: 66 additions & 36 deletions examples/docker/run_ruby.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/bin/bash

ping_url="https://api.rosette.com/rest/v1"
retcode=0
errors=( "Exception" "processingFailure" )

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

#Gets called when the user doesn't provide any args
function HELP {
echo -e "\nusage: source_file.rb API_KEY [ALT_URL]"
Expand All @@ -9,6 +15,56 @@ function HELP {
exit 1
}

#Checks if Rosette API key is valid
function checkAPI {
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden")
if [ ! -z $match ]; then
echo -e "\nInvalid Rosette API Key"
exit 1
fi
}


# strip the trailing slash off of the alt_url if necessary
function cleanURL() {
if [ ! -z "${ALT_URL}" ]; then
case ${ALT_URL} in
*/) ALT_URL=${ALT_URL::-1}
echo "Slash detected"
;;
esac
ping_url=${ALT_URL}
fi
}

#Checks for valid url
function validateURL() {
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API")
if [ "${match}" = "" ]; then
echo -e "\n${ping_url} server not responding\n"
exit 1
fi
}

function runExample() {
echo -e "\n---------- ${1} start -------------"
result=""
if [ -z ${ALT_URL} ]; then
result="$(ruby ${1} ${API_KEY} 2>&1 )"
else
result="$(ruby ${1} ${API_KEY} ${ALT_URL} 2>&1 )"
fi
echo "${result}"
echo -e "\n---------- ${1} end -------------"
for err in "${errors[@]}"; do
if [[ ${result} == *"${err}"* ]]; then
retcode=1
fi
done
}

#------------ End Functions ----------------------------

#Gets API_KEY, FILENAME and ALT_URL if present
while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
case "${arg}" in
Expand All @@ -27,52 +83,26 @@ while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
esac
done

ping_url="https://api.rosette.com/rest/v1"

# strip the trailing slash off of the alt_url if necessary
if [ ! -z "${ALT_URL}" ]; then
case ${ALT_URL} in
*/) ALT_URL=${ALT_URL::-1}
echo "Slash detected"
;;
esac
ping_url=${ALT_URL}
fi

#Checks for valid url
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API")
if [ "${match}" = "" ]; then
echo -e "\n${ping_url} server not responding\n"
exit 1
fi

#Checks if Rosette API key is valid
function checkAPI {
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden")
if [ ! -z $match ]; then
echo -e "\nInvalid Rosette API Key"
exit 1
fi
}
cleanURL

validateURL

#Copy the mounted content in /source to current WORKDIR
cp /source/*.* .
cp -r -n /source/. .

#Run the examples
if [ ! -z ${API_KEY} ]; then
checkAPI
cd examples
if [ ! -z ${FILENAME} ]; then
if [ ! -z ${ALT_URL} ]; then
ruby ${FILENAME} ${API_KEY} ${ALT_URL}
else
ruby ${FILENAME} ${API_KEY}
fi
elif [ ! -z ${ALT_URL} ]; then
find -maxdepth 1 -name '*.rb' -print -exec ruby {} ${API_KEY} ${ALT_URL} \;
runExample ${FILENAME}
else
find -maxdepth 1 -name '*.rb' -print -exec ruby {} ${API_KEY} \;
for file in *.rb; do
runExample ${file}
done
fi
else
HELP
fi

exit ${retcode}

0 comments on commit 21f4c28

Please sign in to comment.