Module: OopRailsServer::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/oop_rails_server/helpers.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rails_server ⇒ Object (readonly)

Returns the value of attribute rails_server.



9
10
11
# File 'lib/oop_rails_server/helpers.rb', line 9

def rails_server
  @rails_server
end

Instance Method Details

#expect_exception(subpath, class_name, message) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

  json['exception'].should be
  json['exception']['class'].should == class_name.to_s
  json['exception']['message'].should match(message)
end

#expect_match(subpath, *args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# 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|
    data.should match(regex)
  end

  data
end

#full_path(subpath) ⇒ Object



12
13
14
# File 'lib/oop_rails_server/helpers.rb', line 12

def full_path(subpath)
  "#{rails_template_name}/#{subpath}"
end

#get(subpath, options = { }) ⇒ Object



16
17
18
# 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 = { }) ⇒ Object



20
21
22
# 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 = { }) ⇒ Object



24
25
26
27
28
# File 'lib/oop_rails_server/helpers.rb', line 24

def get_success(subpath, options = { })
  data = get(subpath, options)
  data.should match(/oop_rails_server_base_template/i) unless options[:no_layout]
  data
end

#oop_rails_server_base_templates ⇒ Object



104
105
106
107
108
# File 'lib/oop_rails_server/helpers.rb', line 104

def oop_rails_server_base_templates
  [
    File.expand_path(File.join(File.dirname(__FILE__), '../../templates/oop_rails_server_base'))
  ]
end

#rails_server_additional_gemfile_lines ⇒ Object



80
81
82
83
84
# File 'lib/oop_rails_server/helpers.rb', line 80

def rails_server_additional_gemfile_lines
  [
    "gem 'fortitude', :path => '#{rails_server_project_root}'"
  ]
end

#rails_server_default_version ⇒ Object



86
87
88
# File 'lib/oop_rails_server/helpers.rb', line 86

def rails_server_default_version
  ENV['FORTITUDE_SPECS_RAILS_VERSION']
end

#rails_server_implicit_template_paths ⇒ Object



110
111
112
# File 'lib/oop_rails_server/helpers.rb', line 110

def rails_server_implicit_template_paths
  [ ]
end

#rails_server_project_root ⇒ Object



67
68
69
70
# File 'lib/oop_rails_server/helpers.rb', line 67

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 ⇒ Object



76
77
78
# File 'lib/oop_rails_server/helpers.rb', line 76

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) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/oop_rails_server/helpers.rb', line 114

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 ⇒ Object



72
73
74
# File 'lib/oop_rails_server/helpers.rb', line 72

def rails_server_templates_root
  @rails_server_templates_root ||= File.join(rails_server_project_root, "spec/rails/templates")
end

#rails_servers ⇒ Object



90
91
92
# File 'lib/oop_rails_server/helpers.rb', line 90

def rails_servers
  @rails_servers ||= { }
end

#rails_template_name ⇒ Object



63
64
65
# File 'lib/oop_rails_server/helpers.rb', line 63

def rails_template_name
  rails_server.name
end

#start_rails_server!(options = { }) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/oop_rails_server/helpers.rb', line 125

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 =
      oop_rails_server_base_templates +
      rails_server_implicit_template_paths +
      templates

    template_paths = rails_server_template_paths(templates)

    additional_gemfile_lines = Array(rails_server_additional_gemfile_lines || [ ])
    additional_gemfile_lines += Array(options[:additional_gemfile_lines] || [ ])

    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], :additional_gemfile_lines => additional_gemfile_lines)

    rails_servers[name] = server

    server
  end

  server.start!
end

#stop_rails_servers! ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/oop_rails_server/helpers.rb', line 160

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