Class: MDQT::Client::MetadataService

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

Instance Method Summary collapse

Constructor Details

#initialize(base_url, options = {}) ⇒ MetadataService

Rails.application.config.active_support.cache_format_version = 7.0 ActiveSupport::Deprecation.behavior = :silence



26
27
28
29
30
31
32
33
# File 'lib/mdqt/client/metadata_service.rb', line 26

def initialize(base_url, options = {})
  @base_url = base_url
  @cache_type = options[:cache_type] ? options[:cache_type].to_sym : :none
  @store_config = options[:cache_store]
  @verbose = options[:verbose] ? true : false
  @explain = options[:explain] ? true : false
  @tls_cert_check = options[:tls_cert_check] ? true : false
end

Instance Method Details

#base_urlObject



35
36
37
# File 'lib/mdqt/client/metadata_service.rb', line 35

def base_url
  @base_url
end

#cache?Boolean

Returns:

  • (Boolean)


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

def cache?
  cache_type == :none ? false : true
end

#cache_typeObject



108
109
110
# File 'lib/mdqt/client/metadata_service.rb', line 108

def cache_type
  @cache_type || :none
end

#exists?(entity_id) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mdqt/client/metadata_service.rb', line 59

def exists?(entity_id)

  entity_id = prepare_id(entity_id)

  begin
    http_response = connection.head do |req|
      req.url request_path(entity_id)
      req.options.timeout = 1000
      req.options.open_timeout = 60
    end
  rescue Faraday::ConnectionFailed => oops
    abort "Error - can't connect to MDQ service at URL #{base_url}: #{oops.to_s}"
  rescue Faraday::TimeoutError => oops
    abort "Error - connection to #{base_url} timed out!"
  end

  http_response.status == 200

end

#explain?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/mdqt/client/metadata_service.rb', line 96

def explain?
  @explain
end

#get(entity_id) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mdqt/client/metadata_service.rb', line 39

def get(entity_id)

  entity_id = prepare_id(entity_id)

  begin
    http_response = connection.get do |req|
      req.url request_path(entity_id)
      req.options.timeout = 1000
      req.options.open_timeout = 60
    end
  rescue Faraday::ConnectionFailed => oops
    abort "Error - can't connect to MDQ service at URL #{base_url}: #{oops.to_s}"
  rescue Faraday::TimeoutError => oops
    abort "Error - connection to #{base_url} timed out!"
  end

  MetadataResponse.new(entity_id, base_url, http_response, explain: explain?)

end

#prepare_id(id) ⇒ Object



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

def prepare_id(id)
  case id
  when :all, "", nil
    ""
  when /^{sha1}/i
    CGI.escape(validate_sha1!(id.downcase.strip))
  when /^\[sha1\]/i
    CGI.escape(validate_sha1!(id.downcase.strip.gsub "[sha1]", "{sha1}"))
  else
    CGI.escape(id.strip)
  end
end

#purge_cache!Object



130
131
132
133
# File 'lib/mdqt/client/metadata_service.rb', line 130

def purge_cache!
  return unless cache_store
  cache_store.clear
end

#store_configObject



112
113
114
# File 'lib/mdqt/client/metadata_service.rb', line 112

def store_config
  @store_config || default_store_config
end

#tidy_cache!Object



125
126
127
128
# File 'lib/mdqt/client/metadata_service.rb', line 125

def tidy_cache!
  return unless cache_store
  cache_store.cleanup
end

#tls_cert_check?Boolean

Returns:

  • (Boolean)


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

def tls_cert_check?
  @tls_cert_check
end

#valid_sha1?(sha1) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/mdqt/client/metadata_service.rb', line 121

def valid_sha1?(sha1)
  (sha1 =~ /^[{\[]sha1[\]}][0-9a-f]{40}$/i).nil? ? false : true
end

#validate_sha1!(sha1) ⇒ Object



116
117
118
119
# File 'lib/mdqt/client/metadata_service.rb', line 116

def validate_sha1!(sha1)
  abort "Error: SHA1 identifier '#{sha1}' is malformed, halting" unless valid_sha1?(sha1)
  sha1
end

#verbose?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/mdqt/client/metadata_service.rb', line 92

def verbose?
  @verbose
end