Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ TextRazor.configure do |config|
end
```

You can also force TextRazor to use the European API endpoint if your account
supports it. You can find more information about this feature in [this blog
post](https://www.textrazor.com/blog/2018/11/new-region-textrazor-eu.html).

```
TextRazor.configure do |config|
config.use_europe_endpoint = true
end
```

### When the client is persisted across different requests

```
Expand Down
16 changes: 15 additions & 1 deletion lib/textrazor/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
module TextRazor

class Configuration
attr_accessor :secure
DEFAULT_ENDPOINT = "api.textrazor.com"
EUROPE_ENDPOINT = "api-eu.textrazor.com"

attr_accessor :secure, :use_europe_endpoint

def initialize
@secure = true
@use_europe_endpoint = false
end

def url
URI::Generic.build(
host: (use_europe_endpoint ? EUROPE_ENDPOINT : DEFAULT_ENDPOINT),
scheme: (secure ? "https" : "http")
).to_s
end

end

end
13 changes: 5 additions & 8 deletions lib/textrazor/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ module TextRazor

class Request

HTTP_URL = 'http://api.textrazor.com/'
HTTPS_URL = 'https://api.textrazor.com/'

OPTIONS_MAPPING = {
extractors: 'extractors',
cleanup_mode: 'cleanup.mode',
Expand All @@ -22,11 +19,11 @@ class Request
}

def self.post(text, options)
::RestClient.post url, build_query(text, options), accept_encoding: 'gzip'
end

def self.url
TextRazor.configuration.secure ? HTTPS_URL : HTTP_URL
::RestClient.post(
TextRazor.configuration.url,
build_query(text, options),
accept_encoding: 'gzip'
)
end

private
Expand Down
33 changes: 33 additions & 0 deletions spec/lib/textrazor/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,37 @@ module TextRazor
end
end
end

describe "#use_europe_endpoint" do
it "default value is false" do
Configuration.new.use_europe_endpoint = false
end
end

describe "#use_europe_endpoint=" do
it "can set value" do
config = Configuration.new
config.use_europe_endpoint = true
expect(config.use_europe_endpoint).to eq(true)
end
end

describe "#url" do
it "right default value" do
config = Configuration.new
expect(config.url).to eq("https://api.textrazor.com")
end

it "right value when not secure" do
config = Configuration.new
config.secure = false
expect(config.url).to eq("http://api.textrazor.com")
end

it "right value when Europe endpoint is forced" do
config = Configuration.new
config.use_europe_endpoint = true
expect(config.url).to eq("https://api-eu.textrazor.com")
end
end
end
38 changes: 2 additions & 36 deletions spec/lib/textrazor/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,6 @@ module TextRazor

describe Request do

describe ".url" do
after :each do
TextRazor.reset
end

let(:url) do
TextRazor::Request.url
end

context "with the config 'secure' set to false" do
before :each do
TextRazor.configure do |config|
config.secure = false
end
end

it "returns the unsecured URL" do
expect(url).to eq 'http://api.textrazor.com/'
end
end

context "with the config 'secure' set to true" do
before :each do
TextRazor.configure do |config|
config.secure = true
end
end

it "returns the unsecured URL" do
expect(url).to eq 'https://api.textrazor.com/'
end
end
end

context ".post" do

context "default options" do
Expand All @@ -46,7 +12,7 @@ module TextRazor
options = {api_key: 'api_key', extractors: %w(entities topics words dependency-trees relations entailments)}

expect(::RestClient).to receive(:post).
with("https://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key',
with("https://api.textrazor.com", { "text" => 'text', "apiKey" => 'api_key',
"extractors" => "entities,topics,words,dependency-trees,relations,entailments" }, accept_encoding: 'gzip')

Request.post('text', options)
Expand All @@ -63,7 +29,7 @@ module TextRazor
enrichment_queries: 'queries', classifiers: 'textrazor_iab'}

expect(::RestClient).to receive(:post).
with("https://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key', "extractors" => "entities,topics,words",
with("https://api.textrazor.com", { "text" => 'text', "apiKey" => 'api_key', "extractors" => "entities,topics,words",
"cleanup.mode" => "raw", "cleanup.returnCleaned" => true, "cleanup.returnRaw" => true, "languageOverride" => 'fre',
"entities.filterDbpediaTypes" => "type1", "entities.filterFreebaseTypes" => "type2" , "entities.allowOverlap" => false,
"entities.enrichmentQueries" => "queries", "classifiers" => 'textrazor_iab'},
Expand Down