Class: Bkblz::V1::Response
- Inherits:
-
Object
- Object
- Bkblz::V1::Response
show all
- Defined in:
- lib/bkblz/v1/response.rb
Constant Summary
collapse
- MissingResponseError =
Class.new Bkblz::BaseError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(http_response, original_request) ⇒ Response
Returns a new instance of Response.
52
53
54
55
56
57
58
59
|
# File 'lib/bkblz/v1/response.rb', line 52
def initialize(http_response, original_request)
@http_response = http_response
@original_request = original_request
@parsed_response = parse http_response
@cache = {}
Bkblz.log.debug { "parsed response => #{@parsed_response}" }
end
|
Instance Attribute Details
#http_response ⇒ Object
Returns the value of attribute http_response.
9
10
11
|
# File 'lib/bkblz/v1/response.rb', line 9
def http_response
@http_response
end
|
#parsed_body ⇒ Object
Returns the value of attribute parsed_body.
9
10
11
|
# File 'lib/bkblz/v1/response.rb', line 9
def parsed_body
@parsed_body
end
|
Class Method Details
.response_accessor(response_field, model_klass = nil, &block) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/bkblz/v1/response.rb', line 24
def response_accessor(response_field, model_klass=nil, &block)
api_map_key_converter = lambda do |map|
raise "not a Hash" unless map.is_a? Hash
Bkblz::MapKeyFormatter.underscore_keys map
end
api_value_transformer = lambda do |value|
return value unless model_klass || block_given?
return yield value if block_given?
if value.is_a? Array
value.map do |v|
model_klass.new api_map_key_converter.call(v)
end
else
model_klass.new api_map_key_converter.call(value)
end
end
define_method response_field do |*args|
raise MissingResponseError unless @parsed_response
return @cache[response_field] if @cache.key? response_field
value = @parsed_response[response_field]
@cache[response_field] = api_value_transformer.call value
end
end
|
.response_accessors(*response_fields) ⇒ Object
18
19
20
21
22
|
# File 'lib/bkblz/v1/response.rb', line 18
def response_accessors(*response_fields)
response_fields.each do |response_field|
response_accessor response_field
end
end
|
.response_model(klass = nil) ⇒ Object
13
14
15
16
|
# File 'lib/bkblz/v1/response.rb', line 13
def response_model(klass=nil)
@response_model = klass unless klass.nil?
@response_model
end
|
Instance Method Details
#[](key) ⇒ Object
64
65
66
|
# File 'lib/bkblz/v1/response.rb', line 64
def [](key)
@parsed_response[key]
end
|
#to_model ⇒ Object
68
69
70
71
|
# File 'lib/bkblz/v1/response.rb', line 68
def to_model
raise 'no response model defined' unless self.class.response_model
self.class.response_model.new @parsed_response.dup
end
|