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
38
39
# File 'lib/archivesspace/client/helpers.rb', line 20

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

  result = get(path, options.merge({ query: { all_ids: true } } ))
  raise RequestError.new result.status if result.status_code != 200
  ids    = result.parsed
  ids    = ids.map{ |object| object["uri"].split("/")[-1] } if parse_id
  ids.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



41
42
43
# File 'lib/archivesspace/client/helpers.rb', line 41

def backend_version
  get "version"
end

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



45
46
47
# File 'lib/archivesspace/client/helpers.rb', line 45

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

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



49
50
51
52
53
# File 'lib/archivesspace/client/helpers.rb', line 49

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



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

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/archivesspace/client/helpers.rb', line 71

def group_user_assignment(users_with_roles, params = { with_members: true })
  updated = []
  groups 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
      id = group["uri"].split("/")[-1]
      response = post( "/groups/#{id}", group, params )
      updated << response.parsed
    end
  end
  updated
end

#groupsObject



64
65
66
67
68
69
# File 'lib/archivesspace/client/helpers.rb', line 64

def groups
  records = all('groups', { parse_id: true }) do |record|
    yield record if block_given?
  end
  records
end

#loginObject



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

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



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

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



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

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

#repositories_with_agentObject



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

def repositories_with_agent
  #
end

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



124
125
126
127
128
# File 'lib/archivesspace/client/helpers.rb', line 124

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/archivesspace/client/helpers.rb', line 130

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



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

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

#usersObject



150
151
152
153
154
155
# File 'lib/archivesspace/client/helpers.rb', line 150

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