forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_wrapper.rb
More file actions
149 lines (122 loc) · 2.92 KB
/
response_wrapper.rb
File metadata and controls
149 lines (122 loc) · 2.92 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# encoding: utf-8
module Github
# A class responsible for proxing to faraday response
class ResponseWrapper
extend Forwardable
include Pagination
include Enumerable
attr_reader :response
attr_reader :current_api
attr_reader :env
def_delegators :body, :empty?, :size, :include?, :length, :to_a, :first, :flatten, :include?, :keys, :[]
def initialize(response, current_api)
@response = response
@current_api = current_api
@env = response.env
end
# Request url
#
def url
response.env[:url].to_s
end
def body=(value)
@body = value
@env[:body] = value
end
# Response raw body
#
def body
@body ? @body : response.body
end
# Response status
#
def status
response.status
end
def success?
response.success?
end
def redirect?
status.to_i >= 300 && status.to_i < 400
end
def client_error?
status.to_i >= 400 && status.to_i < 500
end
def server_error?
status.to_i >= 500 && status.to_i < 600
end
# Return response headers
#
def headers
Github::Response::Header.new(env)
end
# Lookup an attribute from the body if hash, otherwise behave like array index.
# Convert any key to string before calling.
#
def [](key)
if self.body.is_a?(Array)
self.body[key]
else
self.body.send(:"#{key}")
end
end
# Return response body as string
#
def to_s
body.to_s
end
# Convert the ResponseWrapper into a Hash
#
def to_hash
body.to_hash
end
# Convert the ResponseWrapper into an Array
#
def to_ary
body.to_ary
end
# Iterate over each resource inside the body
#
def each
body_parts = self.body.respond_to?(:each) ? self.body : [self.body]
return body_parts.to_enum unless block_given?
body_parts.each { |part| yield(part) }
end
# Check if body has an attribute
#
def has_key?(key)
self.body.is_a?(Hash) && self.body.has_key?(key)
end
# Coerce any method calls for body attributes
#
def method_missing(method_name, *args, &block)
if self.has_key?(method_name.to_s)
self.[](method_name, &block)
else
super
end
end
# Check if method is defined on the body
#
def respond_to?(method_name)
if self.has_key?(method_name.to_s)
true
else
super
end
end
# Print only response body
#
def inspect
"#<#{self.class.name} @body=\"#{self.body}\">"
end
# Compare the wrapper with other wrapper for equality
#
def ==(other)
return false unless other.is_a?(self.class)
return false unless (other.respond_to?(:env) && other.respond_to?(:body))
self.env == other.env && self.body == other.body
end
alias eql? ==
end # ResponseWrapper
end # Github