class TextRazor::Client
Constants
- DEFAULT_CLEANUP_MODE
- DEFAULT_EXTRACTORS
- EmptyApiKey
- EmptyText
- REQUEST_OPTIONS
- TextTooLong
- UnsupportedCleanupMode
- UnsupportedExtractor
- VALID_CLEANUP_MODE_VALUES
Attributes
api_key[R]
request_options[R]
response[R]
Public Class Methods
categories(api_key, text, options = {})
click to toggle source
# File lib/textrazor/client.rb, line 46 def self.categories(api_key, text, options = {}) new(api_key, options.merge(classifiers: ['textrazor_iab'])). analyse(text). categories end
coarse_topics(api_key, text, options = {})
click to toggle source
# File lib/textrazor/client.rb, line 52 def self.coarse_topics(api_key, text, options = {}) new(api_key, options.merge(extractors: ['topics'])). analyse(text). coarse_topics end
entities(api_key, text, options = {})
click to toggle source
# File lib/textrazor/client.rb, line 58 def self.entities(api_key, text, options = {}) new(api_key, options.merge(extractors: ['entities'])). analyse(text). entities end
new(api_key, options = {})
click to toggle source
# File lib/textrazor/client.rb, line 26 def initialize(api_key, options = {}) assign_api_key(api_key) assign_request_options(options) end
phrases(api_key, text, options = {})
click to toggle source
# File lib/textrazor/client.rb, line 70 def self.phrases(api_key, text, options = {}) new(api_key, options.merge(extractors: ['phrases', 'words'])). analyse(text). phrases end
topics(api_key, text, options = {})
click to toggle source
# File lib/textrazor/client.rb, line 40 def self.topics(api_key, text, options = {}) new(api_key, options.merge(extractors: ['topics'])). analyse(text). topics end
words(api_key, text, options = {})
click to toggle source
# File lib/textrazor/client.rb, line 64 def self.words(api_key, text, options = {}) new(api_key, options.merge(extractors: ['words'])). analyse(text). entities end
Public Instance Methods
analyse(text)
click to toggle source
# File lib/textrazor/client.rb, line 31 def analyse(text) assert_text(text) options = { api_key: api_key }.merge(request_options) Response.new(Request.post(text, options)) end
Private Instance Methods
assert_cleanup_mode(cleanup_mode)
click to toggle source
# File lib/textrazor/client.rb, line 109 def assert_cleanup_mode(cleanup_mode) if cleanup_mode && !VALID_CLEANUP_MODE_VALUES.include?(cleanup_mode) raise UnsupportedCleanupMode.new('Unsupported clean up mode') end end
assert_extractors(extractors)
click to toggle source
# File lib/textrazor/client.rb, line 103 def assert_extractors(extractors) if extractors && !extractors.all? { |extractor| DEFAULT_EXTRACTORS.include?(extractor) } raise UnsupportedExtractor.new('Unsupported extractor') end end
assert_text(text)
click to toggle source
# File lib/textrazor/client.rb, line 115 def assert_text(text) if text.nil? || text.empty? raise EmptyText.new("Text to be analysed is nil or empty") end if is_text_bigger_than_200_kb?(text) raise TextTooLong.new("Text is more than 200kb") end end
assign_api_key(api_key)
click to toggle source
# File lib/textrazor/client.rb, line 78 def assign_api_key(api_key) if api_key.nil? || api_key.empty? raise EmptyApiKey.new("API key is either nil or empty") end @api_key = api_key end
assign_request_options(options)
click to toggle source
# File lib/textrazor/client.rb, line 86 def assign_request_options(options) extractors = options.delete(:extractors) assert_extractors(extractors) cleanup_mode = options.delete(:cleanup_mode) assert_cleanup_mode(cleanup_mode) @request_options = { extractors: extractors || DEFAULT_EXTRACTORS, cleanup_mode: cleanup_mode || DEFAULT_CLEANUP_MODE } REQUEST_OPTIONS.each do |key| @request_options[key] = options[key] unless options[key].nil? end end
is_text_bigger_than_200_kb?(text)
click to toggle source
# File lib/textrazor/client.rb, line 125 def is_text_bigger_than_200_kb?(text) text.bytesize/1024.0 > 200 end