Module: Ape::Util::InstanceMethods

Included in:
Service
Defined in:
lib/ape/util.rb

Instance Method Summary collapse

Instance Method Details

#check_resource(uri, name, content_type = nil, report = true) ⇒ Object

Get a resource, optionally check its content-type



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ape/util.rb', line 17

def check_resource(uri, name, content_type = nil, report = true)
  resource = Getter.new(uri, @authent)

  # * Check the URI
  if resource.last_error
    reporter.error(self, "Unacceptable #{name} URI: " + resource.last_error, name) if report
    return nil
  end

  # * Get it, make sure it has the right content-type
  worked = resource.get(content_type)
  reporter.save_dialog(name, resource) if report

  reporter.security_warning(self) if (resource.security_warning && report)

  if !worked
    # oops, couldn't even get get it
    reporter.error(self, "#{name} failed: " + resource.last_error, name) if report
    return nil

  elsif resource.last_error
    # oops, media-type problem
    reporter.error(self, "#{name}: #{resource.last_error}", name) if report
  else
    # resource fetched and is of right type
    reporter.success(self, "#{name}: it exists and is served properly.", name) if report
  end

  return resource
end