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

Returns a new instance of MetadataService.



21
22
23
24
25
26
27
28
# File 'lib/mdqt/client/metadata_service.rb', line 21

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



30
31
32
# File 'lib/mdqt/client/metadata_service.rb', line 30

def base_url
  @base_url
end

#cache?Boolean

Returns:

  • (Boolean)


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

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

#cache_typeObject



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

def cache_type
  @cache_type || :none
end

#explain?Boolean

Returns:

  • (Boolean)


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

def explain?
  @explain
end

#get(entity_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mdqt/client/metadata_service.rb', line 34

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 = 100
      req.options.open_timeout = 5
    end
  rescue Faraday::ConnectionFailed => oops
    abort "Error - can't connect to MDQ service at URL #{base_url}: #{oops.to_s}"
  end

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

end

#prepare_id(id) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mdqt/client/metadata_service.rb', line 52

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



102
103
104
# File 'lib/mdqt/client/metadata_service.rb', line 102

def purge_cache!
  cache_store.clear
end

#store_configObject



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

def store_config
  @store_config || default_store_config
end

#tidy_cache!Object



98
99
100
# File 'lib/mdqt/client/metadata_service.rb', line 98

def tidy_cache!
  cache_type.cleanup
end

#tls_cert_check?Boolean

Returns:

  • (Boolean)


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

def tls_cert_check?
  @tls_cert_check
end

#valid_sha1?(sha1) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/mdqt/client/metadata_service.rb', line 94

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

#validate_sha1!(sha1) ⇒ Object



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

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

#verbose?Boolean

Returns:

  • (Boolean)


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

def verbose?
  @verbose
end