Class: Speechmatics::Response
- Inherits:
-
Object
- Object
- Speechmatics::Response
show all
- Defined in:
- lib/speechmatics/response.rb,
lib/speechmatics/response/error.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(response, request = {}) ⇒ Response
Returns a new instance of Response.
14
15
16
17
|
# File 'lib/speechmatics/response.rb', line 14
def initialize(response, request={})
@raw = response
@request = request
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Coerce any method calls for body attributes
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/speechmatics/response.rb', line 53
def method_missing(method_name, *args, &block)
if self.has_key?(method_name.to_s)
self.[](method_name, &block)
elsif self.body.respond_to?(method_name)
self.body.send(method_name, *args, &block)
elsif self.request[:api].respond_to?(method_name)
self.request[:api].send(method_name, *args, &block)
else
super
end
end
|
Instance Attribute Details
#raw ⇒ Object
Returns the value of attribute raw.
12
13
14
|
# File 'lib/speechmatics/response.rb', line 12
def raw
@raw
end
|
#request ⇒ Object
Returns the value of attribute request.
12
13
14
|
# File 'lib/speechmatics/response.rb', line 12
def request
@request
end
|
Class Method Details
.parse(response, request = {}) ⇒ Object
6
7
8
9
10
|
# File 'lib/speechmatics/response.rb', line 6
def self.parse(response, request={})
new(response, request).tap do |res|
res.check_for_error
end
end
|
Instance Method Details
#[](key) ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/speechmatics/response.rb', line 39
def [](key)
if self.object.is_a?(Array) || self.object.is_a?(Hash)
self.object[key]
else
self.object.send(:"#{key}")
end
end
|
#body ⇒ Object
23
24
25
|
# File 'lib/speechmatics/response.rb', line 23
def body
self.raw.body
end
|
#check_for_error ⇒ Object
19
20
21
|
# File 'lib/speechmatics/response.rb', line 19
def check_for_error
raise Error.classify(self) if raw.status >= 400
end
|
#has_key?(key) ⇒ Boolean
47
48
49
|
# File 'lib/speechmatics/response.rb', line 47
def has_key?(key)
self.object.is_a?(Hash) && self.object.has_key?(key)
end
|
#object ⇒ Object
31
32
33
|
# File 'lib/speechmatics/response.rb', line 31
def object
body[object_name].nil? ? body : body[object_name]
end
|
#object_name ⇒ Object
27
28
29
|
# File 'lib/speechmatics/response.rb', line 27
def object_name
self.request[:api].class.name.split("::").last.underscore
end
|
#objects ⇒ Object
35
36
37
|
# File 'lib/speechmatics/response.rb', line 35
def objects
Array(self.object)
end
|