Class: MDQT::Client::MetadataResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/mdqt/client/metadata_response.rb

Instance Method Summary collapse

Constructor Details

#initialize(identifier, service, http_response, options = {}) ⇒ MetadataResponse

Returns a new instance of MetadataResponse.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mdqt/client/metadata_response.rb', line 9

def initialize(identifier, service, http_response, options = {})

  @requested_identitier = identifier
  @identifier = URI.decode_www_form_component(identifier)
  @service = service
  @code = http_response.status || 500
  @data = http_response.body || ""
  @type = nil
  @content_type = http_response.headers['Content-Type']
  @expires = http_response.headers['Expires']
  @etag = http_response.headers['ETag']
  @last_modified = http_response.headers['Last-Modified']
  @ok = http_response.success?
  @explanation = {}

  if options[:explain]
    @explanation[:url] = http_response.env.url.to_s
    @explanation[:method] = http_response.env.method.to_s.upcase
    @explanation[:status] = http_response.status
    @explanation[:response_headers] = http_response.headers
    @explanation[:request_headers] = http_response.env.request_headers
  end

end

Instance Method Details

#codeObject



57
58
59
# File 'lib/mdqt/client/metadata_response.rb', line 57

def code
  @code
end

#content_typeObject



69
70
71
# File 'lib/mdqt/client/metadata_response.rb', line 69

def content_type
  @content_type
end

#dataObject



61
62
63
# File 'lib/mdqt/client/metadata_response.rb', line 61

def data
  @data || ""
end

#entity_idObject



43
44
45
46
# File 'lib/mdqt/client/metadata_response.rb', line 43

def entity_id
  raise "Incorrect metadata file type - aggregate" if aggregate?
  @entity_id ||= extract_entity_id
end

#entity_idsObject



48
49
50
# File 'lib/mdqt/client/metadata_response.rb', line 48

def entity_ids
  @entity_ids ||= extract_entity_ids
end

#etagObject



77
78
79
# File 'lib/mdqt/client/metadata_response.rb', line 77

def etag
  @etag
end

#expiresObject



73
74
75
# File 'lib/mdqt/client/metadata_response.rb', line 73

def expires
  @expires
end

#explanationObject



149
150
151
# File 'lib/mdqt/client/metadata_response.rb', line 149

def explanation
  @explanation
end

#filenameObject



109
110
111
112
113
114
115
116
117
# File 'lib/mdqt/client/metadata_response.rb', line 109

def filename
  if identifier.empty?
    @filename = "aggregate-#{Digest::SHA1.hexdigest(@service)}.xml"
  else
    @filename ||= identifier.start_with?("{sha1}") ?
                      "#{@identifier.gsub("{sha1}","")}.xml" :
                      "#{Digest::SHA1.hexdigest(@identifier)}.xml"
  end
end

#identifierObject



34
35
36
# File 'lib/mdqt/client/metadata_response.rb', line 34

def identifier
  @identifier
end

#last_modifiedObject



81
82
83
# File 'lib/mdqt/client/metadata_response.rb', line 81

def last_modified
  @last_modified
end

#messageObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/mdqt/client/metadata_response.rb', line 119

def message
  case code
  when 200
    identifier.empty? ? "[200] OK! Data for aggregate has been downloaded from #{service}" :
        "[200] OK! Data for '#{identifier}' has been downloaded from #{service}"
  when 304
    "[304] OK! Data for '#{identifier}' is already available in a local cache"
  when 400
    "[400] The identifier '#{identifier}' ('#{requested_identifier}') is malformed or service URL #{service} is incorrect"
  when 401
    "[401] Credentials are incorrect or missing for '#{identifier}'"
  when 403
    identifier.empty? ? "[403] The MDQ service at #{service} does not support aggregate downloads" :
        "[403] You do not have access rights to '#{identifier}' at #{service}"
  when 404
    identifier.empty? ? "[404] The MDQ service at #{service} is not responding with aggregated metadata or the correct status" :
        "[404] Entity metadata for '#{identifier}' was not found at #{service}"
  when 405
    "[405] The service at #{service} believes the wrong HTTP method was used. We should have used HTTP GET..."
  when 406
    "[406] The requested content type is not available at #{service}"
  when 500
    "[500] An error has occurred at #{service}"
  when 505
    "[505] The service at #{service} claims our request was using pre-1999 web protocols, not HTTP 1.1 or later"
  else
    "[#{code}] Sorry - an unknown error has occurred requesting '#{identifier}' from #{service}.\nPlease report this bug!"
  end
end

#ok?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/mdqt/client/metadata_response.rb', line 85

def ok?
  @ok
end

#requested_identifierObject



39
40
41
# File 'lib/mdqt/client/metadata_response.rb', line 39

def requested_identifier
  @identifier
end

#serviceObject



53
54
55
# File 'lib/mdqt/client/metadata_response.rb', line 53

def service
  @service
end

#signed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mdqt/client/metadata_response.rb', line 89

def signed?
  @data.include? "Signature" # This is... not great
end

#typeObject



65
66
67
# File 'lib/mdqt/client/metadata_response.rb', line 65

def type
  @type ||= calculate_type
end

#valid?Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/mdqt/client/metadata_response.rb', line 99

def valid?
  validator = MetadataValidator.new
  validator.valid?(self)
end

#validation_errorObject



104
105
106
107
# File 'lib/mdqt/client/metadata_response.rb', line 104

def validation_error
  validator = MetadataValidator.new
  validator.validation_error(self)
end

#verified_signature?(certs = [], _ = {}) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/mdqt/client/metadata_response.rb', line 93

def verified_signature?(certs = [], _ = {})
  return true unless ok?  # CHECK ?
  validator = MetadataValidator.new(certs: [certs].flatten)
  validator.verified_signature?(self)
end