Sha256: 57905a555d0075df1a7d1b674ec285845f4fe2a1996201a91042fa4c005ce718

Contents?: true

Size: 809 Bytes

Versions: 12

Compression:

Stored size: 809 Bytes

Contents

require 'backlog_kit/response/middleware'
require 'backlog_kit/resource_file'

module BacklogKit
  class Response
    class FileParser < BacklogKit::Response::Middleware
      def on_complete(env)
        super
        env.body = BacklogKit::ResourceFile.new(decoded_filename, body) if file?
      end

      private

      def file?
        !(no_content? || json?)
      end

      def decoded_filename
        match_data = content_disposition.match(/=(.*)''(.+)\z/)
        encode = match_data[1]
        filename = match_data[2]
        return filename if encode.empty?
        URI.decode_www_form(filename, encode).flatten.first
      end

      def content_disposition
        headers['content-disposition']
      end
    end

    Faraday::Response.register_middleware(file_parser: FileParser)
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
backlog_kit-0.12.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.11.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.10.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.9.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.8.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.7.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.6.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.5.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.4.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.3.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.2.0 lib/backlog_kit/response/file_parser.rb
backlog_kit-0.1.0 lib/backlog_kit/response/file_parser.rb