forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.rb
More file actions
48 lines (38 loc) · 1.07 KB
/
response.rb
File metadata and controls
48 lines (38 loc) · 1.07 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
# encoding: utf-8
require 'faraday'
module Github
# Contains methods and attributes that act on the response returned from the
# request
class Response < Faraday::Response::Middleware
CONTENT_TYPE = 'Content-Type'.freeze
class << self
attr_accessor :parser
end
def self.define_parser(&block)
@parser = block
end
def initialize(app, options = {})
super(app)
@content_types = Array(options[:content_type])
end
def process_body(env)
env[:body] = parse(env[:body])
end
def parse_body?(env)
parse_response_type?(response_type(env)) and parse_response?(env)
end
def response_type(env)
type = env[:response_headers][CONTENT_TYPE].to_s
type = type.split(';', 2).first if type.index(';')
type
end
def parse_response_type?(type)
@content_types.empty? || @content_types.any? { |pattern|
pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
}
end
def parse_response?(env)
env[:body].respond_to?(:to_str)
end
end # Response
end # Github