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



131
132
133
134
135
136
137
138
139
140
# 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) ⇒ Object



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



142
143
144
145
# 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) ⇒ Object



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
128
129
# 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_versionObject



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

def rails_server_default_version
  nil
end

#rails_server_gemfile_modifierObject



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

def rails_server_gemfile_modifier
  Proc.new { |gemfile| }
end

#rails_server_implicit_template_pathsObject



157
158
159
# File 'lib/oop_rails_server/helpers.rb', line 157

def rails_server_implicit_template_paths
  [ ]
end

#rails_server_project_rootObject



69
70
71
72
# 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_directoryObject



78
79
80
# 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) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# 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_rootObject



74
75
76
# 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_serversObject



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

def rails_servers
  @rails_servers ||= { }
end

#rails_template_nameObject



65
66
67
# File 'lib/oop_rails_server/helpers.rb', line 65

def rails_template_name
  rails_server.name
end

#start_rails_server!(options = { }) ⇒ Object



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
203
204
205
206
207
208
209
# 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!Object



211
212
213
214
215
216
217
218
219
220
221
222
# 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