Sha256: 58f9f849d6a7cbfcb3ef2273237258f56be9fb1c94cda2d21a465fc21c09cb61

Contents?: true

Size: 1.07 KB

Versions: 16

Compression:

Stored size: 1.07 KB

Contents

# 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

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
github_api2-1.0.1 lib/github_api2/response.rb
github_api2-1.0.0 lib/github_api2/response.rb
github_api-0.19.0 lib/github_api/response.rb
lingfennan-github_api-0.18.2 lib/github_api/response.rb
github_api-0.18.2 lib/github_api/response.rb
github_api-0.18.1 lib/github_api/response.rb
github_api-0.17.0 lib/github_api/response.rb
github_api-0.16.0 lib/github_api/response.rb
github_api-0.15.0 lib/github_api/response.rb
github_api-0.14.5 lib/github_api/response.rb
github_api-0.14.4 lib/github_api/response.rb
github_api-0.14.3 lib/github_api/response.rb
github_api-0.14.2 lib/github_api/response.rb
github_api-0.14.1 lib/github_api/response.rb
github_api-0.14.0 lib/github_api/response.rb
github_api-0.13.1 lib/github_api/response.rb