Sha256: e8dee4c932dd6e82b5cee671a7d2d5375c8838a4dfa7f8925afb4fbb641228bb

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module Rambo
  module RSpec
    class ExampleGroup

      TEMPLATE_PATH = File.expand_path("../templates/example_group_template.erb", __FILE__)

      attr_reader :resource

      def initialize(resource, options={})
        @resource = resource
        @options  = options || { framework: :rails }
      end

      def template
        @template ||= File.read(TEMPLATE_PATH)
      end

      def create_fixture_files
        resource.http_methods.each do |method|
          if method.request_body
            path = File.expand_path("spec/support/examples/#{@resource.to_s.gsub(/\//, "")}_#{method.method}_request_body.json")
            File.write(path, method.request_body.example)
          end

          method.responses.each do |resp|
            resp.bodies.each do |body|
              path     = body.schema ? response_schema_fixture_path(method) : response_body_fixture_path(method)
              contents = body.schema ? body.schema : body.example
              File.write(path, contents)
            end
          end
        end
      end

      def render
        create_fixture_files
        b = binding
        ERB.new(template, 0, "-", "@result").result(resource.instance_eval { b })
        @result
      end

      private

      def response_schema_fixture_path(method)
        File.expand_path("spec/support/examples/#{@resource.to_s.gsub(/\//, "")}_#{method.method}_response_schema.json")
      end

      def response_body_fixture_path(method)
        File.expand_path("spec/support/examples/#{@resource.to_s.gsub(/\//, "")}_#{method.method}_response_body.json")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rambo_ruby-0.7.1 lib/rambo/rspec/example_group.rb
rambo_ruby-0.7.0 lib/rambo/rspec/example_group.rb
rambo_ruby-0.6.0 lib/rambo/rspec/example_group.rb