Module: Cirneco::Api

Included in:
CLI, DataCenter, Doi, Media, Metadata, Work
Defined in:
lib/cirneco/api.rb

Instance Method Summary collapse

Instance Method Details

#delete_metadata(doi, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/cirneco/api.rb', line 33

def (doi, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

  mds_url = options[:sandbox] ? 'https://mds.test.datacite.org' : 'https://mds.datacite.org'

  url = "#{mds_url}/metadata/#{doi}"
  Maremma.delete(url, username: options[:username], password: options[:password])
end

#get_doi(doi, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/cirneco/api.rb', line 53

def get_doi(doi, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

  mds_url = options[:sandbox] ? 'https://mds.test.datacite.org' : 'https://mds.datacite.org'

  url = "#{mds_url}/doi/#{doi}"
  Maremma.get(url, username: options[:username], password: options[:password])
end

#get_dois(options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/cirneco/api.rb', line 62

def get_dois(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

  mds_url = options[:sandbox] ? 'https://mds.test.datacite.org' : 'https://mds.datacite.org'

  url = "#{mds_url}/doi"
  response = Maremma.get(url, username: options[:username], password: options[:password])
  response.body["data"] = response.body["data"].split("\n") if response.body["data"].present?
  response
end

#get_media(doi, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cirneco/api.rb', line 84

def get_media(doi, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

  mds_url = options[:sandbox] ? 'https://mds.test.datacite.org' : 'https://mds.datacite.org'

  url = "#{mds_url}/media/#{doi}"
  response = Maremma.get(url, accept: 'application/xml', username: options[:username], password: options[:password])
  if response.body["data"].present? && !options[:raw]
    response.body["data"] = response.body["data"].split("\n").map do |m|
      mime_type, url = m.split('=', 2)
      { mime_type: mime_type, url: url }
    end
  end
  response
end

#get_metadata(doi, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/cirneco/api.rb', line 24

def (doi, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

  mds_url = options[:sandbox] ? 'https://mds.test.datacite.org' : 'https://mds.datacite.org'

  url = "#{mds_url}/metadata/#{doi}"
  Maremma.get(url, accept: 'application/xml', username: options[:username], password: options[:password], raw: true)
end

#post_media(doi, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/cirneco/api.rb', line 73

def post_media(doi, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

  payload = options[:raw] ? options[:media] : options[:media].map { |m| "#{m[:mime_type]}=#{m[:url]}" }.join("\n")

  mds_url = options[:sandbox] ? 'https://mds.test.datacite.org' : 'https://mds.datacite.org'

  url = "#{mds_url}/media/#{doi}"
  Maremma.post(url, content_type: 'text/plain;charset=UTF-8', data: payload, username: options[:username], password: options[:password])
end

#post_metadata(data, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/cirneco/api.rb', line 6

def (data, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

  mds_url = options[:sandbox] ? 'https://mds.test.datacite.org' : 'https://mds.datacite.org'

  url = "#{mds_url}/metadata"
  Maremma.post(url, content_type: 'application/xml;charset=UTF-8', data: data, username: options[:username], password: options[:password])
end

#put_doi(doi, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/cirneco/api.rb', line 42

def put_doi(doi, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

  payload = "doi=#{doi}\nurl=#{options[:url]}"

  mds_url = options[:sandbox] ? 'https://mds.test.datacite.org' : 'https://mds.datacite.org'

  url = "#{mds_url}/doi/#{doi}"
  Maremma.put(url, content_type: 'text/plain;charset=UTF-8', data: payload, username: options[:username], password: options[:password])
end

#put_metadata(doi, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/cirneco/api.rb', line 15

def (doi, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

  mds_url = options[:sandbox] ? 'https://mds.test.datacite.org' : 'https://mds.datacite.org'

  url = "#{mds_url}/metadata/#{doi}"
  Maremma.put(url, content_type: 'application/xml;charset=UTF-8', data: options[:data], username: options[:username], password: options[:password])
end