module PDC

Constants

Config
VERSION

Attributes

logger[W]

Public Class Methods

api_url() click to toggle source
# File lib/pdc/config.rb, line 80
def api_url
  URI.join(config.site, config.api_root)
end
config() click to toggle source
# File lib/pdc/config.rb, line 67
def config
  @config ||= Config.new
end
config=(new_config) click to toggle source
# File lib/pdc/config.rb, line 71
def config=(new_config)
  @config = new_config
  apply_config
end
configure() { |config| ... } click to toggle source
# File lib/pdc/config.rb, line 56
def configure
  @config = Config.new
  begin
    yield(@config) if block_given?
  rescue NoMethodError => e
    raise ConfigError, e
  end

  apply_config
end
logger() click to toggle source
# File lib/pdc/logger.rb, line 7
def logger
  @logger ||= ::Logger.new($stdout).tap do |log|
    log.progname = name
  end
end
token() click to toggle source
# File lib/pdc/config.rb, line 84
def token
  return unless config.requires_token
  config.token || Request::TokenFetcher.fetch
end
token_url() click to toggle source
# File lib/pdc/config.rb, line 76
def token_url
  URI.join(api_url, config.token_obtain_path)
end

Private Class Methods

apply_config() click to toggle source
# File lib/pdc/config.rb, line 91
def apply_config
  reset_logger
  reset_token_fetcher
  reset_base_connection
end
cache_store() click to toggle source
# File lib/pdc/config.rb, line 149
def cache_store
  config.cache_store || ActiveSupport::Cache.lookup_store(:memory_store)
end
reset_base_connection() click to toggle source

resets and returns the Faraday connection object

# File lib/pdc/config.rb, line 110
def reset_base_connection
  faraday_config = {
    url:      api_url,
    headers:  PDC::Request.default_headers,
    ssl:      ssl_config,
    # adding this request parameter is about changing how
    # parameters are serialized which is mentioned on faraday's
    # doc, it's about sending a query request with multi values
    # of a field, like product_version: [xxx, xxx].
    request:  {
      params_encoder: Faraday::FlatParamsEncoder
    }
  }

  PDC::Base.connection = Faraday.new(faraday_config) do |c|
    c.request   :append_slash_to_path
    c.request   :pdc_token, token: config.token if config.requires_token

    c.response  :logger, config.logger
    c.response  :pdc_paginator
    c.response  :pdc_json_parser
    c.response  :raise_error
    c.response  :pdc_raise_error

    c.use       FaradayMiddleware::FollowRedirects

    unless config.disable_caching
      c.use Faraday::HttpCache, store: cache_store,
                                logger: PDC.logger,
                                instrumenter: ActiveSupport::Notifications
    end
    c.adapter Faraday.default_adapter
  end
end
reset_logger() click to toggle source
# File lib/pdc/config.rb, line 97
def reset_logger
  PDC.logger = Logger.new(nil) unless config.enable_logging
  logger.level = Logger.const_get(config.log_level.upcase)
end
reset_token_fetcher() click to toggle source
# File lib/pdc/config.rb, line 102
def reset_token_fetcher
  Request::TokenFetcher.configure do |c|
    c.url = token_url
    c.ssl_verify_mode = config.ssl_verify_mode
  end
end
ssl_config() click to toggle source
# File lib/pdc/config.rb, line 145
def ssl_config
  { verify: config.ssl_verify_mode == OpenSSL::SSL::VERIFY_PEER }
end