Module: ArchivesSpace::Helpers

Included in:
Client
Defined in:
lib/archivesspace/client/helpers.rb

Instance Method Summary collapse

Instance Method Details

#accessions(options = {}, &block) ⇒ Object



13
14
15
16
17
18
# File 'lib/archivesspace/client/helpers.rb', line 13

def accessions(options = {}, &block)
  records = all('accessions', options) do |record|
    yield record if block_given?
  end
  records
end

#all(path, options = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/archivesspace/client/helpers.rb', line 20

def all(path, options = {}, &block)
  all    = []
  format = options.delete(:format)
  # options[:headers] -- add xml headers if format

  result = get(path, options.merge({ query: { all_ids: true } } ))
  ap result
  raise RequestError.new result.status if result.status_code != 200
  result.parsed.each do |id|
    path_with_id = format ? "#{format}/#{id}.xml" : "#{path}/#{id}"
    result = get(path_with_id, options)
    raise RequestError.new result.status if result.status_code != 200
    record = format ? Nokogiri::XML(result.body).to_xml : result.parsed
    yield record if block_given?
    all << record
  end
  all
end

#backend_versionObject



39
40
41
# File 'lib/archivesspace/client/helpers.rb', line 39

def backend_version
  get "version"
end

#batch_import(payload, params = {}) ⇒ Object



43
44
45
# File 'lib/archivesspace/client/helpers.rb', line 43

def batch_import(payload, params = {})
  # create "batch_import", payload, params
end

#digital_object_to_xml(digital_object, format = "dublin_core") ⇒ Object



47
48
49
50
51
# File 'lib/archivesspace/client/helpers.rb', line 47

def digital_object_to_xml(digital_object, format = "dublin_core")
  id   = digital_object["uri"].split("/")[-1]
  path = "digital_objects/#{format}/#{id}.xml"
  get_xml path
end

#digital_objects(format = nil, options = {}, &block) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/archivesspace/client/helpers.rb', line 53

def digital_objects(format = nil, options = {}, &block)
  path    = "digital_objects"
  format  = format ? "#{path}/#{format}" : nil
  records = all(path, options.merge({ format: format })) do |record|
    yield record if block_given?
  end
  records
end

#group_user_assignment(users_with_roles, params = { with_members: true }) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/archivesspace/client/helpers.rb', line 66

def group_user_assignment(users_with_roles, params = { with_members: true })
  # updated = []
  # groups.each do |group|
  #   changed = false

  #   users_with_roles.each do |user, roles|
  #     if roles.include? group["group_code"]
  #       unless group["member_usernames"].include? user
  #         group["member_usernames"] << user
  #         changed = true
  #       end
  #     else
  #       if group["member_usernames"].include? user
  #         group["member_usernames"].delete user
  #         changed = true
  #       end
  #     end
  #   end

  #   if changed
  #     updated << update( group, params )
  #   end
    
  #   sleep 1 # moderate requests
  # end
  # updated
end

#groupsObject



62
63
64
# File 'lib/archivesspace/client/helpers.rb', line 62

def groups
  #
end

#loginObject



94
95
96
97
98
99
100
# File 'lib/archivesspace/client/helpers.rb', line 94

def 
  username, password = config.username, config.password
  result = request('POST', "/users/#{username}/login", { query: { password: password } })
  raise ConnectionError.new "Failed to connect to ArchivesSpace backend as #{username} #{password}" unless result.parsed["session"]
  @token = result.parsed["session"]
  self
end

#password_reset(username, password) ⇒ Object



102
103
104
105
106
# File 'lib/archivesspace/client/helpers.rb', line 102

def password_reset(username, password)
  user = all('users').find { |u| u["username"] == username }
  raise RequestError.new user.status unless user
  post(user["uri"], user, { password: password })
end

#repositoriesObject



108
109
110
111
112
113
# File 'lib/archivesspace/client/helpers.rb', line 108

def repositories
  records = get('repositories').parsed.each do |record|
    yield record if block_given?
  end
  records
end

#repositories_with_agentObject



115
116
117
# File 'lib/archivesspace/client/helpers.rb', line 115

def repositories_with_agent
  #
end

#resource_to_xml(resource, format = "ead") ⇒ Object



119
120
121
122
123
# File 'lib/archivesspace/client/helpers.rb', line 119

def resource_to_xml(resource, format = "ead")
  id   = resource["uri"].split("/")[-1]
  path = format == "ead" ? "resource_descriptions/#{id}.xml" : "resources/#{format}/#{id}.xml"
  get_xml path
end

#resources(format = nil, options = {}, &block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/archivesspace/client/helpers.rb', line 125

def resources(format = nil, options = {}, &block)
  path = 'resources'
  # the api is inconsistent with the path structure for resource ead (and pdf)
  if format
    if format =~ /(ead|pdf)/
      format = "resource_descriptions"
    else
      format = "#{path}/#{format}"
    end
  end
  records = all(path, options.merge({ format: format })) do |record|
    yield record if block_given?
  end
  records
end

#search(params) ⇒ Object



141
142
143
# File 'lib/archivesspace/client/helpers.rb', line 141

def search(params)
  # get "search", params
end

#usersObject



145
146
147
148
149
150
# File 'lib/archivesspace/client/helpers.rb', line 145

def users
  records = all('users') do |record|
    yield record if block_given?
  end
  records
end