Module: OrcidClient::Api

Included in:
ExternalIdentifier, Notification, Work
Defined in:
lib/orcid_client/api.rb

Constant Summary collapse

API_VERSION =
"3.0"

Instance Method Summary collapse

Instance Method Details

#create_external_identifier(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/orcid_client/api.rb', line 52

def create_external_identifier(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "ORCID access token missing" }] }) unless orcid_token.present?

  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  url = "#{orcid_api_url}/v#{API_VERSION}/#{orcid}/external-identifiers"
  response = Maremma.post(url, content_type: 'application/vnd.orcid+xml', data: data, bearer: orcid_token)
  put_code = response.headers.present? ? response.headers.fetch("Location", "").split("/").last : nil
  response.body["put_code"] = put_code.present? ? put_code.to_i : nil
  response
end

#create_notification(options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/orcid_client/api.rb', line 87

def create_notification(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Notification access token missing" }] }) unless notification_access_token.present?

  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  url = "#{orcid_api_url}/v#{API_VERSION}/#{orcid}/notification-permission"
  response = Maremma.post(url, content_type: 'application/vnd.orcid+xml', data: data, bearer: notification_access_token)
  put_code = response.headers.present? ? response.headers.fetch("Location", "").split("/").last : nil
  response.body["put_code"] = put_code.present? ? put_code.to_i : nil
  response
end

#create_work(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/orcid_client/api.rb', line 16

def create_work(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "ORCID access token missing" }] }) unless orcid_token.present?

  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  url = "#{orcid_api_url}/v#{API_VERSION}/#{orcid}/work"
  response = Maremma.post(url, content_type: 'application/vnd.orcid+xml', data: data, bearer: orcid_token)
  put_code = response.headers.present? ? response.headers.fetch("Location", "").split("/").last : nil
  response.body["put_code"] = put_code.present? ? put_code.to_i : nil
  response
end

#delete_external_identifier(options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/orcid_client/api.rb', line 64

def delete_external_identifier(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "ORCID access token missing" }] }) unless orcid_token.present?
  return OpenStruct.new(body: { "errors" => [{ "title" => "Put code missing" }] }) unless put_code.present?

  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  url = "#{orcid_api_url}/v#{API_VERSION}/#{orcid}/external-identifiers/#{put_code}"
  Maremma.delete(url, content_type: 'application/vnd.orcid+xml', bearer: orcid_token)
end

#delete_notification(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/orcid_client/api.rb', line 99

def delete_notification(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Notification access token missing" }] }) unless notification_access_token.present?
  return OpenStruct.new(body: { "errors" => [{ "title" => "Put code missing" }] }) unless put_code.present?

  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  url = "#{orcid_api_url}/v#{API_VERSION}/#{orcid}/notification-permission/#{put_code}"
  Maremma.delete(url, content_type: 'application/vnd.orcid+xml', bearer: notification_access_token)
end

#delete_work(options = {}) ⇒ Object



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

def delete_work(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "ORCID access token missing" }] }) unless orcid_token.present?
  return OpenStruct.new(body: { "errors" => [{ "title" => "Put code missing" }] }) unless put_code.present?

  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  url = "#{orcid_api_url}/v#{API_VERSION}/#{orcid}/work/#{put_code}"
  Maremma.delete(url, content_type: 'application/vnd.orcid+xml', bearer: orcid_token)
end

#get_notification(options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/orcid_client/api.rb', line 109

def get_notification(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Notification access token missing" }] }) unless notification_access_token.present?
  return OpenStruct.new(body: { "errors" => [{ "title" => "Put code missing" }] }) unless put_code.present?

  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  url = "#{orcid_api_url}/v#{API_VERSION}/#{orcid}/notification-permission/#{put_code}"
  Maremma.get(url, content_type: 'application/vnd.orcid+xml', bearer: notification_access_token)
end

#get_notification_access_token(client_id:, client_secret:, **options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/orcid_client/api.rb', line 74

def get_notification_access_token(client_id:, client_secret:, **options)
  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  params = { 'client_id' => client_id,
             'client_secret' => client_secret,
             'scope' => '/premium-notification',
             'grant_type' => 'client_credentials' }

  url = "#{orcid_api_url}/oauth/token"
  data = URI.encode_www_form(params)
  Maremma.post(url, content_type: 'application/x-www-form-urlencoded', data: data, accept: 'application/json')
end

#get_works(options = {}) ⇒ Object



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

def get_works(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "ORCID access token missing" }] }) unless orcid_token.present?

  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  url = "#{orcid_api_url}/v#{API_VERSION}/#{orcid}/works"
  Maremma.get(url, accept: 'json', bearer: orcid_token)
end

#update_work(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/orcid_client/api.rb', line 28

def update_work(options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "ORCID access token missing" }] }) unless orcid_token.present?
  return OpenStruct.new(body: { "errors" => [{ "title" => "Put code missing" }] }) unless put_code.present?

  self.visibility = nil
  orcid_api_url = options[:sandbox] ? 'https://api.sandbox.orcid.org' : 'https://api.orcid.org'

  url = "#{orcid_api_url}/v#{API_VERSION}/#{orcid}/work/#{put_code}"
  response = Maremma.put(url, content_type: 'application/vnd.orcid+xml', data: data, bearer: orcid_token)
  put_code = response.headers.present? ? response.headers.fetch("Location", "").split("/").last : nil
  response.body["put_code"] = put_code.present? ? put_code.to_i : nil
  response
end