class MDQT::Client::MetadataService

Public Class Methods

new(base_url, options = {}) click to toggle source
# 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

Public Instance Methods

base_url() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 30
def base_url
  @base_url
end
cache?() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 77
def cache?
  cache_type == :none ? false : true
end
cache_type() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 81
def cache_type
  @cache_type || :none
end
explain?() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 69
def explain?
  @explain
end
get(entity_id) click to toggle source
# 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) click to toggle source
# 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!() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 102
def purge_cache!
  cache_store.clear
end
store_config() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 85
def store_config
  @store_config || default_store_config
end
tidy_cache!() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 98
def tidy_cache!
  cache_type.cleanup
end
tls_cert_check?() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 73
def tls_cert_check?
  @tls_cert_check
end
valid_sha1?(sha1) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 65
def verbose?
  @verbose
end

Private Instance Methods

cache_logger() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 153
def cache_logger
  verbose? ? Logger.new('mdqt_cache.log') : nil
end
cache_store() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 157
def cache_store
  case cache_type
  when :file, :files, nil
    @cache_store = ActiveSupport::Cache.lookup_store(:file_store, store_config)
  when :memcache, :memcached
    @cache_store = ActiveSupport::Cache.lookup_store(:mem_cache_store, [store_config])
  end
end
connection() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 117
def connection
  Faraday.new(:url => base_url) do |faraday|
    faraday.request :url_encoded
    faraday.use FaradayMiddleware::Gzip
    faraday.use FaradayMiddleware::FollowRedirects
    faraday.use :http_cache, faraday_cache_config if cache?
    faraday.ssl.verify = tls_cert_check?
    faraday.headers['Accept']         = 'application/samlmetadata+xml'
    faraday.headers['Accept-Charset'] = 'utf-8'
    faraday.headers['User-Agent']     = "MDQT v#{MDQT::VERSION}"
    #faraday.response :logger
    faraday.adapter :typhoeus
  end
end
default_store_config() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 132
def default_store_config
  case cache_type
  when :none, nil
    nil
  when :file, :files
    File.absolute_path(File.join(Dir.tmpdir, 'mdqt_cache'))
  when :memcached, :memcache
    'localhost:11211'
  end
end
faraday_cache_config() click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 143
def faraday_cache_config
  {
      store: cache_store,
      shared_cache: false,
      serializer: Marshal,
      #logger: cache_logger,
      instrumenter: ActiveSupport::Notifications
  }
end
request_path(entity_id) click to toggle source
# File lib/mdqt/client/metadata_service.rb, line 108
def request_path(entity_id)
  case entity_id
  when nil, ""
    'entities'
  else
    ['entities', entity_id].join('/')
  end
end