Sha256: bc3c9d207d8074716d4568ad01cefe402aeb931779cebecc4e3dcd0fdc28edb5

Contents?: true

Size: 1.57 KB

Versions: 10

Compression:

Stored size: 1.57 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 || { rails: true }
      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

10 entries across 10 versions & 1 rubygems

Version Path
rambo_ruby-0.5.0 lib/rambo/rspec/example_group.rb
rambo_ruby-0.4.0 lib/rambo/rspec/example_group.rb
rambo_ruby-0.3.3 lib/rambo/rspec/example_group.rb
rambo_ruby-0.3.2 lib/rambo/rspec/example_group.rb
rambo_ruby-0.3.1 lib/rambo/rspec/example_group.rb
rambo_ruby-0.3.0 lib/rambo/rspec/example_group.rb
rambo_ruby-0.2.3 lib/rambo/rspec/example_group.rb
rambo_ruby-0.2.2 lib/rambo/rspec/example_group.rb
rambo_ruby-0.2.1 lib/rambo/rspec/example_group.rb
rambo_ruby-0.2.0 lib/rambo/rspec/example_group.rb