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



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

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



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

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



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

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



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

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



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

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



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

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



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

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
# 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?

  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