Sha256: 8fba46181ab72815064f482bd555777e9135e1e37ed29ed44c0f00a91a8daa09

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module Fdoc
  module SpecWatcher
    VERBS = [:get, :post, :put, :delete]

    VERBS.each do |verb|
      define_method(verb) do |*params|
        action, request_params = params

        request_params = if request_params.kind_of(Hash)
          request_params
        else
          begin
            JSON.parse(request_params)
          rescue
            {}
          end
        end

        result = super(*params)

        path = if respond_to?(:example) # Rspec 2
          example.metadata[:fdoc]
        else # Rspec 1.3.2
          opts = {}
          __send__(:example_group_hierarchy).each do |example|
            opts.merge!(example.options)
          end
          opts.merge!(options)
          opts[:fdoc]
        end

        if path
          response_params = begin
            JSON.parse(response.body)
          rescue
            {}
          end
          successful = Fdoc.decide_success(response_params, response.status)
          verify!(verb, path, request_params, response_params, response.status,
            successful)
        end

        result
      end
    end

    private

    def verify!(verb, path, request_params, response_params, response_status,
          successful)
      service = Service.new(Fdoc.service_path)
      endpoint = service.open(verb, path)
      endpoint.consume_request(request_params, successful)
      endpoint.consume_response(response_params, response_status, successful)
      endpoint.persist! if endpoint.respond_to?(:persist!)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fdoc-0.2.5 lib/fdoc/spec_watcher.rb