Skip to content

Commit

Permalink
Merge pull request #27 from SamHausmann/RCB-396
Browse files Browse the repository at this point in the history
removed check version and added new headers to be sent to API to tran…
  • Loading branch information
lauren12292 committed May 12, 2016
2 parents cf57095 + 43477ef commit 1219e34
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 178 deletions.
12 changes: 9 additions & 3 deletions lib/request_builder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: UTF-8
require 'net/http'
require 'net/https'
require 'json'
Expand All @@ -12,12 +13,15 @@ class RequestBuilder
attr_accessor :params
# Rosette API key
attr_accessor :user_key
# Rosette API binding version
attr_accessor :binding_version

def initialize(user_key, alternate_url, params = {}) #:notnew:
def initialize(user_key, alternate_url, params = {}, binding_version) #:notnew:
@user_key = user_key
@alternate_url = alternate_url
@params = params
@retries = 5
@binding_version = binding_version
end

# Prepares a plain POST request for Rosette API.
Expand All @@ -40,6 +44,8 @@ def prepare_plain_request(params)
request['X-RosetteAPI-Key'] = @user_key
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['X-RosetteAPI-Binding'] = 'ruby'
request['X-RosetteAPI-Binding-Version'] = @binding_version
request.body = params.to_json

[http, request]
Expand Down Expand Up @@ -84,6 +90,8 @@ def prepare_multipart_request(params)
request = Net::HTTP::Post.new uri.request_uri
request.add_field 'Content-Type', "multipart/form-data; boundary=#{boundary}"
request.add_field 'X-RosetteAPI-Key', @user_key
request.add_field 'X-RosetteAPI-Binding', 'ruby'
request.add_field 'X-RosetteAPI-Binding-Version', @binding_version
request.body = post_body.join

[http, request]
Expand All @@ -107,8 +115,6 @@ def send_get_request
#
# Returns JSON response or raises RosetteAPIError if encountered.
def send_post_request
params = (@alternate_url.to_s.include? '/info?clientVersion=') ? '{"body": "version check"}' : @params

if !params['filePath'].nil?
http, request = self.prepare_multipart_request params
else
Expand Down
50 changes: 17 additions & 33 deletions lib/rosette_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class RosetteAPI
SENTENCES_ENDPOINT = '/sentences'
# Rosette API info endpoint
INFO = '/info'
# Rosette API version check endpoint
VERSION_CHECK = '/info?clientVersion=' + BINDING_VERSION
# Rosette API ping endpoint
PING = '/ping'

Expand All @@ -49,20 +47,6 @@ def initialize(user_key, alternate_url = 'https://api.rosette.com/rest/v1') #:no
if @alternate_url.to_s.end_with?('/')
@alternate_url = alternate_url.to_s.slice(0..-2)
end

self.check_version_compatibility
end

# Checks binding version compatibility against the Rosette API server.
def check_version_compatibility
response = RequestBuilder.new(@user_key, @alternate_url + VERSION_CHECK)
.send_post_request

unless response['versionChecked']
puts JSON.pretty_generate(response)

exit
end
end

# Identifies in which language(s) the input is written.
Expand All @@ -77,7 +61,7 @@ def get_language(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + LANGUAGE_ENDPOINT, params)
RequestBuilder.new(@user_key, @alternate_url + LANGUAGE_ENDPOINT, params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -95,7 +79,7 @@ def get_morphology_complete(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/complete', params)
RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/complete', params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -112,7 +96,7 @@ def get_compound_components(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/compound-components', params)
RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/compound-components', params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -129,7 +113,7 @@ def get_han_readings(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/han-readings', params)
RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/han-readings', params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -145,7 +129,7 @@ def get_lemmas(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/lemmas', params)
RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/lemmas', params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -162,7 +146,7 @@ def get_parts_of_speech(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/parts-of-speech', params)
RequestBuilder.new(@user_key, @alternate_url + MORPHOLOGY_ENDPOINT + '/parts-of-speech', params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -183,7 +167,7 @@ def get_entities(params, resolve_entities = false)

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

RequestBuilder.new(@user_key, @alternate_url + endpoint, params)
RequestBuilder.new(@user_key, @alternate_url + endpoint, params, BINDING_VERSION)
.send_post_request
end

Expand Down Expand Up @@ -213,7 +197,7 @@ def get_categories(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + CATEGORIES_ENDPOINT, params)
RequestBuilder.new(@user_key, @alternate_url + CATEGORIES_ENDPOINT, params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -229,7 +213,7 @@ def get_relationships(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + RELATIONSHIPS_ENDPOINT, params)
RequestBuilder.new(@user_key, @alternate_url + RELATIONSHIPS_ENDPOINT, params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -245,7 +229,7 @@ def get_sentiment(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + SENTIMENT_ENDPOINT, params)
RequestBuilder.new(@user_key, @alternate_url + SENTIMENT_ENDPOINT, params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -261,7 +245,7 @@ def name_translation(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + NAME_TRANSLATION_ENDPOINT, params)
RequestBuilder.new(@user_key, @alternate_url + NAME_TRANSLATION_ENDPOINT, params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -278,7 +262,7 @@ def name_similarity(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + NAME_SIMILARITY_ENDPOINT, params)
RequestBuilder.new(@user_key, @alternate_url + NAME_SIMILARITY_ENDPOINT, params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -294,7 +278,7 @@ def get_tokens(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + TOKENS_ENDPOINT, params)
RequestBuilder.new(@user_key, @alternate_url + TOKENS_ENDPOINT, params, BINDING_VERSION)
.send_post_request
end

Expand All @@ -310,21 +294,21 @@ def get_sentences(params)

params = params.load_params

RequestBuilder.new(@user_key, @alternate_url + SENTENCES_ENDPOINT, params)
RequestBuilder.new(@user_key, @alternate_url + SENTENCES_ENDPOINT, params, BINDING_VERSION)
.send_post_request
end

# Gets information about the Rosette API, returns name, version, build number
# Gets information about the Rosette API, returns name, build number
# and build time.
def info
RequestBuilder.new(@user_key, @alternate_url + INFO)
RequestBuilder.new(@user_key, @alternate_url + INFO, BINDING_VERSION)
.send_get_request
end

# Pings the Rosette API for a response indicting that the service is
# available.
def ping
RequestBuilder.new(@user_key, @alternate_url + PING)
RequestBuilder.new(@user_key, @alternate_url + PING, BINDING_VERSION)
.send_get_request
end

Expand Down
Loading

0 comments on commit 1219e34

Please sign in to comment.