Skip to content

Commit

Permalink
Merge pull request #24 from lyrasis/fix-tasks-examples
Browse files Browse the repository at this point in the history
Fix tasks that call to_json directly (handled by post/put)
  • Loading branch information
kspurgin authored Jul 12, 2024
2 parents f7905e2 + 920dfba commit 5587841
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 41 deletions.
1 change: 0 additions & 1 deletion examples/export.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "awesome_print"
require "archivesspace/client"

# official sandbox
Expand Down
1 change: 0 additions & 1 deletion examples/password_reset.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "awesome_print"
require "archivesspace/client"

username = "admin"
Expand Down
17 changes: 8 additions & 9 deletions examples/repo_and_user.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "awesome_print"
require "archivesspace/client"

# official sandbox
config = ArchivesSpace::Configuration.new(
{
base_uri: "https://sandbox.archivesspace.org/api",
base_uri: "https://sandbox.archivesspace.org/staff/api",
base_repo: "",
username: "admin",
password: "admin",
Expand All @@ -19,7 +18,7 @@

client = ArchivesSpace::Client.new(config).login

ap ArchivesSpace::Template.list # view available templates
puts ArchivesSpace::Template.list # view available templates

repo_data = {
repo_code: "XYZ",
Expand All @@ -40,20 +39,20 @@
response = client.post("/repositories/with_agent", repository)
if response.result.success?
repository = client.repositories.find { |r| r["repo_code"] == "XYZ" }
ap repository
ap client.delete(repository["uri"])
puts repository
puts client.delete(repository["uri"])
else
ap response.parsed
puts response.parsed
end

user = ArchivesSpace::Template.process("user.json.erb", user_data)
response = client.post("users", user, {password: user_password})
if response.result.success?
user = client.users.find { |r| r["username"] == "lmessi" }
ap user
ap client.delete user["uri"]
puts user
puts client.delete(user["uri"]).inspect
else
ap response.parsed
puts response.parsed
end
rescue ArchivesSpace::RequestError => e
puts e.message
Expand Down
1 change: 0 additions & 1 deletion examples/templates.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "awesome_print"
require "archivesspace/client"

puts ArchivesSpace::Template.list
Expand Down
3 changes: 1 addition & 2 deletions examples/test_connection.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "awesome_print"
require "archivesspace/client"

# official sandbox
config = ArchivesSpace::Configuration.new(
{
base_uri: "https://sandbox.archivesspace.org/api",
base_uri: "https://sandbox.archivesspace.org/staff/api",
base_repo: "",
username: "admin",
password: "admin",
Expand Down
15 changes: 5 additions & 10 deletions examples/user_groups.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "awesome_print"
require "archivesspace/client"

# official sandbox
config = ArchivesSpace::Configuration.new(
{
base_uri: "https://sandbox.archivesspace.org/api",
base_uri: "https://sandbox.archivesspace.org/staff/api",
base_repo: "",
username: "admin",
password: "admin",
Expand All @@ -19,17 +18,13 @@

client = ArchivesSpace::Client.new(config).login

user_data = {
user_data = ArchivesSpace::Template.process("user.json.erb", {
username: "bde",
name: "BDE",
is_admin: false
}
})

client.post(
"users",
ArchivesSpace::Template.process("user.json.erb", user_data),
{password: "123456"}
)
client.post("users", user_data, {password: "123456"})

users_with_roles = {
"bde" => ["repository-basic-data-entry"]
Expand All @@ -38,7 +33,7 @@
begin
client.config.base_repo = "repositories/2"
results = client.group_user_assignment users_with_roles
ap results.map(&:parsed)
puts results.map(&:parsed)
rescue ArchivesSpace::RequestError => e
puts e.message
end
12 changes: 2 additions & 10 deletions lib/archivesspace/client/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
module ArchivesSpace
# Perform specific API tasks
module Task
# def batch_import(payload, params = {})
# # TODO: create "batch_import", payload, params
# end

def group_user_assignment(users_with_roles)
updated = []
groups.each do |group|
Expand All @@ -29,7 +25,7 @@ def group_user_assignment(users_with_roles)

next unless update

response = post("/groups/#{uri_to_id(group["uri"])}", group.to_json)
response = post("/groups/#{uri_to_id(group["uri"])}", group)
updated << response
end
updated
Expand All @@ -54,13 +50,9 @@ def password_reset(username, password)
user = all("users").find { |u| u["username"] == username }
raise RequestError, user.status unless user

post(user["uri"], user.to_json, {password: password})
post(user["uri"], user, {password: password})
end

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

private

def uri_to_id(uri)
Expand Down
6 changes: 3 additions & 3 deletions lib/archivesspace/client/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def extension
def process
t = ERB.new(read_template)
r = t.result(binding).squeeze("\n")
JSON.parse(r).to_json
JSON.parse(r)
end
end

Expand All @@ -60,9 +60,9 @@ def extension
end

def process
::Jbuilder.encode do |json|
::Jbuilder.new do |json|
eval(read_template, binding) # standard:disable Security/Eval
end
end.attributes!
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/archivesspace/templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

it "can process an erb template" do
data = {repo_code: "ABC", name: "ABC Archive", agent_contact_name: "ABC Admin"}
json = JSON.parse(ArchivesSpace::Template.process("repository_with_agent.json.erb", data))
expect(json["repository"]["repo_code"]).to eq data[:repo_code]
record = ArchivesSpace::Template.process("repository_with_agent.json.erb", data)
expect(record["repository"]["repo_code"]).to eq data[:repo_code]
end

it "can process a jbuilder template" do
data = {"title" => "Title", "object_number" => "001.001", "description_level" => "collection"}
json = JSON.parse(ArchivesSpace::Template.process("resource.json.jbuilder", data))
expect(json["id_0"]).to eq data["object_number"]
record = ArchivesSpace::Template.process("resource.json.jbuilder", data)
expect(record["id_0"]).to eq data["object_number"]
end

it "rejects a template that does not match by extension" do
Expand Down

0 comments on commit 5587841

Please sign in to comment.