Class: Ape::ServiceDocumentValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/ape/validators/service_document_validator.rb

Instance Attribute Summary collapse

Attributes inherited from Validator

#authent, #reporter

Instance Method Summary collapse

Methods inherited from Validator

custom_validators, instance

Methods included from Util

included

Methods included from ValidatorDsl

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ape::Validator

Instance Attribute Details

#entry_collectionsObject (readonly)

Returns the value of attribute entry_collections.



5
6
7
# File 'lib/ape/validators/service_document_validator.rb', line 5

def entry_collections
  @entry_collections
end

#media_collectionsObject (readonly)

Returns the value of attribute media_collections.



5
6
7
# File 'lib/ape/validators/service_document_validator.rb', line 5

def media_collections
  @media_collections
end

#service_documentObject (readonly)

Returns the value of attribute service_document.



5
6
7
# File 'lib/ape/validators/service_document_validator.rb', line 5

def service_document
  @service_document
end

Instance Method Details

#init_service_collections(uri) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ape/validators/service_document_validator.rb', line 35

def init_service_collections(uri)
  # * Do we have collections we can post an entry and a picture to?
  #   the requested_* arguments are the requested collection titles; if
  #    provided, try to match them, otherwise just pick the first listed
  #
  begin
    @service_collections = Service.collections(@service, uri)
  rescue Exception
    reporter.error(self, "Couldn't read collections from service doc: #{$!}")
    return
  end
  
  if @service_collections.length > 0
    reporter.start_list(self, "Found these collections")
    @service_collections.each do |collection|
      reporter.list_item("'#{collection.title}' " +
        "accepts #{collection.accept.join(', ')}")
        
      if (collection.accept.index(Names::AtomEntryMediaType))
        @entry_collections ||= []
        @entry_collections << collection
      else
        @media_collections ||= []
        @media_collections << collection
      end
    end
  end
end

#init_service_document(uri) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ape/validators/service_document_validator.rb', line 14

def init_service_document(uri)
  reporter.info(self, "TESTING: Service document and collections.")
  name = 'Retrieval of Service Document'
  service = check_resource(uri, name, Names::AppMediaType)
  return unless service

  # * XML-parse the service doc
  text = service.body
  begin
    @service = REXML::Document.new(text, { :raw => nil })
  rescue REXML::ParseException
    prob = $!.to_s.gsub(/\n/, '<br/>')
    reporter.error(self, "Service document not well-formed: #{prob}")
    return
  end

  # RNC-validate the service doc
  Validator.instance(:schema, @reporter).validate(:schema => Samples.service_RNC, 
    :title => 'Service doc', :doc => @service)
end

#validate(opts = {}) ⇒ Object

Raises:



7
8
9
10
11
12
# File 'lib/ape/validators/service_document_validator.rb', line 7

def validate(opts = {})
  init_service_document(opts[:uri])
  raise ValidationError, "service document not found in: #{opts[uri]}" unless @service
  init_service_collections(opts[:uri])
  raise ValidationError unless @entry_collections && @media_collections
end