Class: ActiveFedora::Fedora

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/fedora.rb

Constant Summary collapse

SLASH =
'/'.freeze
BLANK =
''.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Fedora

Returns a new instance of Fedora.



22
23
24
25
26
# File 'lib/active_fedora/fedora.rb', line 22

def initialize(config)
  @config = config

  validate_options
end

Class Method Details

.instanceObject



11
12
13
14
15
# File 'lib/active_fedora/fedora.rb', line 11

def instance
  register unless ActiveFedora::RuntimeRegistry.fedora_connection

  ActiveFedora::RuntimeRegistry.fedora_connection
end

.register(options = {}) ⇒ Object



7
8
9
# File 'lib/active_fedora/fedora.rb', line 7

def register(options = {})
  ActiveFedora::RuntimeRegistry.fedora_connection = Fedora.new(ActiveFedora.fedora_config.credentials.merge(options))
end

.reset!Object



17
18
19
# File 'lib/active_fedora/fedora.rb', line 17

def reset!
  ActiveFedora::RuntimeRegistry.fedora_connection = nil
end

Instance Method Details

#authorized_connectionObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_fedora/fedora.rb', line 88

def authorized_connection
  options = {}
  options[:ssl] = ssl_options if ssl_options
  options[:request] = request_options if request_options
  Faraday.new(host, options) do |conn|
    conn.response :encoding # use Faraday::Encoding middleware
    conn.adapter Faraday.default_adapter # net/http
    if Gem::Version.new(Faraday::VERSION) < Gem::Version.new('2')
      conn.request :basic_auth, user, password
    else
      conn.request :authorization, :basic, user, password
    end
  end
end

#base_pathObject



32
33
34
# File 'lib/active_fedora/fedora.rb', line 32

def base_path
  @config[:base_path] || SLASH
end

#base_uriObject



36
37
38
# File 'lib/active_fedora/fedora.rb', line 36

def base_uri
  host + base_path.sub(/\/$/, BLANK)
end

#build_connectionObject



80
81
82
83
84
85
86
# File 'lib/active_fedora/fedora.rb', line 80

def build_connection
  # The InboundRelationConnection does provide more data, useful for
  # things like ldp:IndirectContainers, but it's imposes a significant
  # performance penalty on every request
  #   @connection ||= InboundRelationConnection.new(caching_connection(omit_ldpr_interaction_model: true))
  InitializingConnection.new(caching_connection(omit_ldpr_interaction_model: true), root_resource_path)
end

#build_ntriples_connectionObject



111
112
113
# File 'lib/active_fedora/fedora.rb', line 111

def build_ntriples_connection
  ActiveFedora::InitializingConnection.new(ActiveFedora::CachingConnection.new(ntriples_connection, omit_ldpr_interaction_model: true), root_resource_path)
end

#caching_connection(options = {}) ⇒ Object



64
65
66
# File 'lib/active_fedora/fedora.rb', line 64

def caching_connection(options = {})
  CachingConnection.new(authorized_connection, options)
end

#clean_connectionObject



60
61
62
# File 'lib/active_fedora/fedora.rb', line 60

def clean_connection
  @clean_connection ||= CleanConnection.new(connection)
end

#connectionObject



56
57
58
# File 'lib/active_fedora/fedora.rb', line 56

def connection
  @connection ||= build_connection
end

#hostObject



28
29
30
# File 'lib/active_fedora/fedora.rb', line 28

def host
  @config[:url].sub(/\/$/, BLANK)
end

#ldp_resource_serviceObject



68
69
70
# File 'lib/active_fedora/fedora.rb', line 68

def ldp_resource_service
  @service ||= LdpResourceService.new(connection)
end

#ntriples_connectionObject



107
108
109
# File 'lib/active_fedora/fedora.rb', line 107

def ntriples_connection
  authorized_connection.tap { |conn| conn.headers['Accept'] = 'application/n-triples' }
end

#passwordObject



44
45
46
# File 'lib/active_fedora/fedora.rb', line 44

def password
  @config[:password]
end

#request_optionsObject



52
53
54
# File 'lib/active_fedora/fedora.rb', line 52

def request_options
  @config[:request]
end

#root_resource_pathObject

Remove a leading slash from the base_path



76
77
78
# File 'lib/active_fedora/fedora.rb', line 76

def root_resource_path
  @root_resource_path ||= base_path.sub(SLASH, BLANK)
end

#ssl_optionsObject



48
49
50
# File 'lib/active_fedora/fedora.rb', line 48

def ssl_options
  @config[:ssl]
end

#userObject



40
41
42
# File 'lib/active_fedora/fedora.rb', line 40

def user
  @config[:user]
end

#validate_optionsObject



103
104
105
# File 'lib/active_fedora/fedora.rb', line 103

def validate_options
  ActiveFedora::Base.logger.warn "Fedora URL (#{host}) does not end with /rest. This could be a problem. Check your fedora.yml config" unless host.downcase.end_with?("/rest")
end