class TextRazor::Response

Constants

BadRequest
RequestEntityTooLong
Unauthorised

Attributes

raw_response[R]
time[R]

Public Class Methods

new(http_response) click to toggle source
# File lib/textrazor/response.rb, line 13
def initialize(http_response)
  code = http_response.code
  body = http_response.body

  raise BadRequest.new(body) if bad_request?(code)
  raise Unauthorised.new(body) if unauthorised?(code)
  raise RequestEntityTooLong.new(body) if request_entity_too_long?(code)

  json_body = ::JSON::parse(body, symbolize_names: true)

  @time = json_body[:time].to_f
  @ok = json_body[:ok]
  @raw_response = json_body[:response]
end

Public Instance Methods

categories() click to toggle source
# File lib/textrazor/response.rb, line 87
def categories
  @categories ||= parse_categories
end
cleaned_text() click to toggle source
# File lib/textrazor/response.rb, line 43
def cleaned_text
  @cleaned_text ||= raw_response[:cleanedText]
end
coarse_topics() click to toggle source
# File lib/textrazor/response.rb, line 59
def coarse_topics
  @coarse_topics ||= parse_coarse_topics
end
custom_annotation_output() click to toggle source

def message end

# File lib/textrazor/response.rb, line 39
def custom_annotation_output
  @custom_annotation_output ||= raw_response[:customAnnotationOutput]
end
entailments() click to toggle source
# File lib/textrazor/response.rb, line 51
def entailments
  @entailments ||= parse_entailments
end
entities() click to toggle source
# File lib/textrazor/response.rb, line 55
def entities
  @entities ||= parse_entities
end
language() click to toggle source
# File lib/textrazor/response.rb, line 91
def language
  raw_response[:language]
end
language_is_reliable?() click to toggle source
# File lib/textrazor/response.rb, line 95
def language_is_reliable?
  raw_response[:languageIsReliable]
end
ok?() click to toggle source
# File lib/textrazor/response.rb, line 28
def ok?
  @ok
end
phrases() click to toggle source
# File lib/textrazor/response.rb, line 67
def phrases
  @phrases ||= parse_phrases
end
properties() click to toggle source
# File lib/textrazor/response.rb, line 75
def properties
  @properties ||= parse_properties
end
raw_text() click to toggle source
# File lib/textrazor/response.rb, line 47
def raw_text
  @raw_text||= raw_response[:rawText]
end
relations() click to toggle source
# File lib/textrazor/response.rb, line 79
def relations
  @relations ||= parse_relations
end
sentences() click to toggle source
# File lib/textrazor/response.rb, line 83
def sentences
   @sentences ||= parse_sentences
end
topics() click to toggle source
# File lib/textrazor/response.rb, line 63
def topics
  @topics ||= parse_topics
end
words() click to toggle source
# File lib/textrazor/response.rb, line 71
def words
  @words ||= parse_words
end

Private Instance Methods

bad_request?(code) click to toggle source
# File lib/textrazor/response.rb, line 101
def bad_request?(code)
  code == 400
end
parse(type, data) click to toggle source
# File lib/textrazor/response.rb, line 167
def parse(type, data)
  return nil if data.nil?

  klass = Object.const_get("TextRazor::#{type.capitalize}")

  data.map do |data_hash|
    klass.create_from_hash(data_hash)
  end
end
parse_categories() click to toggle source
# File lib/textrazor/response.rb, line 129
def parse_categories
  parse(:category, raw_response[:categories])
end
parse_coarse_topics() click to toggle source
# File lib/textrazor/response.rb, line 121
def parse_coarse_topics
  parse(:topic, raw_response[:coarseTopics])
end
parse_entailments() click to toggle source
# File lib/textrazor/response.rb, line 113
def parse_entailments
  parse(:entailment, raw_response[:entailments])
end
parse_entities() click to toggle source
# File lib/textrazor/response.rb, line 117
def parse_entities
  parse(:entity, raw_response[:entities])
end
parse_phrases() click to toggle source
# File lib/textrazor/response.rb, line 133
def parse_phrases
  raw_phrases = raw_response[:nounPhrases]
  return if raw_phrases.nil?

  raw_phrases.map do |phrase_hash|
    Phrase.create_from_hash(phrase_hash, words)
  end
end
parse_properties() click to toggle source
# File lib/textrazor/response.rb, line 155
def parse_properties
  parse(:property, raw_response[:properties])
end
parse_relations() click to toggle source
# File lib/textrazor/response.rb, line 159
def parse_relations
  parse(:relation, raw_response[:relations])
end
parse_sentences() click to toggle source
# File lib/textrazor/response.rb, line 163
def parse_sentences
  parse(:sentence, raw_response[:sentences])
end
parse_topics() click to toggle source
# File lib/textrazor/response.rb, line 125
def parse_topics
  parse(:topic, raw_response[:topics])
end
parse_words() click to toggle source
# File lib/textrazor/response.rb, line 142
def parse_words
  raw_sentences = raw_response[:sentences]
  return if raw_sentences.nil?

  words = []
  raw_sentences.each do |sentence_hash|
    sentence_hash[:words].each do |word_hash|
      words << Word.create_from_hash(word_hash)
    end
  end
  words
end
request_entity_too_long?(code) click to toggle source
# File lib/textrazor/response.rb, line 109
def request_entity_too_long?(code)
  code == 413
end
unauthorised?(code) click to toggle source
# File lib/textrazor/response.rb, line 105
def unauthorised?(code)
  code == 401
end