Sha256: cc7ac6034beb1945bf7ad159896d7d478cc60f737bee17ff2852b6f36eba23b2

Contents?: true

Size: 1.2 KB

Versions: 18

Compression:

Stored size: 1.2 KB

Contents

module Apidiesel
  module Handlers
    module MockResponse
      class RequestHandler
        def run(request, api_config)
          action = request.action

          return request unless action.respond_to?(:mock_response) && action.mock_response

          file_name = action.mock_response[:file]
          parser    = action.mock_response[:parser]
          file      = File.read(file_name)

          request.response_body = if parser
            parser.call(file)

          elsif file_name.ends_with?('.json')
            JSON.parse(file)

          elsif file_name.ends_with?('.xml')
            Hash.from_xml(file)

          else
            file
          end

          request
        end
      end

      module ActionExtension
        extend ActiveSupport::Concern

        class_methods do
          def mock_response!(file:, &block)
            @mock_response = {
              file: file,
              parser: block
            }
          end

          def mock_response
            @mock_response
          end
        end

        def mock_response
          self.class.mock_response
        end
      end
    end
  end
end

Apidiesel::Action.send(:include, Apidiesel::Handlers::MockResponse::ActionExtension)

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
apidiesel-0.15 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.14 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.13 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.12 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.11 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.10 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.9 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.8 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.7 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.6 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.5 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.4 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.3 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.2 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.1.3 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.1.2 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.1.1 lib/apidiesel/handlers/mock_response.rb
apidiesel-0.1.0 lib/apidiesel/handlers/mock_response.rb