Class: TextRazor::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/textrazor/response.rb

Constant Summary collapse

BadRequest =
Class.new(StandardError)
Unauthorised =
Class.new(StandardError)
RequestEntityTooLong =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ Response

Returns a new instance of Response.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



11
12
13
# File 'lib/textrazor/response.rb', line 11

def raw_response
  @raw_response
end

#timeObject (readonly)

Returns the value of attribute time.



11
12
13
# File 'lib/textrazor/response.rb', line 11

def time
  @time
end

Instance Method Details

#categoriesObject



87
88
89
# File 'lib/textrazor/response.rb', line 87

def categories
  @categories ||= parse_categories
end

#cleaned_textObject



43
44
45
# File 'lib/textrazor/response.rb', line 43

def cleaned_text
  @cleaned_text ||= raw_response[:cleanedText]
end

#coarse_topicsObject



59
60
61
# File 'lib/textrazor/response.rb', line 59

def coarse_topics
  @coarse_topics ||= parse_coarse_topics
end

#custom_annotation_outputObject

def message end



39
40
41
# File 'lib/textrazor/response.rb', line 39

def custom_annotation_output
  @custom_annotation_output ||= raw_response[:customAnnotationOutput]
end

#entailmentsObject



51
52
53
# File 'lib/textrazor/response.rb', line 51

def entailments
  @entailments ||= parse_entailments
end

#entitiesObject



55
56
57
# File 'lib/textrazor/response.rb', line 55

def entities
  @entities ||= parse_entities
end

#languageObject



91
92
93
# File 'lib/textrazor/response.rb', line 91

def language
  raw_response[:language]
end

#language_is_reliable?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/textrazor/response.rb', line 95

def language_is_reliable?
  raw_response[:languageIsReliable]
end

#ok?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/textrazor/response.rb', line 28

def ok?
  @ok
end

#phrasesObject



67
68
69
# File 'lib/textrazor/response.rb', line 67

def phrases
  @phrases ||= parse_phrases
end

#propertiesObject



75
76
77
# File 'lib/textrazor/response.rb', line 75

def properties
  @properties ||= parse_properties
end

#raw_textObject



47
48
49
# File 'lib/textrazor/response.rb', line 47

def raw_text
  @raw_text||= raw_response[:rawText]
end

#relationsObject



79
80
81
# File 'lib/textrazor/response.rb', line 79

def relations
  @relations ||= parse_relations
end

#sentencesObject



83
84
85
# File 'lib/textrazor/response.rb', line 83

def sentences
   @sentences ||= parse_sentences
end

#topicsObject



63
64
65
# File 'lib/textrazor/response.rb', line 63

def topics
  @topics ||= parse_topics
end

#wordsObject



71
72
73
# File 'lib/textrazor/response.rb', line 71

def words
  @words ||= parse_words
end