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.



19
20
21
# File 'lib/active_fedora/fedora.rb', line 19

def initialize(config)
  @config = config
end

Class Method Details

.instanceObject



8
9
10
11
12
# File 'lib/active_fedora/fedora.rb', line 8

def instance
  register unless ActiveFedora::RuntimeRegistry.fedora_connection

  ActiveFedora::RuntimeRegistry.fedora_connection
end

.register(options = {}) ⇒ Object



4
5
6
# File 'lib/active_fedora/fedora.rb', line 4

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

.reset!Object



14
15
16
# File 'lib/active_fedora/fedora.rb', line 14

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

Instance Method Details

#authorized_connectionObject



90
91
92
93
94
95
96
# File 'lib/active_fedora/fedora.rb', line 90

def authorized_connection
  options = {}
  options[:ssl] = ssl_options if ssl_options
  connection = Faraday.new(host, options)
  connection.basic_auth(user, password)
  connection
end

#base_pathObject



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

def base_path
  @config[:base_path] || '/'
end

#build_connectionObject



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

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(CachingConnection.new(authorized_connection))
  CachingConnection.new(authorized_connection, omit_ldpr_interaction_model: true)
end

#clean_connectionObject



50
51
52
# File 'lib/active_fedora/fedora.rb', line 50

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

#connectionObject



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

def connection
  @connection ||= begin
    init_base_path
    build_connection
  end
end

#hostObject



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

def host
  @config[:url]
end

#init_base_pathObject

Call this to create a Container Resource to act as the base path for this connection



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_fedora/fedora.rb', line 62

def init_base_path
  return if @initialized
  connection = build_connection

  connection.head(root_resource_path)
  ActiveFedora::Base.logger.info "Attempted to init base path `#{root_resource_path}`, but it already exists" if ActiveFedora::Base.logger
  @initialized = true
  false
rescue Ldp::NotFound
  unless host.downcase.end_with?("/rest")
    ActiveFedora::Base.logger.warn "Fedora URL (#{host}) does not end with /rest. This could be a problem. Check your fedora.yml config"
  end
  @initialized = connection.put(root_resource_path, BLANK).success?
end

#ldp_resource_serviceObject



54
55
56
# File 'lib/active_fedora/fedora.rb', line 54

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

#passwordObject



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

def password
  @config[:password]
end

#root_resource_pathObject

Remove a leading slash from the base_path



78
79
80
# File 'lib/active_fedora/fedora.rb', line 78

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

#ssl_optionsObject



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

def ssl_options
  @config[:ssl]
end

#userObject



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

def user
  @config[:user]
end