module OopRailsServer::Helpers

Attributes

rails_server[R]

Public Instance Methods

clear_mails!(the_rails_server = nil) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 131
def clear_mails!(the_rails_server = nil)
  the_rails_server ||= rails_server

  mail_directory = mail_directory_for_rails_server(the_rails_server)
  entries = Dir.entries(mail_directory).select do |entry|
    entry !~ /^\./ && entry =~ /^\S.*@/
  end

  entries.each { |e| File.delete(File.join(mail_directory, e)) }
end
expect_exception(subpath, class_name, message) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 42
  def expect_exception(subpath, class_name, message)
    data = get(subpath)

    json = begin
      JSON.parse(data)
    rescue => e
      raise %{Expected a JSON response from '#{subpath}' (because we expected an exception),
but we couldn't parse it as JSON; when we tried, we got:

(#{e.class.name}) #{e.message}

The data is:

#{data.inspect}}
    end

    expect(json['exception']).to be
    expect(json['exception']['class']).to eq(class_name.to_s)
    expect(json['exception']['message']).to match(message)

    json
  end
expect_match(subpath, *args) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 30
def expect_match(subpath, *args)
  options = args.extract_options!
  regexes = args

  data = get_success(subpath, options)
  regexes.each do |regex|
    expect(data).to match(regex)
  end

  data
end
full_path(subpath) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 12
def full_path(subpath)
  "#{rails_template_name}/#{subpath}"
end
get(subpath, options = { }) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 16
def get(subpath, options = { })
  rails_server.get(full_path(subpath), options)
end
get_response(subpath, options = { }) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 20
def get_response(subpath, options = { })
  rails_server.get_response(full_path(subpath), options)
end
get_success(subpath, options = { }) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 24
def get_success(subpath, options = { })
  data = get(subpath, options)
  expect(data).to match(/oop_rails_server_base_template/i) unless options[:no_layout]
  data
end
mail_directory_for_rails_server(the_rails_server = nil) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 142
def mail_directory_for_rails_server(the_rails_server = nil)
  the_rails_server ||= rails_server
  File.join(the_rails_server.rails_root, 'tmp', 'mails')
end
mail_sent_to(destination_address, the_rails_server = nil) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 94
def mail_sent_to(destination_address, the_rails_server = nil)
  the_rails_server ||= rails_server

  mail_file = File.join(mail_directory_for_rails_server(the_rails_server), destination_address)

  if File.exist?(mail_file)
    contents = File.read(mail_file).split(/\n/)
    headers = { }
    last_header = nil
    body = ""
    have_started_body = false

    contents.each do |line|
      if have_started_body
        body << "#{line.chomp}\n"
      elsif line.strip.length == 0
        have_started_body = true
      elsif line =~ /^(\S+):\s+(.*?)\s*$/i
        last_header = $1
        headers[last_header] = $2
      elsif line =~ /^\s+(.*?)\s*$/i && last_header
        headers[last_header] << " #{$1}"
      else
        raise "Unexpected line in #{mail_file.inspect}:\n#{line}"
      end
    end

    { :headers => headers, :body => body }
  else
    if File.exist?(mail_directory_for_rails_server(the_rails_server))
      $stderr.puts "WARNING: No mails for #{destination_address.inspect}: #{Dir.entries(mail_directory_for_rails_server(the_rails_server))}"
    else
      $stderr.puts "WARNING: No mails: no dir: #{mail_directory_for_rails_server(the_rails_server)}"
    end
  end
end
rails_server_default_version() click to toggle source
# File lib/oop_rails_server/helpers.rb, line 86
def rails_server_default_version
  nil
end
rails_server_gemfile_modifier() click to toggle source
# File lib/oop_rails_server/helpers.rb, line 82
def rails_server_gemfile_modifier
  Proc.new { |gemfile| }
end
rails_server_implicit_template_paths() click to toggle source
# File lib/oop_rails_server/helpers.rb, line 157
def rails_server_implicit_template_paths
  [ ]
end
rails_server_project_root() click to toggle source
# File lib/oop_rails_server/helpers.rb, line 69
    def rails_server_project_root
      raise %{You must override #rails_server_project_root in this class (#{self.class.name}) to use OopRailsServer::Helpers;
it should return the fully-qualified path to the root of your project (gem, application, or whatever).}
    end
rails_server_runtime_base_directory() click to toggle source
# File lib/oop_rails_server/helpers.rb, line 78
def rails_server_runtime_base_directory
  @rails_server_runtime_base_directory ||= File.join(rails_server_project_root, "tmp/spec/rails")
end
rails_server_template_paths(template_names) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 161
def rails_server_template_paths(template_names)
  template_names.map do |template_name|
    template_name = template_name.to_s
    if template_name =~ %r{^/}
      template_name
    else
      File.join(rails_server_templates_root, template_name)
    end
  end
end
rails_server_templates_root() click to toggle source
# File lib/oop_rails_server/helpers.rb, line 74
def rails_server_templates_root
  @rails_server_templates_root ||= File.join(rails_server_project_root, "spec/rails/templates")
end
rails_servers() click to toggle source
# File lib/oop_rails_server/helpers.rb, line 90
def rails_servers
  @rails_servers ||= { }
end
rails_template_name() click to toggle source
# File lib/oop_rails_server/helpers.rb, line 65
def rails_template_name
  rails_server.name
end
start_rails_server!(options = { }) click to toggle source
# File lib/oop_rails_server/helpers.rb, line 172
def start_rails_server!(options = { })
  templates = Array(options[:templates] || options[:name] || [ ])
  raise "You must specify a template" unless templates.length >= 1

  name = options[:name]
  name ||= templates[0] if templates.length == 1
  name = name.to_s.strip
  raise "You must specify a name" unless name && name.to_s.strip.length > 0

  server = rails_servers[name]
  server ||= begin
    templates =
      rails_server_implicit_template_paths +
      templates

    template_paths = rails_server_template_paths(templates)

    rsgm = rails_server_gemfile_modifier
    agm = options[:additional_gemfile_modifier] || (Proc.new { |gemfile| })

    gemfile_modifier = Proc.new do |gemfile|
      rsgm.call(gemfile)
      agm.call(gemfile)
    end

    server = ::OopRailsServer::RailsServer.new(
      :name => name, :template_paths => template_paths,
      :runtime_base_directory => rails_server_runtime_base_directory,
      :rails_version => (options[:rails_version] || rails_server_default_version),
      :rails_env => options[:rails_env], :gemfile_modifier => gemfile_modifier)

    rails_servers[name] = server

    server
  end

  server.start!
end
stop_rails_servers!() click to toggle source
# File lib/oop_rails_server/helpers.rb, line 211
def stop_rails_servers!
  exceptions = [ ]
  rails_servers.each do |name, server|
    begin
      server.stop!
    rescue => e
      exceptions << [ name, e ]
    end
  end

  raise "Unable to stop all Rails servers! Got:\n#{exceptions.join("\n")}" if exceptions.length > 0
end