Class: Datacite::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/datacite/client.rb

Overview

The connection to DataCite API

Instance Method Summary collapse

Constructor Details

#initialize(username:, password:, host: "api.test.datacite.org") ⇒ Client

Returns a new instance of Client.

Parameters:

  • username (String)
  • password (String)
  • host (String) (defaults to: "api.test.datacite.org")


15
16
17
18
19
20
21
22
23
24
# File 'lib/datacite/client.rb', line 15

def initialize(username:, password:, 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.response :json
  end
end

Instance Method Details

#autogenerate_doi(prefix:) ⇒ Object

Creates a random DOI

Parameters:

  • prefix (String)


29
30
31
32
# File 'lib/datacite/client.rb', line 29

def autogenerate_doi(prefix:)
  request_body = AutogenerateDoiRequestBody.new(prefix: prefix).to_json
  register(request_body)
end

#register_doi(prefix:, suffix:) ⇒ Object

Creates a specific DOI

Parameters:

  • prefix (String)
  • suffix (String)


38
39
40
41
# File 'lib/datacite/client.rb', line 38

def register_doi(prefix:, suffix:)
  request_body = RegisterDoiRequestBody.new(prefix: prefix, suffix: suffix).to_json
  register(request_body)
end

#update(id:, attributes:) ⇒ Object

Update a DOI

Parameters:

  • id (String)
  • value (String)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/datacite/client.rb', line 47

def update(id:, attributes:)
  Validator.validate!(attributes)
  request_body = {
    data: {
      attributes: attributes
    }
  }

  response = conn.put("/dois/#{id}", request_body.to_json)
  response.success? ? Success(Response.new(response)) : Failure(response)
end