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: nil, password: nil, host: "api.test.datacite.org") ⇒ Client

Returns a new instance of Client.

Parameters:

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


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

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 if username
    conn.response :json
  end
end

Instance Method Details

#autogenerate_doi(prefix:) ⇒ Object

Creates a random DOI

Parameters:

  • prefix (String)


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

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

#exists?(id:) ⇒ Boolean

Determines if a DOI exists

Parameters:

  • id (String)

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/datacite/client.rb', line 60

def exists?(id:)
  response = conn.head("/dois/#{id}")
  case response.status
  when 200
    Success(true)
  when 404
    Success(false)
  else
    Failure(response)
  end
end

#metadata(id:) ⇒ Object

Fetches the metadata for a DOI

Parameters:

  • id (String)


75
76
77
78
79
80
81
82
# File 'lib/datacite/client.rb', line 75

def (id:)
  response = conn.get("/dois/#{id}")
  if response.success?
    Success(response.body)
  else
    Failure(response)
  end
end

#register_doi(prefix:, suffix:) ⇒ Object

Creates a specific DOI

Parameters:

  • prefix (String)
  • suffix (String)


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

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)


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

def update(id:, attributes:)
  request_body = {
    data: {
      attributes: attributes
    }
  }

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