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_serverObject (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

#clear_mails!(the_rails_server = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/oop_rails_server/helpers.rb', line 129

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

  expect(json['exception']).to be
  expect(json['exception']['class']).to eq(class_name.to_s)
  expect(json['exception']['message']).to 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|
    expect(data).to 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)
  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) ⇒ Object



140
141
142
143
# File 'lib/oop_rails_server/helpers.rb', line 140

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/oop_rails_server/helpers.rb', line 92

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_additional_gemfile_linesObject



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

def rails_server_additional_gemfile_lines
  [ ]
end

#rails_server_default_versionObject



84
85
86
# File 'lib/oop_rails_server/helpers.rb', line 84

def rails_server_default_version
  nil
end

#rails_server_implicit_template_pathsObject



155
156
157
# File 'lib/oop_rails_server/helpers.rb', line 155

def rails_server_implicit_template_paths
  [ ]
end

#rails_server_project_rootObject



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_directoryObject



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



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

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_rootObject



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_serversObject



88
89
90
# File 'lib/oop_rails_server/helpers.rb', line 88

def rails_servers
  @rails_servers ||= { }
end

#rails_template_nameObject



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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/oop_rails_server/helpers.rb', line 170

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)

    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



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/oop_rails_server/helpers.rb', line 204

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