Class: Github::Response

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/github_api2/response.rb,
lib/github_api2/response/header.rb

Overview

Contains methods and attributes that act on the response returned from the request

Direct Known Subclasses

AtomParser, Jsonize, Mashify, Xmlize

Defined Under Namespace

Classes: AtomParser, FollowRedirects, Header, Jsonize, Mashify, RaiseError, Xmlize

Constant Summary collapse

CONTENT_TYPE =
'Content-Type'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Response

Returns a new instance of Response.



19
20
21
22
# File 'lib/github_api2/response.rb', line 19

def initialize(app, options = {})
  super(app)
  @content_types = Array(options[:content_type])
end

Class Attribute Details

.parserObject

Returns the value of attribute parser.



12
13
14
# File 'lib/github_api2/response.rb', line 12

def parser
  @parser
end

Class Method Details

.define_parser(&block) ⇒ Object



15
16
17
# File 'lib/github_api2/response.rb', line 15

def self.define_parser(&block)
  @parser = block
end

Instance Method Details

#parse_body?(env) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/github_api2/response.rb', line 28

def parse_body?(env)
  parse_response_type?(response_type(env)) and parse_response?(env)
end

#parse_response?(env) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/github_api2/response.rb', line 44

def parse_response?(env)
  env[:body].respond_to?(:to_str)
end

#parse_response_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/github_api2/response.rb', line 38

def parse_response_type?(type)
  @content_types.empty? || @content_types.any? { |pattern|
    pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
  }
end

#process_body(env) ⇒ Object



24
25
26
# File 'lib/github_api2/response.rb', line 24

def process_body(env)
  env[:body] = parse(env[:body])
end

#response_type(env) ⇒ Object



32
33
34
35
36
# File 'lib/github_api2/response.rb', line 32

def response_type(env)
  type = env[:response_headers][CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end