-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathresponse.rb
62 lines (52 loc) · 1.47 KB
/
response.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
# Copyright (C) 2019 Twitter, Inc.
module TwitterAds
# Generic container for API responses.
class Response
attr_reader :code,
:headers,
:raw_body,
:body
# Creates a new Response object instance.
#
# @example
# response = Response.new(code, headers, body)
#
# @param code [String] The HTTP status code.
# @param headers [Hash] A Hash object containing HTTP response headers.
# @param body [String] The response body.
#
# @since 0.1.0
#
# @return [Response] The Response object instance.
def initialize(code, headers, body)
@code = code.to_i
@headers = headers
@raw_body = body
# handle non-JSON responses
begin
@body = TwitterAds::Utils.symbolize!(MultiJson.load(body))
rescue MultiJson::ParseError
@body = raw_body
end
self
end
# Returns an inspection string for the current Response instance.
#
# @example
# response.inspect
#
# @since 0.1.0
#
# @return [String] The inspection string.
def inspect
"#<#{self.class.name}:0x#{object_id} code=\"#{@code}\" error=\"#{error?}\">"
end
# Helper method for determining if the current Response contains an error.
#
# @return [Boolean] True or false indicating if this Response contains an error.
def error?
@error ||= (@code >= 400 && @code <= 599)
end
end
end