lib/datacite/client.rb in datacite-0.3.0 vs lib/datacite/client.rb in datacite-0.4.0
- old
+ new
@@ -10,17 +10,17 @@
include Dry::Monads[:result]
# @param [String] username
# @param [String] password
# @param [String] host
- def initialize(username:, password:, host: "api.test.datacite.org")
+ def initialize(username: nil, password: nil, host: "api.test.datacite.org")
@conn = Faraday.new(
url: "https://#{host}",
headers: headers
) do |conn|
conn.request :json
- conn.request :authorization, :basic, username, password
+ conn.request :authorization, :basic, username, password if username
conn.response :json
end
end
# Creates a random DOI
@@ -52,9 +52,24 @@
}
}
response = conn.put("/dois/#{id}", request_body.to_json)
response.success? ? Success(Response.new(response)) : Failure(response)
+ end
+
+ # Determines if a DOI exists
+ # @param [String] id
+ # @returns [Dry::Monads::Result]
+ def exists?(id:)
+ response = conn.head("/dois/#{id}")
+ case response.status
+ when 200
+ Success(true)
+ when 404
+ Success(false)
+ else
+ Failure(response)
+ end
end
private
# @returns [Dry::Monads::Result]