Skip to content

Commit

Permalink
RCB-542 1.9 release (#51)
Browse files Browse the repository at this point in the history
* Added User-Agent

- created unit test
- updated mocks to use User-Agent

* WS-1272 Multilingual example

* Example updates
  • Loading branch information
cp2boston authored and Li Xu committed Jan 3, 2018
1 parent 39cb764 commit d4a6fb1
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
rosette_api = RosetteAPI.new(api_key, url)
end

categories_url_data = 'http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/'
categories_url_data = "http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/"
begin
params = DocumentParameters.new(content_uri: categories_url_data)
response = rosette_api.get_categories(params)
Expand Down
2 changes: 1 addition & 1 deletion examples/entities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
rosette_api = RosetteAPI.new(api_key, url)
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"
entities_text_data = "The Securities and Exchange Commission today announced the leadership of the agency’s trial unit. Bridget Fitzpatrick has been named Chief Litigation Counsel of the SEC and David Gottesman will continue to serve as the agency’s Deputy Chief Litigation Counsel. Since December 2016, Ms. Fitzpatrick and Mr. Gottesman have served as Co-Acting Chief Litigation Counsel. In that role, they were jointly responsible for supervising the trial unit at the agency’s Washington D.C. headquarters as well as coordinating with litigators in the SEC’s 11 regional offices around the country."
begin
params = DocumentParameters.new(content: entities_text_data, genre: 'social-media')
response = rosette_api.get_entities(params)
Expand Down
20 changes: 20 additions & 0 deletions examples/language_multilingual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rosette_api'

api_key, url = ARGV

if !url
rosette_api = RosetteAPI.new(api_key)
else
rosette_api = RosetteAPI.new(api_key, url)
end

language_multilingual_data = "On Thursday, as protesters gathered in Washington D.C., the United States Federal Communications Commission under Chairman Ajit Pai voted 3-2 to overturn a 2015 decision, commonly called Net Neutrality, that forbade Internet service providers (ISPs) such as Verizon, Comcast, and AT&T from blocking individual websites or charging websites or customers more for faster load times. Quatre femmes ont été nommées au Conseil de rédaction de la loi du Qatar. Jeudi, le décret royal du Qatar a annoncé que 28 nouveaux membres ont été nommés pour le Conseil de la Choura du pays. ذكرت مصادر أمنية يونانية، أن 9 موقوفين من منظمة \"د هـ ك ب ج\" الذين كانت قد أوقفتهم الشرطة اليونانية في وقت سابق كانوا يخططون لاغتيال الرئيس التركي رجب طيب أردوغان."
begin
params = DocumentParameters.new(content: language_multilingual_data)
params.rosette_options = { 'multilingual' => 'true' }
params.custom_headers = { 'X-RosetteAPI-App' => 'ruby-app' }
response = rosette_api.get_language(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
2 changes: 1 addition & 1 deletion examples/morphology_complete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
rosette_api = RosetteAPI.new(api_key, url)
end

morphology_complete_data = "The quick brown fox jumped over the lazy dog. Yes he did."
morphology_complete_data = "The quick brown fox jumped over the lazy dog. 👍🏾 Yes he did. B)"
begin
params = DocumentParameters.new(content: morphology_complete_data)
response = rosette_api.get_morphology_complete(params)
Expand Down
2 changes: 1 addition & 1 deletion examples/name_deduplication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
rosette_api = RosetteAPI.new(api_key, url)
end

name_dedupe_data = 'John Smith,Johnathon Smith,Fred Jones'
name_dedupe_data = "Alice Terry,Alice Thierry,Betty Grable,Betty Gable,Norma Shearer,Norm Shearer,Brigitte Helm,Bridget Helem,Judy Holliday,Julie Halliday"

threshold = 0.75
names = name_dedupe_data.split(',').map { |n| NameParameter.new(n) }
Expand Down
4 changes: 2 additions & 2 deletions examples/name_similarity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
rosette_api = RosetteAPI.new(api_key, url)
end

matched_name_data1 = 'Michael Jackson'
matched_name_data2 = '迈克尔·杰克逊'
matched_name_data1 = "Michael Jackson"
matched_name_data2 = "迈克尔·杰克逊"
begin
name1 = NameParameter.new(matched_name_data1, entity_type: 'PERSON', language: 'eng')
params = NameSimilarityParameters.new(name1, matched_name_data2)
Expand Down
2 changes: 1 addition & 1 deletion examples/name_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
rosette_api = RosetteAPI.new(api_key, url)
end

translated_name_data = 'معمر محمد أبو منيار القذاف'
translated_name_data = "معمر محمد أبو منيار القذاف"
begin
params = NameTranslationParameters.new(translated_name_data, 'eng', target_script: 'Latn')
response = rosette_api.get_name_translation(params)
Expand Down
2 changes: 1 addition & 1 deletion examples/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
rosette_api = RosetteAPI.new(api_key, url)
end

relationships_text_data = "Bill Gates, Microsoft's former CEO, is a philanthropist."
relationships_text_data = "FLIR Systems is headquartered in Oregon and produces thermal imaging, night vision, and infrared cameras and sensor systems. According to the SEC’s order instituting a settled administrative proceeding, FLIR entered into a multi-million dollar contract to provide thermal binoculars to the Saudi government in November 2008. Timms and Ramahi were the primary sales employees responsible for the contract, and also were involved in negotiations to sell FLIR’s security cameras to the same government officials. At the time, Timms was the head of FLIR’s Middle East office in Dubai."
begin
params = DocumentParameters.new(content: relationships_text_data)
params.rosette_options = { accuracyMode: 'PRECISION' }
Expand Down
2 changes: 1 addition & 1 deletion examples/sentences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
rosette_api = RosetteAPI.new(api_key, url)
end

sentences_data = "This land is your land. This land is my land\nFrom California to the New York island;\nFrom the red wood forest to the Gulf Stream waters\n\nThis land was made for you and Me.\n\nAs I was walking that ribbon of highway,\nI saw above me that endless skyway:\nI saw below me that golden valley:\nThis land was made for you and me."
sentences_data = "This land is your land. This land is my land, from California to the New York island; from the red wood forest to the Gulf Stream waters. This land was made for you and Me. As I was walking that ribbon of highway, I saw above me that endless skyway: I saw below me that golden valley: This land was made for you and me."

begin
params = DocumentParameters.new
Expand Down
2 changes: 1 addition & 1 deletion examples/sentiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end

file = Tempfile.new(%w[foo .html])
sentiment_file_data = '<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>'
sentiment_file_data = "<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>"
file.write(sentiment_file_data)
file.close
begin
Expand Down
2 changes: 1 addition & 1 deletion examples/syntax_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
rosette_api = RosetteAPI.new(api_key, url)
end

syntax_dependencies_data = 'Yoshinori Ohsumi, a Japanese cell biologist, was awarded the Nobel Prize in Physiology or Medicine on Monday.'
syntax_dependencies_data = "Yoshinori Ohsumi, a Japanese cell biologist, was awarded the Nobel Prize in Physiology or Medicine on Monday."
begin
params = DocumentParameters.new(content: syntax_dependencies_data)
response = rosette_api.get_syntax_dependencies(params)
Expand Down
2 changes: 1 addition & 1 deletion examples/text_embedding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
rosette_api = RosetteAPI.new(api_key, url)
end

embeddings_data = 'Cambridge, Massachusetts'
embeddings_data = "Cambridge, Massachusetts"
begin
params = DocumentParameters.new(content: embeddings_data)
response = rosette_api.get_text_embedding(params)
Expand Down
2 changes: 1 addition & 1 deletion examples/tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
rosette_api = RosetteAPI.new(api_key, url)
end

tokens_data = '北京大学生物系主任办公室内部会议'
tokens_data = "北京大学生物系主任办公室内部会议"
begin
params = DocumentParameters.new(content: tokens_data)
response = rosette_api.get_tokens(params)
Expand Down
6 changes: 6 additions & 0 deletions lib/request_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ class RequestBuilder
attr_accessor :user_key
# Rosette API binding version
attr_accessor :binding_version
# User-Agent string
attr_reader :user_agent

def initialize(user_key, alternate_url, http_client, params = {}, url_parameters = nil, binding_version)
@user_key = user_key
@alternate_url = alternate_url
@http_client = http_client
@params = params
@binding_version = binding_version
@user_agent = 'Ruby/' + binding_version + '/' + RUBY_VERSION

return unless url_parameters
@alternate_url = @alternate_url + '?' + URI.encode_www_form(url_parameters)
Expand Down Expand Up @@ -61,6 +64,7 @@ def prepare_plain_request(params)
request['X-RosetteAPI-Key'] = @user_key
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['User-Agent'] = @user_agent
request['X-RosetteAPI-Binding'] = 'ruby'
request['X-RosetteAPI-Binding-Version'] = @binding_version
request.body = params.to_json
Expand Down Expand Up @@ -123,6 +127,7 @@ def prepare_multipart_request(params)
end

request.add_field 'Content-Type', "multipart/form-data; boundary=#{boundary}"
request.add_field 'User-Agent', @user_agent
request.add_field 'X-RosetteAPI-Key', @user_key
request.add_field 'X-RosetteAPI-Binding', 'ruby'
request.add_field 'X-RosetteAPI-Binding-Version', @binding_version
Expand All @@ -143,6 +148,7 @@ def send_get_request
raise RosetteAPIError.new 'connectionError', 'Failed to establish connection with Rosette API server.'
end
request['X-RosetteAPI-Key'] = @user_key
request['User-Agent'] = @user_agent

get_response @http_client, request
end
Expand Down
5 changes: 5 additions & 0 deletions lib/rosette_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ def ping
.send_get_request
end

# Gets the User-Agent string
def user_agent
RequestBuilder.new(@user_key, @alternate_url + PING, @http_client, @url_parameters, BINDING_VERSION).user_agent
end

private

# Checks that the right parameter type is being passed in.
Expand Down
Loading

0 comments on commit d4a6fb1

Please sign in to comment.