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.



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

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

  @requested_identitier = identifier
  @identifier = URI.decode(identifier)
  @service = service
  @code = http_response.status || 500
  @data = http_response.body || ""
  @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



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

def code
  @code
end

#dataObject



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

def data
  @data || ""
end

#etagObject



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

def etag
  @etag
end

#expiresObject



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

def expires
  @expires
end

#explanationObject



122
123
124
# File 'lib/mdqt/client/metadata_response.rb', line 122

def explanation
  @explanation
end

#filenameObject



82
83
84
85
86
87
88
89
90
# File 'lib/mdqt/client/metadata_response.rb', line 82

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



32
33
34
# File 'lib/mdqt/client/metadata_response.rb', line 32

def identifier
  @identifier
end

#last_modifiedObject



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

def last_modified
  @last_modified
end

#messageObject



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
# File 'lib/mdqt/client/metadata_response.rb', line 92

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)


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

def ok?
  @ok
end

#requested_identifierObject



36
37
38
# File 'lib/mdqt/client/metadata_response.rb', line 36

def requested_identifier
  @identifier
end

#serviceObject



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

def service
  @service
end

#signed?Boolean

Returns:

  • (Boolean)


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

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

#typeObject



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

def type
  @type
end

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

Returns:

  • (Boolean)


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

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